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

main
ts 53 lines 2.13 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Neon from "@alchemy.run/neon";
3 import * as SQL from "@alchemy.run/sql/Postgres";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import { lookupConfigName } from "./lookup.ts";
8
9 /**
10 * An existing Hyperdrive configuration looked up by name, with a `dev`
11 * origin (the real Neon Postgres) so `alchemy dev` can emulate the binding —
12 * the API never returns a looked-up configuration's origin password.
13 */
14 export const LocalLookupHyperdrive = Effect.gen(function* () {
15 const project = yield* Neon.Project("HyperdriveLookupLocalProject");
16 return Cloudflare.Hyperdrive.lookup("HyperdriveLookupLocal", {
17 name: lookupConfigName("local"),
18 dev: project.origin,
19 });
20 });
21
22 /**
23 * Effect-native Worker binding the looked-up configuration. `/query` runs
24 * SQL over the binding's connection string, which under `alchemy dev`
25 * points at the `dev` origin.
26 */
27 export default class HyperdriveLookupLocalWorker extends Cloudflare.Worker<HyperdriveLookupLocalWorker>()(
28 "HyperdriveLookupLocalWorker",
29 { main: import.meta.url },
30 Effect.gen(function* () {
31 const hd = yield* Cloudflare.Hyperdrive.Connect(yield* LocalLookupHyperdrive);
32 const sql = yield* SQL.Postgres({ url: hd.connectionString });
33
34 return {
35 fetch: Effect.gen(function* () {
36 const request = yield* HttpServerRequest;
37 const url = new URL(request.url, "http://x");
38 if (url.pathname === "/query") {
39 const rows = (yield* sql`
40 SELECT 1 + 1 AS sum, current_database() AS db
41 `) as ReadonlyArray<{ sum: number; db: string }>;
42 const host = yield* hd.host;
43 return yield* HttpServerResponse.json({ row: rows[0], host });
44 }
45 return HttpServerResponse.text("Not Found", { status: 404 });
46 }).pipe(
47 Effect.catchCause((cause) =>
48 HttpServerResponse.json({ error: String(cause) }, { status: 500 }),
49 ),
50 ),
51 };
52 }).pipe(Effect.provide(Cloudflare.Hyperdrive.ConnectBinding)),
53 ) {}