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 ctor by default #15346

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
27 changes: 7 additions & 20 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ const {
isArrayBufferView,
isUint8Array
} = require('internal/util/types');
const {
pendingDeprecation
} = process.binding('config');
const errors = require('internal/errors');

const internalBuffer = require('internal/buffer');
Expand Down Expand Up @@ -131,33 +128,23 @@ const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
'methods instead.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be more explicit on how to fix this problem. This specific text does not guide the user on resolving the actual problem itself, we might want to add a link to a guide on how to update the relevant code.


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 :
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();
showDeprecation();
// Common case.
if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {
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