1import type { AsyncWorkerEnv } from "./stack.ts";
2
3/**
4 * Async (non-Effect) Worker handler that exercises the RateLimit binding
5 * declared via `env: { THROTTLE: Cloudflare.RateLimit(...) }`. `InferEnv` maps
6 * the binding to the native `cf.RateLimit`, so the handler calls
7 * `env.THROTTLE.limit({ key })` directly.
8 *
9 * `/burst` calls the limiter `n` times within a single isolate so the
10 * throttling is deterministic: with `limit: 2` the first two calls for a key
11 * succeed and the rest are denied.
12 */
13export default {
14 fetch: async (request: Request, env: AsyncWorkerEnv) => {
15 const url = new URL(request.url);
16
17 if (url.pathname === "/burst") {
18 const key = url.searchParams.get("key") ?? "default";
19 const n = Number(url.searchParams.get("n") ?? "5");
20
21 const results: boolean[] = [];
22 for (let i = 0; i < n; i++) {
23 const { success } = await env.THROTTLE.limit({ key });
24 results.push(success);
25 }
26
27 return new Response(JSON.stringify({ key, results }), {
28 headers: { "content-type": "application/json" },
29 });
30 }
31
32 return new Response("ok");
33 },
34};