Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure consistent behavior of Buffer#subarray (WIP) #356

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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