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 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import { SandboxContainer } from "./SandboxContainer.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Durable Object that fronts {@link SandboxContainer} — the only way a |
| 10 | * Worker reaches a Container. `EchoWorker` proxies `GET /sandbox` here. |
| 11 | */ |
| 12 | export default class SandboxDO extends Cloudflare.DurableObject<SandboxDO>()( |
| 13 | "SandboxDO", |
| 14 | Effect.gen(function* () { |
| 15 | const container = yield* SandboxContainer; |
| 16 | |
| 17 | return Effect.gen(function* () { |
| 18 | // Starting is idempotent: a no-op once the container is running. |
| 19 | const start = container.start({ enableInternet: true }); |
| 20 | return { |
| 21 | fetch: Effect.gen(function* () { |
| 22 | const request = yield* HttpServerRequest; |
| 23 | const url = new URL(request.url, "http://container"); |
| 24 | // Forward the subpath: `/sandbox/host-fetch` → `/host-fetch`. |
| 25 | const path = url.pathname.replace(/^\/sandbox/, "") || "/"; |
| 26 | yield* start; |
| 27 | const { fetch } = yield* container.getTcpPort(3000); |
| 28 | const response = yield* fetch(HttpClientRequest.get(`http://container${path}`)); |
| 29 | return HttpServerResponse.text(yield* response.text, { |
| 30 | status: response.status, |
| 31 | headers: response.headers, |
| 32 | }); |
| 33 | }), |
| 34 | }; |
| 35 | }); |
| 36 | }), |
| 37 | ) {} |