diff --git a/specs/epics/README.md b/specs/epics/README.md index 29155e3..ad503bf 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 | |------|---------|--------|------------| +| [`turso-per-user/`](./turso-per-user/) | **TP** | In progress (TP0) | Vercel's Turso Per User Starter — a Next.js todo app with a Turso database per user, created through Turso's Platform API — on Cloudflare Workers + Static Assets through OpenNext, Turso kept as a SaaS over libSQL's HTTP protocol, Clerk's and Turso's keys as Orun secrets, and an Orun baseline that instantiates it (TP2) | As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`. diff --git a/specs/epics/turso-per-user/IMPLEMENTATION-STATUS.md b/specs/epics/turso-per-user/IMPLEMENTATION-STATUS.md new file mode 100644 index 0000000..00ee537 --- /dev/null +++ b/specs/epics/turso-per-user/IMPLEMENTATION-STATUS.md @@ -0,0 +1,11 @@ +# turso-per-user — implementation status + +| Milestone | State | Landed | +|---|---|---| +| TP0 — the spec | ✅ | the repository's first commit | +| TP1 — a database per user on Workers | planned | TP-2 | +| TP2 — a baseline | planned | TP-3 | + +## Departures from the upstream template + +Recorded as they land, from TP1 on. diff --git a/specs/epics/turso-per-user/README.md b/specs/epics/turso-per-user/README.md new file mode 100644 index 0000000..5ce6c96 --- /dev/null +++ b/specs/epics/turso-per-user/README.md @@ -0,0 +1,36 @@ +# turso-per-user — Vercel's Turso Per User Starter on Cloudflare Workers + +**The Turso Per User Starter built by OpenNext into one Cloudflare Worker, +deployed by Orun to stage and prod on every merge — every user who signs up +with Clerk given a Turso database of their own by Turso's Platform API, their +todos read and written in it over libSQL's HTTP protocol, and a site that +serves before its owner has connected either — and the baseline that makes a +copy of it somebody else's in one landing.** + +| | | +|---|---| +| Status | In progress — TP0 on main | +| Upstream | [notrab/turso-per-user-starter](https://github.com/notrab/turso-per-user-starter), at `590144a` (2025-04-03) | +| Reference | [notion-waitlist](https://github.com/sourceplane/notion-waitlist) — portfolio-starter-kit's scaffolding, tests and bootstrap, with a SaaS's keys as optional Orun secrets; [clerk-auth-starter](https://github.com/sourceplane/clerk-auth-starter) — Clerk on Workers and the site before Clerk is connected; [turso-starter](https://github.com/sourceplane/turso-starter) — the canonical shape | +| Key | `TP` (turso-starter's is `TU`) | + +## Read in this order + +1. [design.md](design.md) — the template, how each Vercel piece maps to + Cloudflare, Turso kept as a SaaS, the webhook, the site before Clerk and + Turso are connected, 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 template. + +## Milestones + +| Milestone | What it lands | Task | +|---|---|---| +| TP0 — the spec | the repository, its intent, CI, tooling and spec index; this doc set | TP-1 | +| TP1 — a database per user on Workers | the template in `apps/web`, built by OpenNext into one Worker; the web libSQL client; the Clerk and Turso switches; the webhook checked on its raw body; the Worker-secret push; `tests/web` | TP-2 | +| TP2 — a baseline | the console card, the build document, the context pack and the baseline's tests | TP-3 | diff --git a/specs/epics/turso-per-user/design.md b/specs/epics/turso-per-user/design.md new file mode 100644 index 0000000..49e15fc --- /dev/null +++ b/specs/epics/turso-per-user/design.md @@ -0,0 +1,99 @@ +# turso-per-user — design + +## The template + +[notrab/turso-per-user-starter](https://github.com/notrab/turso-per-user-starter) +(`590144a`, the template behind Vercel's "Turso Per User Starter") is a +Next.js 14 todo app on Clerk 5 and Turso. A visitor signs up with Clerk; their +database is named after their Clerk user id (`md5`) and created through +Turso's Platform API (`@tursodatabase/api`) as a copy of a seed database, in +the `default` group — by Clerk's `user.created` webhook (`/webhooks/clerk`, +verified with Svix) or, if it has not run, on their first visit +(`/welcome`). The dashboard's server actions add, toggle and remove todos in +that database through drizzle over `@libsql/client`, authenticated with the +group's token; the user menu's "Download my data" streams Turso's SQL dump of +it (`/dump.sql`). The schema is drizzle's (`db/schema.ts`, `migrations/`), +and the README seeds the parent database from `dump.sql`. + +## How each Vercel piece maps to Cloudflare + +| Vercel | Cloudflare | Notes | +|---|---|---| +| Next.js on Vercel | `@opennextjs/cloudflare` → one Worker + Static Assets | Next 16.3.8, React 19; `next build --webpack`; 1.94 MiB gzipped, inside Workers Free | +| `@libsql/client` (Node entry) | `@libsql/client/web` and `drizzle-orm/libsql/web` | libSQL's HTTP protocol over `fetch`; the Node entry carries a native module | +| `@tursodatabase/api` | the same client, from the Worker | Its calls use the global `fetch`; its `baseUrl` is what the e2e tier points at a stand-in | +| Clerk 5 `middleware.ts` | Clerk 7 `proxy.ts` | Next 16's name; `auth()` and `auth.protect()` are async | +| Project env vars | Orun secrets per environment → the build (Clerk's publishable key) and Worker secrets (the rest) | Below | +| `next/font/google` | `next/font/local` over `@fontsource-variable/inter` | Google Fonts at build time fails on GitHub's runners | +| `app/opengraph-image.png` | the same image in `public/`, named in `metadata` | As a file convention Next compiles the image into the Worker | + +## Turso stays a SaaS + +The brief keeps Turso's Platform API as the SaaS it is: there is no +D1-per-tenant composition, and the template's point is Turso's +database-per-user model. Each user's database lives in the owner's Turso +organization, created from the owner's seed database; the Worker reaches it +at `libsql://-.turso.io` with the group's token, as +upstream (risk TP-F). Nothing is provisioned on Cloudflare but the Worker. + +libSQL's client is one of Next's server-external packages, so the build +traces it for Node and copies only `@libsql/isomorphic-ws`'s Node files; the +Worker bundle resolves the package's `workerd` export, so `next.config.ts` +traces that package whole (`outputFileTracingIncludes`). + +## The webhook + +Upstream parses the body (`req.json()`), re-serialises it and verifies the +signature against that; Svix signs the exact bytes it sent, which a +re-serialisation is not guaranteed to reproduce. Here the raw body is read +once, the signature checked against it first, and only a verified +`user.created` with an id creates anything (`lib/webhook.ts`). Without its +secret the endpoint answers 503 (upstream threw) and users' databases are +created on first visit instead. + +## Before Clerk and Turso are connected + +No platform brokers Clerk or Turso, so a site is born without them. +Clerk's publishable key is inlined at build, so whether Clerk is connected +is the build's to decide (`lib/clerk.ts`, clerk-auth-starter's switch): with +both keys (the secret key's existence only, `CLERK_SECRET_KEY_SET`) the site +is the template; without them there is no ``, the proxy passes +requests through, and sign-up, sign-in and the dashboard say Clerk is not +connected yet. Turso's four values are read per request (`lib/turso.ts`): +without them the dashboard names what is missing and `/dump.sql` answers +503. The layout that reads them is `force-dynamic`, so no build bakes its +answer in. + +The seven keys are optional Orun secrets the component resolves as +`optionalSecretEnv`. The build runs without the Worker's six (`env -u`), so +no bundle and no build cache holds one, and `tooling/wire/worker-secrets.mjs` +hands those that are set to the Worker as Worker secrets (`wrangler secret +bulk`, the values over stdin) from the deploy lane only. `/api/health` +reports which are connected and fails a Clerk-built Worker without +`CLERK_SECRET_KEY`, so the deploy's smoke catches it. + +## The repository and the build + +notion-waitlist's shape (portfolio-starter-kit's): `apps/web` (the site), +`tests/web` (vitest — the webhook against Svix-signed deliveries, the +Platform API calls against a local stand-in, the todo queries on a real +libSQL file), `tooling/` (TypeScript and ESLint configuration, the rename +engine, the bootstrap helpers, the wrangler renderer, the Worker-secret +push), one `intent.yaml` (dev verify-only, stage and prod deployed), +`ci.yml` the only workflow a site carries, and the baseline's own +`blueprint.yaml`, `repo-blueprint.yaml`, `.baseline/`, `testing/` and +`baseline.yml`. The site's name is one seam (`SITE_NAME` in +`apps/web/app/site.ts`), its origin `NEXT_PUBLIC_SITE_URL` per environment. + +## Alternatives rejected + +- **D1 per user.** There is no D1-per-tenant composition, and a Worker + binds a fixed set of databases; the brief keeps Turso. +- **One shared database with a user column.** It is a different template: + the template exists to show a database per user. +- **Read each user's hostname from the Platform API.** More robust to a + hostname change, but a Platform API call on every server action; + upstream derives it, and so does this (risk TP-F). +- **Verify the webhook with Clerk's own `verifyWebhook`.** Equivalent, but + upstream's dependency is Svix; the change is to verify the raw body, not + the library. diff --git a/specs/epics/turso-per-user/epic.yaml b/specs/epics/turso-per-user/epic.yaml new file mode 100644 index 0000000..73e02b1 --- /dev/null +++ b/specs/epics/turso-per-user/epic.yaml @@ -0,0 +1,34 @@ +# The epic's declaration. metadata.name equals the directory name; metadata.key +# is the task-key prefix this epic reserves (TP → TP-1, TP-2, …; TU is +# turso-starter's). Owner, target date and health are the platform's — never +# written here. +apiVersion: orun.io/v1 +kind: Epic +metadata: + name: turso-per-user + key: TP +spec: + title: "Vercel's Turso Per User Starter on Cloudflare Workers" + summary: "The Turso Per User Starter — a Next.js todo app where everyone who signs up with Clerk gets a Turso database of their own, created through Turso's Platform API — runs on Cloudflare Workers + Static Assets through @opennextjs/cloudflare, Turso kept as the SaaS it is and reached over libSQL's HTTP protocol, Clerk's and Turso's keys as Orun secrets and a site that serves before they exist, inside the Cirrus baseline's repository conventions, and becomes an Orun baseline that takes a new site from nothing to live on stage and prod in one landing." + state: started + milestones: + - name: "TP0 — 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: [TP-1] + - name: "TP1 — a database per user on Workers" + exitCriteria: + - "apps/web is the upstream template, built by OpenNext into one Worker with Static Assets" + - "a signed Clerk user.created webhook creates the user's database through Turso's Platform API as the template does, and a forged, stale or altered one creates nothing" + - "the todo list reads and writes the user's own database with the web libSQL client" + - "without Clerk's or Turso's keys the site serves and says what to connect, and nothing throws" + - "no key but Clerk's publishable key is in any bundle: the deploy hands them to the Worker as Worker secrets" + - "`pnpm typecheck`, `pnpm lint`, `pnpm test` and `pnpm build` are green, and the Worker fits Workers Free" + - "the README says how to run, test, preview and deploy it, how to connect Clerk and Turso, and how every Vercel piece maps to Cloudflare" + tasks: [TP-2] + - name: "TP2 — a baseline" + exitCriteria: + - "blueprint.yaml (the console card) and repo-blueprint.yaml (the build) take a new site from nothing to live on stage and prod in one landing, then record it" + - "a site built from it carries the site only, renamed throughout to its own name, repository and Worker, with the template's credit 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, not connected and connected to a stand-in of Turso's Platform API" + tasks: [TP-3] diff --git a/specs/epics/turso-per-user/implementation-plan.md b/specs/epics/turso-per-user/implementation-plan.md new file mode 100644 index 0000000..37b55cc --- /dev/null +++ b/specs/epics/turso-per-user/implementation-plan.md @@ -0,0 +1,50 @@ +# turso-per-user — implementation plan + +Three milestones, each one reviewed pull request (TP0 is the repository's +first commit), each green before it merges. + +## TP0 — the spec (TP-1) + +The repository is born: `intent.yaml` with the placeholder workspace +`ws_CHANGE_ME`, `ci.yml` (turso-starter's, which plans nothing until the +workspace is set), the shared TypeScript and ESLint configuration, the spec +index, this doc set and the task contracts. + +## TP1 — a database per user on Workers (TP-2) + +- The template's `app/`, `db/`, `migrations/`, `dump.sql`, Tailwind + configuration and favicons in `apps/web`; its dependencies on Next 16.3.8, + React 19 and Clerk 7, with `@opennextjs/cloudflare` and `wrangler`; Inter + self-hosted. +- `next.config.ts` (`output: 'standalone'`, the workspace root, images + unoptimized, the Clerk switch, `@libsql/isomorphic-ws` traced whole), + `open-next.config.ts` with the static-assets incremental cache, + `wrangler.jsonc`, and the build on webpack. +- `lib/clerk.ts`, `lib/turso.ts` (the Turso switch, the Platform API calls), + `lib/db.ts` (the user's database over `@libsql/client/web`), + `lib/todos.ts` (the queries), `lib/webhook.ts` (the raw-body signature + check); `proxy.ts`; the pages and routes reading them; the not-connected + notices; `/api/health`. +- `SITE_NAME`, `SOURCE_URL` and `SITE_URL` in `app/site.ts`; the landing, + the metadata and the user menu read them. +- `tooling/wire/worker-secrets.mjs`; `component.yaml`: the optional secret + refs, the deploy contract (the origin per environment, the build without + the Worker's keys, the secret push) and its smoke. `tests/web`: the + identity and the Clerk switch, Turso's connection and the Platform API + calls, the webhook, the todo queries, the secret push. +- The README's "How it maps to Vercel". + +## TP2 — a baseline (TP-3) + +notion-waitlist's machinery, renamed: `blueprint.yaml`, +`repo-blueprint.yaml`, `.baseline/` (identity, the product's README with +"Connect Clerk", "Connect Turso", "The webhook" and "Change the schema", its +`AGENTS.md`, task contracts), `tooling/rebrand` (the template's repository +and demo masked as credit), `tooling/bootstrap`, +`hooks/render-deployment-docs.sh` (probing `/` and `/api/health`, naming the +seven keys), `docs/phases/`, the context pack, `testing/` (the leak and +placement gates on this tree's files, the optional refs checked in the plan; +the e2e tier building and serving the site not connected, then with +made-up keys against a local stand-in of Turso's Platform API — a Svix-signed +webhook creating the user's database, forged, stale and re-serialised ones +refused — then without Clerk's secret key) and `baseline.yml` / `tag.yml`. diff --git a/specs/epics/turso-per-user/risks-and-open-questions.md b/specs/epics/turso-per-user/risks-and-open-questions.md new file mode 100644 index 0000000..46b655f --- /dev/null +++ b/specs/epics/turso-per-user/risks-and-open-questions.md @@ -0,0 +1,11 @@ +# turso-per-user — risks and open questions + +| ID | Risk | State | Notes | +|---|---|---|---| +| TP-A | `baseline-v1` is not yet proven live | OPEN | Proven offline in Baseline CI: the offline bootstrap with the blueprint's own hooks, the stage lane's own steps in a staged job tree, the site served by `wrangler dev` and smoke-tested with the component's own command — not connected, and connected to a local stand-in. A console bootstrap closes it | +| TP-B | A real sign-up, database and todo | OPEN | Offline, Clerk's middleware answers a signed-out visitor (redirects to sign-in) but nobody signs in — that needs Clerk's servers; the webhook's request to Turso is read back from a stand-in; the todo queries run on a local libSQL file through the libsql client's file mode, since there is no libSQL server here (the Worker uses the web client over HTTP with the same driver). The first `wrangler secret bulk` on a Worker that does not exist yet has not run live (clerk-auth-starter's CK-B is the same push). Closed by storing the keys on a live site's stage, signing up once and adding a todo | +| TP-C | A schema change reaches only databases created after it | ACCEPTED | `db:migrate` applies to the seed; every user's database is a copy taken when it was created, as upstream. The product's README says so ("Change the schema") | +| TP-D | Next 16.3.8, not the template's 14 | ACCEPTED | OpenNext 1.20 refuses Next 15.5 at build, and every OpenNext starter here is proven on 16.3.8; Clerk 5 → 7 came with it (`auth()` is async, `middleware.ts` is `proxy.ts`) | +| TP-E | No licence | ACCEPTED | GitHub records none, the repository has no LICENSE file, its `package.json` no `license`, its README no licence section. The registry lists the row unlisted until its owner decides; the README credits the template by name and link | +| TP-F | A user's database URL is derived, not looked up | WATCH | `libsql://-.turso.io`, as upstream and its `drizzle.config.ts`; the Platform API's own hostname would survive a change of form, at the cost of a call per request | +| TP-G | `/api/health` names what is connected | ACCEPTED | Names and states only, never a value; the deploy's smoke needs it | diff --git a/tasks/TP-1.TaskContract.yaml b/tasks/TP-1.TaskContract.yaml new file mode 100644 index 0000000..0236223 --- /dev/null +++ b/tasks/TP-1.TaskContract.yaml @@ -0,0 +1,15 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: TP-1 + title: "The spec" +spec: + goal: The turso-per-user repository is born with its intent, CI, tooling and spec index, and the turso-per-user epic — declaration, doc set and task contracts — is on main + affects: + - specs/epics/turso-per-user + - 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/TP-2.TaskContract.yaml b/tasks/TP-2.TaskContract.yaml new file mode 100644 index 0000000..05761ef --- /dev/null +++ b/tasks/TP-2.TaskContract.yaml @@ -0,0 +1,20 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: TP-2 + title: "A database per user on Workers" +spec: + goal: The Turso Per User Starter runs on Cloudflare Workers + Static Assets through OpenNext, creates each user's Turso database through the Platform API from a signed Clerk webhook or their first visit, reads and writes it with the web libSQL client, serves before Clerk and Turso are connected, keeps their keys out of every bundle, and has its tests and a README that says how it maps to Vercel + affects: + - apps/web + - tests/web + - tooling/wire + - pnpm-lock.yaml + - pnpm-workspace.yaml + - README.md + - .gitignore + doneWhen: + - pnpm typecheck, lint, test and build are green + - wrangler dev serves the site without the keys and with them, a signed webhook creating a database at a stand-in of the Platform API and a forged one refused + - the Worker is under Workers Free's 3 MiB gzipped + gates: [] diff --git a/tasks/TP-3.TaskContract.yaml b/tasks/TP-3.TaskContract.yaml new file mode 100644 index 0000000..fab4295 --- /dev/null +++ b/tasks/TP-3.TaskContract.yaml @@ -0,0 +1,29 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: TP-3 + title: "turso-per-user becomes an Orun baseline" +spec: + goal: blueprint.yaml and repo-blueprint.yaml take a new database-per-user site 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/turso-per-user + - README.md + doneWhen: + - every Baseline CI tier is green + - a site placed from it carries the site only, branded, with the template's credit intact + gates: []