/** * Rate Limiter * * Token bucket algorithm implementation for rate limiting * with support for burst limits and sliding windows. */ /** * Token bucket configuration */ export interface TokenBucketConfig { /** Maximum number of tokens (requests per window) */ capacity: number; /** Window size in milliseconds */ window: number; /** Burst limit - maximum tokens that can be consumed at once */ burstLimit?: number; /** Initial token count (defaults to capacity) */ initialTokens?: number; } /** * Rate limit check result */ export interface RateLimitResult { allowed: boolean; remaining: number; resetAt: number; retryAfter?: number; } /** * Token bucket rate limiter */ export declare class TokenBucket { private tokens; private lastRefill; private readonly capacity; private readonly refillRate; private readonly burstLimit; constructor(config: TokenBucketConfig); /** * Try to consume tokens */ consume(count?: number): RateLimitResult; /** * Check if tokens are available without consuming */ check(count?: number): RateLimitResult; /** * Get current token count */ getTokens(): number; /** * Check whether the bucket has refilled to capacity */ isAtCapacity(): boolean; /** * Reset the bucket to full capacity */ reset(): void; private refill; private calculateResetAt; } /** * Rate limiter that manages multiple buckets by key */ export declare class RateLimiter { private buckets; private readonly config; private cleanupInterval?; constructor(config: TokenBucketConfig, cleanupIntervalMs?: number); /** * Check rate limit for a key */ check(key: string, count?: number): RateLimitResult; /** * Consume tokens for a key */ consume(key: string, count?: number): RateLimitResult; /** * Reset rate limit for a key */ reset(key: string): void; /** * Remove a key's bucket */ remove(key: string): void; /** * Get all tracked keys */ getKeys(): string[]; /** * Dispose the rate limiter */ dispose(): void; private getBucket; private cleanup; } /** * Sliding window rate limiter * More accurate than token bucket for some use cases */ export declare class SlidingWindowRateLimiter { private windows; private readonly maxRequests; private readonly windowMs; private cleanupInterval?; constructor(maxRequests: number, windowMs: number, cleanupIntervalMs?: number); /** * Check if request is allowed and record it */ consume(key: string): RateLimitResult; /** * Check without consuming */ check(key: string): RateLimitResult; /** * Reset a key's window */ reset(key: string): void; /** * Dispose the rate limiter */ dispose(): void; private getWindow; private cleanup; } /** * Create a token bucket rate limiter */ export declare function createRateLimiter(config: TokenBucketConfig): RateLimiter; /** * Create a sliding window rate limiter */ export declare function createSlidingWindowLimiter(maxRequests: number, windowMs: number): SlidingWindowRateLimiter; //# sourceMappingURL=rate-limiter.d.ts.map