Skip to content

Commit 834338e

Browse files
committed
Ensure ES2016 engines construct Uint8Array (not Buffer) from Buffer.prototype.slice
In the ES2016 draft specification, TypedArray methods like %TypedArray%.prototype.subarray() call out to a constructor for the result based on the receiver. Ordinarily, the constructor is instance.constructor, but subclasses can override this using the Symbol.species property on the constructor. Buffer.prototype.slice calls out to %TypedArray%.prototype.subarray, which calls this calculated constructor with three arguments. The argument pattern doesn't correspond to a constructor for Buffer, so without setting Symbol.species appropriately, the wrong kind of result is created. This patch sets Buffer[Symbol.species] to Uint8Array when appropriate, to address the issue.
1 parent 8182ec0 commit 834338e

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

lib/buffer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ function Buffer(arg, encoding) {
7070
Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
7171
Object.setPrototypeOf(Buffer, Uint8Array);
7272

73+
if (Symbol.species && Buffer[Symbol.species] === Buffer) {
74+
Object.defineProperty(Buffer, Symbol.species,
75+
{ value: null, configurable: true });
76+
}
77+
7378

7479
function SlowBuffer(length) {
7580
if (+length != length)

0 commit comments

Comments
 (0)