From 7e7fbcfeeb9dde5ed78f68649d59372511712da8 Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Fri, 3 Jun 2016 23:51:13 +0300 Subject: [PATCH 1/4] buffer: hard-deprecate Buffer constructor --- lib/buffer.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 21f7d2f21d15a3..83d339b8b4e2f7 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,17 +55,19 @@ function alignPool() { } } +const bufferDeprecationWarning = + deprecate(() => {}, 'Buffer constructor is deprecated. ' + + 'Use 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, - * developers should use one of the three new factory APIs: Buffer.from(), - * Buffer.allocUnsafe() or Buffer.alloc() based on their specific needs. There - * is no hard deprecation because of the extent to which the Buffer constructor - * is used in the ecosystem currently -- a hard deprecation would introduce too - * much breakage at this time. It's not likely that the Buffer constructors - * would ever actually be removed. + * The Buffer() construtor is deprecated ... that is, it should not be used + * moving forward. Rather, developers should use one of the three new factory + * APIs: Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() based on their + * specific needs. It's not likely that the Buffer constructors would ever + * actually be removed. **/ function Buffer(arg, encodingOrOffset, length) { + bufferDeprecationWarning(); // Common case. if (typeof arg === 'number') { if (typeof encodingOrOffset === 'string') { From 1093f3c0dae2fa6ec2e1f1b545200b6b1f3685f7 Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Sat, 18 Jun 2016 17:46:56 +0300 Subject: [PATCH 2/4] Make sure Uint8Array methods are not affected by the deprecation --- lib/buffer.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 83d339b8b4e2f7..5c512f0f15ce4d 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1,3 +1,4 @@ +/* eslint-disable no-class-assign */ /* eslint-disable require-buffer */ 'use strict'; @@ -7,9 +8,12 @@ const { deprecate } = require('internal/util'); const bindingObj = {}; const internalUtil = require('internal/util'); -class FastBuffer extends Uint8Array {} +class Buffer extends Uint8Array {} + +// to avoid code churn... +const FastBuffer = Buffer; +Buffer = DummyBuffer; -FastBuffer.prototype.constructor = Buffer; Buffer.prototype = FastBuffer.prototype; exports.Buffer = Buffer; @@ -66,7 +70,7 @@ const bufferDeprecationWarning = * specific needs. It's not likely that the Buffer constructors would ever * actually be removed. **/ -function Buffer(arg, encodingOrOffset, length) { +function DummyBuffer(arg, encodingOrOffset, length) { bufferDeprecationWarning(); // Common case. if (typeof arg === 'number') { @@ -101,8 +105,6 @@ Buffer.from = function(value, encodingOrOffset, length) { return fromObject(value); }; -Object.setPrototypeOf(Buffer, Uint8Array); - function assertSize(size) { if (typeof size !== 'number') { const err = new TypeError('"size" argument must be a number'); From e11e640e7a1d8594ec98d50462909c3cdb935c5d Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Sun, 7 Aug 2016 19:47:27 +0300 Subject: [PATCH 3/4] simplify things --- lib/buffer.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 5c512f0f15ce4d..f2fcb91294d96f 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1,4 +1,3 @@ -/* eslint-disable no-class-assign */ /* eslint-disable require-buffer */ 'use strict'; @@ -8,11 +7,7 @@ const { deprecate } = require('internal/util'); const bindingObj = {}; const internalUtil = require('internal/util'); -class Buffer extends Uint8Array {} - -// to avoid code churn... -const FastBuffer = Buffer; -Buffer = DummyBuffer; +const FastBuffer = (() => class Buffer extends Uint8Array {})(); Buffer.prototype = FastBuffer.prototype; @@ -70,7 +65,7 @@ const bufferDeprecationWarning = * specific needs. It's not likely that the Buffer constructors would ever * actually be removed. **/ -function DummyBuffer(arg, encodingOrOffset, length) { +function Buffer(arg, encodingOrOffset, length) { bufferDeprecationWarning(); // Common case. if (typeof arg === 'number') { From c6149751db369a7860f5e6572712cbcb3a85d950 Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Thu, 11 Aug 2016 12:04:50 +0300 Subject: [PATCH 4/4] revert unnecessary change --- lib/buffer.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/buffer.js b/lib/buffer.js index f2fcb91294d96f..027cb2ab6b8346 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -100,6 +100,8 @@ Buffer.from = function(value, encodingOrOffset, length) { return fromObject(value); }; +Object.setPrototypeOf(Buffer, Uint8Array); + function assertSize(size) { if (typeof size !== 'number') { const err = new TypeError('"size" argument must be a number');