Skip to content

Latest commit

 

History

History
205 lines (170 loc) · 10.7 KB

README.md

File metadata and controls

205 lines (170 loc) · 10.7 KB

Reference: a notebook on the recommended stack

What a new application looks like on the stack greenfield.md recommends: one Cloudflare Worker over D1, Yielded Auth for sign-in, pages rendered by the Worker as the person reading them, and Remote, Guard and Site in the page. It is small on purpose: one entity, three screens, three writes. Every seam a real application crosses is crossed once, and tested on a local workerd and in Chromium.

A shared notebook. Anyone may sign up; a member reads every note, and writes, edits and deletes only their own. Another member's note joins the list as it is written.

It imports only published names (foldkit-plus/…, foldkit, effect, Yielded, Drizzle), as an application outside this repository would.

Who owns what

Fact Owner Where
What a note and a member are; a title's and a body's limits the domain, declared once domain.ts; the table's CHECKs are compiled from it
Who may write a note: its author the guard; the server decides, the page is told (may) guard.ts, server.ts
The notes and the accounts D1 server.ts, auth.ts, migrations/
Who a request is Yielded's session cookie, read by AuthServer.host's resolvers auth.ts, worker.ts
What the page shows of the notes Remote's cache, a field of the Model main.ts
A note added that the server has not answered Remote's outbox, in this browser's localStorage, shared by its tabs main.ts (durable), client.ts
Which page, its title, who may see it the site tree; the route is in the Model main.ts
The first paint the Worker, rendering as the reader page.ts, worker.ts
How it looks the shipped theme (AppStyle.make({})) and recipes style.ts, view.ts

Nothing keeps state of its own in the page: Remote's cache, the forms' drafts and the route are fields of one Model, changed only by update. The outbox's store is a copy of the Model's queue of unsent notes, kept so a reload can read it back; the server decides each note once it arrives.

The shape

One Worker answers everything:

fetch(request, env)
  /assets/*  -> env.ASSETS                    the page's build (Vite)
  /auth/*    -> Yielded's routes              sign up, sign in, sign out
  /remote    -> serveFetch                    reads, writes, live streams over D1
  otherwise  -> a page, rendered as the reader into the build's index.html

A page is read in the Worker's own process, as the session's member, with no HTTP to itself, and handed to the browser with what it read, so the browser asks for none of it again:

GET /  -> pageReader (the navigation's principal) -> Data.satisfy(actives) -> 303, or render
       -> the browser adopts the page (SSR.hydrate) -> Remote reads only what it lacks

A write is shown before the server answers, and the server decides it:

Add -> NameNote (id) -> Data.mutate(AddNote, { durable })   the note, in its place, and its author's count, at once
    -> the outbox stores it, then sends it (one tab sends; every tab shows it)
    -> POST /remote -> what the Note sets from the context (author, time) -> GuardServer.writer: the guard, the decode, the CHECKs -> D1
    -> the answer, reduced into the Model; another member's page hears it from pollLive

The notebook reads its list live (liveQuery(Data, Notes, {}, { … })): the page's stream names the list as its read does, and the Worker's pollLive re-reads it inside that stream's own request.

Run it

pnpm --filter foldkit-example-reference dev      # build the page, start the Worker, print its origin

Nothing needs an account: the Worker runs on a local workerd, configured by wrangler.toml through foldkit-plus/testing/workers, with a fresh, migrated D1. A change to anything the Worker bundles, or to wrangler.toml, swaps it in place, keeping D1; a change to the page needs pnpm dev again.

From the repository root:

npx vitest run examples/reference                                       # unit and Worker tests
npx vitest run --config vitest.e2e.config.ts examples/reference         # Chromium, end to end
pnpm mutate examples/reference/test/reference.mutations.ts              # each test can fail

Walking through it

The domain (domain.ts, guard.ts). A Member and a Note are Entities, related by the note's author. A member's noteCount is Derived.count(Note.relations.author): the server counts, the page predicts. A note's author and its time are Entity.sets over Expr.viewer(Member) and Expr.now: no input sets them, the server fills them from who asks and the write's time, and the page predicts them, so a new note shows in its place in the newest-first list at once. The title's and the body's limits are Schema checks, in code points, which is what SQL counts. The guard says a note is written by its author; Guards.may has the server tell the page which notes the reader may edit.

The server (server.ts). The Entities are bound to D1's tables through Drizzle. Adding, editing and deleting are declared writes (Mutation.write), landed by the guarded writer, which judges the rule, the row's revision and the decode in the statement that writes. The notes table's CHECKs come from checkConstraints, its revision trigger from revisionTrigger, and the log pollLive reads from rowChangeLog: test/migrations.test.ts holds migrations/ to what each returns, so no rule is written by hand.

The writer keeps a ledger (write_ledger, migrations/0003_write_ledger.sql): each write it lands is recorded by who asked, the operation and the row. A write whose answer was lost and is sent again under the same operation, as the page's Retry does, is answered with its row, where a create of a taken id would otherwise be refused; the same operation id sent with another request is refused. D1 has no transactions, so the row and its record are written in one batch, and two copies sent at once land once.

Auth (authApi.ts, auth.ts). Yielded's password strategy over @yielded/auth-persistence-drizzle/D1. Sign-up writes the accounts row in the batch that binds the email, so no account exists half made. The Worker builds Yielded's services once per isolate. Every scripted call's Origin is checked against one configured origin, the ORIGIN var, not the host the request arrived at, so a second host reaching the same Worker (workers.dev beside a domain) is refused. A deploy sets ORIGIN in [vars]; the local stack gives the Worker its own (vars: origin => ({ ORIGIN: origin }) in stack.ts), and a Worker without one answers everything but its assets with a 500 naming it.

The page (main.ts, view.ts, forms.ts). Each screen declares what it reads with a Surface; Site makes the notebook (and the note pages below it) members' only, and the sign-in page visitors'. A sign-in or a sign-out loads the next page, which the Worker renders as the new reader, so the page never forgets a principal in place. Yielded's client is a chunk of its own, loaded by the first sign-in (Resource.lazy). Hovering a note's link reads its page ahead (Site.routing's preload).

Unsent notes (main.ts's durable, client.ts). An add is a durable write: the outbox stores it in localStorage before sending it, sends it again until the server answers, and reads it back after a reload. The Worker renders a page with none of that queue, since it cannot read the browser's store, and the page's first client view must be the one it sent. So the browser starts from the outbox's session (SSR.hydrate's prepare), which mints the id this page's writes are named by and draws nothing, and the queue joins the page a moment later, through the outbox's own Subscription, as any Message would:

Worker:  page drawn from D1 (no queue)
browser: SSR.hydrate -> prepare: durable.session -> adopts the Worker's page as it is
         -> outbox.restore reads localStorage -> OutboxRead -> the unsent notes join the list

The ledger makes a resend safe: a note the server wrote but whose answer was lost is answered from its record. Edits and deletes are plain writes.

The Worker (worker.ts, page.ts). Its order is foldkit-host's Host.routes: the build's assets, then the ORIGIN check, then /auth/ and /remote, then pages. Yielded's services and the routes over them are built once per D1 binding (Host.isolate). The template is the build's index.html, read through the assets binding once per isolate (Host.template). The build writes its id into it (Host.buildTag in vite.config.ts), and the Worker renders under that id, so a page is always adopted by its own build's script. Each page's description (the plan's meta) comes in its rendered root, and SSR.headed moves it into the template's head, where a link preview reads it. Stack bundles the Worker with esbuild and brands its views as the Foldkit plugin brands the page's: unbranded, Foldkit found every served element's view identity missing and rebuilt the whole page on hydration.

When something fails

  • A write the server refuses (another's note, a title too long) changes nothing it stores. The note leaves the list, and the page lists the failed write with Retry and Dismiss (Data.failedWrites).
  • An add nothing answered (offline, or a gateway's error page) stays in the outbox, sent again after a wait and as soon as the browser is back online, across a reload; the page says how many notes are not sent yet. The ledger answers a resend that already landed with its row, so it is written once.
  • An edit or a delete nothing answered fails as Unreachable: the server may or may not have written it, and the page says so. Retry sends the same operation, which the ledger answers the same way.
  • A session that ended reads nothing: the page is rendered unread, and the browser's read is refused with a reason. A page that needs a member sends the reader to sign in, naming where they were.
  • A live stream that breaks reconnects with backoff, and what it missed is read again. The stream asks every five seconds who it serves, so a sign-out in another tab ends it.
  • A page that cannot be read is a 500 that logs why in the Worker.

Limits

  • No password reset or email: Yielded's management actions are not served.
  • The deploy is left out: pnpm build writes dist/client and dist/worker.js, and wrangler d1 migrations apply and wrangler deploy take it from there, with a real database_id in wrangler.toml.

See also