Browse Source

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>
v7.x
Nikolai Vavilov 8 years ago
committed by James M Snell
parent
commit
f2fe5583c4
  1. 10
      lib/buffer.js
  2. 17
      test/parallel/test-buffer-deprecated.js

10
lib/buffer.js

@ -64,7 +64,17 @@ function alignPool() {
* much breakage at this time. It's not likely that the Buffer constructors
* would ever actually be removed.
**/
var newBufferWarned = false;
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.
if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {

17
test/parallel/test-buffer-deprecated.js

@ -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…
Cancel
Save