Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: runtime-deprecate Buffer(num) by default #15608

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,42 +133,38 @@ 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;
}
}

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
);
}
return Buffer.alloc(arg);
}
doFlaggedDeprecation();
return Buffer.from(arg, encodingOrOffset, length);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Flags: --pending-deprecation --no-warnings
// Flags: --no-warnings
'use strict';

const common = require('../common');
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down