Repository navigation
fix(res.send): preserve raw bytes of ArrayBuffer views - #7574
jipeng6036-del wants to merge 3 commits into
Conversation
…end() A non-Buffer ArrayBuffer view reached res.end() unconverted, which 500s on small payloads, or passed through Buffer.from(view), which writes one byte per element instead of the view's bytes, silently truncating values like 0x0102 to 0x02. Wrap any non-Buffer view in a Buffer sharing its memory, so Content-Length, ETag and the body all agree on the raw bytes. Fixes expressjs#7538 (cherry picked from commit ca6d202)
Exercise byte-offset windows, cross-realm/shared views, ETags, HEAD, chunked responses and cache revalidation. Derive the original typed-array expectations from its raw storage instead of assuming little-endian order. Co-authored-by: Prince Jain <princejain756@gmail.com>
agammann
left a comment
There was a problem hiding this comment.
I traced the new Buffer conversion through res.send: it happens before Content-Length and ETag calculation, so the byte range selected by byteOffset/byteLength is also what reaches res.end. The HTTP tests exercise nonzero-offset DataView and wider typed arrays, plus HEAD and conditional 304 responses, which checks the header/body consistency this change is meant to restore.
|
|
||
| describe('res.send ArrayBuffer views', function () { | ||
| views.forEach(function (view) { | ||
| [false, 'weak', 'strong'].forEach(function (etag) { |
There was a problem hiding this comment.
Suggestion · tests
The two test sets for this fix overlap. The new file runs 13 view types x 6 HTTP cases (78 tests, each with its own app and request). The three tests added to test/res.send.js (lines 220-273: Uint16Array, Uint16Array with etag disabled, DataView) repeat cases the matrix already covers. Running the HEAD, 304 and chunked checks for every view type also tests unchanged code paths, because the fix only touches the length and Buffer wrapping in res.send. Keep the coverage in one place, and keep only the distinct behaviours: an offset window, a DataView, a wider type with ETag on and off, a large payload, and HEAD/304 once.
code-check · pragmatist, skeptic
There was a problem hiding this comment.
Thanks for pointing out the overlap. I moved the coverage into test/res.send.js and kept six HTTP regressions for offset windows, DataView, ETag on/off, a large payload, HEAD, and 304. All six fail on the original base and pass with the fix. The full suite passes (1,296 tests), and lint passes.
res.send()already treats ArrayBuffer views as binary, but non-byte TypedArrays can be truncated to one byte per element or rejected by Node, depending on ETag generation and payload size. Wrap the view's existing byte range before calculating length, hashing or sending it, so the body, Content-Length and ETag agree.Based on #7542 by Prince Jain (
ca6d20298f21a751f55c596214a5feb3f8f4902f); his implementation commit, author and source reference are retained. The regression tests intest/res.send.jscover offset windows, DataView, wider typed arrays with ETag enabled and disabled, a large response, HEAD, and 304 revalidation. They compare raw bytes without assuming the host byte order.Fixes #7538.
Validation:
51505499:npm run lintandnpm run test-cipass (1,296 tests). All six HTTP regressions fail on the base commit and pass with the fix.fca6bf6b, all 27 jobs in the unchanged fork workflows passed, including Node 16–26/Linux/Windows, lint, coverage and CodeQL: CodeQL, legacy, ci.fca6bf6b, three independently packed and installed npm artifacts were exercised with nativehttp.request, without NODE_PATH overrides: baseline 15/40, original patch 40/40, this version 40/40. Every packed file matches its source Git blob.Reproduction source and verification records are bound to commit
fca6bf6b44be148981340301e6f5ec37c1ff849c.Prepared and validated with Codex; no human review is claimed.