fix(res.send): send a raw ArrayBuffer as binary instead of an empty JSON object - #7415
Closed
bilashcse wants to merge 2 commits into
Closed
fix(res.send): send a raw ArrayBuffer as binary instead of an empty JSON object#7415bilashcse wants to merge 2 commits into
bilashcse wants to merge 2 commits into
Conversation
res.send() only treated ArrayBuffer views (TypedArray/DataView) as binary, so a raw ArrayBuffer fell through to res.json() and was serialized as an empty object with a JSON content type. Raw ArrayBuffer and SharedArrayBuffer bodies are now copied into a Buffer and sent as application/octet-stream unless a Content-Type is already set.
Contributor
|
Duplicate of #7410 |
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.
Closes #7362
Problem
res.send()treatsBufferand ArrayBuffer views (TypedArray, DataView) as binary, butArrayBuffer.isView()returns false for a rawArrayBuffer. A rawArrayBuffertherefore falls through tores.json()and is sent as an empty object withContent-Type: application/jsoninstead of its bytes.Fix
The object branch of
res.send()now also accepts a rawArrayBuffer(andSharedArrayBuffer). The bytes are copied into aBufferwithBuffer.from(), and when noContent-Typehas been set the response type defaults tobin(application/octet-stream), mirroring what already happens for views. Views keep their current behaviour and are not copied.Detection uses a small private
isArrayBuffer()helper based on the internal class rather thaninstanceof, so buffers created in another realm are handled too. The JSDoc forres.send()was updated to listArrayBufferas an accepted body type.Tests
test/res.send.jsgains a.send(ArrayBuffer)suite: one test asserts the response isapplication/octet-streamand contains the exact bytes, and one asserts that an explicitly setContent-Typeis preserved.Notes
I could not run the test suite locally, so CI is the source of truth for this change. There are already open pull requests for this issue (#7363 and #7410); happy to close this one if a maintainer prefers either of those.