docs: edgeHandler — WebSocket on Cloudflare Workers / edge
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+42
-5
No files matched your search
@@ -13,6 +13,7 @@ green-tea uses calendar versioning: `YY.MM.PATCH`.
|
|||||||
(WebSocket and mesh remain Node-only).
|
(WebSocket and mesh remain Node-only).
|
||||||
- **Deno adapter** (`@green-tea/core/deno`): `serveDeno(app)` runs HTTP + SSE + WebSocket on Deno.
|
- **Deno adapter** (`@green-tea/core/deno`): `serveDeno(app)` runs HTTP + SSE + WebSocket on Deno.
|
||||||
- **Bun adapter** (`@green-tea/core/bun`): `serveBun(app)` runs HTTP + SSE + WebSocket on Bun, reusing the neutral `app.upgrade` / `WsSocket` capability. WebSocket, rooms, and channels behave identically to Node and Deno.
|
- **Bun adapter** (`@green-tea/core/bun`): `serveBun(app)` runs HTTP + SSE + WebSocket on Bun, reusing the neutral `app.upgrade` / `WsSocket` capability. WebSocket, rooms, and channels behave identically to Node and Deno.
|
||||||
|
- **Cloudflare Workers / edge adapter** (`@green-tea/core/edge`): `edgeHandler(app)` runs HTTP + SSE + WebSocket on workerd, reusing the neutral `app.upgrade` / `WsSocket` capability. Requires the `nodejs_compat` compatibility flag. Green Tea now runs on Node, Deno, Bun, and the edge — with identical WebSocket, rooms, and channel behaviour on all four.
|
||||||
- **`app.upgrade(request, socket)`**: neutral WebSocket entry point for non-Node runtimes, built on a shared `WsSocket` capability. WebSocket logic is now runtime-agnostic (`src/http/ws-core.ts`).
|
- **`app.upgrade(request, socket)`**: neutral WebSocket entry point for non-Node runtimes, built on a shared `WsSocket` capability. WebSocket logic is now runtime-agnostic (`src/http/ws-core.ts`).
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
@@ -25,9 +25,10 @@ export default { fetch: app.fetch };
|
|||||||
**Node is the reference implementation** — every runtime returns the same status, headers,
|
**Node is the reference implementation** — every runtime returns the same status, headers,
|
||||||
and body for the same request (enforced by a parity test suite).
|
and body for the same request (enforced by a parity test suite).
|
||||||
|
|
||||||
**Not yet on edge runtimes:** WebSocket (`@Ws`) and mesh — still Node-only there; Deno and Bun
|
**All four runtimes now have full WebSocket support:** Node, Deno, and Bun via `serveDeno` /
|
||||||
now have full WebSocket support via `serveDeno` and `serveBun` (see below). `app.listen()` and
|
`serveBun`, and Cloudflare Workers / edge via `edgeHandler` (see below) — same graph, same
|
||||||
TLS remain Node-only; on Deno/Bun/edge you get `app.fetch`.
|
`@Ws` handlers, same rooms/channels everywhere. `app.listen()`, TLS, per-request timeouts,
|
||||||
|
and mesh remain Node-only; on Deno/Bun/edge you get `app.fetch` + the runtime's adapter.
|
||||||
|
|
||||||
**Body-size enforcement differs slightly:** on `app.fetch`, an oversized body is read in full
|
**Body-size enforcement differs slightly:** on `app.fetch`, an oversized body is read in full
|
||||||
before the `413` is returned, whereas Node aborts mid-stream once the limit is hit — so on
|
before the `413` is returned, whereas Node aborts mid-stream once the limit is hit — so on
|
||||||
@@ -54,8 +55,8 @@ run on Node. Behaviour matches the Node reference.
|
|||||||
|
|
||||||
**Advanced:** `app.upgrade(request, socket)` is the neutral primitive `serveDeno` uses.
|
**Advanced:** `app.upgrade(request, socket)` is the neutral primitive `serveDeno` uses.
|
||||||
Any runtime can build a `WsSocket` capability (`inbound`, `abort`, `isOpen`, `send`,
|
Any runtime can build a `WsSocket` capability (`inbound`, `abort`, `isOpen`, `send`,
|
||||||
`close`, `terminate`) and drive the graph — `serveBun` builds on the same primitive
|
`close`, `terminate`) and drive the graph — `serveBun` and `edgeHandler` build on the
|
||||||
(see below), and this is how future edge support will land.
|
same primitive (see below).
|
||||||
|
|
||||||
**Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. On Deno you
|
**Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. On Deno you
|
||||||
get `app.fetch` + `serveDeno`.
|
get `app.fetch` + `serveDeno`.
|
||||||
@@ -81,3 +82,38 @@ run on Node and Deno. Behaviour matches the Node reference.
|
|||||||
|
|
||||||
**Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. On Bun you
|
**Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. On Bun you
|
||||||
get `app.fetch` + `serveBun`.
|
get `app.fetch` + `serveBun`.
|
||||||
|
|
||||||
|
## Cloudflare Workers / edge
|
||||||
|
|
||||||
|
`app.fetch` covers HTTP + SSE on every runtime. WebSocket needs a runtime-specific
|
||||||
|
upgrade, so Cloudflare Workers get a dedicated adapter at `@green-tea/core/edge`:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { createApp } from '@green-tea/core';
|
||||||
|
import { edgeHandler } from '@green-tea/core/edge';
|
||||||
|
|
||||||
|
const app = createApp({ modules: [ChatModule] });
|
||||||
|
|
||||||
|
export default { fetch: edgeHandler(app) };
|
||||||
|
```
|
||||||
|
|
||||||
|
`edgeHandler` routes normal requests through `app.fetch` and WebSocket upgrades through
|
||||||
|
`WebSocketPair` and `app.upgrade` — the same neutral primitive `serveDeno` and `serveBun`
|
||||||
|
use. Same graph, same `@Ws` handlers, same rooms/channels as Node/Deno/Bun; behaviour
|
||||||
|
matches the Node reference; validated on real workerd (Miniflare).
|
||||||
|
|
||||||
|
**Requirement:** the Worker must enable the `nodejs_compat` compatibility flag —
|
||||||
|
`wrangler.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
compatibility_flags = ["nodejs_compat"]
|
||||||
|
compatibility_date = "2024-09-23" # or later
|
||||||
|
```
|
||||||
|
|
||||||
|
Green Tea's core statically imports Node built-ins that workerd only provides under
|
||||||
|
this flag; without it, the Worker fails to load.
|
||||||
|
|
||||||
|
**Still Node-only:** `app.listen()`, TLS, per-request timeouts, and mesh. Cloudflare's
|
||||||
|
Durable Objects and WebSocket Hibernation are **not** used — `edgeHandler` accepts
|
||||||
|
WebSockets with the standard `WebSocketPair` model, so a Worker holds the connection
|
||||||
|
open for its lifetime rather than hibernating between messages.
|
||||||
Reference in new issue
Block a user