Better API for passing headers to SSE & WS (#99) - #102
Draft
nounder wants to merge 2 commits into
Draft
Conversation
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>
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.
Fixes #99
Summary
Replaces the awkward
Route.use+Entity.mergepattern for adding headers (e.g. CORS) to SSE/WS responses with two complementary helpers.Changes
Route.withHeaders(headers)middleware combinatorRoute.addHeaders(headers)yieldable Effect for procedural use inside handlersRouteHttpfor all response typesStartServer.upgradeEntity.mergeHeadersextracted for shared set-cookie-aware mergingTest plan
bun test test/RouteSse.test.ts test/RouteHttp.test.ts test/RouteSocket.test.ts