@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 66 lines 2.61 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import { PORTS } from "./ports.ts";
6
7 /**
8 * Effect-native Cloudflare Container. Under `alchemy dev` the local
9 * provider builds/pulls the image and runs it in Docker; `SandboxDO`
10 * fronts it and `EchoWorker` proxies `GET /sandbox` into it.
11 *
12 * It exists in this stack so the stress suite covers a local resource
13 * whose restart is EXPENSIVE: the point of the container assertions is
14 * that unrelated churn (worker-source edits, stack reloads, broken
15 * intermediate states) must NOT bounce the container.
16 */
17 export class SandboxContainer extends Cloudflare.Container<SandboxContainer, {}>()(
18 "SandboxContainer",
19 ) {}
20
21 export const SandboxLive = /* @__PURE__ */ SandboxContainer.make(
22 {
23 main: import.meta.url,
24 image: "oven/bun:latest",
25 env: {
26 SANDBOX_GREETING: "hello-from-container",
27 // A LOCALHOST url on purpose: it points at the AWS StaticSite dev
28 // server running on the HOST machine. Inside the container,
29 // `localhost` is the container itself — the dev runtime must rewrite
30 // the host to the `host.docker.localhost` alias for this to resolve —
31 // exactly what breaks database URLs from local containers
32 // (alchemy-run/alchemy#1334). `GET /host-fetch` proves it.
33 HOST_SERVICE_URL: `http://localhost:${PORTS.awsSite}`,
34 },
35 },
36 Effect.gen(function* () {
37 return SandboxContainer.of({
38 fetch: Effect.gen(function* () {
39 const request = yield* HttpServerRequest;
40 const url = new URL(request.url, "http://container");
41 if (url.pathname.endsWith("/host-fetch")) {
42 // Reach a service on the DEVELOPER'S machine through an env var
43 // that was written as `http://localhost:…` — the database shape
44 // of #1334, minus the database.
45 const target = process.env.HOST_SERVICE_URL;
46 const body = yield* Effect.tryPromise(() =>
47 fetch(`${target}/__dev-env`).then((res) => res.text()),
48 ).pipe(Effect.orElseSucceed(() => "UNREACHABLE"));
49 return yield* HttpServerResponse.json({
50 target: target ?? null,
51 body,
52 });
53 }
54 return yield* HttpServerResponse.json({
55 greeting: process.env.SANDBOX_GREETING ?? null,
56 // <<SANDBOX_MARKER>>
57 marker: "sandbox-v1",
58 // <</SANDBOX_MARKER>>
59 path: url.pathname,
60 });
61 }),
62 });
63 }),
64 );
65
66 export default SandboxLive;