From d6ce4ecb57575ed953dfe52d479f6ec8fef9e4a6 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Thu, 29 Mar 2018 13:02:52 -0700 Subject: [PATCH] buffer: do not emit deprecation notice on Buffer.of MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/19682 Reviewed-By: Anna Henningsen Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/buffer.js | 14 ++++++++++++++ test/parallel/test-buffer-of-no-deprecation.js | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/parallel/test-buffer-of-no-deprecation.js diff --git a/lib/buffer.js b/lib/buffer.js index 753b16933c1b57..1fbfb756f03171 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -236,6 +236,20 @@ Buffer.from = function from(value, encodingOrOffset, length) { ); }; +// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated +// 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 +// 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); // 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);