interface RateLimiterOptions { windowMs: number; max: number; } interface Entry { count: number; resetAt: number; } export class RateLimiter { private store = new Map(); private windowMs: number; private max: number; constructor({ windowMs = 60_000, max = 60 }: RateLimiterOptions) { this.windowMs = windowMs; this.max = max; } check(key: string): { allowed: boolean; remaining: number; resetAt: number } { const now = Date.now(); const entry = this.store.get(key); if (!entry || now >= entry.resetAt) { const resetAt = now + this.windowMs; this.store.set(key, { count: 1, resetAt }); return { allowed: true, remaining: this.max - 1, resetAt }; } if (entry.count >= this.max) { return { allowed: false, remaining: 0, resetAt: entry.resetAt }; } entry.count++; return { allowed: true, remaining: this.max - entry.count, resetAt: entry.resetAt }; } reset(key: string): void { this.store.delete(key); } }