Skip to content

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

Open

Description

@myselfsiddharth

Bug

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`.

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

Cause

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.

Activity

  1. cschanhniem commented on Jul 8, 2026

    @cschanhniem

    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.

  2. myselfsiddharth commented on Jul 9, 2026

    @myselfsiddharth
    Author

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

  3. haoku123 commented on Aug 14, 2026

    @haoku123

    Opened #7419 to fix this — sends bare ArrayBuffer/SharedArrayBuffer as binary instead of {} JSON.

  4. myselfsiddharth commented on Aug 14, 2026

    @myselfsiddharth
    Author

    @haoku123 Thanks !

  5. dajiaohuang commented on Sep 3, 2026

    @dajiaohuang

    I created PR #7453 which fixes this issue by adding an instanceof ArrayBuffer check to handle raw ArrayBuffer like TypedArray views.

  6. LeonxLJX commented on Sep 4, 2026

    @LeonxLJX

    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)

  7. usualdesigner commented on Sep 12, 2026

    @usualdesigner

    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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions