Skip to content

Commit d6ce4ec

Browse files
TimothyGujasnell
authored andcommitted
buffer: do not emit deprecation notice on Buffer.of
PR-URL: #19682 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent daef2e7 commit d6ce4ec

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/buffer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ Buffer.from = function from(value, encodingOrOffset, length) {
236236
);
237237
};
238238

239+
// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
240+
// Buffer() constructor. Must use arrow function syntax to avoid automatically
241+
// adding a `prototype` property and making the function a constructor.
242+
//
243+
// Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of
244+
// Refs: https://esdiscuss.org/topic/isconstructor#content-11
245+
const of = (...items) => {
246+
const newObj = createUnsafeBuffer(items.length);
247+
for (var k = 0; k < items.length; k++)
248+
newObj[k] = items[k];
249+
return newObj;
250+
};
251+
Buffer.of = of;
252+
239253
Object.setPrototypeOf(Buffer, Uint8Array);
240254

241255
// The 'assertSize' method will remove itself from the callstack when an error
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Flags: --pending-deprecation --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
6+
process.on('warning', common.mustNotCall());
7+
8+
Buffer.of(0, 1);

0 commit comments

Comments
 (0)