Skip to content

fix(res.send): handle raw ArrayBuffer as binary data#7363

Open
myselfsiddharth wants to merge 2 commits into
expressjs:masterfrom
myselfsiddharth:fix/res-send-arraybuffer
Open

fix(res.send): handle raw ArrayBuffer as binary data#7363
myselfsiddharth wants to merge 2 commits into
expressjs:masterfrom
myselfsiddharth:fix/res-send-arraybuffer

Conversation

@myselfsiddharth

@myselfsiddharth myselfsiddharth commented Jul 6, 2026

Copy link
Copy Markdown

Summary

  • Fixes res.send(ArrayBuffer) silently sending {} as JSON with Content-Type: application/json
  • Adds a private isArrayBuffer() helper in lib/response.js based on Symbol.toStringTag, so both ArrayBuffer and SharedArrayBuffer are converted to Buffer and sent with a binary content type — consistent with the existing ArrayBuffer.isView branch from Added check to support Uint8Array in response sending #6285
  • The Symbol.toStringTag check also handles buffers from other realms, where instanceof would fail
  • Adds tests for ArrayBuffer and SharedArrayBuffer with explicit and default content types, plus a regression test comparing Buffer.alloc(size), new ArrayBuffer(size), and new SharedArrayBuffer(size) byte-for-byte (as suggested in review)

Closes #7362

Test plan

  • npm test -- test/res.send.js — all ArrayBuffer/SharedArrayBuffer tests pass
  • npm run lint — clean
  • Verified res.send(new ArrayBuffer(4)) and res.send(new SharedArrayBuffer(4)) return application/octet-stream with the correct bytes instead of {} as application/json

myselfsiddharth and others added 2 commits July 6, 2026 00:29
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant