You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
9.8 KiB

4 years ago
  1. # depd
  2. [![NPM Version][npm-version-image]][npm-url]
  3. [![NPM Downloads][npm-downloads-image]][npm-url]
  4. [![Node.js Version][node-image]][node-url]
  5. [![Linux Build][travis-image]][travis-url]
  6. [![Windows Build][appveyor-image]][appveyor-url]
  7. [![Coverage Status][coveralls-image]][coveralls-url]
  8. Deprecate all the things
  9. > With great modules comes great responsibility; mark things deprecated!
  10. ## Install
  11. This module is installed directly using `npm`:
  12. ```sh
  13. $ npm install depd
  14. ```
  15. This module can also be bundled with systems like
  16. [Browserify](http://browserify.org/) or [webpack](https://webpack.github.io/),
  17. though by default this module will alter it's API to no longer display or
  18. track deprecations.
  19. ## API
  20. <!-- eslint-disable no-unused-vars -->
  21. ```js
  22. var deprecate = require('depd')('my-module')
  23. ```
  24. This library allows you to display deprecation messages to your users.
  25. This library goes above and beyond with deprecation warnings by
  26. introspection of the call stack (but only the bits that it is interested
  27. in).
  28. Instead of just warning on the first invocation of a deprecated
  29. function and never again, this module will warn on the first invocation
  30. of a deprecated function per unique call site, making it ideal to alert
  31. users of all deprecated uses across the code base, rather than just
  32. whatever happens to execute first.
  33. The deprecation warnings from this module also include the file and line
  34. information for the call into the module that the deprecated function was
  35. in.
  36. **NOTE** this library has a similar interface to the `debug` module, and
  37. this module uses the calling file to get the boundary for the call stacks,
  38. so you should always create a new `deprecate` object in each file and not
  39. within some central file.
  40. ### depd(namespace)
  41. Create a new deprecate function that uses the given namespace name in the
  42. messages and will display the call site prior to the stack entering the
  43. file this function was called from. It is highly suggested you use the
  44. name of your module as the namespace.
  45. ### deprecate(message)
  46. Call this function from deprecated code to display a deprecation message.
  47. This message will appear once per unique caller site. Caller site is the
  48. first call site in the stack in a different file from the caller of this
  49. function.
  50. If the message is omitted, a message is generated for you based on the site
  51. of the `deprecate()` call and will display the name of the function called,
  52. similar to the name displayed in a stack trace.
  53. ### deprecate.function(fn, message)
  54. Call this function to wrap a given function in a deprecation message on any
  55. call to the function. An optional message can be supplied to provide a custom
  56. message.
  57. ### deprecate.property(obj, prop, message)
  58. Call this function to wrap a given property on object in a deprecation message
  59. on any accessing or setting of the property. An optional message can be supplied
  60. to provide a custom message.
  61. The method must be called on the object where the property belongs (not
  62. inherited from the prototype).
  63. If the property is a data descriptor, it will be converted to an accessor
  64. descriptor in order to display the deprecation message.
  65. ### process.on('deprecation', fn)
  66. This module will allow easy capturing of deprecation errors by emitting the
  67. errors as the type "deprecation" on the global `process`. If there are no
  68. listeners for this type, the errors are written to STDERR as normal, but if
  69. there are any listeners, nothing will be written to STDERR and instead only
  70. emitted. From there, you can write the errors in a different format or to a
  71. logging source.
  72. The error represents the deprecation and is emitted only once with the same
  73. rules as writing to STDERR. The error has the following properties:
  74. - `message` - This is the message given by the library
  75. - `name` - This is always `'DeprecationError'`
  76. - `namespace` - This is the namespace the deprecation came from
  77. - `stack` - This is the stack of the call to the deprecated thing
  78. Example `error.stack` output:
  79. ```
  80. DeprecationError: my-cool-module deprecated oldfunction
  81. at Object.<anonymous> ([eval]-wrapper:6:22)
  82. at Module._compile (module.js:456:26)
  83. at evalScript (node.js:532:25)
  84. at startup (node.js:80:7)
  85. at node.js:902:3
  86. ```
  87. ### process.env.NO_DEPRECATION
  88. As a user of modules that are deprecated, the environment variable `NO_DEPRECATION`
  89. is provided as a quick solution to silencing deprecation warnings from being
  90. output. The format of this is similar to that of `DEBUG`:
  91. ```sh
  92. $ NO_DEPRECATION=my-module,othermod node app.js
  93. ```
  94. This will suppress deprecations from being output for "my-module" and "othermod".
  95. The value is a list of comma-separated namespaces. To suppress every warning
  96. across all namespaces, use the value `*` for a namespace.
  97. Providing the argument `--no-deprecation` to the `node` executable will suppress
  98. all deprecations (only available in Node.js 0.8 or higher).
  99. **NOTE** This will not suppress the deperecations given to any "deprecation"
  100. event listeners, just the output to STDERR.
  101. ### process.env.TRACE_DEPRECATION
  102. As a user of modules that are deprecated, the environment variable `TRACE_DEPRECATION`
  103. is provided as a solution to getting more detailed location information in deprecation
  104. warnings by including the entire stack trace. The format of this is the same as
  105. `NO_DEPRECATION`:
  106. ```sh
  107. $ TRACE_DEPRECATION=my-module,othermod node app.js
  108. ```
  109. This will include stack traces for deprecations being output for "my-module" and
  110. "othermod". The value is a list of comma-separated namespaces. To trace every
  111. warning across all namespaces, use the value `*` for a namespace.
  112. Providing the argument `--trace-deprecation` to the `node` executable will trace
  113. all deprecations (only available in Node.js 0.8 or higher).
  114. **NOTE** This will not trace the deperecations silenced by `NO_DEPRECATION`.
  115. ## Display
  116. ![message](files/message.png)
  117. When a user calls a function in your library that you mark deprecated, they
  118. will see the following written to STDERR (in the given colors, similar colors
  119. and layout to the `debug` module):
  120. ```
  121. bright cyan bright yellow
  122. | | reset cyan
  123. | | | |
  124. ▼ ▼ ▼ ▼
  125. my-cool-module deprecated oldfunction [eval]-wrapper:6:22
  126. ▲ ▲ ▲ ▲
  127. | | | |
  128. namespace | | location of mycoolmod.oldfunction() call
  129. | deprecation message
  130. the word "deprecated"
  131. ```
  132. If the user redirects their STDERR to a file or somewhere that does not support
  133. colors, they see (similar layout to the `debug` module):
  134. ```
  135. Sun, 15 Jun 2014 05:21:37 GMT my-cool-module deprecated oldfunction at [eval]-wrapper:6:22
  136. ▲ ▲ ▲ ▲ ▲
  137. | | | | |
  138. timestamp of message namespace | | location of mycoolmod.oldfunction() call
  139. | deprecation message
  140. the word "deprecated"
  141. ```
  142. ## Examples
  143. ### Deprecating all calls to a function
  144. This will display a deprecated message about "oldfunction" being deprecated
  145. from "my-module" on STDERR.
  146. ```js
  147. var deprecate = require('depd')('my-cool-module')
  148. // message automatically derived from function name
  149. // Object.oldfunction
  150. exports.oldfunction = deprecate.function(function oldfunction () {
  151. // all calls to function are deprecated
  152. })
  153. // specific message
  154. exports.oldfunction = deprecate.function(function () {
  155. // all calls to function are deprecated
  156. }, 'oldfunction')
  157. ```
  158. ### Conditionally deprecating a function call
  159. This will display a deprecated message about "weirdfunction" being deprecated
  160. from "my-module" on STDERR when called with less than 2 arguments.
  161. ```js
  162. var deprecate = require('depd')('my-cool-module')
  163. exports.weirdfunction = function () {
  164. if (arguments.length < 2) {
  165. // calls with 0 or 1 args are deprecated
  166. deprecate('weirdfunction args < 2')
  167. }
  168. }
  169. ```
  170. When calling `deprecate` as a function, the warning is counted per call site
  171. within your own module, so you can display different deprecations depending
  172. on different situations and the users will still get all the warnings:
  173. ```js
  174. var deprecate = require('depd')('my-cool-module')
  175. exports.weirdfunction = function () {
  176. if (arguments.length < 2) {
  177. // calls with 0 or 1 args are deprecated
  178. deprecate('weirdfunction args < 2')
  179. } else if (typeof arguments[0] !== 'string') {
  180. // calls with non-string first argument are deprecated
  181. deprecate('weirdfunction non-string first arg')
  182. }
  183. }
  184. ```
  185. ### Deprecating property access
  186. This will display a deprecated message about "oldprop" being deprecated
  187. from "my-module" on STDERR when accessed. A deprecation will be displayed
  188. when setting the value and when getting the value.
  189. ```js
  190. var deprecate = require('depd')('my-cool-module')
  191. exports.oldprop = 'something'
  192. // message automatically derives from property name
  193. deprecate.property(exports, 'oldprop')
  194. // explicit message
  195. deprecate.property(exports, 'oldprop', 'oldprop >= 0.10')
  196. ```
  197. ## License
  198. [MIT](LICENSE)
  199. [npm-version-image]: https://img.shields.io/npm/v/depd.svg
  200. [npm-downloads-image]: https://img.shields.io/npm/dm/depd.svg
  201. [npm-url]: https://npmjs.org/package/depd
  202. [travis-image]: https://img.shields.io/travis/dougwilson/nodejs-depd/master.svg?label=linux
  203. [travis-url]: https://travis-ci.org/dougwilson/nodejs-depd
  204. [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/nodejs-depd/master.svg?label=windows
  205. [appveyor-url]: https://ci.appveyor.com/project/dougwilson/nodejs-depd
  206. [coveralls-image]: https://img.shields.io/coveralls/dougwilson/nodejs-depd/master.svg
  207. [coveralls-url]: https://coveralls.io/r/dougwilson/nodejs-depd?branch=master
  208. [node-image]: https://img.shields.io/node/v/depd.svg
  209. [node-url]: https://nodejs.org/en/download/