feat: add first-class WebSocket support via ctx.upgrade() and app.ws() (#3774)

## Summary

- **`ctx.upgrade()`** on the `Context` class with two overloads:
- **Bare mode**: `ctx.upgrade()` or `ctx.upgrade(options)` returns `{
socket, response }` for manual event wiring
- **Managed mode**: `ctx.upgrade(handlers)` accepts `{ open, message,
close, error }` and returns the upgrade `Response` directly
- **`app.ws(path, handlers, options?)`** shorthand that registers a GET
route with automatic WebSocket upgrade (managed mode only)
- Both support `idleTimeout` and `protocol` options forwarded to
`Deno.upgradeWebSocket()`
- Throws `HttpError(400)` on non-WebSocket requests
- Case-insensitive `Upgrade` header check per RFC 6455 §4.2.1
- Exports `WebSocketHandlers` and `WebSocketUpgradeOptions` types from
`fresh`
- Full documentation page with examples for managed mode, bare mode,
broadcast chat, and client-side usage
This commit is contained in:
Bartek Iwańczuk
2026-04-22 19:17:43 +02:00
committed by GitHub
parent 49c4be1ac6
commit 0ab5a2ab28
8 changed files with 605 additions and 25 deletions
+7 -1
View File
@@ -20,7 +20,13 @@ export {
} from "./middlewares/ip_filter.ts";
export { csp, type CSPOptions } from "./middlewares/csp.ts";
export type { FreshConfig, ResolvedFreshConfig } from "./config.ts";
export type { Context, FreshContext, Island } from "./context.ts";
export type {
Context,
FreshContext,
Island,
WebSocketHandlers,
WebSocketUpgradeOptions,
} from "./context.ts";
export { createDefine, type Define } from "./define.ts";
export type { Method } from "./router.ts";
export { HttpError } from "./error.ts";