(userId: string)
| 10 | const rateLimits = new Map<string, RateLimit>() |
| 11 | |
| 12 | export function isRateLimited(userId: string): boolean { |
| 13 | const now = Date.now() |
| 14 | const userRateLimit = rateLimits.get(userId) |
| 15 | |
| 16 | // Clean up expired rate limits |
| 17 | if (userRateLimit && userRateLimit.resetAt < now) { |
| 18 | rateLimits.delete(userId) |
| 19 | } |
| 20 | |
| 21 | if (!rateLimits.has(userId)) { |
| 22 | rateLimits.set(userId, { |
| 23 | count: 1, |
| 24 | resetAt: now + RATE_LIMIT_WINDOW, |
| 25 | }) |
| 26 | return false |
| 27 | } |
| 28 | |
| 29 | const limit = rateLimits.get(userId)! |
| 30 | limit.count++ |
| 31 | |
| 32 | return limit.count > MAX_REQUESTS |
| 33 | } |
no test coverage detected