Skip to content

Commit 2e60129

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 15b5805 commit 2e60129

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ function fromJsonObject (that, object) {
217217
if (Buffer.TYPED_ARRAY_SUPPORT) {
218218
Buffer.prototype.__proto__ = Uint8Array.prototype
219219
Buffer.__proto__ = Uint8Array
220+
if (Symbol.species && Buffer[Symbol.species] === Buffer) {
221+
Object.defineProperty(Buffer, Symbol.species,
222+
{ value: null, configurable: true })
223+
}
220224
} else {
221225
// pre-set for values that may exist in the future
222226
Buffer.prototype.length = undefined

0 commit comments

Comments
 (0)