Skip to content

Commit

Permalink
buffer: do not emit deprecation notice on Buffer.of
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
TimothyGu authored and jasnell committed Apr 16, 2018
1 parent daef2e7 commit d6ce4ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-buffer-of-no-deprecation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Flags: --pending-deprecation --no-warnings
'use strict';

const common = require('../common');

process.on('warning', common.mustNotCall());

Buffer.of(0, 1);

0 comments on commit d6ce4ec

Please sign in to comment.