buffer: runtime deprecation of calling Buffer without new
PR-URL: https://github.com/nodejs/node/pull/8169 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
379d9162a2
commit
f2fe5583c4
@ -64,7 +64,17 @@ function alignPool() {
|
|||||||
* much breakage at this time. It's not likely that the Buffer constructors
|
* much breakage at this time. It's not likely that the Buffer constructors
|
||||||
* would ever actually be removed.
|
* would ever actually be removed.
|
||||||
**/
|
**/
|
||||||
|
var newBufferWarned = false;
|
||||||
function Buffer(arg, encodingOrOffset, length) {
|
function Buffer(arg, encodingOrOffset, length) {
|
||||||
|
if (!new.target && !newBufferWarned) {
|
||||||
|
newBufferWarned = true;
|
||||||
|
process.emitWarning(
|
||||||
|
'Using Buffer without `new` will soon stop working. ' +
|
||||||
|
'Use `new Buffer()`, or preferably ' +
|
||||||
|
'`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.',
|
||||||
|
'DeprecationWarning'
|
||||||
|
);
|
||||||
|
}
|
||||||
// Common case.
|
// Common case.
|
||||||
if (typeof arg === 'number') {
|
if (typeof arg === 'number') {
|
||||||
if (typeof encodingOrOffset === 'string') {
|
if (typeof encodingOrOffset === 'string') {
|
||||||
|
17
test/parallel/test-buffer-deprecated.js
Normal file
17
test/parallel/test-buffer-deprecated.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
|
const expected =
|
||||||
|
'Using Buffer without `new` will soon stop working. ' +
|
||||||
|
'Use `new Buffer()`, or preferably ' +
|
||||||
|
'`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.';
|
||||||
|
|
||||||
|
process.on('warning', common.mustCall((warning) => {
|
||||||
|
assert.strictEqual(warning.name, 'DeprecationWarning');
|
||||||
|
assert.strictEqual(warning.message, expected,
|
||||||
|
`unexpected error message: "${warning.message}"`);
|
||||||
|
}, 1));
|
||||||
|
|
||||||
|
Buffer(1);
|
||||||
|
Buffer(1);
|
Loading…
x
Reference in New Issue
Block a user