From 996389c0b9bf87e3b393d06e18e722635035d579 Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Thu, 18 Aug 2016 22:03:56 +0300 Subject: [PATCH 1/5] buffer: hard-deprecate calling Buffer without new --- lib/buffer.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/buffer.js b/lib/buffer.js index e40b81a693c9fd..0b15ea4ed3c15b 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -3,6 +3,7 @@ const binding = process.binding('buffer'); const { isArrayBuffer } = process.binding('util'); +const { deprecate } = require('internal/util'); const bindingObj = {}; const internalUtil = require('internal/util'); @@ -54,6 +55,11 @@ function alignPool() { } } +const bufferDeprecationWarning = + deprecate(() => {}, 'Using Buffer without `new` will soon stop working. ' + + 'Use `new Buffer`, or preferably ' + + 'Buffer.from, Buffer.allocUnsafe or Buffer.alloc instead.'); + /** * The Buffer() construtor is "soft deprecated" ... that is, it is deprecated * in the documentation and should not be used moving forward. Rather, @@ -65,6 +71,9 @@ function alignPool() { * would ever actually be removed. **/ function Buffer(arg, encodingOrOffset, length) { + if (!(this instanceof Buffer)) { + bufferDeprecationWarning(); + } // Common case. if (typeof arg === 'number') { if (typeof encodingOrOffset === 'string') { From 63172e42c8c36c7fbe5cddd3f3af238b3bc97838 Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Fri, 19 Aug 2016 12:16:19 +0300 Subject: [PATCH 2/5] use shiny new syntax --- lib/buffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index 0b15ea4ed3c15b..74e9e7d50da0e5 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -71,7 +71,7 @@ const bufferDeprecationWarning = * would ever actually be removed. **/ function Buffer(arg, encodingOrOffset, length) { - if (!(this instanceof Buffer)) { + if (!new.target) { bufferDeprecationWarning(); } // Common case. From 54ca2d832c801b49d1f1a3ba352ea3ffadc8568b Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Fri, 19 Aug 2016 16:59:07 +0300 Subject: [PATCH 3/5] use emitWarning directly --- lib/buffer.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 74e9e7d50da0e5..8c0a19bb33f825 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -3,7 +3,6 @@ const binding = process.binding('buffer'); const { isArrayBuffer } = process.binding('util'); -const { deprecate } = require('internal/util'); const bindingObj = {}; const internalUtil = require('internal/util'); @@ -55,11 +54,6 @@ function alignPool() { } } -const bufferDeprecationWarning = - deprecate(() => {}, 'Using Buffer without `new` will soon stop working. ' + - 'Use `new Buffer`, or preferably ' + - 'Buffer.from, Buffer.allocUnsafe or Buffer.alloc instead.'); - /** * The Buffer() construtor is "soft deprecated" ... that is, it is deprecated * in the documentation and should not be used moving forward. Rather, @@ -70,9 +64,16 @@ const bufferDeprecationWarning = * 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) { - bufferDeprecationWarning(); + 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') { From fccd0386b720c5a63a9d57bb78586092ac4ae71d Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Fri, 19 Aug 2016 18:15:53 +0300 Subject: [PATCH 4/5] add backticks and parentheses --- lib/buffer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 8c0a19bb33f825..a6582aef9df696 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -70,8 +70,8 @@ function Buffer(arg, encodingOrOffset, length) { 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.', + 'Use `new Buffer()`, or preferably ' + + '`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.', 'DeprecationWarning' ); } From 91c72fab36031d8d08030e3a1a045d8013d4789a Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Fri, 19 Aug 2016 19:11:30 +0300 Subject: [PATCH 5/5] add test --- test/parallel/test-buffer-deprecated.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 test/parallel/test-buffer-deprecated.js diff --git a/test/parallel/test-buffer-deprecated.js b/test/parallel/test-buffer-deprecated.js new file mode 100644 index 00000000000000..21a9ce9ed6dcd6 --- /dev/null +++ b/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);