fix(aws-lambda): always send the metadata prelude - #101
Conversation
The Lambda runtime only sends the prelude armed by `HttpResponseStream.from()` ahead of the first `write` call. `end(chunk)` bypasses it and empty bodies never write, so buffered, empty, and empty-stream responses lost their status, headers, and cookies. Trigger the prelude with an empty write right after `from()`. The test mock now defers the prelude to the first `write` like the runtime does, so it no longer hides this.
@standard-server/aws-lambda
@standard-server/core
@standard-server/fastify
@standard-server/fetch
@standard-server/node
@standard-server/peer
@standard-server/shared
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
res.write('')forces the prelude —sendStandardResponsenow writes an empty chunk right afterHttpResponseStream.from(), so status/headers/cookies are flushed even when the body isundefined, an emptyBlob/stream, or a string sent viaend(chunk).- Test mock now matches the runtime —
fromSpyarms the prelude on the firstwriteonly (restoring the originalwritefirst) instead of emitting the prelude itself, which is what hid the bug. - New
chunked (empty)test — covers an emptyBlobbody, the case wherepipe()never callswrite. - JSDoc wording —
from()now documented as arming the prelude, sent only ahead of the firstwrite.
I verified the fix against the aws-lambda-ric source and with a local reproduction: HttpResponseStream.from() only sets _onBeforeFirstWrite, the patched request.write emits the prelude when status is still ready, and Node's Writable.end(chunk) bypasses the overridden public write (so end(chunk) indeed skipped the prelude). Replicating the RIC logic against a raw TCP server shows the prelude + 8-byte delimiter emitted exactly once for empty, JSON, and streamed bodies, with no spurious empty HTTP chunk. Reverting res.write('') makes exactly the three named tests fail, so the new coverage is real. Tests, type:check, and eslint on the changed files all pass.
DeepSeek Flash (default — pick a model for stronger reviews) | 𝕏

On AWS Lambda,
sendStandardResponsecould send a response without its metadata prelude, so the status, headers, and cookies were lost.HttpResponseStream.from()only sets the prelude up, and the runtime sends it just before the firstwrite()call.end(chunk)skips that, and empty bodies never callwrite(). The fix makes one empty write right afterfrom()so every response sends its prelude.Fixes
undefined, empty-Blob, and empty-stream responses now send their status, headers, and cookies. Before, they reached the wire as just the body, or as zero bytes.Testing
aws-lambda-ric4.0.2 (3.3.0 uses the same prelude logic). The bytes were captured by a local HTTP server standing in for the Lambda API. Every body type now carries the prelude and delimiter exactly once. Not tested on a real Lambda deployment.write()like the runtime. Without the fix, 3 tests fail:buffered (empty),buffered (json), and the newchunked (empty).