feat: add first-class WebSocket support via ctx.upgrade() and app.ws()#3774
Merged
Conversation
Adds ctx.upgrade() to the Context class with two overloads:
- Bare mode returns { socket, response } for manual event wiring
- Managed mode accepts handlers object and returns Response directly
Adds app.ws() shorthand for registering WebSocket endpoints.
Throws HttpError(400) on non-WebSocket requests.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add doc comment on isWebSocketHandlers explaining duck-typing dispatch and when it would need updating - Remove sanitizeOps/sanitizeResources: false from integration tests (both sanitizers pass cleanly with defaults) - Add test for bare-mode ctx.upgrade() with protocol option - Clarify bare-mode options example in docs and note that app.ws() is managed-mode only
- Compare upgrade header case-insensitively per RFC 6455 §4.2.1
- Document the empty-object ({}) edge case in isWebSocketHandlers
- Add test for app.ws() with protocol option
- Add test exercising the error handler callback wiring
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.
Summary
ctx.upgrade()on theContextclass with two overloads:ctx.upgrade()orctx.upgrade(options)returns{ socket, response }for manual event wiringctx.upgrade(handlers)accepts{ open, message, close, error }and returns the upgradeResponsedirectlyapp.ws(path, handlers, options?)shorthand that registers a GET route with automatic WebSocket upgrade (managed mode only)idleTimeoutandprotocoloptions forwarded toDeno.upgradeWebSocket()HttpError(400)on non-WebSocket requestsUpgradeheader check per RFC 6455 §4.2.1WebSocketHandlersandWebSocketUpgradeOptionstypes fromfreshTest plan
deno test -A packages/fresh/src/websocket_test.ts— 9/9 passctx.upgrade()andapp.ws())app.ws()shorthand echo (integration, real server)app.ws()with protocol option (integration, real server)deno fmt --check— cleandeno lint— clean🤖 Generated with Claude Code