@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, 06:00 UTC

main
ts 72 lines 2.55 KB
Raw
1 /// <reference types="@cloudflare/workers-types" />
2
3 import { Container, getContainer } from "@cloudflare/containers";
4
5 interface Env {
6 BENCH_CONTAINER: DurableObjectNamespace<BenchContainer>;
7 CRASH_CONTAINER: DurableObjectNamespace<CrashContainer>;
8 }
9
10 /**
11 * Plain `@cloudflare/containers` container — no Alchemy involved. The DO proxies
12 * `fetch` to the Bun HTTP server on port 8080; the first `fetch` blocks through
13 * the container's cold start (the helper calls `startAndWaitForPorts`
14 * internally), so timing a single `fetch` measures start → reachable.
15 */
16 export class BenchContainer extends Container<Env> {
17 defaultPort = 8080;
18 sleepAfter = "2m";
19 }
20
21 /**
22 * Crash-loop control: its image exits immediately, so `fetch` can never reach
23 * it. Native surfaces the fatal crash from its monitor and returns a 5xx; we
24 * time how fast that happens to baseline Alchemy's fail-fast behaviour.
25 */
26 export class CrashContainer extends Container<Env> {
27 defaultPort = 8080;
28 sleepAfter = "2m";
29 }
30
31 export default {
32 async fetch(request, env): Promise<Response> {
33 const url = new URL(request.url);
34
35 // `/start?name=X` boots the container instance named `X` and reports the
36 // cold-start-to-reachable latency measured inside the Worker.
37 if (url.pathname === "/start") {
38 const name = url.searchParams.get("name") ?? "default";
39 const container = getContainer(env.BENCH_CONTAINER, name);
40 const start = Date.now();
41 try {
42 const res = await container.fetch(new Request("http://container/"));
43 await res.text();
44 return Response.json({ ms: Date.now() - start, status: res.status });
45 } catch (err) {
46 return Response.json({ error: String(err), ms: Date.now() - start }, { status: 500 });
47 }
48 }
49
50 // `/crashloop?name=X` attempts to reach a container that crash-loops and
51 // reports how long until the failure surfaces (native returns a 5xx). `ok`
52 // is true only on a 2xx — always false here.
53 if (url.pathname === "/crashloop") {
54 const name = url.searchParams.get("name") ?? "default";
55 const container = getContainer(env.CRASH_CONTAINER, name);
56 const start = Date.now();
57 try {
58 const res = await container.fetch(new Request("http://container/"));
59 await res.text();
60 return Response.json({ ms: Date.now() - start, ok: res.ok });
61 } catch (err) {
62 return Response.json({
63 ms: Date.now() - start,
64 ok: false,
65 error: String(err),
66 });
67 }
68 }
69
70 return new Response("ok");
71 },
72 } satisfies ExportedHandler<Env>;