1// Vite site worker that produces to a queue binding AND consumes the same
2// queue (the shape from issue #1109): API routes handle the produce/poll,
3// everything else falls through to the assets worker.
4interface Env {
5 ASSETS: { fetch(request: Request): Promise<Response> };
6 QUEUE: {
7 send(body: unknown, options?: { contentType?: string }): Promise<void>;
8 };
9}
10
11interface MessageLike {
12 body: unknown;
13}
14
15const received: string[] = [];
16
17export default {
18 fetch: async (request: Request, env: Env) => {
19 const url = new URL(request.url);
20 if (url.pathname === "/api/send") {
21 const text = url.searchParams.get("text") ?? "hello";
22 await env.QUEUE.send(text, { contentType: "text" });
23 return Response.json({ sent: text });
24 }
25 if (url.pathname === "/api/received") {
26 return Response.json({ received });
27 }
28 return env.ASSETS.fetch(request);
29 },
30 queue: async (batch: { messages: MessageLike[] }) => {
31 for (const message of batch.messages) {
32 received.push(String(message.body));
33 }
34 },
35};