fix(ext/node): make Buffer.prototype methods generic-callable#33466
Merged
Conversation
Match Node's lib/buffer.js so that Buffer.prototype.<method>.call(uint8array, ...)
behaves identically to buffer.<method>(...). Without this, calls fall through
to Uint8Array.prototype methods (or to undefined slice/write helpers) and
produce wrong results.
- includes: dispatch through bidirectionalIndexOf instead of this.indexOf, so
the call doesn't resolve to Uint8Array.prototype.indexOf.
- readUIntLE/BE, readIntLE/BE: dispatch the byteLength=1/2/4 cases through
Buffer.prototype.read{U}Int{8,16,32}{LE,BE}.call(this, offset).
- toString/write fast paths: route to Buffer.prototype.{base64,utf8}{Slice,Write}.
- encodingOps.{ascii,base64,base64url,hex,latin1,ucs2,utf8,utf16le}: route
slice/write through Buffer.prototype.<encoding>{Slice,Write}.call so they
work when buf is a plain Uint8Array.
- inspect: use this.constructor.name (default Buffer on failure) and route
the hex toString through Buffer.prototype.toString.call so generic calls
produce <Uint8Array ...> in the same hex form as <Buffer ...>.
Enables parallel/test-buffer-generic-methods.js in the node_compat suite.
nathanwhitbot
commented
Apr 25, 2026
nathanwhitbot
left a comment
Contributor
Author
There was a problem hiding this comment.
Faithful port of Node's lib/buffer.js — Buffer.prototype.<method>.call(this, ...) dispatch through prototype, this.constructor.name with try/catch fallback in inspect, bidirectionalIndexOf directly in includes. Encoding ops mirror Node's standalone-helper shape via .call(buf, ...). The Buffer.prototype.slice keeps this.subarray deliberately so generic Uint8Array this stays a Uint8Array — matches Node L869. ObjectPrototypeHasOwnProperty correctly added to primordials destructure. LGTM.
prefer-primordials lint flags Buffer.prototype.<method>.call(...) as global intrinsic access. Switch to FunctionPrototypeCall imported from the primordials object.
ban-unused-ignore lint flagged the deno-lint-ignore prefer-primordials comment on the this.subarray call — the underlying violation is gone since the dispatch is on a Uint8Array prototype method, not a global intrinsic.
bartlomieju
approved these changes
Apr 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Match Node's
lib/buffer.jsso thatBuffer.prototype.<method>.call(uint8array, ...)behaves identically tobuffer.<method>(...). Without this, calls fall through toUint8Array.prototypemethods (or to undefinedslice/writehelpers) and produce wrong results.Changes
includes: dispatch throughbidirectionalIndexOfinstead ofthis.indexOf, so the call doesn't resolve toUint8Array.prototype.indexOf.readUIntLE/readUIntBE/readIntLE/readIntBE: dispatch the byteLength = 1/2/4 cases throughBuffer.prototype.read{U}Int{8,16,32}{LE,BE}.call(this, offset).toString/writefast paths: route toBuffer.prototype.{base64,utf8}{Slice,Write}.encodingOps(ascii/base64/base64url/hex/latin1/ucs2/utf8/utf16le): routeslice/writethroughBuffer.prototype.<encoding>{Slice,Write}.callso they work whenbufis a plain Uint8Array.inspect: usethis.constructor.name(defaultBufferon failure) and route the hextoStringthroughBuffer.prototype.toString.callso generic calls produce<Uint8Array ...>in the same hex form as<Buffer ...>.Enables
parallel/test-buffer-generic-methods.jsin the node_compat suite.