The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | // Mirrors `@cloudflare/vite-plugin`'s `playground/containers`: Durable Objects |
| 2 | // drive their containers through the native `ctx.container` API. The |
| 3 | // `dockerfile` class's container is built from `../Dockerfile`; the |
| 4 | // `named-images` class reads the same build from `ctx.container.images`. |
| 5 | import { DurableObject } from "cloudflare:workers"; |
| 6 | |
| 7 | class ContainerObject extends DurableObject { |
| 8 | override async fetch(req: Request) { |
| 9 | const container = this.ctx.container; |
| 10 | if (container === undefined) { |
| 11 | return new Response("Container is not configured.", { status: 404 }); |
| 12 | } |
| 13 | const images = (container as unknown as { images: Record<string, string> }).images; |
| 14 | const path = new URL(req.url).pathname; |
| 15 | switch (path) { |
| 16 | case "/status": |
| 17 | return new Response(JSON.stringify(container.running)); |
| 18 | |
| 19 | case "/images": |
| 20 | return Response.json(Object.keys(images)); |
| 21 | |
| 22 | case "/start": |
| 23 | container.start({ |
| 24 | ...(images.app === undefined ? {} : ({ image: images.app } as {})), |
| 25 | entrypoint: ["node", "app.js"], |
| 26 | env: { A: "B", C: "D", L: "F", MESSAGE: "from vite" }, |
| 27 | enableInternet: false, |
| 28 | }); |
| 29 | // this doesn't instantly start, so we will need to poll /fetch |
| 30 | return new Response("Container create request sent..."); |
| 31 | |
| 32 | case "/fetch": { |
| 33 | const res = await container |
| 34 | .getTcpPort(8080) |
| 35 | // actual request doesn't matter |
| 36 | .fetch("http://foo/bar/baz", { method: "POST", body: "hello" }); |
| 37 | return new Response(await res.text()); |
| 38 | } |
| 39 | |
| 40 | default: |
| 41 | return new Response("Hi from Container DO"); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | export class DockerfileContainer extends ContainerObject {} |
| 47 | export class NamedImagesContainer extends ContainerObject {} |
| 48 | |
| 49 | type Exports = { |
| 50 | DockerfileContainer: DurableObjectNamespace<DockerfileContainer>; |
| 51 | NamedImagesContainer: DurableObjectNamespace<NamedImagesContainer>; |
| 52 | }; |
| 53 | |
| 54 | export default { |
| 55 | async fetch(request, _env, ctx): Promise<Response> { |
| 56 | const url = new URL(request.url); |
| 57 | const exports = ctx.exports as unknown as Exports; |
| 58 | |
| 59 | let service: Fetcher | undefined; |
| 60 | let prefix = ""; |
| 61 | if (url.pathname === "/dockerfile" || url.pathname.startsWith("/dockerfile/")) { |
| 62 | service = exports.DockerfileContainer.getByName("container"); |
| 63 | prefix = "/dockerfile"; |
| 64 | } else if (url.pathname === "/named-images" || url.pathname.startsWith("/named-images/")) { |
| 65 | service = exports.NamedImagesContainer.getByName("container"); |
| 66 | prefix = "/named-images"; |
| 67 | } |
| 68 | |
| 69 | if (!service) { |
| 70 | return new Response("Not found. Use `/dockerfile/...` or `/named-images/...`.", { |
| 71 | status: 404, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | const forwardedUrl = new URL(url); |
| 76 | forwardedUrl.pathname = url.pathname.slice(prefix.length) || "/"; |
| 77 | return service.fetch(new Request(forwardedUrl.toString(), request)); |
| 78 | }, |
| 79 | } satisfies ExportedHandler; |