Skip to content

Commit

Permalink
Avoid augmenting subarrays post-ES2016
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Feb 4, 2024
1 parent 54885d4 commit b445655
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,8 @@ function utf16leSlice (buf, start, end) {
return res
}

let subarrayCallsChild = false;

Buffer.prototype.slice = function slice (start, end) {
const len = this.length
start = ~~start
Expand All @@ -1139,8 +1141,11 @@ 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)

if (!subarrayCallsChild) {
// Return an augmented `Uint8Array` instance
Object.setPrototypeOf(newBuf, Buffer.prototype)
}

return newBuf
}
Expand Down Expand Up @@ -2152,3 +2157,7 @@ function defineBigIntMethod (fn) {
function BufferBigIntNotDefined () {
throw new Error('BigInt not supported')
}

if (Buffer.TYPED_ARRAY_SUPPORT) {
subarrayCallsChild = createBuffer(1).subarray(0, 1) instanceof Buffer
}

0 comments on commit b445655

Please sign in to comment.