Skip to content

fix(res.send): send a raw ArrayBuffer as binary instead of an empty JSON object - #7415

Closed
bilashcse wants to merge 2 commits into
expressjs:masterfrom
bilashcse:fix/res-send-arraybuffer
Closed

fix(res.send): send a raw ArrayBuffer as binary instead of an empty JSON object#7415
bilashcse wants to merge 2 commits into
expressjs:masterfrom
bilashcse:fix/res-send-arraybuffer

Conversation

@bilashcse

Copy link
Copy Markdown

Closes #7362

Problem

res.send() treats Buffer and ArrayBuffer views (TypedArray, DataView) as binary, but ArrayBuffer.isView() returns false for a raw ArrayBuffer. A raw ArrayBuffer therefore falls through to res.json() and is sent as an empty object with Content-Type: application/json instead of its bytes.

Fix

The object branch of res.send() now also accepts a raw ArrayBuffer (and SharedArrayBuffer). The bytes are copied into a Buffer with Buffer.from(), and when no Content-Type has been set the response type defaults to bin (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 than instanceof, so buffers created in another realm are handled too. The JSDoc for res.send() was updated to list ArrayBuffer as an accepted body type.

Tests

test/res.send.js gains a .send(ArrayBuffer) suite: one test asserts the response is application/octet-stream and contains the exact bytes, and one asserts that an explicitly set Content-Type is 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.

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.
@krzysdz

krzysdz commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Duplicate of #7410

@krzysdz krzysdz marked this as a duplicate of #7410 Aug 10, 2026
@krzysdz krzysdz closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

res.send(ArrayBuffer) silently sends {} as JSON

2 participants