Skip to content

Add V2 streaming Lambda adapter and fix body-less 502 - #649

Draft
kieran-sf wants to merge 2 commits into
mainfrom
streaming-lambda-adapter-v2
Draft

Add V2 streaming Lambda adapter and fix body-less 502#649
kieran-sf wants to merge 2 commits into
mainfrom
streaming-lambda-adapter-v2

Conversation

@kieran-sf

Copy link
Copy Markdown
Contributor

Summary

  • Adds createStreamingLambdaAdapterV2, a new streaming Lambda adapter for MRT SSR that uses a socket-intercept design: it drives an unmodified Node http.ServerResponse on a fake net.Socket, parses the raw HTTP wire bytes, and forwards them to the AWS response stream. This avoids monkey-patching ServerResponse (as V1 does), improving compatibility with both Express 4 and 5. Supports br/gzip/deflate response compression with Accept-Encoding negotiation via the new StreamingCompressionConfig type. V1 remains the default.
  • Fixes body-less streaming responses returning a 502 at the MRT edge. The AWS runtime (awslambda.HttpResponseStream.from) emits the status/headers metadata prelude lazily — only just before the first write to the stream. A response that reaches end() without ever writing (302 redirects, 204/304, HEAD, bare res.end()) ships with no prelude, so the streaming edge cannot frame it and returns its own 502 InternalServerErrorException regardless of the intended status code. Both adapters now force a zero-length write before ending the stream.

Fix details

  • V1 (create-lambda-adapter.ts): adds a body-less branch in res.end() that calls writeChunk(Buffer.alloc(0)), which also flushes any active compressor.
  • V2 (create-lambda-adapter-v2.ts): writes an empty string in onReceiveHead, which fires whenever the native ServerResponse flushes its head (always, even for body-less responses).

Testing

  • New regression tests in both adapters' test suites use a faithful RIC mock (prelude emitted only on first write) — the default identity from stub cannot catch this bug. Cases: bare res.end(), body-less redirect (asserts 302), 204 No Content, and body-less compressed.
  • Verified the tests have teeth: with the fix temporarily disabled, the identity body-less cases fail; restored, the full suite passes (655 passing, 0 failing).
  • Typecheck and lint clean for the affected packages; all modified files are prettier-clean.

Changesets

  • @salesforce/mrt-utilities minor — V2 adapter.
  • @salesforce/mrt-utilities patch — body-less 502 fix.

Add createStreamingLambdaAdapterV2, a socket-intercept streaming Lambda
adapter for MRT SSR. It drives an unmodified Node http.ServerResponse
instead of monkey-patching it (better Express 4/5 compatibility), parses
the raw HTTP wire bytes, and forwards them to the AWS response stream. It
supports br/gzip/deflate compression with Accept-Encoding negotiation via
the new StreamingCompressionConfig type. V1 remains the default.

Also fix body-less streaming responses (302 redirects, 204/304, HEAD,
bare res.end()) returning a 502 at the MRT edge. The AWS runtime emits the
status/headers metadata prelude lazily, only just before the first write,
so a response that ends without writing ships no prelude and the streaming
edge cannot frame it. Both adapters now force a zero-length write before
ending. Adds regression tests using a faithful RIC mock.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant