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 | export class ExternalContainer extends Cloudflare.Container<ExternalContainer>()( |
| 6 | "ExternalContainer", |
| 7 | { |
| 8 | // Use a template string rather than `path.join(import.meta.dirname, …)`: |
| 9 | // this module is bundled into the Worker (it defines the DO), and |
| 10 | // `import.meta.dirname` is `undefined` in the Worker runtime — calling |
| 11 | // `path.join(undefined, …)` there throws a ScriptStartupError at module |
| 12 | // load. `context` is only consumed at build time, so a plain string is |
| 13 | // sufficient and never evaluates `path.join` at runtime. |
| 14 | context: `${import.meta.dirname}/context`, |
| 15 | observability: { logs: { enabled: true } }, |
| 16 | }, |
| 17 | ) {} |
| 18 | |
| 19 | /** |
| 20 | * The same nginx server built from inline `dockerfile.content` instead of a |
| 21 | * context directory, so the image build has no user-supplied build context. |
| 22 | */ |
| 23 | export class InlineDockerfileContainer extends Cloudflare.Container<InlineDockerfileContainer>()( |
| 24 | "InlineDockerfileContainer", |
| 25 | { |
| 26 | dockerfile: { |
| 27 | content: [ |
| 28 | "FROM nginxinc/nginx-unprivileged:alpine", |
| 29 | "USER root", |
| 30 | "RUN rm -f /var/log/nginx/access.log /var/log/nginx/error.log \\", |
| 31 | " && touch /var/log/nginx/access.log /var/log/nginx/error.log \\", |
| 32 | " && chown nginx:nginx /var/log/nginx/access.log /var/log/nginx/error.log \\", |
| 33 | " && echo 'hello from inline dockerfile container' > /usr/share/nginx/html/index.html", |
| 34 | "USER nginx", |
| 35 | "EXPOSE 8080", |
| 36 | "", |
| 37 | ].join("\n"), |
| 38 | }, |
| 39 | observability: { logs: { enabled: true } }, |
| 40 | }, |
| 41 | ) {} |
| 42 | |
| 43 | /** |
| 44 | * Durable Object that binds and starts the {@link ExternalContainer} and |
| 45 | * proxies an HTTP request to the nginx server running on port 8080 inside it. |
| 46 | */ |
| 47 | export class ExternalContainerObject extends Cloudflare.DurableObject<ExternalContainerObject>()( |
| 48 | "ExternalContainerObject", |
| 49 | Effect.gen(function* () { |
| 50 | const container = yield* ExternalContainer; |
| 51 | |
| 52 | return Effect.gen(function* () { |
| 53 | // Starting is idempotent: a no-op once the container is running. |
| 54 | const start = container.start({ enableInternet: true }); |
| 55 | const { fetch } = yield* container.getTcpPort(8080); |
| 56 | |
| 57 | return { |
| 58 | hello: Effect.fn("hello")(function* () { |
| 59 | yield* start; |
| 60 | const response = yield* fetch(HttpClientRequest.get("http://container/")); |
| 61 | return yield* response.text; |
| 62 | }), |
| 63 | }; |
| 64 | }); |
| 65 | }), |
| 66 | ) {} |
| 67 | |
| 68 | /** Durable Object that binds and proxies to the {@link InlineDockerfileContainer}. */ |
| 69 | export class InlineDockerfileContainerObject extends Cloudflare.DurableObject<InlineDockerfileContainerObject>()( |
| 70 | "InlineDockerfileContainerObject", |
| 71 | Effect.gen(function* () { |
| 72 | const container = yield* InlineDockerfileContainer; |
| 73 | |
| 74 | return Effect.gen(function* () { |
| 75 | const start = container.start({ enableInternet: true }); |
| 76 | const { fetch } = yield* container.getTcpPort(8080); |
| 77 | |
| 78 | return { |
| 79 | hello: Effect.fn("hello")(function* () { |
| 80 | yield* start; |
| 81 | const response = yield* fetch(HttpClientRequest.get("http://container/")); |
| 82 | return yield* response.text; |
| 83 | }), |
| 84 | }; |
| 85 | }); |
| 86 | }), |
| 87 | ) {} |