From 4db3b0e4f364897fd08fa95a48425806953778fd Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Mon, 25 Sep 2017 12:04:55 +0300 Subject: [PATCH] buffer: runtime-deprecate Buffer(num) by default --- lib/buffer.js | 22 ++++++++----------- ...recation.js => test-buffer-deprecation.js} | 2 +- ...ingdep-map.js => test-buffer-nodep-map.js} | 6 ++--- 3 files changed, 13 insertions(+), 17 deletions(-) rename test/parallel/{test-buffer-pending-deprecation.js => test-buffer-deprecation.js} (93%) rename test/parallel/{test-buffer-nopendingdep-map.js => test-buffer-nodep-map.js} (55%) diff --git a/lib/buffer.js b/lib/buffer.js index 7a44ca0f260539..d5813b27bfe0cf 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -133,11 +133,8 @@ const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' + 'Buffer.allocUnsafe(), or Buffer.from() construction ' + 'methods instead.'; -function showFlaggedDeprecation() { +function showDeprecation() { if (bufferWarn) { - // This is a *pending* deprecation warning. It is not emitted by - // default unless the --pending-deprecation command-line flag is - // used or the NODE_PENDING_DEPRECATION=1 env var is set. process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005'); bufferWarn = false; } @@ -145,23 +142,21 @@ function showFlaggedDeprecation() { const doFlaggedDeprecation = pendingDeprecation ? - showFlaggedDeprecation : + showDeprecation : function() {}; /** - * 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) { - doFlaggedDeprecation(); // Common case. if (typeof arg === 'number') { + showDeprecation(); if (typeof encodingOrOffset === 'string') { throw new errors.TypeError( 'ERR_INVALID_ARG_TYPE', 'string', 'string', arg @@ -169,6 +164,7 @@ function Buffer(arg, encodingOrOffset, length) { } return Buffer.alloc(arg); } + doFlaggedDeprecation(); return Buffer.from(arg, encodingOrOffset, length); } diff --git a/test/parallel/test-buffer-pending-deprecation.js b/test/parallel/test-buffer-deprecation.js similarity index 93% rename from test/parallel/test-buffer-pending-deprecation.js rename to test/parallel/test-buffer-deprecation.js index 060eae2a51622e..93eed4d51dc43a 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);