pax_global_header00006660000000000000000000000064124064767460014532gustar00rootroot0000000000000052 comment=08b5a2182c8c1fdf7420e4ff8532bfd7e266a7b2 nodejs-depd-1.0.0/000077500000000000000000000000001240647674600137245ustar00rootroot00000000000000nodejs-depd-1.0.0/.gitignore000066400000000000000000000000461240647674600157140ustar00rootroot00000000000000coverage/ node_modules/ npm-debug.log nodejs-depd-1.0.0/.travis.yml000066400000000000000000000004031240647674600160320ustar00rootroot00000000000000language: node_js node_js: - "0.6" - "0.8" - "0.10" - "0.11" matrix: allow_failures: - node_js: "0.11" fast_finish: true script: "npm run-script test-travis" after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls" nodejs-depd-1.0.0/History.md000066400000000000000000000026431240647674600157140ustar00rootroot000000000000001.0.0 / 2014-09-17 ================== * No changes 0.4.5 / 2014-09-09 ================== * Improve call speed to functions using the function wrapper * Support Node.js 0.6 0.4.4 / 2014-07-27 ================== * Work-around v8 generating empty stack traces 0.4.3 / 2014-07-26 ================== * Fix exception when global `Error.stackTraceLimit` is too low 0.4.2 / 2014-07-19 ================== * Correct call site for wrapped functions and properties 0.4.1 / 2014-07-19 ================== * Improve automatic message generation for function properties 0.4.0 / 2014-07-19 ================== * Add `TRACE_DEPRECATION` environment variable * Remove non-standard grey color from color output * Support `--no-deprecation` argument * Support `--trace-deprecation` argument * Support `deprecate.property(fn, prop, message)` 0.3.0 / 2014-06-16 ================== * Add `NO_DEPRECATION` environment variable 0.2.0 / 2014-06-15 ================== * Add `deprecate.property(obj, prop, message)` * Remove `supports-color` dependency for node.js 0.8 0.1.0 / 2014-06-15 ================== * Add `deprecate.function(fn, message)` * Add `process.on('deprecation', fn)` emitter * Automatically generate message when omitted from `deprecate()` 0.0.1 / 2014-06-15 ================== * Fix warning for dynamic calls at singe call site 0.0.0 / 2014-06-15 ================== * Initial implementation nodejs-depd-1.0.0/LICENSE000066400000000000000000000021011240647674600147230ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. nodejs-depd-1.0.0/Readme.md000066400000000000000000000227321240647674600154510ustar00rootroot00000000000000# depd [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Node.js Version][node-image]][node-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Gratipay][gratipay-image]][gratipay-url] Deprecate all the things > With great modules comes great responsibility; mark things deprecated! ## Install ```sh $ npm install depd ``` ## API ```js var deprecate = require('depd')('my-module') ``` This library allows you to display deprecation messages to your users. This library goes above and beyond with deprecation warnings by introspection of the call stack (but only the bits that it is interested in). Instead of just warning on the first invocation of a deprecated function and never again, this module will warn on the first invocation of a deprecated function per unique call site, making it ideal to alert users of all deprecated uses across the code base, rather than just whatever happens to execute first. The deprecation warnings from this module also include the file and line information for the call into the module that the deprecated function was in. **NOTE** this library has a similar interface to the `debug` module, and this module uses the calling file to get the boundary for the call stacks, so you should always create a new `deprecate` object in each file and not within some central file. ### depd(namespace) Create a new deprecate function that uses the given namespace name in the messages and will display the call site prior to the stack entering the file this function was called from. It is highly suggested you use the name of your module as the namespace. ### deprecate(message) Call this function from deprecated code to display a deprecation message. This message will appear once per unique caller site. Caller site is the first call site in the stack in a different file from the caller of this function. If the message is omitted, a message is generated for you based on the site of the `deprecate()` call and will display the name of the function called, similar to the name displayed in a stack trace. ### deprecate.function(fn, message) Call this function to wrap a given function in a deprecation message on any call to the function. An optional message can be supplied to provide a custom message. ### deprecate.property(obj, prop, message) Call this function to wrap a given property on object in a deprecation message on any accessing or setting of the property. An optional message can be supplied to provide a custom message. The method must be called on the object where the property belongs (not inherited from the prototype). If the property is a data descriptor, it will be converted to an accessor descriptor in order to display the deprecation message. ### process.on('deprecation', fn) This module will allow easy capturing of deprecation errors by emitting the errors as the type "deprecation" on the global `process`. If there are no listeners for this type, the errors are written to STDERR as normal, but if there are any listeners, nothing will be written to STDERR and instead only emitted. From there, you can write the errors in a different format or to a logging source. The error represents the deprecation and is emitted only once with the same rules as writing to STDERR. The error has the following properties: - `message` - This is the message given by the library - `name` - This is always `'DeprecationError'` - `namespace` - This is the namespace the deprecation came from - `stack` - This is the stack of the call to the deprecated thing Example `error.stack` output: ``` DeprecationError: my-cool-module deprecated oldfunction at Object. ([eval]-wrapper:6:22) at Module._compile (module.js:456:26) at evalScript (node.js:532:25) at startup (node.js:80:7) at node.js:902:3 ``` ### process.env.NO_DEPRECATION As a user of modules that are deprecated, the environment variable `NO_DEPRECATION` is provided as a quick solution to silencing deprecation warnings from being output. The format of this is similar to that of `DEBUG`: ```sh $ NO_DEPRECATION=my-module,othermod node app.js ``` This will suppress deprecations from being output for "my-module" and "othermod". The value is a list of comma-separated namespaces. To suppress every warning across all namespaces, use the value `*` for a namespace. Providing the argument `--no-deprecation` to the `node` executable will suppress all deprecations (only available in Node.js 0.8 or higher). **NOTE** This will not suppress the deperecations given to any "deprecation" event listeners, just the output to STDERR. ### process.env.TRACE_DEPRECATION As a user of modules that are deprecated, the environment variable `TRACE_DEPRECATION` is provided as a solution to getting more detailed location information in deprecation warnings by including the entire stack trace. The format of this is the same as `NO_DEPRECATION`: ```sh $ TRACE_DEPRECATION=my-module,othermod node app.js ``` This will include stack traces for deprecations being output for "my-module" and "othermod". The value is a list of comma-separated namespaces. To trace every warning across all namespaces, use the value `*` for a namespace. Providing the argument `--trace-deprecation` to the `node` executable will trace all deprecations (only available in Node.js 0.8 or higher). **NOTE** This will not trace the deperecations silenced by `NO_DEPRECATION`. ## Display ![message](files/message.png) When a user calls a function in your library that you mark deprecated, they will see the following written to STDERR (in the given colors, similar colors and layout to the `debug` module): ``` bright cyan bright yellow | | reset cyan | | | | ▼ ▼ ▼ ▼ my-cool-module deprecated oldfunction [eval]-wrapper:6:22 ▲ ▲ ▲ ▲ | | | | namespace | | location of mycoolmod.oldfunction() call | deprecation message the word "deprecated" ``` If the user redirects their STDERR to a file or somewhere that does not support colors, they see (similar layout to the `debug` module): ``` Sun, 15 Jun 2014 05:21:37 GMT my-cool-module deprecated oldfunction at [eval]-wrapper:6:22 ▲ ▲ ▲ ▲ ▲ | | | | | timestamp of message namespace | | location of mycoolmod.oldfunction() call | deprecation message the word "deprecated" ``` ## Examples ### Deprecating all calls to a function This will display a deprecated message about "oldfunction" being deprecated from "my-module" on STDERR. ```js var deprecate = require('depd')('my-cool-module') // message automatically derived from function name // Object.oldfunction exports.oldfunction = deprecate.function(function oldfunction() { // all calls to function are deprecated }) // specific message exports.oldfunction = deprecate.function(function () { // all calls to function are deprecated }, 'oldfunction') ``` ### Conditionally deprecating a function call This will display a deprecated message about "weirdfunction" being deprecated from "my-module" on STDERR when called with less than 2 arguments. ```js var deprecate = require('depd')('my-cool-module') exports.weirdfunction = function () { if (arguments.length < 2) { // calls with 0 or 1 args are deprecated deprecate('weirdfunction args < 2') } } ``` When calling `deprecate` as a function, the warning is counted per call site within your own module, so you can display different deprecations depending on different situations and the users will still get all the warnings: ```js var deprecate = require('depd')('my-cool-module') exports.weirdfunction = function () { if (arguments.length < 2) { // calls with 0 or 1 args are deprecated deprecate('weirdfunction args < 2') } else if (typeof arguments[0] !== 'string') { // calls with non-string first argument are deprecated deprecate('weirdfunction non-string first arg') } } ``` ### Deprecating property access This will display a deprecated message about "oldprop" being deprecated from "my-module" on STDERR when accessed. A deprecation will be displayed when setting the value and when getting the value. ```js var deprecate = require('depd')('my-cool-module') exports.oldprop = 'something' // message automatically derives from property name deprecate.property(exports, 'oldprop') // explicit message deprecate.property(exports, 'oldprop', 'oldprop >= 0.10') ``` ## License [MIT](LICENSE) [npm-version-image]: https://img.shields.io/npm/v/depd.svg?style=flat [npm-downloads-image]: https://img.shields.io/npm/dm/depd.svg?style=flat [npm-url]: https://npmjs.org/package/depd [travis-image]: https://img.shields.io/travis/dougwilson/nodejs-depd.svg?style=flat [travis-url]: https://travis-ci.org/dougwilson/nodejs-depd [coveralls-image]: https://img.shields.io/coveralls/dougwilson/nodejs-depd.svg?style=flat [coveralls-url]: https://coveralls.io/r/dougwilson/nodejs-depd?branch=master [node-image]: https://img.shields.io/node/v/depd.svg?style=flat [node-url]: http://nodejs.org/download/ [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat [gratipay-url]: https://www.gratipay.com/dougwilson/ nodejs-depd-1.0.0/benchmark/000077500000000000000000000000001240647674600156565ustar00rootroot00000000000000nodejs-depd-1.0.0/benchmark/index.js000066400000000000000000000012361240647674600173250ustar00rootroot00000000000000var fs = require('fs'); var path = require('path'); var spawn = require('child_process').spawn; var exe = process.argv[0]; var cwd = process.cwd(); runScripts(fs.readdirSync(__dirname)); function runScripts(fileNames) { var fileName = fileNames.shift(); if (!fileName) return; if (!/\.js$/i.test(fileName)) return runScripts(fileNames); if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames); var fullPath = path.join(__dirname, fileName); console.log('> %s %s', exe, path.relative(cwd, fullPath)); var proc = spawn(exe, [fullPath], { 'stdio': 'inherit' }); proc.on('exit', function () { runScripts(fileNames); }); } nodejs-depd-1.0.0/benchmark/wrapfunction.js000066400000000000000000000012461240647674600207360ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') /** * Globals for benchmark.js */ process.env.NO_DEPRECATION = 'my-lib' global.mylib = require('../test/fixtures/my-lib') var suite = new benchmark.Suite suite.add({ name: 'function', minSamples: 100, fn: 'mylib.fn()' }) suite.add({ name: 'wrapped', minSamples: 100, fn: 'mylib.oldfn()' }) suite.add({ name: 'call log', minSamples: 100, fn: 'mylib.old()' }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) nodejs-depd-1.0.0/benchmark/wrapproperty.js000066400000000000000000000011641240647674600207740ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') /** * Globals for benchmark.js */ process.env.NO_DEPRECATION = 'my-lib' global.mylib = require('../test/fixtures/my-lib') var suite = new benchmark.Suite suite.add({ name: 'property', minSamples: 100, fn: 'mylib.prop = mylib.prop' }) suite.add({ name: 'wrapped', minSamples: 100, fn: 'mylib.propa = mylib.propa' }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) nodejs-depd-1.0.0/files/000077500000000000000000000000001240647674600150265ustar00rootroot00000000000000nodejs-depd-1.0.0/files/message.png000066400000000000000000000054511240647674600171650ustar00rootroot00000000000000PNG  IHDRF3gAMA aPLTE+3:33:33+:++LHf+L3\+l:fH\3f:fH\\\HnnHn\nnLL+fl+f:L+lLLff:L\nf+l::\nffې۶Ll:Lfl+ې۶LlGi pHYs(JtEXtSoftwarePaint.NET v3.5.11GB7IDAThC {Eǯ6TP-%TDP~o^vgvJ(O{}LY,W\Vt䦰 w2@铓6Lv 3D""RyH Eё xpg=Rv S9Kpw1E:-%im{rqFkT2c::M>O6F ْlO|mip&:_)[ 9n<~=ۂχT&b:[%;TTf& bDCلIU%ou)Lk/rux|NNzdxoK᧙< _?&). ۖZS{5<ޟMLN;ɖݣUI 66RdWcϙ޶&ͷA\W'1l>.x_?x76'.֥p!Ax"/Lou;DvU}%ӼeL.Xm-`H3oKd)w8Wo3qHf@z؃e/ 0[ mI̳:ә,Ӧu}>/Vߜh,=:f0!Ok]tb] 7F,Ll{'xGscE1MK/'*au Nx5`8TɈ>19e)wiKia_rcNr 4,Ni =mL Ŝ`r&w\j|=Ǡ3H0d&i=>$źv[3JMKi@~f%#ǻ.08үNz fXJSd66\FxŚ<}Rf[S5e $^`xlPH>Hf;5,0Kvx~j֝fxt=+!K(Hv9Dsc [XN8ssa1xKl0'$eb͉F8-[.qVqjzM%IXg]+`UJcp#.@_-o(k9Obc TNZSy4Nrk|Y6(+W._i.  Dq@sMtfd$e%y{- Z NYP#,?-$,{{7V_ s&\'DYse7w\ZudkkA'ˀXdv,[[v _>sgbq[7pmMpb襌,=K03@E@ Ncz֜f8s!w%htEzW2ONuul]K/1 724AcXR&"of`ٲXtrwLRH=$N2)gHXOx ;L_A@|Yz&1໚P9{9 ZwucUě hHr?~,NMS̗.x}Qٯ Se%~n|OǛ;s:VZjuTUwҪ:8GIENDB`nodejs-depd-1.0.0/index.js000066400000000000000000000245041240647674600153760ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ /** * Module dependencies. */ var callSiteToString = require('./lib/compat').callSiteToString var EventEmitter = require('events').EventEmitter var relative = require('path').relative /** * Module exports. */ module.exports = depd /** * Get the path to base files on. */ var basePath = process.cwd() /** * Get listener count on event emitter. */ /*istanbul ignore next*/ var eventListenerCount = EventEmitter.listenerCount || function (emitter, type) { return emitter.listeners(type).length } /** * Determine if namespace is contained in the string. */ function containsNamespace(str, namespace) { var val = str.split(/[ ,]+/) namespace = String(namespace).toLowerCase() for (var i = 0 ; i < val.length; i++) { if (!(str = val[i])) continue; // namespace contained if (str === '*' || str.toLowerCase() === namespace) { return true } } return false } /** * Convert a data descriptor to accessor descriptor. */ function convertDataDescriptorToAccessor(obj, prop, message) { var descriptor = Object.getOwnPropertyDescriptor(obj, prop) var value = descriptor.value descriptor.get = function getter() { return value } if (descriptor.writable) { descriptor.set = function setter(val) { return value = val } } delete descriptor.value delete descriptor.writable Object.defineProperty(obj, prop, descriptor) return descriptor } /** * Create arguments string to keep arity. */ function createArgumentsString(arity) { var str = '' for (var i = 0; i < arity; i++) { str += ', arg' + i } return str.substr(2) } /** * Create stack string from stack. */ function createStackString(stack) { var str = this.name + ': ' + this.namespace if (this.message) { str += ' deprecated ' + this.message } for (var i = 0; i < stack.length; i++) { str += '\n at ' + callSiteToString(stack[i]) } return str } /** * Create deprecate for namespace in caller. */ function depd(namespace) { if (!namespace) { throw new TypeError('argument namespace is required') } var stack = getStack() var site = callSiteLocation(stack[1]) var file = site[0] function deprecate(message) { // call to self as log log.call(deprecate, message) } deprecate._file = file deprecate._ignored = isignored(namespace) deprecate._namespace = namespace deprecate._traced = istraced(namespace) deprecate._warned = Object.create(null) deprecate.function = wrapfunction deprecate.property = wrapproperty return deprecate } /** * Determine if namespace is ignored. */ function isignored(namespace) { /* istanbul ignore next: tested in a child processs */ if (process.noDeprecation) { // --no-deprecation support return true } var str = process.env.NO_DEPRECATION || '' // namespace ignored return containsNamespace(str, namespace) } /** * Determine if namespace is traced. */ function istraced(namespace) { /* istanbul ignore next: tested in a child processs */ if (process.traceDeprecation) { // --trace-deprecation support return true } var str = process.env.TRACE_DEPRECATION || '' // namespace traced return containsNamespace(str, namespace) } /** * Display deprecation message. */ function log(message, site) { var haslisteners = eventListenerCount(process, 'deprecation') !== 0 // abort early if no destination if (!haslisteners && this._ignored) { return } var caller var callFile var callSite var i = 0 var seen = false var stack = getStack() var file = this._file if (site) { // provided site callSite = callSiteLocation(stack[1]) callSite.name = site.name file = callSite[0] } else { // get call site i = 2 site = callSiteLocation(stack[i]) callSite = site } // get caller of deprecated thing in relation to file for (; i < stack.length; i++) { caller = callSiteLocation(stack[i]) callFile = caller[0] if (callFile === file) { seen = true } else if (callFile === this._file) { file = this._file } else if (seen) { break } } var key = caller ? site.join(':') + '__' + caller.join(':') : undefined if (key !== undefined && key in this._warned) { // already warned return } this._warned[key] = true // generate automatic message from call site if (!message) { message = callSite === site || !callSite.name ? defaultMessage(site) : defaultMessage(callSite) } // emit deprecation if listeners exist if (haslisteners) { var err = DeprecationError(this._namespace, message, stack.slice(i)) process.emit('deprecation', err) return } // format and write message var format = process.stderr.isTTY ? formatColor : formatPlain var msg = format.call(this, message, caller, stack.slice(i)) process.stderr.write(msg + '\n', 'utf8') return } /** * Get call site location as array. */ function callSiteLocation(callSite) { var file = callSite.getFileName() || '' var line = callSite.getLineNumber() var colm = callSite.getColumnNumber() if (callSite.isEval()) { file = callSite.getEvalOrigin() + ', ' + file } var site = [file, line, colm] site.callSite = callSite site.name = callSite.getFunctionName() return site } /** * Generate a default message from the site. */ function defaultMessage(site) { var callSite = site.callSite var funcName = site.name var typeName = callSite.getTypeName() // make useful anonymous name if (!funcName) { funcName = '' } // make useful type name if (typeName === 'Function') { typeName = callSite.getThis().name || typeName } return callSite.getMethodName() ? typeName + '.' + funcName : funcName } /** * Format deprecation message without color. */ function formatPlain(msg, caller, stack) { var timestamp = new Date().toUTCString() var formatted = timestamp + ' ' + this._namespace + ' deprecated ' + msg // add stack trace if (this._traced) { for (var i = 0; i < stack.length; i++) { formatted += '\n at ' + callSiteToString(stack[i]) } return formatted } if (caller) { formatted += ' at ' + formatLocation(caller) } return formatted } /** * Format deprecation message with color. */ function formatColor(msg, caller, stack) { var formatted = '\x1b[36;1m' + this._namespace + '\x1b[22;39m' // bold cyan + ' \x1b[33;1mdeprecated\x1b[22;39m' // bold yellow + ' \x1b[0m' + msg + '\x1b[39m' // reset // add stack trace if (this._traced) { for (var i = 0; i < stack.length; i++) { formatted += '\n \x1b[36mat ' + callSiteToString(stack[i]) + '\x1b[39m' // cyan } return formatted } if (caller) { formatted += ' \x1b[36m' + formatLocation(caller) + '\x1b[39m' // cyan } return formatted } /** * Format call site location. */ function formatLocation(callSite) { return relative(basePath, callSite[0]) + ':' + callSite[1] + ':' + callSite[2] } /** * Get the stack as array of call sites. */ function getStack() { var limit = Error.stackTraceLimit var obj = {} var prep = Error.prepareStackTrace Error.prepareStackTrace = prepareObjectStackTrace Error.stackTraceLimit = Math.max(10, limit) // capture the stack Error.captureStackTrace(obj) // slice this function off the top var stack = obj.stack.slice(1) Error.prepareStackTrace = prep Error.stackTraceLimit = limit return stack } /** * Capture call site stack from v8. */ function prepareObjectStackTrace(obj, stack) { return stack } /** * Return a wrapped function in a deprecation message. */ function wrapfunction(fn, message) { if (typeof fn !== 'function') { throw new TypeError('argument fn must be a function') } var args = createArgumentsString(fn.length) var deprecate = this var stack = getStack() var site = callSiteLocation(stack[1]) site.name = fn.name var deprecatedfn = eval('(function (' + args + ') {\n' + '"use strict"\n' + 'log.call(deprecate, message, site)\n' + 'return fn.apply(this, arguments)\n' + '})') return deprecatedfn } /** * Wrap property in a deprecation message. */ function wrapproperty(obj, prop, message) { if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) { throw new TypeError('argument obj must be object') } var descriptor = Object.getOwnPropertyDescriptor(obj, prop) if (!descriptor) { throw new TypeError('must call property on owner object') } if (!descriptor.configurable) { throw new TypeError('property must be configurable') } var deprecate = this var stack = getStack() var site = callSiteLocation(stack[1]) // set site name site.name = prop // convert data descriptor if ('value' in descriptor) { descriptor = convertDataDescriptorToAccessor(obj, prop, message) } var get = descriptor.get var set = descriptor.set // wrap getter if (typeof get === 'function') { descriptor.get = function getter() { log.call(deprecate, message, site) return get.apply(this, arguments) } } // wrap setter if (typeof set === 'function') { descriptor.set = function setter() { log.call(deprecate, message, site) return set.apply(this, arguments) } } Object.defineProperty(obj, prop, descriptor) } /** * Create DeprecationError for deprecation */ function DeprecationError(namespace, message, stack) { var error = new Error() var stackString Object.defineProperty(error, 'constructor', { value: DeprecationError }) Object.defineProperty(error, 'message', { configurable: true, enumerable: false, value: message, writable: true }) Object.defineProperty(error, 'name', { enumerable: false, configurable: true, value: 'DeprecationError', writable: true }) Object.defineProperty(error, 'namespace', { configurable: true, enumerable: false, value: namespace, writable: true }) Object.defineProperty(error, 'stack', { configurable: true, enumerable: false, get: function () { if (stackString !== undefined) { return stackString } // prepare stack trace return stackString = createStackString.call(this, stack) }, set: function setter(val) { stackString = val } }) return error } nodejs-depd-1.0.0/lib/000077500000000000000000000000001240647674600144725ustar00rootroot00000000000000nodejs-depd-1.0.0/lib/compat/000077500000000000000000000000001240647674600157555ustar00rootroot00000000000000nodejs-depd-1.0.0/lib/compat/buffer-concat.js000066400000000000000000000007541240647674600210370ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ /** * Module exports. */ module.exports = bufferConcat /** * Concatenate an array of Buffers. */ function bufferConcat(bufs) { var length = 0 for (var i = 0, len = bufs.length; i < len; i++) { length += bufs[i].length } var buf = new Buffer(length) var pos = 0 for (var i = 0, len = bufs.length; i < len; i++) { bufs[i].copy(buf, pos) pos += bufs[i].length } return buf } nodejs-depd-1.0.0/lib/compat/callsite-tostring.js000066400000000000000000000042441240647674600217660ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ /** * Module exports. */ module.exports = callSiteToString /** * Format a CallSite file location to a string. */ function callSiteFileLocation(callSite) { var fileName var fileLocation = '' if (callSite.isNative()) { fileLocation = 'native' } else if (callSite.isEval()) { fileName = callSite.getScriptNameOrSourceURL() if (!fileName) { fileLocation = callSite.getEvalOrigin() } } else { fileName = callSite.getFileName() } if (fileName) { fileLocation += fileName var lineNumber = callSite.getLineNumber() if (lineNumber != null) { fileLocation += ':' + lineNumber var columnNumber = callSite.getColumnNumber() if (columnNumber) { fileLocation += ':' + columnNumber } } } return fileLocation || 'unknown source' } /** * Format a CallSite to a string. */ function callSiteToString(callSite) { var addSuffix = true var fileLocation = callSiteFileLocation(callSite) var functionName = callSite.getFunctionName() var isConstructor = callSite.isConstructor() var isMethodCall = !(callSite.isToplevel() || isConstructor) var line = '' if (isMethodCall) { var methodName = callSite.getMethodName() var typeName = getConstructorName(callSite) if (functionName) { if (typeName && functionName.indexOf(typeName) !== 0) { line += typeName + '.' } line += functionName if (methodName && functionName.lastIndexOf('.' + methodName) !== functionName.length - methodName.length - 1) { line += ' [as ' + methodName + ']' } } else { line += typeName + '.' + (methodName || '') } } else if (isConstructor) { line += 'new ' + (functionName || '') } else if (functionName) { line += functionName } else { addSuffix = false line += fileLocation } if (addSuffix) { line += ' (' + fileLocation + ')' } return line } /** * Get constructor name of reviver. */ function getConstructorName(obj) { var receiver = obj.receiver return (receiver.constructor && receiver.constructor.name) || null } nodejs-depd-1.0.0/lib/compat/index.js000066400000000000000000000023511240647674600174230ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ /** * Module exports. */ lazyProperty(module.exports, 'bufferConcat', function bufferConcat() { return Buffer.concat || require('./buffer-concat') }) lazyProperty(module.exports, 'callSiteToString', function callSiteToString() { var limit = Error.stackTraceLimit var obj = {} var prep = Error.prepareStackTrace function prepareObjectStackTrace(obj, stack) { return stack } Error.prepareStackTrace = prepareObjectStackTrace Error.stackTraceLimit = 2 // capture the stack Error.captureStackTrace(obj) // slice the stack var stack = obj.stack.slice() Error.prepareStackTrace = prep Error.stackTraceLimit = limit return stack[0].toString ? toString : require('./callsite-tostring') }) /** * Define a lazy property. */ function lazyProperty(obj, prop, getter) { function get() { var val = getter() Object.defineProperty(obj, prop, { configurable: true, enumerable: true, value: val }) return val } Object.defineProperty(obj, prop, { configurable: true, enumerable: true, get: get }) } /** * Call toString() on the obj */ function toString(obj) { return obj.toString() } nodejs-depd-1.0.0/package.json000066400000000000000000000016371240647674600162210ustar00rootroot00000000000000{ "name": "depd", "description": "Deprecate all the things", "version": "1.0.0", "author": "Douglas Christopher Wilson ", "license": "MIT", "keywords": [ "deprecate", "deprecated" ], "repository": "dougwilson/nodejs-depd", "devDependencies": { "benchmark": "1.0.0", "beautify-benchmark": "0.2.4", "istanbul": "0.3.2", "mocha": "~1.21.4", "should": "~4.0.4" }, "files": [ "lib/", "History.md", "LICENSE", "index.js", "Readme.md" ], "engines": { "node": ">= 0.6" }, "scripts": { "bench": "node benchmark/index.js", "test": "mocha --reporter spec --bail --require should test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --require should test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --require should test/" } } nodejs-depd-1.0.0/test/000077500000000000000000000000001240647674600147035ustar00rootroot00000000000000nodejs-depd-1.0.0/test/fixtures/000077500000000000000000000000001240647674600165545ustar00rootroot00000000000000nodejs-depd-1.0.0/test/fixtures/cool-lib.js000066400000000000000000000003071240647674600206120ustar00rootroot00000000000000 var deprecate1 = require('../..')('cool-lib') var deprecate2 = require('../..')('neat-lib') exports.cool = function () { deprecate1('cool') } exports.neat = function () { deprecate2('neat') } nodejs-depd-1.0.0/test/fixtures/multi-lib.js000066400000000000000000000003151240647674600210070ustar00rootroot00000000000000 var deprecate1 = require('../..')('multi-lib') var deprecate2 = require('../..')('multi-lib-other') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } nodejs-depd-1.0.0/test/fixtures/my-lib.js000066400000000000000000000031651240647674600203100ustar00rootroot00000000000000 var deprecate = require('../..')('my-lib') exports.fn = fn exports.prop = 'thingie' exports.old = function () { deprecate('old') } exports.old2 = function () { deprecate('old2') } exports.oldfn = deprecate.function(fn, 'oldfn') exports.oldfnauto = deprecate.function(fn) exports.oldfnautoanon = deprecate.function(function () {}) exports.propa = 'thingie' exports.propauto = 'thingie' Object.defineProperty(exports, 'propget', { configurable: true, value: 'thingie', writable: false }) Object.defineProperty(exports, 'propdyn', { configurable: true, get: function () { return 'thingie' }, set: function () {} }) Object.defineProperty(exports, 'propgetter', { configurable: true, get: function () { return 'thingie' } }) Object.defineProperty(exports, 'propsetter', { configurable: true, set: function () {} }) deprecate.property(exports, 'propa', 'propa gone') deprecate.property(exports, 'propauto') deprecate.property(exports, 'propdyn') deprecate.property(exports, 'propget') deprecate.property(exports, 'propgetter') deprecate.property(exports, 'propsetter') exports.automsg = function () { deprecate() } exports.automsgnamed = function automsgnamed() { deprecate() } exports.automsganon = function () { (function () { deprecate() }()) } exports.fnprop = function thefn() {} exports.fnprop.propa = 'thingie' exports.fnprop.propautomsg = 'thingie' deprecate.property(exports.fnprop, 'propa', 'fn propa gone') deprecate.property(exports.fnprop, 'propautomsg') exports.layerfn = function () { exports.oldfn() } exports.layerprop = function () { exports.propa } function fn(a1, a2) { return a2 } nodejs-depd-1.0.0/test/fixtures/new-lib.js000066400000000000000000000001371240647674600204500ustar00rootroot00000000000000 var deprecate = require('../..')('new-lib') exports.old = function () { deprecate('old') } nodejs-depd-1.0.0/test/fixtures/old-lib.js000066400000000000000000000005001240647674600204270ustar00rootroot00000000000000 var deprecate1 = require('../..')('old-lib') var deprecate2 = require('../..')('old-lib-other') var deprecate3 = require('../..')('my-cool-module') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } exports.oldfunction = function () { deprecate3('oldfunction') } nodejs-depd-1.0.0/test/fixtures/script.js000066400000000000000000000001251240647674600204140ustar00rootroot00000000000000 var oldlib = require('./old-lib') run() function run() { oldlib.oldfunction() } nodejs-depd-1.0.0/test/fixtures/thing-lib.js000066400000000000000000000001411240647674600207630ustar00rootroot00000000000000 var deprecate = require('../..')('thing-lib') exports.old = function () { deprecate('old') } nodejs-depd-1.0.0/test/fixtures/trace-lib.js000066400000000000000000000003151240647674600207530ustar00rootroot00000000000000 var deprecate1 = require('../..')('trace-lib') var deprecate2 = require('../..')('trace-lib-other') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } nodejs-depd-1.0.0/test/test.js000066400000000000000000000506241240647674600162270ustar00rootroot00000000000000 var basename = require('path').basename var bufferConcat = require('../lib/compat').bufferConcat var depd = require('..') var mylib = require('./fixtures/my-lib') var path = require('path') var script = path.join(__dirname, 'fixtures', 'script.js') var should = require('should') var spawn = require('child_process').spawn describe('depd(namespace)', function () { it('creates deprecated function', function () { depd('test').should.be.a.function }) it('requires namespace', function () { depd.bind().should.throw(/namespace.*required/) }) }) describe('deprecate(message)', function () { it('should log namespace', function () { function callold() { mylib.old() } captureStderr(callold).should.containEql('my-lib') }) it('should log deprecation', function () { function callold() { mylib.old() } captureStderr(callold).should.containEql('deprecate') }) it('should log message', function () { function callold() { mylib.old() } captureStderr(callold).should.containEql('old') }) it('should log call site', function () { function callold() { mylib.old() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.match(/\.js:[0-9]+:[0-9]+/) }) it('should log call site regardless of Error.stackTraceLimit', function () { function callold() { mylib.old() } var limit = Error.stackTraceLimit try { Error.stackTraceLimit = 1 var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.match(/\.js:[0-9]+:[0-9]+/) } finally { Error.stackTraceLimit = limit } }) it('should log call site within eval', function () { function callold() { eval('mylib.old()') } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql(':1:') stderr.should.match(/\.js:[0-9]+:[0-9]+/) }) it('should only warn once per call site', function () { function callold() { for (var i = 0; i < 5; i++) { mylib.old() // single call site process.stderr.write('invoke ' + i + '\n') } } var stderr = captureStderr(callold) stderr.split('deprecated').should.have.length(2) stderr.split('invoke').should.have.length(6) }) it('should warn for different fns on same call site', function () { var prop function callold() { mylib[prop]() // call from same site } prop = 'old' captureStderr(callold).should.containEql(basename(__filename)) prop = 'old2' captureStderr(callold).should.containEql(basename(__filename)) }) it('should warn for different calls on same line', function () { function callold() { mylib.old(), mylib.old() } var stderr = captureStderr(callold) var fileline = stderr.match(/\.js:[0-9]+:/) stderr.should.containEql(basename(__filename)) stderr.split('deprecated').should.have.length(3) stderr.split(fileline[0]).should.have.length(3) }) describe('when message omitted', function () { it('should generate message for method call on named function', function () { function callold() { mylib.automsgnamed() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.containEql(' Object.automsgnamed ') }) it('should generate message for function call on named function', function () { function callold() { var fn = mylib.automsgnamed fn() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.containEql(' automsgnamed ') }) it('should generate message for method call on unnamed function', function () { function callold() { mylib.automsg() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.containEql(' Object.exports.automsg ') }) it('should generate message for function call on unnamed function', function () { function callold() { var fn = mylib.automsg fn() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.containEql(' exports.automsg ') }) it('should generate message for function call on anonymous function', function () { function callold() { mylib.automsganon() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.match(/ exports\.automsganon | /) }) }) describe('when output supports colors', function () { var stderr before(function () { function callold() { mylib.old() } stderr = captureStderr(callold, true) }) it('should log in color', function () { stderr.should.not.be.empty stderr.should.containEql('\x1b[') }) it('should log namespace', function () { stderr.should.containEql('my-lib') }) it('should log deprecation', function () { stderr.should.containEql('deprecate') }) it('should log message', function () { stderr.should.containEql('old') }) it('should log call site', function () { stderr.should.containEql(basename(__filename)) stderr.should.match(/\.js:[0-9]+:[0-9]+/) }) }) describe('when output does not support colors', function () { var stderr before(function () { function callold() { mylib.old() } stderr = captureStderr(callold, false) }) it('should not log in color', function () { stderr.should.not.be.empty stderr.should.not.containEql('\x1b[') }) it('should log namespace', function () { stderr.should.containEql('my-lib') }) it('should log timestamp', function () { stderr.should.match(/\w+, \d+ \w+ \d{4} \d{2}:\d{2}:\d{2} \w{3}/) }) it('should log deprecation', function () { stderr.should.containEql('deprecate') }) it('should log message', function () { stderr.should.containEql('old') }) it('should log call site', function () { stderr.should.containEql(basename(__filename)) stderr.should.match(/\.js:[0-9]+:[0-9]+/) }) }) }) describe('deprecate.function(fn, message)', function () { it('should thrown when not given function', function () { var deprecate = depd('test') deprecate.function.bind(deprecate, 2).should.throw(/fn.*function/) }) it('should log on call to function', function () { function callold() { mylib.oldfn() } captureStderr(callold).should.containEql(' oldfn ') }) it('should have same arity', function () { mylib.oldfn.should.have.length(2) }) it('should pass arguments', function () { var ret function callold() { ret = mylib.oldfn(1, 2) } captureStderr(callold).should.containEql(' oldfn ') ret.should.equal(2) }) it('should show call site outside scope', function () { function callold() { mylib.layerfn() } var stderr = captureStderr(callold) stderr.should.containEql(' oldfn ') stderr.should.match(/test.js:[0-9]+:[0-9]+/) }) it('should only warn once per call site', function () { function callold() { for (var i = 0; i < 5; i++) { mylib.oldfn() // single call site process.stderr.write('invoke ' + i + '\n') } } var stderr = captureStderr(callold) stderr.split('deprecated').should.have.length(2) stderr.split('invoke').should.have.length(6) }) it('should handle rapid calling of deprecated thing', function () { function callold() { for (var i = 0; i < 10000; i++) { mylib.oldfn() } } var stderr = captureStderr(callold) stderr.split('deprecated').should.have.length(2) }) it('should warn for different calls on same line', function () { function callold() { mylib.oldfn(), mylib.oldfn() } var stderr = captureStderr(callold) var fileline = stderr.match(/\.js:[0-9]+:/) stderr.should.containEql(basename(__filename)) stderr.split('deprecated').should.have.length(3) stderr.split(fileline[0]).should.have.length(3) }) describe('when message omitted', function () { it('should generate message for method call on named function', function () { function callold() { mylib.oldfnauto() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.containEql(' Object.fn ') stderr.should.match(/ at [^:]+test\.js:/) }) it('should generate message for method call on anonymous function', function () { function callold() { mylib.oldfnautoanon() } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.match(/ /) stderr.should.match(/ at [^:]+test\.js:/) }) }) }) describe('deprecate.property(obj, prop, message)', function () { it('should throw when given primitive', function () { var deprecate = depd('test') deprecate.property.bind(deprecate, 2).should.throw(/obj.*object/) }) it('should throw when given missing property', function () { var deprecate = depd('test') var obj = {} deprecate.property.bind(deprecate, obj, 'blargh').should.throw(/property.*owner/) }) it('should throw when given non-configurable property', function () { var deprecate = depd('test') var obj = {} Object.defineProperty(obj, 'thing', {value: 'thingie'}) deprecate.property.bind(deprecate, obj, 'thing').should.throw(/property.*configurable/) }) it('should log on access to property', function () { function callprop() { mylib.propa } var stderr = captureStderr(callprop) stderr.should.containEql(' deprecated ') stderr.should.containEql(' propa gone ') }) it('should log on setting property', function () { var val function callprop() { val = mylib.propa } function setprop() { mylib.propa = 'newval' } var stderr = captureStderr(setprop) stderr.should.containEql(' deprecated ') stderr.should.containEql(' propa gone ') captureStderr(callprop).should.containEql(' deprecated ') val.should.equal('newval') }) it('should only warn once per call site', function () { function callold() { for (var i = 0; i < 5; i++) { mylib.propa // single call site process.stderr.write('access ' + i + '\n') } } var stderr = captureStderr(callold) stderr.split('deprecated').should.have.length(2) stderr.split('access').should.have.length(6) }) it('should warn for different accesses on same line', function () { function callold() { mylib.propa, mylib.propa } var stderr = captureStderr(callold) var fileline = stderr.match(/\.js:[0-9]+:/) stderr.should.containEql(basename(__filename)) stderr.split('deprecated').should.have.length(3) stderr.split(fileline[0]).should.have.length(3) }) it('should show call site outside scope', function () { function callold() { mylib.layerprop() } var stderr = captureStderr(callold) stderr.should.containEql(' propa ') stderr.should.match(/test.js:[0-9]+:[0-9]+/) }) describe('when obj is a function', function () { it('should log on access to property on function', function () { function callprop() { mylib.fnprop.propa } var stderr = captureStderr(callprop) stderr.should.containEql(' deprecated ') stderr.should.containEql(' fn propa gone ') }) it('should generate message on named function', function () { function callprop() { mylib.fnprop.propautomsg } var stderr = captureStderr(callprop) stderr.should.containEql(' deprecated ') stderr.should.containEql(' thefn.propautomsg ') }) }) describe('when value descriptor', function () { it('should log on access and set', function () { function callold() { mylib.propa } function setold() { mylib.propa = 'val' } captureStderr(callold).should.containEql(' deprecated ') captureStderr(setold).should.containEql(' deprecated ') }) it('should not log on set to non-writable', function () { function callold() { mylib.propget } function setold() { mylib.propget = 'val' } captureStderr(callold).should.containEql(' deprecated ') captureStderr(setold).should.be.empty }) }) describe('when accessor descriptor', function () { it('should log on access and set', function () { function callold() { mylib.propdyn } function setold() { mylib.propdyn = 'val' } captureStderr(callold).should.containEql(' deprecated ') captureStderr(setold).should.containEql(' deprecated ') }) it('should not log on access when no accessor', function () { function callold() { mylib.propsetter } captureStderr(callold).should.be.empty }) it('should not log on set when no setter', function () { function callold() { mylib.propgetter = 'val' } captureStderr(callold).should.be.empty }) }) describe('when message omitted', function () { it('should generate message for method call on named function', function () { function callold() { mylib.propauto } var stderr = captureStderr(callold) stderr.should.containEql(basename(__filename)) stderr.should.containEql('deprecated') stderr.should.containEql(' Object.propauto ') stderr.should.match(/ at [^:]+test\.js:/) }) }) }) describe('process.on(\'deprecation\', fn)', function () { var error var stderr before(function () { process.on('deprecation', ondeprecation) function callold() { mylib.old() } stderr = captureStderr(callold) }) after(function () { process.removeListener('deprecation', ondeprecation) }) function ondeprecation(err) { error = err } it('should not write when listener exists', function () { stderr.should.be.empty }) it('should emit error', function () { error.should.be.ok }) it('should emit DeprecationError', function () { error.name.should.equal('DeprecationError') }) it('should emit DeprecationError', function () { error.name.should.equal('DeprecationError') }) it('should emit error with message', function () { error.message.should.equal('old') }) it('should emit error with namespace', function () { error.namespace.should.equal('my-lib') }) it('should emit error with proper [[Class]]', function () { Object.prototype.toString.call(error).should.equal('[object Error]') }) it('should be instanceof Error', function () { error.should.be.instanceof(Error) }) it('should emit error with proper stack', function () { var stack = error.stack.split('\n') stack[0].should.equal('DeprecationError: my-lib deprecated old') stack[1].should.match(/ at callold \(.+test\.js:[0-9]+:[0-9]+\)/) }) it('should have writable properties', function () { error.name = 'bname' error.name.should.equal('bname') error.message = 'bmessage' error.message.should.equal('bmessage') error.stack = 'bstack' error.stack.should.equal('bstack') }) }) describe('process.env.NO_DEPRECATION', function () { var error function ondeprecation(err) { error = err } beforeEach(function () { error = null }) afterEach(function () { process.removeListener('deprecation', ondeprecation) }) after(function () { process.env.NO_DEPRECATION = '' }) it('should suppress given namespace', function () { process.env.NO_DEPRECATION = 'old-lib' var oldlib = require('./fixtures/old-lib') captureStderr(oldlib.old).should.be.empty captureStderr(oldlib.old2).should.not.be.empty }) it('should suppress multiple namespaces', function () { process.env.NO_DEPRECATION = 'cool-lib,neat-lib' var coollib = require('./fixtures/cool-lib') captureStderr(coollib.cool).should.be.empty captureStderr(coollib.neat).should.be.empty }) it('should be case-insensitive', function () { process.env.NO_DEPRECATION = 'NEW-LIB' var newlib = require('./fixtures/new-lib') captureStderr(newlib.old).should.be.empty }) it('should emit "deprecation" events anyway', function () { process.env.NO_DEPRECATION = 'thing-lib' var thinglib = require('./fixtures/thing-lib') process.on('deprecation', ondeprecation) captureStderr(thinglib.old).should.be.empty error.namespace.should.equal('thing-lib') }) describe('when *', function () { it('should suppress any namespace', function () { process.env.NO_DEPRECATION = '*' var multilib = require('./fixtures/multi-lib') captureStderr(multilib.old).should.be.empty captureStderr(multilib.old2).should.be.empty }) }) }) describe('process.env.TRACE_DEPRECATION', function () { before(function () { process.env.TRACE_DEPRECATION = 'trace-lib' }) after(function () { process.env.TRACE_DEPRECATION = '' }) it('should trace given namespace', function () { var tracelib = require('./fixtures/trace-lib') function callold() { tracelib.old() } captureStderr(callold).should.containEql(' trace-lib deprecated old\n at callold (') }) it('should not trace non-given namespace', function () { var tracelib = require('./fixtures/trace-lib') function callold() { tracelib.old2() } captureStderr(callold).should.containEql(' trace-lib-other deprecated old2 at ') }) describe('when output supports colors', function () { var stderr before(function () { var tracelib = require('./fixtures/trace-lib') function callold() { tracelib.old() } stderr = captureStderr(callold, true) }) it('should log in color', function () { stderr.should.not.be.empty stderr.should.containEql('\x1b[') }) it('should log namespace', function () { stderr.should.containEql('trace-lib') }) it('should log call site in color', function () { stderr.should.containEql(basename(__filename)) stderr.should.match(/\x1b\[\d+mat callold \(/) }) }) }) describe('node script.js', function () { it('should display deprecation message', function (done) { captureChildStderr([script], function (err, stderr) { if (err) return done(err) var filename = path.relative(process.cwd(), script) stderr = stderr.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__') stderr.should.equal('__timestamp__ my-cool-module deprecated oldfunction at ' + filename + ':7:10\n') done() }) }) }) ;(function () { // --*-deprecation switches are 0.8+ // no good way to feature detect this sync var describe = /^v0\.6\./.test(process.version) ? global.describe.skip : global.describe describe('node --no-deprecation script.js', function () { it('should suppress deprecation message', function (done) { captureChildStderr(['--no-deprecation', script], function (err, stderr) { if (err) return done(err) stderr.should.be.empty done() }) }) }) describe('node --trace-deprecation script.js', function () { it('should suppress deprecation message', function (done) { captureChildStderr(['--trace-deprecation', script], function (err, stderr) { if (err) return done(err) stderr = stderr.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__') stderr.should.startWith('__timestamp__ my-cool-module deprecated oldfunction\n at run (' + script + ':7:10)\n at') done() }) }) }) }()) function captureChildStderr(args, callback) { var chunks = [] var env = {PATH: process.env.PATH} var exec = process.argv[0] var proc = spawn(exec, args, { env: env }) proc.stdout.resume() proc.stderr.on('data', function ondata(chunk) { chunks.push(chunk) }) proc.on('error', callback) proc.on('exit', function () { var stderr = bufferConcat(chunks).toString('utf8') callback(null, stderr) }) } function captureStderr(fn, color) { var chunks = [] var isTTY = process.stderr.isTTY var write = process.stderr.write process.stderr.isTTY = Boolean(color) process.stderr.write = function write(chunk, encoding) { chunks.push(new Buffer(chunk, encoding)) } try { fn() } finally { process.stderr.isTTY = isTTY process.stderr.write = write } return bufferConcat(chunks).toString('utf8') }