The best way to control Claude Code, Codex and more from any computer or phone

main
ts 45 lines 1.51 KB
Raw
1 import type * as cf from "@cloudflare/workers-types";
2 import * as Effect from "effect/Effect";
3 import type * as Binding from "./Binding.ts";
4 import { makeBindingLayer } from "./BindingLayer.ts";
5 import {
6 RateLimit,
7 RateLimitError,
8 type RateLimitClient,
9 type RateLimitPeriod,
10 } from "./RateLimit.ts";
11
12 /** The binding value produced by calling {@link RateLimit} (declared on `env` or `yield*`-ed). */
13 export type RateLimitBinding = Binding.Binding<RateLimit["key"], RateLimitClient, RateLimit> & {
14 readonly namespaceId: string;
15 readonly simple: { limit: number; period: RateLimitPeriod };
16 };
17
18 /**
19 * The layer that provides the Effect-native interface for the Cloudflare
20 * Workers Rate Limit binding.
21 *
22 * Provide it on the Worker effect (`Effect.provide(Cloudflare.Workers.RateLimitBinding)`)
23 * so that yielding a {@link RateLimit} binding attaches the native `ratelimit`
24 * binding to the surrounding Worker at deploy time and, at runtime, resolves to
25 * the Effect-native {@link RateLimitClient}.
26 */
27 export const RateLimitBinding = makeBindingLayer<RateLimit, cf.RateLimit, RateLimitClient>(
28 RateLimit,
29 (raw) => ({
30 raw,
31 limit: (options) =>
32 raw.pipe(
33 Effect.flatMap((binding) =>
34 Effect.tryPromise({
35 try: () => binding.limit(options),
36 catch: (error) =>
37 new RateLimitError({
38 message: error instanceof Error ? error.message : "Unknown RateLimit error",
39 cause: error,
40 }),
41 }),
42 ),
43 ),
44 }),
45 );