Skip to content

Better API for passing headers to SSE & WS (#99) - #102

Draft
nounder wants to merge 2 commits into
mainfrom
cursor/fix-99-sse-ws-headers-c215
Draft

Better API for passing headers to SSE & WS (#99)#102
nounder wants to merge 2 commits into
mainfrom
cursor/fix-99-sse-ws-headers-c215

Conversation

@nounder

@nounder nounder commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Fixes #99

Summary

Replaces the awkward Route.use + Entity.merge pattern for adding headers (e.g. CORS) to SSE/WS responses with two complementary helpers.

Changes

  • Route.withHeaders(headers) middleware combinator
  • Route.addHeaders(headers) yieldable Effect for procedural use inside handlers
  • Accumulated headers applied in RouteHttp for all response types
  • WS handshake receives headers via extended StartServer.upgrade
  • Entity.mergeHeaders extracted for shared set-cookie-aware merging

Test plan

  • bun test test/RouteSse.test.ts test/RouteHttp.test.ts test/RouteSocket.test.ts
Open in Web Open in Cursor 

cursoragent and others added 2 commits August 12, 2026 00:46
Co-authored-by: Ralph Gutkowski <nounder@users.noreply.github.com>
Replaces the Route.use + Entity.merge workaround for adding headers
(e.g. CORS) to a route's response with:

- Route.withHeaders(headers): a middleware combinator used via
  Route.use(Route.withHeaders({...})) that merges headers onto every
  response produced downstream, including Route.sse streams and the
  Route.ws upgrade handshake.
- Route.addHeaders(headers): a yieldable Effect for adding headers
  procedurally from inside any generator handler.

Both accumulate onto a new per-request Route.RouteHeaders context
reference. RouteHttp merges the accumulated headers onto the final
entity before building the Response; Route.ws reads them eagerly,
before upgrading, so headers set via middleware reach the WebSocket
handshake response (StartServer.upgrade and BunServer's Bun.upgrade
now accept an optional headers argument).

Also extracts Entity.mergeHeaders out of Entity.merge so the same
set-cookie-aware header merging logic can be reused by Route.addHeaders.

Co-authored-by: Ralph Gutkowski <nounder@users.noreply.github.com>
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.

Figure out a better way to pass headers to SSE & WS

2 participants