From 0eaff92eba7b4b92f47161b208bb199c18222a51 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Thu, 29 Mar 2018 13:02:52 -0700 Subject: [PATCH 1/3] buffer: do not emit deprecation notice on Buffer.of --- lib/buffer.js | 11 +++++++++++ test/parallel/test-buffer-of-no-deprecation.js | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/parallel/test-buffer-of-no-deprecation.js diff --git a/lib/buffer.js b/lib/buffer.js index 45c1282f2d9503..c5c023711eb8af 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -237,6 +237,17 @@ Buffer.from = function from(value, encodingOrOffset, length) { ); }; +// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated +// Buffer() constructor. +// +// Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of +Buffer.of = (...items) => { + const newObj = allocate(items.length); + for (var k = 0; k < items.length; k++) + newObj[k] = items[k]; + return newObj; +}; + Object.setPrototypeOf(Buffer, Uint8Array); // The 'assertSize' method will remove itself from the callstack when an error diff --git a/test/parallel/test-buffer-of-no-deprecation.js b/test/parallel/test-buffer-of-no-deprecation.js new file mode 100644 index 00000000000000..92d78a541575dc --- /dev/null +++ b/test/parallel/test-buffer-of-no-deprecation.js @@ -0,0 +1,8 @@ +// Flags: --pending-deprecation --no-warnings +'use strict'; + +const common = require('../common'); + +process.on('warning', common.mustNotCall()); + +Buffer.of(0, 1); From 9491d3ba7980d1072561d3a0cb15c2625d107e10 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Thu, 29 Mar 2018 14:04:58 -0700 Subject: [PATCH 2/3] fixup! buffer: do not emit deprecation notice on Buffer.of --- lib/buffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index c5c023711eb8af..df497dc3c8e31c 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -242,7 +242,7 @@ Buffer.from = function from(value, encodingOrOffset, length) { // // Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of Buffer.of = (...items) => { - const newObj = allocate(items.length); + const newObj = createUnsafeBuffer(items.length); for (var k = 0; k < items.length; k++) newObj[k] = items[k]; return newObj; From 4be99628ed45cd35d0e6220ec0a29cc651c23a55 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Fri, 30 Mar 2018 00:03:14 -0700 Subject: [PATCH 3/3] fixup! buffer: do not emit deprecation notice on Buffer.of Buffer.of.name === 'of' --- lib/buffer.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index df497dc3c8e31c..92ec9a7d38e58d 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -238,15 +238,18 @@ Buffer.from = function from(value, encodingOrOffset, length) { }; // Identical to the built-in %TypedArray%.of(), but avoids using the deprecated -// Buffer() constructor. +// Buffer() constructor. Must use arrow function syntax to avoid automatically +// adding a `prototype` property and making the function a constructor. // // Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of -Buffer.of = (...items) => { +// Refs: https://esdiscuss.org/topic/isconstructor#content-11 +const of = (...items) => { const newObj = createUnsafeBuffer(items.length); for (var k = 0; k < items.length; k++) newObj[k] = items[k]; return newObj; }; +Buffer.of = of; Object.setPrototypeOf(Buffer, Uint8Array);