Repository navigation
fix(res.send): send the bytes covered by a DataView - #7513
chiragjangiir wants to merge 2 commits into
Conversation
DataView objects reference a slice of a parent ArrayBuffer, and Buffer.from(dataView) returns an empty Buffer, so res.send() on a DataView produced HTTP 200 with Content-Length: 0 and no body. Materialise the exact byte range the view covers so the response carries the same bytes a Uint8Array would, including the byteOffset and byteLength. Closes expressjs#7510
Co-authored-by: Buffy <buffy@upstageai.com>
|
Thanks for the pointer — I checked rather than guessed, and you're right. I ran The DataView handling here is the same expression as #7511's — So there isn't anything this PR does that #7511 doesn't, and #7511 is both earlier and smaller. Closing this as a duplicate — #7511 is the one to land. Sorry for the duplicate. |

fix(res.send): send the bytes covered by a DataView
res.send()recognized aDataViewas binary (ArrayBuffer.isView()istrue), but later called
Buffer.from(dataView), which produces an emptyBuffer. A non-empty view therefore returned HTTP 200 withContent-Length: 0and an empty body.Root cause
lib/response.jsin the object case:When
chunkis aDataView, this setsContent-Type: application/octet-streambut leaves
chunkas theDataView. The length/body handling then falls into:Buffer.from(dataView)yields an empty buffer, so the view's bytes aresilently dropped. The same
Buffer.from(typedArray)behavior is correct forUint8Array(it materializes the view correctly), but aDataViewis not atyped array — it only references a slice of a parent
ArrayBuffer.Fix
After the existing
this.type('bin')guard (which preserves a manually-setContent-Type), materialise aDataViewby its exact byte range instead ofby the
DataViewitself:This preserves:
byteOffset..byteOffset + byteLength),Content-Typewhen the user set one,Uint8Arraypath (which already worked viaBuffer.from(typedArray)).Regression tests
New describe block
res.send(DataView)intest/res.send.jswith 6 cases, allpassing:
Content-TypeDataViews unchangedArrayBufferbehaviourValidation
npx mocha test/res.send.js→ 82 passing (the 6 new DataView tests included)npm test→ 1267 passingnpm run lint→ clean (no errors)Related
merging by its author) and the bare
ArrayBuffersupport work in res.send(ArrayBuffer) silently sends {} as JSON #7362.