import { Context, Middleware } from 'koa'; import { C as ConcurrencyGuard, j as UnifiedAdmitter, k as UnifiedAxis } from './unified-BouIz5EX.js'; import { C as Clock, D as Decision } from './types-DKirIBQt.js'; import { a as CommonAdapterOptions, L as LimiterOrStrategy } from './core-DcpxT2lH.js'; import './store-CQjuAFM_.js'; /** * Koa v3 adapter. Wraps a {@link Limiter} (prebuilt or constructed inline) as Koa `Middleware` * that derives a proxy-correct client-IP key by default, enforces the limit, emits standards * headers, and responds `429` with `Retry-After` on a denial — with explicit fail-open/closed * behavior when the store is unreachable. Mirrors the Express adapter's control flow, adapted to * Koa's single `ctx`. See THROTTLEKIT.md §§14,15. * * Also exposes {@link koaUnifiedAdmission} and {@link koaAdaptiveConcurrency} (0.9.2, TK-1325) * that wire `release()` to `ctx.res.on("finish")` + `ctx.res.on("close")`. */ type KoaRateLimitOptions = LimiterOrStrategy & CommonAdapterOptions & { /** Cost of a request in limiter units. A function computes it per request. Default 1. */ cost?: number | ((ctx: Context) => number); /** Derive the limit key from the context. Default: proxy-correct, aggregated client IP. */ key?: (ctx: Context) => string; /** Observability hook fired on every denial, before the response is written. */ onLimited?: (ctx: Context, decision: Decision) => void; /** Observability hook fired when the store throws (before the fail policy is applied). */ onError?: (ctx: Context, err: unknown) => void; /** * Custom 429 responder. When provided, it fully owns the denial response (set `ctx.status` / * `ctx.body` yourself). The standards headers are already applied before it runs. */ handler?: (ctx: Context, decision: Decision) => void; }; /** * Create a Koa middleware enforcing a rate limit. The default client-IP key reads * `ctx.req` (the Node `IncomingMessage`), so it is correct independent of Koa's `app.proxy` * setting; headers are applied with `ctx.set`. * * @example * app.use(koaRateLimit({ strategy: gcra({ limit: 100, periodMs: 60_000 }) })); */ declare function koaRateLimit(options: KoaRateLimitOptions): Middleware; /** Per-axis Decision snapshot from `admitter.lastDecisions()`. */ type AxisSnapshot = Readonly>>; /** Options for {@link koaUnifiedAdmission}. */ type KoaUnifiedAdmissionOptions = Pick & { /** The unified admitter to enforce. */ admitter: UnifiedAdmitter; /** Cost passed to the cost axis. Defaults to 1. */ cost?: number | ((ctx: Context) => number); /** Key passed to the rate/cost axes. Default: client IP. */ key?: (ctx: Context) => string; /** Clock used for header delta math. */ clock?: Clock; /** Treat 5xx as `dropped: true`. Default false. */ dropOn5xx?: boolean; /** Fired on every denial. */ onLimited?: (ctx: Context, decision: Decision, axes: AxisSnapshot) => void; /** Fired when `admit()` throws. */ onError?: (ctx: Context, err: unknown) => void; /** Custom 429 responder. */ handler?: (ctx: Context, decision: Decision, axes: AxisSnapshot) => void; }; /** * Koa middleware enforcing a {@link UnifiedAdmitter}. On admit it wires `release()` to * `ctx.res` (the Node response). The middleware does NOT `await next()` itself for the * lifecycle — the response events fire from Node's stream, so the `await next()` pattern * would double-fire. See `research/bigger-bets/middleware-integration/DESIGN.md` §4. */ declare function koaUnifiedAdmission(options: KoaUnifiedAdmissionOptions): Middleware; /** Options for {@link koaAdaptiveConcurrency}. */ type KoaAdaptiveConcurrencyOptions = Pick & { /** The concurrency guard to enforce. */ guard: ConcurrencyGuard; /** Clock used for header delta math. */ clock?: Clock; /** Treat 5xx as `dropped: true`. Default false. */ dropOn5xx?: boolean; /** Fired on every denial. */ onLimited?: (ctx: Context, decision: Decision) => void; /** Custom 429 responder. */ handler?: (ctx: Context, decision: Decision) => void; }; /** * Koa middleware enforcing an adaptive {@link ConcurrencyGuard}. Wires release to ctx.res * lifecycle so completions/aborts feed the controller. */ declare function koaAdaptiveConcurrency(options: KoaAdaptiveConcurrencyOptions): Middleware; export { CommonAdapterOptions, type KoaAdaptiveConcurrencyOptions, type KoaRateLimitOptions, type KoaUnifiedAdmissionOptions, LimiterOrStrategy, koaAdaptiveConcurrency, koaRateLimit, koaUnifiedAdmission };