29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
export type RateLimitArgs = {
|
|
/** Identifier to limit on — typically the client IP. */
|
|
key: string;
|
|
/** Max submissions allowed per window. Defaults to 5. */
|
|
max?: number;
|
|
/** Separate counter space per feature, e.g. 'form', 'search', 'mcp'. Default 'form'. */
|
|
scope?: string;
|
|
/** Window length in ms. Defaults to 60_000 (one minute). */
|
|
windowMs?: number;
|
|
};
|
|
/**
|
|
* Returns true when the request is within the limit, false when it should be
|
|
* rejected. A missing key (no IP) is allowed through — better to accept a
|
|
* submission than to block everyone behind a proxy that strips the header.
|
|
*/
|
|
export declare function checkRateLimit({ cost, key, max, scope, windowMs }: {
|
|
cost?: number;
|
|
} & RateLimitArgs): boolean;
|
|
/** Seconds until the key's window resets — for a Retry-After header. */
|
|
export declare function retryAfterSeconds(key: string, scope?: string): number;
|
|
/**
|
|
* Client IP from a Request. Cloudflare's `cf-connecting-ip` first (set by the
|
|
* edge, the real visitor), then the first `x-forwarded-for` hop, then
|
|
* `x-real-ip`. Empty string when none — the limiter then lets the request through.
|
|
*/
|
|
export declare function clientIp(req: Request): string;
|
|
/** Standard 429 for route handlers. */
|
|
export declare function tooManyRequests(key: string, scope: string, body: unknown): Response;
|