Skip to content

Commit d2d32ea

Browse files
committed
buffer: add pending deprecation warning
The pending deprecation warning is off by default. Launch the node process with --pending-deprecation or NODE_PENDING_DEPRECATION=1 env var set. PR-URL: #11968 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a16b570 commit d2d32ea

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

lib/buffer.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222
'use strict';
2323

2424
const binding = process.binding('buffer');
25+
const config = process.binding('config');
2526
const { compare: compare_, compareOffset } = binding;
2627
const { isAnyArrayBuffer, isUint8Array } = process.binding('util');
2728
const bindingObj = {};
2829
const internalUtil = require('internal/util');
30+
const pendingDeprecation = !!config.pendingDeprecation;
2931

3032
class FastBuffer extends Uint8Array {
3133
constructor(arg1, arg2, arg3) {
3234
super(arg1, arg2, arg3);
3335
}
3436
}
35-
3637
FastBuffer.prototype.constructor = Buffer;
38+
3739
Buffer.prototype = FastBuffer.prototype;
3840

3941
exports.Buffer = Buffer;
@@ -83,6 +85,28 @@ function alignPool() {
8385
}
8486
}
8587

88+
var bufferWarn = true;
89+
const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
90+
'recommended for use due to security and usability ' +
91+
'concerns. Please use the new Buffer.alloc(), ' +
92+
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
93+
'methods instead.';
94+
95+
function showFlaggedDeprecation() {
96+
if (bufferWarn) {
97+
// This is a *pending* deprecation warning. It is not emitted by
98+
// default unless the --pending-deprecation command-line flag is
99+
// used or the NODE_PENDING_DEPRECATION=1 envvar is set.
100+
process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005');
101+
bufferWarn = false;
102+
}
103+
}
104+
105+
const doFlaggedDeprecation =
106+
pendingDeprecation ?
107+
showFlaggedDeprecation :
108+
function() {};
109+
86110
/**
87111
* The Buffer() construtor is deprecated in documentation and should not be
88112
* used moving forward. Rather, developers should use one of the three new
@@ -94,6 +118,7 @@ function alignPool() {
94118
* Deprecation Code: DEP0005
95119
**/
96120
function Buffer(arg, encodingOrOffset, length) {
121+
doFlaggedDeprecation();
97122
// Common case.
98123
if (typeof arg === 'number') {
99124
if (typeof encodingOrOffset === 'string') {
@@ -106,6 +131,12 @@ function Buffer(arg, encodingOrOffset, length) {
106131
return Buffer.from(arg, encodingOrOffset, length);
107132
}
108133

134+
Object.defineProperty(Buffer, Symbol.species, {
135+
enumerable: false,
136+
configurable: true,
137+
get() { return FastBuffer; }
138+
});
139+
109140
/**
110141
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
111142
* if value is a number.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Flags: --no-warnings --pending-deprecation
2+
'use strict';
3+
4+
const common = require('../common');
5+
const Buffer = require('buffer').Buffer;
6+
7+
process.on('warning', common.mustNotCall('A warning should not be emitted'));
8+
9+
// With the --pending-deprecation flag, the deprecation warning for
10+
// new Buffer() should not be emitted when Uint8Array methods are called.
11+
12+
Buffer.from('abc').map((i) => i);
13+
Buffer.from('abc').filter((i) => i);
14+
Buffer.from('abc').slice(1, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Flags: --pending-deprecation --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
const Buffer = require('buffer').Buffer;
6+
7+
const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
8+
'recommended for use due to security and usability ' +
9+
'concerns. Please use the new Buffer.alloc(), ' +
10+
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
11+
'methods instead.';
12+
13+
common.expectWarning('DeprecationWarning', bufferWarning);
14+
15+
new Buffer(10);

0 commit comments

Comments
 (0)