@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 45 lines 1.69 KB
Raw
1 /**
2 * The user's own worker entry — the whole point of this fixture.
3 *
4 * It wraps waku's emitted fetch handler (so every framework route still
5 * works) and ADDITIONALLY exports the `Counter` Durable Object class, which
6 * must live on the same worker for the `COUNTER` namespace binding to
7 * resolve. This mirrors alchemy's Website.Vite "Custom Worker Entry"
8 * pattern (`main: "worker/index.ts"` re-exporting DO classes).
9 *
10 * `virtual:waku/server-entry` is the import seam for waku's server handler
11 * (waku's real entry, `dist/lib/vite-entries/entry.server.js`, is not on
12 * waku's exports map, so the integration exposes it — the same way React
13 * Router exposes `virtual:react-router/server-build`).
14 */
15 import { DurableObject } from "cloudflare:workers";
16 import wakuHandler from "virtual:waku/server-entry";
17
18 /** A SQLite-backed Durable Object owned by the user app, not the framework. */
19 export class Counter extends DurableObject {
20 #ensureTable() {
21 this.ctx.storage.sql.exec(
22 "CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY, value INTEGER NOT NULL)",
23 );
24 }
25
26 async get(): Promise<number> {
27 this.#ensureTable();
28 const rows = this.ctx.storage.sql.exec("SELECT value FROM counter WHERE id = 0").toArray();
29 return Number(rows[0]?.value ?? 0);
30 }
31
32 async increment(): Promise<number> {
33 this.#ensureTable();
34 const rows = this.ctx.storage.sql
35 .exec(
36 "INSERT INTO counter (id, value) VALUES (0, 1) ON CONFLICT (id) DO UPDATE SET value = value + 1 RETURNING value",
37 )
38 .toArray();
39 return Number(rows[0]!.value);
40 }
41 }
42
43 export default {
44 fetch: (request: Request, env: unknown, ctx: unknown) => wakuHandler.fetch(request, env, ctx),
45 };