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);