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

main
ts 25 lines 983 Bytes
Raw
1 import type { D1Database } from "@cloudflare/workers-types";
2
3 /**
4 * Async Worker bound to a D1 database as `DB`, used by the
5 * same-name replacement test in `Database.test.ts`. `/write` inserts a row
6 * and `/read` counts them, so the test can prove which physical database
7 * the deployed binding points at.
8 */
9 export default {
10 async fetch(request: Request, env: { DB: D1Database }): Promise<Response> {
11 const url = new URL(request.url);
12 if (request.method === "POST" && url.pathname === "/write") {
13 await env.DB.exec("CREATE TABLE IF NOT EXISTS entries (v TEXT NOT NULL)");
14 await env.DB.prepare("INSERT INTO entries (v) VALUES ('hello')").run();
15 return new Response("ok");
16 }
17 if (url.pathname === "/read") {
18 const row = await env.DB.prepare("SELECT count(*) AS n FROM entries").first<{
19 n: number;
20 }>();
21 return Response.json({ rows: row?.n ?? 0 });
22 }
23 return new Response("not found", { status: 404 });
24 },
25 };