Permalink
Browse files

buffer: runtime deprecation of calling Buffer without new

PR-URL: #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>
  • Loading branch information...
seishun authored and jasnell committed Aug 18, 2016
1 parent 379d916 commit f2fe5583c434e9864a1171209908c3a24b9d54bb
Showing with 27 additions and 0 deletions.
  1. +10 −0 lib/buffer.js
  2. +17 −0 test/parallel/test-buffer-deprecated.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') {
@@ -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);

0 comments on commit f2fe558

Please sign in to comment.