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.

274 lines
9.6 KiB

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