import { C as ConcurrencyGuard, j as UnifiedAdmitter, k as UnifiedAxis } from './unified-BouIz5EX.js'; import { D as Decision, C as Clock } from './types-DKirIBQt.js'; import { L as LimiterOrStrategy, a as CommonAdapterOptions } from './core-DcpxT2lH.js'; import './store-CQjuAFM_.js'; /** * Web-standard `fetch` adapter for edge runtimes (Cloudflare Workers, Deno, Bun, Next.js edge). * Wraps a `(Request, ...args) => Response` handler with a rate-limit gate: on allow it forwards to * the handler and copies the rate-limit headers onto the returned `Response`; on deny it returns a * `429` with `Retry-After`. Store outages resolve via the explicit fail policy. Uses the global * Web `Request`/`Response`/`Headers` (Node 18+). See THROTTLEKIT.md §§14,15. * * Also exposes {@link withUnifiedAdmission} and {@link withAdaptiveConcurrency} (0.9.2, TK-1326) * that wire `release()` by wrapping the returned `Response.body` ReadableStream — completion * (`done`), errors, and consumer cancellation each map to a single release call with the right * `dropped` value. See `research/bigger-bets/middleware-integration/DESIGN.md` §6. */ /** A Web-`fetch` style handler: receives a `Request` (plus any runtime args) and returns a `Response`. */ type FetchHandler = (request: Request, ...args: unknown[]) => Response | Promise; type FetchRateLimitOptions = LimiterOrStrategy & CommonAdapterOptions & { /** Cost of a request in limiter units. A function computes it per request. Default 1. */ cost?: number | ((request: Request) => number); /** Derive the limit key from a request. Default: `cf-connecting-ip` → `x-forwarded-for` → `"anon"`. */ key?: (request: Request) => string; /** Observability hook fired on every denial. */ onLimited?: (request: Request, decision: Decision) => void; /** Observability hook fired when the store throws (before the fail policy is applied). */ onError?: (request: Request, err: unknown) => void; /** Custom 429 responder. When provided, it fully owns the denial response. */ handler?: (request: Request, decision: Decision) => Response | Promise; }; /** * Wrap a `fetch` handler with rate limiting. * * @example * export default { fetch: withRateLimit(handler, { strategy: gcra({ limit: 30, periodMs: 10_000 }) }) }; */ declare function withRateLimit(handler: FetchHandler, options: FetchRateLimitOptions): (request: Request, ...args: unknown[]) => Promise; /** Per-axis Decision snapshot from `admitter.lastDecisions()`. */ type AxisSnapshot = Readonly>>; /** Options for {@link withUnifiedAdmission}. */ type WithUnifiedAdmissionOptions = Pick & { admitter: UnifiedAdmitter; cost?: number | ((request: Request) => number); key?: (request: Request) => string; clock?: Clock; dropOn5xx?: boolean; onLimited?: (request: Request, decision: Decision, axes: AxisSnapshot) => void; onError?: (request: Request, err: unknown) => void; handler?: (request: Request, decision: Decision, axes: AxisSnapshot) => Response | Promise; }; /** * Wrap a fetch handler with a {@link UnifiedAdmitter}. On admit it forwards to the handler, * then wraps `Response.body` so the release fires when the body stream completes / errors / * is cancelled. * * @example * export default { * fetch: withUnifiedAdmission(myHandler, { admitter }), * }; */ declare function withUnifiedAdmission(handler: FetchHandler, options: WithUnifiedAdmissionOptions): (request: Request, ...args: unknown[]) => Promise; /** Options for {@link withAdaptiveConcurrency}. */ type WithAdaptiveConcurrencyOptions = Pick & { guard: ConcurrencyGuard; clock?: Clock; dropOn5xx?: boolean; onLimited?: (request: Request, decision: Decision) => void; handler?: (request: Request, decision: Decision) => Response | Promise; }; /** * Wrap a fetch handler with an adaptive {@link ConcurrencyGuard}. The release is wired to the * Response body stream lifecycle. */ declare function withAdaptiveConcurrency(handler: FetchHandler, options: WithAdaptiveConcurrencyOptions): (request: Request, ...args: unknown[]) => Promise; export { CommonAdapterOptions, type FetchHandler, type FetchRateLimitOptions, LimiterOrStrategy, type WithAdaptiveConcurrencyOptions, type WithUnifiedAdmissionOptions, withAdaptiveConcurrency, withRateLimit, withUnifiedAdmission };