You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
res.send(ArrayBuffer) silently sends {} as JSON #7362
res.send() handles Buffer and TypedArray (ArrayBuffer.isView) as binary data, but a raw ArrayBuffer — the standard JavaScript binary data type — falls through to this.json(chunk), producing {}withContent-Type: application/json`.
PR #6285 (merged as 55869f4) fixed Uint8Array/DataView support by adding an ArrayBuffer.isView() check. However, ArrayBuffer.isView(new ArrayBuffer(...)) returns false, so raw ArrayBuffer was missed.
Proposed fix
Add an instanceof ArrayBuffer check in lib/response.js that converts to Buffer and sets the binary content type, consistent with the existing ArrayBuffer.isView branch.
Real-world impact
ArrayBuffer is the standard binary type used by fetch, Web Crypto, WebSocket, FileReader, and similar APIs. Passing it directly to res.send() is a common scenario that currently fails silently.
This is a good catch. The fix should also handle SharedArrayBuffer since instanceof SharedArrayBuffer would miss it — Buffer.from(new SharedArrayBuffer(10)) works but the check wouldn't reach it. Either add both instanceof ArrayBuffer and instanceof SharedArrayBuffer branches, or use a helper like typeof chunk === 'object' && chunk !== null && chunk[Symbol.toStringTag]?.endsWith('ArrayBuffer'). The existing ArrayBuffer.isView() check covers Uint8Array/DataView but not the raw buffer itself. A regression test with Buffer.alloc(size) as baseline vs new ArrayBuffer(size) vs new SharedArrayBuffer(size) would lock this down.
@cschanhniem Thanks for the feedback, good point about SharedArrayBuffer. I've updated #7363 to use the unified helper approach you suggested — an isArrayBuffer() check based on Symbol.toStringTag that covers both ArrayBuffer and SharedArrayBuffer (and as a bonus works across realms where instanceof would fail). Also added the regression tests comparing Buffer.alloc(size), new ArrayBuffer(size), and new SharedArrayBuffer(size) byte-for-byte, plus default Content-Type coverage for both.
I'd like to take this one. res.send(ArrayBuffer) silently sending {} as JSON is a type-coercion bug. Express's res.send checks the body argument and routes based on typeof. ArrayBuffer is an object with no enumerable props, so it falls into the object branch, gets JSON.stringify'd, and JSON.stringify(new ArrayBuffer(8)) is '{}'.
Root cause. In lib/response.js#send, after the Buffer check, the code calls typeof body === 'object' ? JSON.stringify(body) : String(body) (paraphrased). ArrayBuffer matches typeof === 'object', so it hits JSON.stringify and produces '{}'. The Content-Type is set to application/json, but the body is wrong.
Alternatives I considered: (a) Add a body instanceof ArrayBuffer branch that writes the buffer directly with application/octet-stream (smallest, IMO). (b) Use node:util.isArrayBuffer and Buffer.from(arrayBuffer) to normalize to a Buffer (cleaner, also fixes TypedArrays). (c) Document: 'use res.send(Buffer.from(arrayBuffer))' (workaround).
Suggestion to maintainer: (b) is the right shape — also fixes DataView / TypedArray in one go. Two questions: (i) Does Express want to detect Uint8Array (not just ArrayBuffer) and convert via Buffer.from(uint8)? (ii) Should TypedArray.send() set Content-Type to application/octet-stream, or sniff the original ArrayBuffer.userAgent / accept header? I'd add a test that sends an 8-byte ArrayBuffer and asserts the response body has 8 bytes. (claiming via @LeonxLJX)
Solid catch. For anyone hitting this before it's merged: res.send(Buffer.from(someArrayBuffer)) sidesteps it completely today, since Buffer.from correctly wraps a raw ArrayBuffer and that path already gets the right content-type handling. Worth remembering if you're piping straight from fetch()/Web Crypto output into res.send().
Activity
cschanhniem commentedon Jul 8, 2026
This is a good catch. The fix should also handle
SharedArrayBuffersinceinstanceof SharedArrayBufferwould miss it —Buffer.from(new SharedArrayBuffer(10))works but the check wouldn't reach it. Either add bothinstanceof ArrayBufferandinstanceof SharedArrayBufferbranches, or use a helper liketypeof chunk === 'object' && chunk !== null && chunk[Symbol.toStringTag]?.endsWith('ArrayBuffer'). The existingArrayBuffer.isView()check coversUint8Array/DataViewbut not the raw buffer itself. A regression test withBuffer.alloc(size)as baseline vsnew ArrayBuffer(size)vsnew SharedArrayBuffer(size)would lock this down.myselfsiddharth commentedon Jul 9, 2026
@cschanhniem Thanks for the feedback, good point about SharedArrayBuffer. I've updated #7363 to use the unified helper approach you suggested — an isArrayBuffer() check based on Symbol.toStringTag that covers both ArrayBuffer and SharedArrayBuffer (and as a bonus works across realms where instanceof would fail). Also added the regression tests comparing Buffer.alloc(size), new ArrayBuffer(size), and new SharedArrayBuffer(size) byte-for-byte, plus default Content-Type coverage for both.
haoku123 commentedon Aug 14, 2026
Opened #7419 to fix this — sends bare ArrayBuffer/SharedArrayBuffer as binary instead of {} JSON.
myselfsiddharth commentedon Aug 14, 2026
@haoku123 Thanks !
dajiaohuang commentedon Sep 3, 2026
I created PR #7453 which fixes this issue by adding an instanceof ArrayBuffer check to handle raw ArrayBuffer like TypedArray views.
LeonxLJX commentedon Sep 4, 2026
I'd like to take this one.
res.send(ArrayBuffer)silently sending{}as JSON is a type-coercion bug. Express'sres.sendchecks thebodyargument and routes based ontypeof.ArrayBufferis an object with no enumerable props, so it falls into the object branch, getsJSON.stringify'd, andJSON.stringify(new ArrayBuffer(8))is'{}'.Root cause. In
lib/response.js#send, after theBuffercheck, the code callstypeof body === 'object' ? JSON.stringify(body) : String(body)(paraphrased).ArrayBuffermatchestypeof === 'object', so it hits JSON.stringify and produces'{}'. The Content-Type is set toapplication/json, but the body is wrong.Alternatives I considered: (a) Add a
body instanceof ArrayBufferbranch that writes the buffer directly withapplication/octet-stream(smallest, IMO). (b) Usenode:util.isArrayBufferandBuffer.from(arrayBuffer)to normalize to a Buffer (cleaner, also fixes TypedArrays). (c) Document: 'use res.send(Buffer.from(arrayBuffer))' (workaround).Suggestion to maintainer: (b) is the right shape — also fixes
DataView/TypedArrayin one go. Two questions: (i) Does Express want to detectUint8Array(not justArrayBuffer) and convert viaBuffer.from(uint8)? (ii) ShouldTypedArray.send()set Content-Type toapplication/octet-stream, or sniff the originalArrayBuffer.userAgent/acceptheader? I'd add a test that sends an 8-byte ArrayBuffer and asserts the response body has 8 bytes. (claiming via @LeonxLJX)usualdesigner commentedon Sep 12, 2026
Solid catch. For anyone hitting this before it's merged: res.send(Buffer.from(someArrayBuffer)) sidesteps it completely today, since Buffer.from correctly wraps a raw ArrayBuffer and that path already gets the right content-type handling. Worth remembering if you're piping straight from fetch()/Web Crypto output into res.send().