@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, 07:15 UTC

main
ts 101 lines 3.97 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 * as Layer from "effect/Layer";
6
7 /**
8 * Effect-native Worker fixture that exercises all three Cloudflare tunnel
9 * runtime bindings from a single Worker:
10 *
11 * - {@link Cloudflare.Tunnel.ReadTunnel} — read-only (`/read`)
12 * - {@link Cloudflare.Tunnel.WriteTunnel} — mutating (`/write`)
13 * - {@link Cloudflare.Tunnel.ReadWriteTunnel} — full CRUD (`/readwrite`)
14 *
15 * Each binding provisions its own least-privilege {@link Cloudflare.ApiToken.AccountApiToken}
16 * (binding the token's outputs into the Worker) and the routes below run a
17 * self-contained scenario per binding so the test can assert one behavior each.
18 */
19 export default class TunnelEffectWorker extends Cloudflare.Worker<TunnelEffectWorker>()(
20 "TunnelEffectWorker",
21 {
22 main: import.meta.url,
23 },
24 Effect.gen(function* () {
25 const read = yield* Cloudflare.Tunnel.ReadTunnel();
26 const write = yield* Cloudflare.Tunnel.WriteTunnel();
27 const tunnels = yield* Cloudflare.Tunnel.ReadWriteTunnel();
28
29 // Tests use deterministic tunnel names, and Cloudflare rejects duplicate
30 // names — delete any leftover tunnel from a previously crashed run so
31 // create always succeeds.
32 const purge = Effect.fn(function* (...names: string[]) {
33 for (const name of names) {
34 const list = yield* tunnels.list({ name, isDeleted: false }).pipe(Effect.orDie);
35 yield* Effect.forEach(
36 (list.result ?? []).filter((t) => t.name === name && t.id),
37 (t) => tunnels.delete(t.id!).pipe(Effect.orDie),
38 );
39 }
40 });
41
42 return {
43 fetch: Effect.gen(function* () {
44 const request = yield* HttpServerRequest;
45 const url = new URL(request.originalUrl);
46 const name = url.searchParams.get("name") ?? "alchemy-tunnel";
47
48 // Read-only: prove the read token works by listing tunnels.
49 if (url.pathname === "/read") {
50 const list = yield* read.list({ isDeleted: false }).pipe(Effect.orDie);
51 return yield* HttpServerResponse.json({ count: list.result?.length });
52 }
53
54 // Write: create then delete with the write-scoped token.
55 if (url.pathname === "/write") {
56 yield* purge(name);
57 const tunnel = yield* write.create({ name, configSrc: "cloudflare" }).pipe(Effect.orDie);
58 yield* write.delete(tunnel.id!).pipe(Effect.orDie);
59 return yield* HttpServerResponse.json({
60 id: tunnel.id,
61 deleted: true,
62 });
63 }
64
65 // Read + write: drive the full CRUD surface end to end. Purge the
66 // renamed variant too — a crash after the rename step would
67 // otherwise leak it forever.
68 if (url.pathname === "/readwrite") {
69 yield* purge(name, `${name}-renamed`);
70 const created = yield* tunnels
71 .create({ name, configSrc: "cloudflare" })
72 .pipe(Effect.orDie);
73 const id = created.id!;
74 const got = yield* tunnels.get(id).pipe(Effect.orDie);
75 const list = yield* tunnels.list().pipe(Effect.orDie);
76 const updated = yield* tunnels.update(id, { name: `${name}-renamed` }).pipe(Effect.orDie);
77 const token = yield* tunnels.getToken(id).pipe(Effect.orDie);
78 yield* tunnels.delete(id).pipe(Effect.orDie);
79 return yield* HttpServerResponse.json({
80 id,
81 getName: got.name,
82 count: list.result?.length,
83 updatedName: updated.name,
84 hasToken: token.length > 0,
85 deleted: true,
86 });
87 }
88
89 return HttpServerResponse.text("ok");
90 }),
91 };
92 }).pipe(
93 Effect.provide(
94 Layer.mergeAll(
95 Cloudflare.Tunnel.ReadTunnelBinding,
96 Cloudflare.Tunnel.WriteTunnelBinding,
97 Cloudflare.Tunnel.ReadWriteTunnelBinding,
98 ),
99 ),
100 ),
101 ) {}