@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 137 lines 5.21 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Stream from "effect/Stream";
7
8 /**
9 * Per-key counter that the queue handler increments on every
10 * message it processes, and that the test polls back via the
11 * worker's `GET /count?name=...` route. Persists in DO storage so
12 * the count survives DO hibernation.
13 */
14 export class Counter extends Cloudflare.DurableObject<Counter>()(
15 "Counter",
16 Effect.gen(function* () {
17 return Effect.gen(function* () {
18 const state = yield* Cloudflare.DurableObjectState;
19 let count = (yield* state.storage.get<number>("count")) ?? 0;
20 const lastBodies = (yield* state.storage.get<string[]>("lastBodies")) ?? [];
21 return {
22 record: Effect.fn(function* (body: string) {
23 count += 1;
24 lastBodies.push(body);
25 yield* state.storage.put("count", count);
26 yield* state.storage.put("lastBodies", lastBodies);
27 }),
28 snapshot: () =>
29 Effect.succeed({
30 count,
31 lastBodies,
32 }),
33 };
34 });
35 }),
36 ) {}
37
38 /**
39 * The queue resources the worker produces to and consumes from. The
40 * test deploys this stack, sends N messages via `POST /send?name=K`,
41 * waits for the consumer to push them through to the Counter DO,
42 * and polls `GET /count?name=K` until the count matches.
43 *
44 * `Cloudflare.Queues.consumeQueueMessages(...)` (in QueueWorker below)
45 * auto-creates the matching `Cloudflare.Queues.Consumer` resource at
46 * deploy time, so this fixture has no separate consumer wiring.
47 */
48 export const RoundTripQueue = Cloudflare.Queues.Queue("RoundTripQueue");
49 export const SecondaryRoundTripQueue = Cloudflare.Queues.Queue("SecondaryRoundTripQueue");
50
51 interface QueueMessageBody {
52 name: string;
53 text: string;
54 }
55
56 export default class QueueWorker extends Cloudflare.Worker<QueueWorker>()(
57 "QueueRoundTripWorker",
58 {
59 main: import.meta.url,
60 },
61 Effect.gen(function* () {
62 const counters = yield* Counter;
63 const queueResource = yield* RoundTripQueue;
64 const queue = yield* Cloudflare.Queues.WriteQueue(queueResource);
65 const secondaryQueueResource = yield* SecondaryRoundTripQueue;
66 const secondaryQueue = yield* Cloudflare.Queues.WriteQueue(secondaryQueueResource);
67
68 // Effect-style queue consumer. The handler delegates to the
69 // Counter DO so the test can verify the message landed by
70 // polling the DO's snapshot.
71 //
72 // Mixed `Duration.Input` forms are intentional: the e2e test
73 // exercises that a `Duration` value (`maxWaitTime`) and a
74 // string (`retryDelay: "1 second"`) both type-check at the
75 // `Cloudflare.Queues.consumeQueueMessages(...)` call site and survive the convert-
76 // and-forward path into Cloudflare's Consumer settings.
77 // Values are kept small so the round-trip latency stays well
78 // under the test's 240s timeout.
79 yield* Cloudflare.Queues.consumeQueueMessages<QueueMessageBody>(
80 queueResource,
81 {
82 batchSize: 10,
83 maxRetries: 3,
84 maxWaitTime: Duration.millis(500),
85 retryDelay: "1 second",
86 },
87 (stream) =>
88 Stream.runForEach(stream, (msg) => counters.getByName(msg.body.name).record(msg.body.text)),
89 );
90
91 // A second queue subscription on the same Worker verifies that
92 // dispatch reaches every listener for the `queue` event. Each
93 // listener still scopes itself by queue name internally.
94 yield* Cloudflare.Queues.consumeQueueMessages<QueueMessageBody>(
95 secondaryQueueResource,
96 {
97 batchSize: 1,
98 maxRetries: 2,
99 retryDelay: "2 seconds",
100 },
101 (stream) =>
102 Stream.runForEach(stream, (msg) => counters.getByName(msg.body.name).record(msg.body.text)),
103 );
104
105 return {
106 fetch: Effect.gen(function* () {
107 const request = yield* HttpServerRequest;
108 const url = new URL(request.url, "http://x");
109
110 if (request.method === "POST" && url.pathname === "/send") {
111 const name = url.searchParams.get("name") ?? "default";
112 const text = yield* request.text;
113 yield* queue.send({ name, text }).pipe(Effect.orDie);
114 return yield* HttpServerResponse.json({ sent: { name, text } }, { status: 202 });
115 }
116
117 if (request.method === "POST" && url.pathname === "/send-secondary") {
118 const name = url.searchParams.get("name") ?? "default";
119 const text = yield* request.text;
120 yield* secondaryQueue.send({ name, text }).pipe(Effect.orDie);
121 return yield* HttpServerResponse.json({ sent: { name, text } }, { status: 202 });
122 }
123
124 if (request.method === "GET" && url.pathname === "/count") {
125 const name = url.searchParams.get("name") ?? "default";
126 const snapshot = yield* counters.getByName(name).snapshot();
127 return yield* HttpServerResponse.json(snapshot);
128 }
129
130 return HttpServerResponse.text("Not Found", { status: 404 });
131 }),
132 };
133 }).pipe(
134 Effect.provide(Cloudflare.Queues.WriteQueueBinding),
135 Effect.provide(Cloudflare.Queues.EventSourceLive),
136 ),
137 ) {}