feat(sse): allow returning EventStream directly from handlers#1508
Conversation
EventStream now extends HTTPResponse, so handlers can `return eventStream` instead of `return eventStream.send()`. The existing `toResponse` / HTTPResponse branch renders it as the response (streaming the readable side with the SSE headers), so no core/response changes are needed. `send()` is kept for backward compatibility. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughSSE handlers now return ChangesSSE direct response flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/sse.test.ts (1)
58-62: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert all guaranteed SSE headers.
This regression test checks status and
Content-Type, but it would still pass ifcache-controlorx-accel-bufferingregressed. Add assertions for those headers while leaving the conditionalconnectionheader runtime-dependent.Proposed test additions
expect(res.headers.get("Content-Type")).toBe("text/event-stream"); + expect(res.headers.get("cache-control")).toBe( + "private, no-cache, no-store, no-transform, must-revalidate, max-age=0", + ); + expect(res.headers.get("x-accel-buffering")).toBe("no");🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/sse.test.ts` around lines 58 - 62, Extend the SSE response assertions in the test around the existing status and Content-Type checks to also verify the guaranteed cache-control and x-accel-buffering headers. Do not assert the connection header, since its value is runtime-dependent, and preserve the existing message-count assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/sse.test.ts`:
- Around line 58-62: Extend the SSE response assertions in the test around the
existing status and Content-Type checks to also verify the guaranteed
cache-control and x-accel-buffering headers. Do not assert the connection
header, since its value is runtime-dependent, and preserve the existing
message-count assertion.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: de9a9bcc-6b89-459d-ab35-021e31253c8f
📒 Files selected for processing (6)
docs/1.guide/901.advanced/2.websocket.mddocs/2.utils/2.response.mdexamples/server-sent-events.mjssrc/utils/event-stream.tssrc/utils/internal/event-stream.tstest/sse.test.ts
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Summary
Handlers can now
return eventStreaminstead ofreturn eventStream.send().EventStreamextendsHTTPResponse, so the existingtoResponse→prepareResponseBodybranch (val instanceof HTTPResponse) renders it as theresponse directly — streaming the readable side with the SSE headers. No
core /
response.tschanges were needed.Details
EventStream extends HTTPResponse: the transform stream is created beforesuper()so its readable side becomes the response body, withstatus: 200and the SSE headers passed to
super().eventStreamHeaders(event)helper shared by the direct-returnpath and
setEventStreamHeaders().send()is kept and fully backward-compatible (still stages headers onevent.resand returns the readable), soreturn eventStream.send()keepsworking.
event.res.headersstill merge in as before;HEAD/ null-bodyhandling comes for free via the
HTTPResponsepath.Tests
text/event-streamand status 200 (runs in both web + node matrix).return eventStream).🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Tests