fix(appkit): harden SSE response headers in setupHeaders#331
Merged
MarioCadenas merged 3 commits intomainfrom Apr 30, 2026
Merged
fix(appkit): harden SSE response headers in setupHeaders#331MarioCadenas merged 3 commits intomainfrom
MarioCadenas merged 3 commits intomainfrom
Conversation
- Set Content-Type to text/event-stream; charset=utf-8 - Change Cache-Control to no-cache, no-transform (prevents proxies from buffering/transforming the stream) - Add X-Accel-Buffering: no to disable nginx/proxy response buffering (respected by Cloudflare, AWS ALB, GCP, and most corporate proxies) - Remove Connection: keep-alive (forbidden by HTTP/2; Node manages it) - Remove Content-Encoding: none (invalid header value; causes 502/RST in strict intermediaries) - Write a ": ok\n\n" sentinel comment immediately after flushHeaders so any buffering proxy is forced to release the first chunk to the client before the upstream generator yields its first real event. Prevents "Failed to fetch" on cold-start SQL warehouses. Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
Writing res.write() synchronously inside setupHeaders fires before the caller attaches error handlers to the response stream. A failed write can emit an async error event on the stream that is not caught by the try/catch, potentially surfacing as a connection reset. The heartbeat already keeps the stream alive during cold starts, so the sentinel is redundant and risky. Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
- Update ServingPlugin._handleStream to use the same hardened headers as SSEWriter (charset=utf-8, no-transform, X-Accel-Buffering: no, drop Connection and Content-Encoding) - Update all test assertions to reflect the new header values Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
atilafassina
approved these changes
Apr 30, 2026
Contributor
atilafassina
left a comment
There was a problem hiding this comment.
Nice detective work! 🚀
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Improves the SSE response headers set by
SSEWriter.setupHeaders()to be more correct and proxy-friendly.Why
The previous header set had two issues that could cause problems in real deployments:
Connection: keep-aliveis forbidden by the HTTP/2 spec and was being stripped or rejected by HTTP/2 intermediaries anyway. Node.js manages keep-alive internally.Content-Encoding: noneis not a valid header value and could trigger502 Bad GatewayorRST_STREAMerrors in strict proxies (nginx, AWS ALB, GCP LB, Cloudflare).There were also two missing proxy-buffering mitigations:
Cache-Control: no-transformtells proxies not to modify the response body (important for streaming).X-Accel-Buffering: nois the de-facto header for disabling response buffering in nginx and nginx-backed CDNs/proxies (Cloudflare, AWS CloudFront, GCP, and most corporate reverse proxies honour it).Changes
packages/appkit/src/stream/sse-writer.tsConnectionandContent-Encodingremoved;X-Accel-Buffering: noadded