-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: do deprecation warning outside
node_modules
In addition to `--pending-deprecation`, emit a deprecation warning for usage of the `Buffer()` constructor for call sites that are outside of `node_modules`. The goal of this is to better target developers, rather than burdening users with an omnipresent and quickly ignored warning. This implements the result of a TSC meeting discussion from March 22, 2018. PR-URL: #19524 Refs: #19079 (comment) Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Myles Borins <[email protected]>
- Loading branch information
Showing
6 changed files
with
163 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/parallel/test-buffer-constructor-node-modules-paths.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const child_process = require('child_process'); | ||
const assert = require('assert'); | ||
const common = require('../common'); | ||
|
||
if (process.env.NODE_PENDING_DEPRECATION) | ||
common.skip('test does not work when NODE_PENDING_DEPRECATION is set'); | ||
|
||
function test(main, callSite, expected) { | ||
const { stderr } = child_process.spawnSync(process.execPath, ['-p', ` | ||
process.mainModule = { filename: ${JSON.stringify(main)} }; | ||
vm.runInNewContext('new Buffer(10)', { Buffer }, { | ||
filename: ${JSON.stringify(callSite)} | ||
});`], { encoding: 'utf8' }); | ||
if (expected) | ||
assert(stderr.includes('[DEP0005] DeprecationWarning'), stderr); | ||
else | ||
assert.strictEqual(stderr.trim(), ''); | ||
} | ||
|
||
test('/a/node_modules/b.js', '/a/node_modules/x.js', true); | ||
test('/a/node_modules/b.js', '/a/node_modules/foo/node_modules/x.js', false); | ||
test('/a/node_modules/foo/node_modules/b.js', '/a/node_modules/x.js', false); | ||
test('/node_modules/foo/b.js', '/node_modules/foo/node_modules/x.js', false); | ||
test('/a.js', '/b.js', true); | ||
test('/a.js', '/node_modules/b.js', false); | ||
test('c:\\a\\node_modules\\b.js', 'c:\\a\\node_modules\\x.js', true); | ||
test('c:\\a\\node_modules\\b.js', | ||
'c:\\a\\node_modules\\foo\\node_modules\\x.js', false); | ||
test('c:\\node_modules\\foo\\b.js', | ||
'c:\\node_modules\\foo\\node_modules\\x.js', false); | ||
test('c:\\a.js', 'c:\\b.js', true); | ||
test('c:\\a.js', 'c:\\node_modules\\b.js', false); |
29 changes: 29 additions & 0 deletions
29
test/parallel/test-buffer-constructor-outside-node-modules.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Flags: --no-warnings | ||
'use strict'; | ||
|
||
const vm = require('vm'); | ||
const assert = require('assert'); | ||
const common = require('../common'); | ||
|
||
if (new Error().stack.includes('node_modules')) | ||
common.skip('test does not work when inside `node_modules` directory'); | ||
if (process.env.NODE_PENDING_DEPRECATION) | ||
common.skip('test does not work when NODE_PENDING_DEPRECATION is set'); | ||
|
||
const bufferWarning = 'Buffer() is deprecated due to security and usability ' + | ||
'issues. Please use the Buffer.alloc(), ' + | ||
'Buffer.allocUnsafe(), or Buffer.from() methods instead.'; | ||
|
||
process.addListener('warning', common.mustCall((warning) => { | ||
assert(warning.stack.includes('this_should_emit_a_warning'), warning.stack); | ||
})); | ||
|
||
vm.runInNewContext('new Buffer(10)', { Buffer }, { | ||
filename: '/a/node_modules/b' | ||
}); | ||
|
||
common.expectWarning('DeprecationWarning', bufferWarning, 'DEP0005'); | ||
|
||
vm.runInNewContext('new Buffer(10)', { Buffer }, { | ||
filename: '/this_should_emit_a_warning' | ||
}); |