The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 4 | |
| 5 | /** |
| 6 | * Deterministic port the test's host-side HTTP server listens on. It stands |
| 7 | * in for a local dev database (e.g. the `@prisma/dev` server) bound to the |
| 8 | * developer machine's loopback — the container must be able to reach it even |
| 9 | * though `localhost` inside the container is the container itself. |
| 10 | */ |
| 11 | export const HOST_PROBE_PORT = 42117; |
| 12 | |
| 13 | /** |
| 14 | * A literal local Prisma Postgres URL shape (`@prisma/dev` hands these out |
| 15 | * in dev). Prisma's client only speaks plain HTTP to `prisma+postgres://` |
| 16 | * hosts that look local (`localhost`/`127.0.0.1`/`[::1]`), so whatever the |
| 17 | * dev runtime rewrites the host to must still contain "localhost". |
| 18 | */ |
| 19 | export const PPG_URL = "prisma+postgres://localhost:51216/?api_key=test-key"; |
| 20 | |
| 21 | /** |
| 22 | * Cloud SQL URLs (Neon pooled, PlanetScale Postgres pooled, PlanetScale |
| 23 | * MySQL). The rewrite must leave these hosts alone — they are not loopback, |
| 24 | * and mangling them is how a "fix localhost" change would break Neon / |
| 25 | * PlanetScale from a container. |
| 26 | */ |
| 27 | export const NEON_URL = |
| 28 | "postgres://neondb_owner:secret@ep-cool-name-123456-pooler.us-east-1.aws.neon.tech/neondb?sslmode=require"; |
| 29 | export const PLANETSCALE_PG_URL = |
| 30 | "postgresql://user:secret@xxxx.pg.psdb.cloud:6432/postgres?sslmode=verify-full"; |
| 31 | export const PLANETSCALE_MYSQL_URL = |
| 32 | "mysql://user:secret@aws.connect.psdb.cloud:3306/db?sslaccept=strict"; |
| 33 | |
| 34 | class HostReachContainer extends Cloudflare.Container<HostReachContainer>()("HostReachContainer", { |
| 35 | // Template string, not `path.join(import.meta.dirname, …)`: this module is |
| 36 | // bundled into the Worker and `import.meta.dirname` is undefined there. |
| 37 | context: `${import.meta.dirname}/context`, |
| 38 | env: { |
| 39 | TARGET_URL: `http://localhost:${HOST_PROBE_PORT}/hello`, |
| 40 | PPG_URL, |
| 41 | NEON_URL, |
| 42 | PLANETSCALE_PG_URL, |
| 43 | PLANETSCALE_MYSQL_URL, |
| 44 | }, |
| 45 | observability: { logs: { enabled: true } }, |
| 46 | }) {} |
| 47 | |
| 48 | /** |
| 49 | * Durable Object that binds the {@link HostReachContainer} and exposes the |
| 50 | * probe server's routes to the Worker. |
| 51 | */ |
| 52 | export class HostReachContainerObject extends Cloudflare.DurableObject<HostReachContainerObject>()( |
| 53 | "HostReachContainerObject", |
| 54 | Effect.gen(function* () { |
| 55 | const container = yield* HostReachContainer; |
| 56 | |
| 57 | return Effect.gen(function* () { |
| 58 | // Starting is idempotent: a no-op once the container is running. |
| 59 | const start = container.start({ enableInternet: true }); |
| 60 | const { fetch } = yield* container.getTcpPort(8080); |
| 61 | |
| 62 | const get = (path: string) => |
| 63 | Effect.gen(function* () { |
| 64 | yield* start; |
| 65 | const response = yield* fetch(HttpClientRequest.get(`http://container${path}`)); |
| 66 | return yield* response.text; |
| 67 | }); |
| 68 | |
| 69 | return { |
| 70 | getEnv: () => get("/env"), |
| 71 | getProbe: () => get("/probe"), |
| 72 | }; |
| 73 | }); |
| 74 | }), |
| 75 | ) {} |