diff --git a/specs/epics/README.md b/specs/epics/README.md index 29155e3..8c678cc 100644 --- a/specs/epics/README.md +++ b/specs/epics/README.md @@ -7,5 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace. | Epic | Cluster | Status | What it is | |------|---------|--------|------------| +| [`nitro-websockets/`](./nitro-websockets/) | **WS** | In progress (WS0) | Vercel's Nitro + WebSockets Starter, its page and room kept, built by Nitro's cloudflare_durable preset into one Cloudflare Worker whose room is a Durable Object — and an Orun baseline that instantiates it in one landing (WS2) | As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`. diff --git a/specs/epics/nitro-websockets/IMPLEMENTATION-STATUS.md b/specs/epics/nitro-websockets/IMPLEMENTATION-STATUS.md new file mode 100644 index 0000000..37dfd4f --- /dev/null +++ b/specs/epics/nitro-websockets/IMPLEMENTATION-STATUS.md @@ -0,0 +1,11 @@ +# nitro-websockets — implementation status + +| Milestone | State | Landed | +|---|---|---| +| WS0 — the spec | ✅ | WS-1 | +| WS1 — the room on Workers | planned | WS-2 | +| WS2 — a baseline | planned | WS-3 | + +## Departures from the upstream template + +Recorded as they land, from WS1 on. diff --git a/specs/epics/nitro-websockets/README.md b/specs/epics/nitro-websockets/README.md new file mode 100644 index 0000000..42c6525 --- /dev/null +++ b/specs/epics/nitro-websockets/README.md @@ -0,0 +1,38 @@ +# nitro-websockets — Vercel's Nitro + WebSockets Starter on Cloudflare Workers + +**The starter is a realtime room — live cursors, presence and emoji reactions +over one WebSocket, on a Nitro v3 + React page — and Nitro ships a preset that +runs crossws's pub/sub on a Durable Object, so the port keeps the app as it +is: Vite and Nitro's own `cloudflare_durable` preset build it into one +Cloudflare Worker that sends every upgrade to one Durable Object of +hibernatable WebSockets, with the room's state moved onto its sockets so it +survives a wake, an `/api/health` route beside it, deployed by Orun to stage +and prod on every merge — and the baseline that makes a copy of it somebody +else's app in one landing.** + +| | | +|---|---| +| Status | In progress — WS0 on main | +| Upstream | [vercel/examples `websockets/nitro`](https://github.com/vercel/examples/tree/main/websockets/nitro) (MIT), at `de86723` (2026-10-16) | +| Reference | [nitro-boilerplate](https://github.com/sourceplane/nitro-boilerplate) — the same Nitro on a plain Worker, scaffolding, tests and bootstrap; [commerce](https://github.com/sourceplane/commerce) — a Durable Object declared in wrangler | +| Key | `WS` | + +## Read in this order + +1. [design.md](design.md) — the starter, how each Vercel piece maps to + Cloudflare, what the spike proved, the repository and the build, and the + alternatives rejected. +2. [implementation-plan.md](implementation-plan.md) — the milestones and + what each one lands. +3. [risks-and-open-questions.md](risks-and-open-questions.md) — what is not + proven, and what was accepted. +4. [IMPLEMENTATION-STATUS.md](IMPLEMENTATION-STATUS.md) — what shipped, and + every departure from the upstream starter. + +## Milestones + +| Milestone | What it lands | Task | +|---|---|---| +| WS0 — the spec | the repository, its intent, CI, tooling and spec index; this doc set | WS-1 | +| WS1 — the room on Workers | the starter in `apps/web`, built by the `cloudflare_durable` preset, its room on one Durable Object, its tests, the README | WS-2 | +| WS2 — a baseline | the console card, the build document, the context pack and the baseline's tests | WS-3 | diff --git a/specs/epics/nitro-websockets/design.md b/specs/epics/nitro-websockets/design.md new file mode 100644 index 0000000..667becb --- /dev/null +++ b/specs/epics/nitro-websockets/design.md @@ -0,0 +1,88 @@ +# nitro-websockets — design + +## 1. The starter + +vercel/examples `websockets/nitro` at `de86723` is a Nitro v3 +(`3.0.260610-beta`) + React 19 app built by Vite 8 through `nitro/vite`: +`app/` is the page (a hero, a live canvas of cursors and reactions, a +presence bar, three feature cards; shadcn/ui on Base UI, Tailwind 4), +`shared/types/realtime.ts` the wire protocol, and `server/api/ws.ts` one +`defineWebSocketHandler` at `/api/ws`. Each connection gets an anonymous +identity (`server/utils/identity.ts`) and subscribes to one `room` topic; +cursor moves, reactions, joins and leaves are broadcast with crossws's +`peer.publish`; the roster is a module-level `Map` replayed in the `welcome` +frame; each peer's identity sits in `peer.context`. On Vercel the preset +bridges the upgrade through `crossws/adapters/vercel` (the Vercel Functions +WebSockets beta). The template ships no tests. + +## 2. How each Vercel piece maps, and the one thing a Durable Object changes + +| Vercel | Here | +|---|---| +| The Vercel preset's socket bridge | Nitro's `cloudflare_durable` preset: the Worker forwards every upgrade to ONE Durable Object (class and binding `$DurableObject`, instance `server`), where crossws's Cloudflare adapter accepts it with `ctx.acceptWebSocket` — the Hibernation API — so `peer.publish` reaches every socket in the room | +| The function's memory for room state | Each socket's serialized attachment, and the room's live sockets | +| Vercel's CDN for the SPA | Workers Static Assets over `.output/public`; the preset's renderer answers every other page path with `index.html` | +| Deploy on push | Orun's `cloudflare-worker-turbo` lane: `wrangler deploy --env `, smoke on `/api/health` and the `/api/ws` handshake | + +**Hibernation.** A Durable Object whose sockets are quiet is evicted; the +sockets stay open, and the next message constructs the object again with new +socket wrappers. Module memory and every `peer.context` are gone; what +survives is each socket's attachment (`serializeAttachment`), where crossws +keeps the peer's id, URL and topics. The spike measured it under `wrangler +dev`: two clients connected, the room left idle 75 s, the object was +constructed again (a `cloudflare:durable:init` hook logged it) when the next +cursor arrived. With upstream's handler that cursor was dropped (`identity` +was undefined, `if (!identity) return`) and the next leave was never +announced; the roster survived only because local workerd kept the isolate's +module memory, which production does not promise. The port keeps the +handler's shape and moves the state: `rememberIdentity` writes the identity +into the attachment beside crossws's own fields (extending the state object +crossws caches on the socket, so its later writes keep it), +`recallIdentity` reads it back after a wake, and `othersOf` builds the roster +from `peer.peers` — the object's live sockets. The same idle run then passed: +the cursor arrived as its sender's, a new joiner got both identities, and +the leave reached everyone. + +## 3. The repository and the build + +nitro-boilerplate's monorepo, unchanged in shape: `apps/web` is the app, +`tests/web` its tests, `tooling/` the shared configs and the wrangler +renderer. `vite build` writes `.output/server/index.mjs` (the Worker, with +the `$DurableObject` class exported) and `.output/public` (the page's +assets); Nitro's own wrangler output is off (`cloudflare.deployConfig: +false`), and the committed `wrangler.jsonc` names both, binds the Durable +Object in every env block (wrangler does not inherit `durable_objects` into +an environment; it warns and binds nothing) and declares one top-level +`new_sqlite_classes` migration, so Workers Free runs it. The Durable Object +needs no Terraform: the first deploy creates it from the migration. + +crossws is pinned to 0.4.6 for the whole workspace: `presence.ts` relies on +how that version keeps a socket's attachment, and the tests drive the same +version through its real adapter. + +## 4. Tests + +The starter has none. `tests/web` drives the exported `room` hooks through +`crossws/adapters/cloudflare` over a stand-in Durable Object with the +Hibernation API's shape (`acceptWebSocket`, `getWebSockets`, attachments +that round-trip through structured clone): the welcome roster, a cursor fanned +out and clamped, rejected frames, ping, leave, the identity in the +attachment, and a simulated wake — new socket wrappers, a fresh adapter, no +context — after which cursors, the roster and leaves still work. Without the +attachment the wake test fails. The baseline's e2e then drives real +WebSocket clients against the built Worker under `wrangler dev`, including +an 80-second idle past a wake. + +## 5. Alternatives rejected + +- **A hand-written Durable Object** in place of the preset: it would replace + the template's handler, and the preset already does exactly this routing. +- **Nitro 2 with the same preset**: the template is Nitro v3 (`nitro/vite`, + `defineWebSocketHandler` from `nitro`); moving it back would rewrite it. +- **Deriving identities from the peer id** (no attachment writes): it would + survive a wake with public API alone, but an owner who swaps in an + authenticated user (the template's own suggestion) needs somewhere to keep + that user; the attachment is that place. +- **Durable Object storage for the roster**: the handler cannot reach the + object's storage after a wake (a restored peer carries no request context), + and the live sockets already are the roster. diff --git a/specs/epics/nitro-websockets/epic.yaml b/specs/epics/nitro-websockets/epic.yaml new file mode 100644 index 0000000..2c847e1 --- /dev/null +++ b/specs/epics/nitro-websockets/epic.yaml @@ -0,0 +1,31 @@ +# The epic's declaration. metadata.name equals the directory name; metadata.key +# is the task-key prefix this epic reserves (WS → WS-1, WS-2, …). Owner, target +# date and health are the platform's — never written here. +apiVersion: orun.io/v1 +kind: Epic +metadata: + name: nitro-websockets + key: WS +spec: + title: "Vercel's Nitro + WebSockets Starter on Cloudflare Workers" + summary: "Vercel's Nitro + WebSockets Starter — live cursors, presence and reactions over one WebSocket on a Nitro v3 + React page — runs as one Cloudflare Worker whose room is a Durable Object of hibernatable WebSockets, built by Vite and Nitro's own cloudflare_durable preset, the room's state kept with its sockets so it survives the object hibernating, inside the Cirrus baseline's repository conventions, and becomes an Orun baseline that takes a new realtime app from nothing to live on stage and prod in one landing." + state: started + milestones: + - name: "WS0 — the spec" + exitCriteria: + - "the repository is born with its intent, CI, tooling and spec index, and this doc set and the task contracts are on main" + tasks: [WS-1] + - name: "WS1 — the room on Workers" + exitCriteria: + - "apps/web is the upstream app (the React page, the wire protocol, the one WebSocket handler at /api/ws), built by Vite and the cloudflare_durable preset into the Worker and its Static Assets that the committed wrangler.jsonc names, every upgrade sent to one Durable Object, with an /api/health route and one name seam" + - "each connection's identity lives in its socket's attachment and the roster is read from the live sockets, so the room keeps working after its Durable Object hibernates" + - "`pnpm typecheck`, `pnpm lint`, `pnpm test` and `pnpm build` are green, the tests driving the room through crossws's own Cloudflare adapter across a simulated hibernation" + - "`wrangler dev` over the build serves the page and real WebSocket clients see each other's joins, cursors, reactions and leaves, before and after the room idles past a wake" + - "the README says how to run, test and deploy it, and how every Vercel piece maps to Cloudflare" + tasks: [WS-2] + - name: "WS2 — a baseline" + exitCriteria: + - "blueprint.yaml (the console card) and repo-blueprint.yaml (the build) take a new realtime app from nothing to live on stage and prod in one landing, then record it" + - "an app built from it carries the app only, renamed throughout to its own name, repository and Worker, with the example's licence intact" + - "Baseline CI is green: contract tests, generated component docs, the blueprint read by the pinned orun, the leak gate, the offline bootstrap and the end-to-end tier — the stage lane's steps in a staged job tree, the Worker smoke-tested (health and WebSocket handshake), every route read and real clients in the room" + tasks: [WS-3] diff --git a/specs/epics/nitro-websockets/implementation-plan.md b/specs/epics/nitro-websockets/implementation-plan.md new file mode 100644 index 0000000..2839b42 --- /dev/null +++ b/specs/epics/nitro-websockets/implementation-plan.md @@ -0,0 +1,38 @@ +# nitro-websockets — implementation plan + +Three milestones, one task each, each landed as one pull request. + +## WS0 — the spec (WS-1) + +The repository is born with nitro-boilerplate's frame — `intent.yaml` (dev +verify-only, stage, prod after stage; the placeholder workspace +`ws_CHANGE_ME`, so CI plans nothing until it is linked), `ci.yml` byte for +byte turso-starter's (as hono-api carries it), the shared TypeScript and +ESLint configuration, the wrangler renderer, the pnpm workspace and turbo +pipeline, and the spec index — and this doc set and the three task +contracts land on it. + +## WS1 — the room on Workers (WS-2) + +- `apps/web` is the upstream app: the page, the protocol, the handler, the + public files, upstream's dependency versions. +- `vite.config.ts` adds the `cloudflare_durable` preset and the name plugin; + `wrangler.jsonc` binds the Durable Object per environment with its + migration; `component.yaml` deploys it, smoke-testing `/api/health` and + the `/api/ws` handshake. +- `server/utils/presence.ts` keeps identities in the sockets' attachments; + `server/api/ws.ts` reads them back and builds the roster from the live + sockets; `server/api/health.ts` and `server/middleware/missing-file.ts` + are added; `shared/site.ts` is the name seam. +- `tests/web` drives the room through crossws's adapter across a simulated + wake. +- The README says how to run, test and deploy it, how the room runs on + Cloudflare, and how each Vercel piece maps. + +## WS2 — a baseline (WS-3) + +The console card, the build document (`01-product` lands the whole app in +one pull request and one convergence deploys it; `02-docs` records it), the +context pack and the product README, the rename engine and its tests, and +the baseline's tests — contract, leak, placement and the e2e with real +WebSocket clients — then the `baseline-v1` tag. diff --git a/specs/epics/nitro-websockets/risks-and-open-questions.md b/specs/epics/nitro-websockets/risks-and-open-questions.md new file mode 100644 index 0000000..d6ff677 --- /dev/null +++ b/specs/epics/nitro-websockets/risks-and-open-questions.md @@ -0,0 +1,58 @@ +# nitro-websockets — risks and open questions + +## WS-A — No live bootstrap yet (OPEN) + +Everything short of the platform is proven on every pull request: the card +against the build, the blueprint read by the pinned orun, the leak gate, the +offline bootstrap with the blueprint's own hooks, the app's own plan for the +01-product merge, and the produced app built, dry-deployed, served, +smoke-tested, read route by route and driven by real WebSocket clients in +the job tree orun stages. Not proven: the repository created and linked, the +token minted, a real `wrangler deploy` creating the Durable Object, the +smoke against workers.dev, and two browsers in the deployed room. + +## WS-B — `nitro` pinned to the template's beta (ACCEPTED) + +`3.0.260610-beta`, exactly as the template pins it. Nitro v3 is in beta; an +upgrade is a pull request that bumps the pin, regenerates the lockfile and +re-runs the tests and the e2e (the preset's class name and output layout are +what the committed config names, WS-C). + +## WS-C — The committed config names Nitro's output and class (WATCH) + +`wrangler.jsonc`'s `main` is `.output/server/index.mjs`, its assets +directory `.output/public`, and its Durable Object class `$DurableObject`: +the `cloudflare_durable` preset's today. A Nitro release that moves any of +them breaks the dry run or the deploy; the e2e runs the lane's own build and +dry run over the output on every change. + +## WS-D — One room per deployment (ACCEPTED) + +The preset sends every upgrade to one Durable Object instance (`server`), so +every visitor of an environment is in one room, as on Vercel. A room per page +or per team is an owner's change (crossws's `resolveDurableStub`). + +## WS-E — The first landing relies on orun's seeded initial commit (WATCH) + +`orun plan --changed` plans nothing for a push with no `before`; the first +landing merges into the empty commit orun seeds. Confirmed by the first live +bootstrap. + +## WS-F — stack-tectonic 0.19.0 (ACCEPTED) + +The family's one pin. + +## WS-G — The deploy smoke is a passer-by (ACCEPTED) + +The smoke opens a real WebSocket to prove the upgrade reaches the room; for +the 3 seconds it holds it, anyone on the page sees an anonymous visitor join +and leave. + +## WS-H — Identities depend on crossws's attachment state (WATCH) + +`server/utils/presence.ts` reaches the Durable Object's socket at +`peer._internal.ws` and extends the state object crossws caches there +(`_crosswsState`), so crossws's own later writes keep the identity. Neither +is public API. crossws is pinned to 0.4.6 workspace-wide, `tests/web` drives +the room through that adapter across a simulated wake, and the e2e idles real +clients past a wake; a crossws bump that changes either fails them. diff --git a/tasks/WS-1.TaskContract.yaml b/tasks/WS-1.TaskContract.yaml new file mode 100644 index 0000000..53650d4 --- /dev/null +++ b/tasks/WS-1.TaskContract.yaml @@ -0,0 +1,15 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: WS-1 + title: "The spec" +spec: + goal: The nitro-websockets repository is born with its intent, CI, tooling and spec index, and the nitro-websockets epic — declaration, doc set and task contracts — is on main + affects: + - specs/epics/nitro-websockets + - specs/epics/README.md + - tasks + doneWhen: + - epic.yaml, README, design, implementation plan, risks and status are on main + - the three task contracts exist + gates: [] diff --git a/tasks/WS-2.TaskContract.yaml b/tasks/WS-2.TaskContract.yaml new file mode 100644 index 0000000..46a14eb --- /dev/null +++ b/tasks/WS-2.TaskContract.yaml @@ -0,0 +1,17 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: WS-2 + title: "The room on Workers" +spec: + goal: Vercel's Nitro + WebSockets Starter runs as one Cloudflare Worker — the template's page and room built by Vite and Nitro's cloudflare_durable preset, every upgrade sent to one Durable Object, the room's state kept with its sockets so it survives hibernation — with its tests, and its README saying how it maps to Vercel + affects: + - apps/web + - tests/web + - pnpm-lock.yaml + - pnpm-workspace.yaml + - README.md + doneWhen: + - pnpm typecheck, lint, test and build are green + - wrangler dev over the build serves the page and real WebSocket clients see each other, before and after the room idles past a wake + gates: [] diff --git a/tasks/WS-3.TaskContract.yaml b/tasks/WS-3.TaskContract.yaml new file mode 100644 index 0000000..6a05337 --- /dev/null +++ b/tasks/WS-3.TaskContract.yaml @@ -0,0 +1,29 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: WS-3 + title: "nitro-websockets becomes an Orun baseline" +spec: + goal: blueprint.yaml and repo-blueprint.yaml take a new realtime app from nothing to live on stage and prod in one landing, held by Baseline CI + affects: + - blueprint.yaml + - repo-blueprint.yaml + - BOOTSTRAP.md + - .baseline + - .github/workflows/baseline.yml + - .github/workflows/tag.yml + - ai/context + - docs/phases + - hooks + - testing + - tooling/rebrand + - tooling/bootstrap + - tooling/catalog + - apps/web/docs + - tests/web/docs + - specs/epics/nitro-websockets + - README.md + doneWhen: + - every Baseline CI tier is green + - an app placed from it carries the app only, branded, with the example's licence intact + gates: []