diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 0b4d4986e16e64..d41a7e4260010d 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -72,7 +72,7 @@ be used. ### DEP0005: Buffer() constructor -Type: Runtime (supports [`--pending-deprecation`][]) +Type: Runtime The `Buffer()` function and `new Buffer()` constructor are deprecated due to API usability issues that can potentially lead to accidental security issues. @@ -93,10 +93,6 @@ is strongly recommended: * [`Buffer.from(string[, encoding])`][from_string_encoding] - Create a `Buffer` that copies `string`. -As of v10.0.0, a deprecation warning is printed at runtime when -`--pending-deprecation` is used or when the calling code is -outside `node_modules` in order to better target developers, rather than users. - ### DEP0006: child\_process options.customFds diff --git a/lib/buffer.js b/lib/buffer.js index 061ff7ebe440da..8219a54d717cc5 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -48,7 +48,6 @@ try { } const { customInspectSymbol, - isInsideNodeModules, normalizeEncoding, kIsEncodingSymbol } = require('internal/util'); @@ -56,9 +55,6 @@ const { isArrayBufferView, isUint8Array } = require('internal/util/types'); -const { - pendingDeprecation -} = process.binding('config'); const { ERR_BUFFER_OUT_OF_BOUNDS, ERR_INDEX_OUT_OF_RANGE, @@ -137,22 +133,12 @@ function alignPool() { } let bufferWarningAlreadyEmitted = false; -let nodeModulesCheckCounter = 0; const bufferWarning = 'Buffer() is deprecated due to security and usability ' + 'issues. Please use the Buffer.alloc(), ' + 'Buffer.allocUnsafe(), or Buffer.from() methods instead.'; -function showFlaggedDeprecation() { - if (bufferWarningAlreadyEmitted || - ++nodeModulesCheckCounter > 10000 || - (!pendingDeprecation && - isInsideNodeModules())) { - // We don't emit a warning, because we either: - // - Already did so, or - // - Already checked too many times whether a call is coming - // from node_modules and want to stop slowing down things, or - // - We aren't running with `--pending-deprecation` enabled, - // and the code is inside `node_modules`. +function showDeprecation() { + if (bufferWarningAlreadyEmitted) { return; } @@ -161,17 +147,15 @@ function showFlaggedDeprecation() { } /** - * The Buffer() constructor is deprecated in documentation and should not be - * used moving forward. Rather, developers should use one of the three new - * factory APIs: Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() based on - * their specific needs. There is no runtime deprecation because of the extent - * to which the Buffer constructor is used in the ecosystem currently -- a - * runtime deprecation would introduce too much breakage at this time. It's not - * likely that the Buffer constructors would ever actually be removed. + * The Buffer() constructor is deprecated and should not be used moving forward. + * Rather, developers should use one of the three new factory APIs: + * Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() based on their specific + * needs. It's not likely that the Buffer constructors would ever actually be + * removed. * Deprecation Code: DEP0005 */ function Buffer(arg, encodingOrOffset, length) { - showFlaggedDeprecation(); + showDeprecation(); // Common case. if (typeof arg === 'number') { if (typeof encodingOrOffset === 'string') { diff --git a/lib/internal/util.js b/lib/internal/util.js index 07515e2e090daa..67ab24b0165f3a 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -330,47 +330,6 @@ function spliceOne(list, index) { list.pop(); } -const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/; - -let getStructuredStack; - -function isInsideNodeModules() { - if (getStructuredStack === undefined) { - // Lazy-load to avoid a circular dependency. - const { runInNewContext } = require('vm'); - // Use `runInNewContext()` to get something tamper-proof and - // side-effect-free. Since this is currently only used for a deprecated API, - // the perf implications should be okay. - getStructuredStack = runInNewContext(`(function() { - Error.prepareStackTrace = function(err, trace) { - err.stack = trace; - }; - Error.stackTraceLimit = Infinity; - - return function structuredStack() { - return new Error().stack; - }; - })()`, {}, { filename: 'structured-stack' }); - } - - const stack = getStructuredStack(); - - // Iterate over all stack frames and look for the first one not coming - // from inside Node.js itself: - if (Array.isArray(stack)) { - for (const frame of stack) { - const filename = frame.getFileName(); - // If a filename does not start with / or contain \, - // it's likely from Node.js core. - if (!/^\/|\\/.test(filename)) - continue; - return kNodeModulesRE.test(filename); - } - } - return false; -} - - module.exports = { assertCrypto, cachedResult, @@ -383,7 +342,6 @@ module.exports = { getConstructorOf, getSystemErrorName, isError, - isInsideNodeModules, join, normalizeEncoding, objectToString, diff --git a/test/parallel/test-buffer-constructor-node-modules-paths.js b/test/parallel/test-buffer-constructor-node-modules-paths.js deleted file mode 100644 index c6a419f82ade79..00000000000000 --- a/test/parallel/test-buffer-constructor-node-modules-paths.js +++ /dev/null @@ -1,37 +0,0 @@ -'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', false); -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('/node_modules/a.js.js', '/b.js', true); -test('c:\\a\\node_modules\\b.js', 'c:\\a\\node_modules\\x.js', false); -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); -test('c:\\node_modules\\a.js', 'c:\\b.js', true); diff --git a/test/parallel/test-buffer-constructor-outside-node-modules.js b/test/parallel/test-buffer-constructor-outside-node-modules.js deleted file mode 100644 index 1a1e146f2dd9d7..00000000000000 --- a/test/parallel/test-buffer-constructor-outside-node-modules.js +++ /dev/null @@ -1,29 +0,0 @@ -// 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' -}); diff --git a/test/parallel/test-buffer-pending-deprecation.js b/test/parallel/test-buffer-deprecation.js similarity index 92% rename from test/parallel/test-buffer-pending-deprecation.js rename to test/parallel/test-buffer-deprecation.js index 1bb7b0bee19f9b..b3a6a85c6f2525 100644 --- a/test/parallel/test-buffer-pending-deprecation.js +++ b/test/parallel/test-buffer-deprecation.js @@ -1,4 +1,4 @@ -// Flags: --pending-deprecation --no-warnings +// Flags: --no-warnings 'use strict'; const common = require('../common'); diff --git a/test/parallel/test-buffer-nopendingdep-map.js b/test/parallel/test-buffer-nodep-map.js similarity index 55% rename from test/parallel/test-buffer-nopendingdep-map.js rename to test/parallel/test-buffer-nodep-map.js index c85d184fbc1246..e72b7f205983b8 100644 --- a/test/parallel/test-buffer-nopendingdep-map.js +++ b/test/parallel/test-buffer-nodep-map.js @@ -1,12 +1,12 @@ -// Flags: --no-warnings --pending-deprecation +// Flags: --no-warnings 'use strict'; const common = require('../common'); process.on('warning', common.mustNotCall('A warning should not be emitted')); -// With the --pending-deprecation flag, the deprecation warning for -// new Buffer() should not be emitted when Uint8Array methods are called. +// The deprecation warning for new Buffer() should not be emitted when +// Uint8Array methods are called. Buffer.from('abc').map((i) => i); Buffer.from('abc').filter((i) => i);