const rateLimitMap = new Map< string, { count: number; resetTime: number } >(); export function rateLimit( identifier: string, limit: number = 5, windowMs: number = 60 * 60 * 1000 // 1 hour ): { success: boolean; remaining: number } { const now = Date.now(); const entry = rateLimitMap.get(identifier); if (!entry || now > entry.resetTime) { rateLimitMap.set(identifier, { count: 1, resetTime: now + windowMs }); return { success: true, remaining: limit - 1 }; } if (entry.count >= limit) { return { success: false, remaining: 0 }; } entry.count++; return { success: true, remaining: limit - entry.count }; }