Skip to content

Commit

Permalink
Ensure consistent behavior of Buffer#subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Mar 1, 2024
1 parent e3c6f06 commit 154e4b6
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,7 @@ Buffer.prototype.slice = function slice (start, end) {

if (end < start) end = start

const newBuf = this.subarray(start, end)
// Return an augmented `Uint8Array` instance
Object.setPrototypeOf(newBuf, Buffer.prototype)

return newBuf
return this.subarray(start, end)
}

/*
Expand Down Expand Up @@ -1813,6 +1809,19 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) {
return this
}

// A number of mobile web views do not seem to implement the
// ES2016 "subarray instantiates child class" behavior. Test
// for this and monkey-patch subarray if need be. Only check
// for this once we're done setting up the Buffer prototype.
// See: https://github.com/feross/buffer/issues/329
if (!(createBuffer(1).subarray(0, 1) instanceof Buffer)) {
Buffer.prototype.subarray = function subarray() {
const buf = Uint8Array.prototype.subarray.apply(this, arguments)
Object.setPrototypeOf(buf, Buffer.prototype)
return buf
}
}

// CUSTOM ERRORS
// =============

Expand Down

0 comments on commit 154e4b6

Please sign in to comment.