@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 52 lines 1.69 KB
Raw
1 // Async (non-Effect) Worker that produces to a queue binding AND consumes
2 // the same queue, recording delivered bodies in instance memory so the test
3 // can poll them back over HTTP. Producer + consumer in one worker is the
4 // supported local shape: the broker lives in the consuming instance.
5 interface Env {
6 QUEUE: {
7 send(body: unknown, options?: { contentType?: string }): Promise<void>;
8 sendBatch(messages: Iterable<{ body: unknown; contentType?: string }>): Promise<void>;
9 };
10 }
11
12 interface MessageLike {
13 body: unknown;
14 }
15
16 const received: string[] = [];
17 const batches: number[] = [];
18
19 export default {
20 fetch: async (request: Request, env: Env) => {
21 const url = new URL(request.url);
22 if (url.pathname === "/plain") {
23 return Response.json({ ok: true });
24 }
25 if (url.pathname === "/send") {
26 const text = url.searchParams.get("text") ?? "hello";
27 await env.QUEUE.send(text, { contentType: "text" });
28 return Response.json({ sent: text });
29 }
30 if (url.pathname === "/sendbatch") {
31 const text = url.searchParams.get("text") ?? "hello";
32 await env.QUEUE.sendBatch([
33 { body: `${text}-a`, contentType: "text" },
34 { body: { text, index: 2 }, contentType: "json" },
35 ]);
36 return Response.json({ sent: 2 });
37 }
38 if (url.pathname === "/received") {
39 return Response.json({ received });
40 }
41 if (url.pathname === "/batches") {
42 return Response.json({ batches });
43 }
44 return new Response("not found", { status: 404 });
45 },
46 queue: async (batch: { messages: MessageLike[] }) => {
47 batches.push(batch.messages.length);
48 for (const message of batch.messages) {
49 received.push(String(message.body));
50 }
51 },
52 };