import { C as ConcurrencyGuard, j as UnifiedAdmitter, k as UnifiedAxis } from './unified-D0OOwfM-.cjs'; import { C as Clock, D as Decision } from './types-DKirIBQt.cjs'; import { a as CommonAdapterOptions, L as LimiterOrStrategy } from './core-CS9fzc6a.cjs'; import './store-BZNM-FbH.cjs'; /** * Elysia adapter. Builds an `onBeforeHandle` hook that gates the routes it guards: it sets the * standards headers on the context, then either lets the handler run (returns `undefined`) or * short-circuits with a `429` body and `set.status`; a fail-closed store outage short-circuits with * `503`. The key derives from the Web `Request` — `cf-connecting-ip`/trusted `x-forwarded-for` → * `"anon"` (audit TK-S01) — overridable. The context shape is modeled structurally, so a real Elysia * context satisfies it with no dependency. See THROTTLEKIT.md §§14,15. * * Also exposes {@link elysiaUnifiedAdmission} and {@link elysiaAdaptiveConcurrency} (0.9.2, * TK-1326) — wrap-style functions the user calls inside the route handler: * `app.get("/", (ctx) => admit(ctx, async () => "result"))` * * The wrap pattern is universal across Elysia's runtime versions; it doesn't require Elysia's * own plugin lifecycle hooks. The release fires when the wrapped handler returns / throws. */ /** The slice of an Elysia context the adapter reads/writes: the request and the mutable response `set`. */ interface ElysiaContextLike { request: Request; set: { status?: number | string; headers: Record; }; } /** An Elysia `onBeforeHandle` hook: returns `undefined` to proceed, or a value to short-circuit. */ type ElysiaRateLimitHook = (ctx: ElysiaContextLike) => unknown; type ElysiaRateLimitOptions = LimiterOrStrategy & CommonAdapterOptions & { /** Cost of a request in limiter units. A function computes it per context. Default 1. */ cost?: number | ((ctx: ElysiaContextLike) => number); /** Derive the limit key from the context. Default: edge client IP (see {@link edgeClientIp}). */ key?: (ctx: ElysiaContextLike) => string; /** Observability hook fired on every denial. */ onLimited?: (ctx: ElysiaContextLike, decision: Decision) => void; /** Observability hook fired when the store throws (before the fail policy is applied). */ onError?: (ctx: ElysiaContextLike, err: unknown) => void; /** Custom denial responder; its return value becomes the short-circuit response body. */ handler?: (ctx: ElysiaContextLike, decision: Decision) => unknown; }; /** * Build an Elysia `onBeforeHandle` hook that rate-limits requests. * * @example * ```ts * import { Elysia } from "elysia"; * import { elysiaRateLimit } from "throttlekit/elysia"; * import { gcra } from "throttlekit"; * new Elysia() * .onBeforeHandle(elysiaRateLimit({ strategy: gcra({ limit: 30, periodMs: 10_000 }) })) * .get("/", () => "ok"); * ``` */ declare function elysiaRateLimit(options: ElysiaRateLimitOptions): ElysiaRateLimitHook; /** Per-axis Decision snapshot. */ type AxisSnapshot = Readonly>>; /** Options for {@link elysiaUnifiedAdmission}. */ type ElysiaUnifiedAdmissionOptions = Pick & { admitter: UnifiedAdmitter; cost?: number | ((ctx: ElysiaContextLike) => number); key?: (ctx: ElysiaContextLike) => string; clock?: Clock; dropOn5xx?: boolean; onLimited?: (ctx: ElysiaContextLike, decision: Decision, axes: AxisSnapshot) => void; onError?: (ctx: ElysiaContextLike, err: unknown) => void; }; /** A `(ctx, body) => result` wrap; user calls inside their handler. */ type ElysiaUnifiedAdmissionWrap = (ctx: ElysiaContextLike, body: () => Promise | R) => Promise; /** * Build an Elysia admission wrap. The user calls `await admit(ctx, async () => ...)` inside * their handler. The wrap admits (denying with 429 if blocked), forwards to the body inside * a try/finally, and fires release with `dropped = thrown`. * * @example * const admit = elysiaUnifiedAdmission({ admitter }); * app.get("/", (ctx) => admit(ctx, async () => "ok")); */ declare function elysiaUnifiedAdmission(options: ElysiaUnifiedAdmissionOptions): ElysiaUnifiedAdmissionWrap; /** Options for {@link elysiaAdaptiveConcurrency}. */ type ElysiaAdaptiveConcurrencyOptions = Pick & { guard: ConcurrencyGuard; clock?: Clock; dropOn5xx?: boolean; onLimited?: (ctx: ElysiaContextLike, decision: Decision) => void; }; /** Build an Elysia adaptive-concurrency wrap. */ declare function elysiaAdaptiveConcurrency(options: ElysiaAdaptiveConcurrencyOptions): ElysiaUnifiedAdmissionWrap; export { CommonAdapterOptions, type ElysiaAdaptiveConcurrencyOptions, type ElysiaContextLike, type ElysiaRateLimitHook, type ElysiaRateLimitOptions, type ElysiaUnifiedAdmissionOptions, type ElysiaUnifiedAdmissionWrap, LimiterOrStrategy, elysiaAdaptiveConcurrency, elysiaRateLimit, elysiaUnifiedAdmission };