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

main
ts 83 lines 2.96 KB
Raw
1 import type { AsyncWorkerEnv } from "./stack.ts";
2
3 const STYLE = "async";
4
5 /**
6 * Async (non-Effect) Worker fixture. The shared D1 database is declared on
7 * the Worker `env` as `DB` (see `stack.ts`); `InferEnv` maps the
8 * `Cloudflare.D1.Database` marker to the native `cf.D1Database`, so the
9 * handler calls `env.DB.prepare(...)` / `env.DB.exec(...)` / `env.DB.batch(...)`
10 * directly — proving the engine injected the native binding into `env`.
11 *
12 * Drives the same SQL surface as the effect-worker, stamping rows with
13 * `style = "async"`. Mirrors the route contract in `routes.ts`.
14 */
15 export default {
16 async fetch(request: Request, env: AsyncWorkerEnv): Promise<Response> {
17 const url = new URL(request.url, "http://x");
18 const db = env.DB;
19
20 if (request.method === "POST" && url.pathname === "/init") {
21 const result = await db.exec(
22 "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, style TEXT NOT NULL, name TEXT NOT NULL)",
23 );
24 return Response.json({ count: result.count, duration: result.duration });
25 }
26
27 if (request.method === "POST" && url.pathname === "/seed") {
28 const insert = db.prepare("INSERT OR REPLACE INTO users (id, style, name) VALUES (?, ?, ?)");
29 const results = await db.batch([
30 insert.bind(1, STYLE, "alice"),
31 insert.bind(2, STYLE, "bob"),
32 insert.bind(3, STYLE, "carol"),
33 ]);
34 return Response.json({
35 batches: results.length,
36 success: results.every((r) => r.success),
37 });
38 }
39
40 if (request.method === "POST" && url.pathname === "/users") {
41 const body = (await request.json()) as { id: number; name: string };
42 const result = await db
43 .prepare("INSERT OR REPLACE INTO users (id, style, name) VALUES (?, ?, ?)")
44 .bind(body.id, STYLE, body.name)
45 .run();
46 return Response.json({
47 success: result.success,
48 meta: { changes: result.meta.changes },
49 });
50 }
51
52 if (request.method === "GET" && url.pathname === "/users") {
53 const result = await db
54 .prepare("SELECT id, name FROM users WHERE style = ? ORDER BY id")
55 .bind(STYLE)
56 .all<{ id: number; name: string }>();
57 return Response.json({
58 success: result.success,
59 results: result.results,
60 });
61 }
62
63 const userMatch = url.pathname.match(/^\/users\/(\d+)$/);
64 if (request.method === "GET" && userMatch) {
65 const id = Number(userMatch[1]);
66 const row = await db
67 .prepare("SELECT id, name FROM users WHERE id = ? AND style = ?")
68 .bind(id, STYLE)
69 .first<{ id: number; name: string }>();
70 return Response.json({ row });
71 }
72
73 if (request.method === "GET" && url.pathname === "/raw") {
74 const result = await db
75 .prepare("SELECT COUNT(*) as count FROM users WHERE style = ?")
76 .bind(STYLE)
77 .first<{ count: number }>();
78 return Response.json({ count: result?.count ?? 0 });
79 }
80
81 return new Response("Not Found", { status: 404 });
82 },
83 };