@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 42 lines 1.62 KB
Raw
1 /**
2 * The USER's own worker entry — the nitro entry/exports seam.
3 *
4 * The deploy target maps the configured `main` onto `nitro.options.entry`,
5 * so nitro's rollup bundles THIS module as the worker entry: its exports are
6 * the worker's exports. It wraps nitro's emitted cloudflare-module handler
7 * (every framework route, API route, and asset check still works) and
8 * ADDITIONALLY exports the `Counter` Durable Object class, which must live
9 * on the same worker for the `COUNTER` namespace binding to resolve.
10 */
11 import { DurableObject } from "cloudflare:workers";
12 // The wrappable-handler seam: nitro's cloudflare_module runtime entry,
13 // importable by its public specifier (bundled with the same virtuals and
14 // aliases as nitro's own default entry).
15 import nitroHandler from "nitropack/presets/cloudflare/runtime/cloudflare-module";
16
17 /** A SQLite-backed Durable Object owned by the user app, not the framework. */
18 export class Counter extends DurableObject {
19 #ensureTable() {
20 this.ctx.storage.sql.exec(
21 "CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY, value INTEGER NOT NULL)",
22 );
23 }
24
25 async get(): Promise<number> {
26 this.#ensureTable();
27 const rows = this.ctx.storage.sql.exec("SELECT value FROM counter WHERE id = 0").toArray();
28 return Number(rows[0]?.value ?? 0);
29 }
30
31 async increment(): Promise<number> {
32 this.#ensureTable();
33 const rows = this.ctx.storage.sql
34 .exec(
35 "INSERT INTO counter (id, value) VALUES (0, 1) ON CONFLICT (id) DO UPDATE SET value = value + 1 RETURNING value",
36 )
37 .toArray();
38 return Number(rows[0]!.value);
39 }
40 }
41
42 export default nitroHandler;