RateLimit
Source:
src/Cloudflare/Workers/RateLimit.ts
A Cloudflare Rate Limit binding for counting arbitrary keys inside Workers — a Worker-only binding with no backing cloud resource.
RateLimit is a single value that is at once the Binding.Service tag, the
callable that produces a {@link RateLimitBinding}, and the type. Declare it on
a Worker’s env (it flows through InferEnv → the native cf.RateLimit) or
yield* it inside an Effect-native Worker to attach the binding and obtain
the {@link RateLimitClient}.
Declaring on a Worker’s env
Section titled “Declaring on a Worker’s env”export const Worker = Cloudflare.Worker("Worker", { main: "./src/worker.ts", env: { THROTTLE: Cloudflare.RateLimit("THROTTLE", { namespaceId: 1001, simple: { limit: 10, period: 60 }, }), },});
export type WorkerEnv = Cloudflare.InferEnv<typeof Worker>;// { THROTTLE: RateLimit } — the native Cloudflare binding
// worker.tsexport default { fetch: async (req: Request, env: WorkerEnv) => { const { success } = await env.THROTTLE.limit({ key: "ip" }); return new Response(success ? "ok" : "rate limited"); },};Binding inside an Effect-native Worker
Section titled “Binding inside an Effect-native Worker”Cloudflare.Worker("Worker", { main: "./src/worker.ts" }, Effect.gen(function* () { // Attaches the binding to this Worker AND returns the runtime client. const throttle = yield* Cloudflare.RateLimit("THROTTLE", { namespaceId: 1001, simple: { limit: 10, period: 60 }, });
return { fetch: Effect.gen(function* () { const { success } = yield* throttle.limit({ key: "ip" }); return HttpServerResponse.text(success ? "ok" : "rate limited"); }), }; }).pipe(Effect.provide(Cloudflare.Workers.RateLimitBinding)),);