fix(res.send): handle raw ArrayBuffer as binary data#7363
Open
myselfsiddharth wants to merge 2 commits into
Open
fix(res.send): handle raw ArrayBuffer as binary data#7363myselfsiddharth wants to merge 2 commits into
myselfsiddharth wants to merge 2 commits into
Conversation
Closes expressjs#7362 Co-authored-by: Cursor <cursoragent@cursor.com>
Replace the instanceof ArrayBuffer check with an isArrayBuffer helper based on Symbol.toStringTag so both ArrayBuffer and SharedArrayBuffer are sent as binary data. Also works across realms where instanceof fails. Add regression tests comparing Buffer.alloc, ArrayBuffer, and SharedArrayBuffer byte-for-byte, as suggested in review. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
res.send(ArrayBuffer)silently sending{}as JSON withContent-Type: application/jsonisArrayBuffer()helper inlib/response.jsbased onSymbol.toStringTag, so bothArrayBufferandSharedArrayBufferare converted toBufferand sent with a binary content type — consistent with the existingArrayBuffer.isViewbranch from Added check to support Uint8Array in response sending #6285Symbol.toStringTagcheck also handles buffers from other realms, whereinstanceofwould failArrayBufferandSharedArrayBufferwith explicit and default content types, plus a regression test comparingBuffer.alloc(size),new ArrayBuffer(size), andnew SharedArrayBuffer(size)byte-for-byte (as suggested in review)Closes #7362
Test plan
npm test -- test/res.send.js— all ArrayBuffer/SharedArrayBuffer tests passnpm run lint— cleanres.send(new ArrayBuffer(4))andres.send(new SharedArrayBuffer(4))returnapplication/octet-streamwith the correct bytes instead of{}asapplication/json