const attempts = new Map() const WINDOW_MS = 60_000 const MAX_ATTEMPTS = 5 export function rateLimit(key: string): { allowed: boolean; retryAfterMs: number } { const now = Date.now() const entry = attempts.get(key) if (!entry || now > entry.resetAt) { attempts.set(key, { count: 1, resetAt: now + WINDOW_MS }) return { allowed: true, retryAfterMs: 0 } } entry.count++ if (entry.count > MAX_ATTEMPTS) { return { allowed: false, retryAfterMs: entry.resetAt - now } } return { allowed: true, retryAfterMs: 0 } } setInterval(() => { const now = Date.now() for (const [k, v] of attempts) { if (now > v.resetAt) attempts.delete(k) } }, 60_000)