Skip to content

fix: send raw ArrayBuffer and SharedArrayBuffer as binary - #7419

Closed
haoku123 wants to merge 1 commit into
expressjs:masterfrom
haoku123:fix/res-send-arraybuffer
Closed

fix: send raw ArrayBuffer and SharedArrayBuffer as binary#7419
haoku123 wants to merge 1 commit into
expressjs:masterfrom
haoku123:fix/res-send-arraybuffer

Conversation

@haoku123

Copy link
Copy Markdown

Problem

Closes #7362

res.send() handles Buffer and TypedArray views via ArrayBuffer.isView(), but a bare ArrayBuffer — the standard binary type returned by fetch, Web Crypto, WebSocket, FileReader and similar APIs — falls through to the JSON branch:

res.send(new ArrayBuffer(10))
// Sends: {} (Content-Type: application/json)
// Expected: 10 zero bytes (Content-Type: application/octet-stream)

ArrayBuffer.isView(new ArrayBuffer(...)) returns false, so the #6285 fix that added Uint8Array/DataView support missed bare buffers.

Solution

Add an isArrayBuffer() check in lib/response.js that converts to Buffer and applies the binary content type, consistent with the existing ArrayBuffer.isView branch. SharedArrayBuffer is covered too:

} else if (isArrayBuffer(chunk)) {
  chunk = Buffer.from(chunk);
  if (!this.get('Content-Type')) {
    this.type('bin');
  }
}

Tests

Four tests in test/res.send.js:

  • ArrayBuffer with explicit Content-Type sends bytes unchanged
  • ArrayBuffer without Content-Type gets application/octet-stream
  • Same two cases for SharedArrayBuffer

Full suite passes (npm test → 1264 passing), lint clean.

Note

Re-implements the stalled #7363 (author inactive since July) on current master, with the same behavior and test coverage.

res.send() handled Buffer and TypedArray views via ArrayBuffer.isView(),
but a bare ArrayBuffer (or SharedArrayBuffer) - the standard binary type
returned by fetch, Web Crypto, WebSocket and FileReader - fell through
to the JSON branch and was silently sent as {} with a JSON content type.

Convert bare ArrayBuffers to Buffer and apply the binary content type,
consistent with the existing isView branch.

Fixes expressjs#7362
@krzysdz

krzysdz commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

The author of #7363 is not inactive and has given a reaction (rocket emoji) on your comment. Closing as duplicate.

@myselfsiddharth

Copy link
Copy Markdown

The author of #7363 is not inactive and has given a reaction (rocket emoji) on your comment. Closing as duplicate.

A rocket for you too !
Dw, ive got plenty

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

3 participants