(ip, now = Date.now())
| 64 | } |
| 65 | |
| 66 | function checkRateLimit(ip, now = Date.now()) { |
| 67 | cleanupExpiredEntries(now); |
| 68 | const { windowMs, maxRequests } = getRateLimitConfig(); |
| 69 | |
| 70 | const existing = ipRateLimits.get(ip); |
| 71 | if (!existing) { |
| 72 | ipRateLimits.set(ip, { count: 1, windowStart: now }); |
| 73 | return { allowed: true, remaining: maxRequests - 1, retryAfterSeconds: 0 }; |
| 74 | } |
| 75 | |
| 76 | if (now - existing.windowStart >= windowMs) { |
| 77 | ipRateLimits.set(ip, { count: 1, windowStart: now }); |
| 78 | return { allowed: true, remaining: maxRequests - 1, retryAfterSeconds: 0 }; |
| 79 | } |
| 80 | |
| 81 | if (existing.count >= maxRequests) { |
| 82 | const retryAfterMs = windowMs - (now - existing.windowStart); |
| 83 | const retryAfterSeconds = Math.max(1, Math.ceil(retryAfterMs / 1000)); |
| 84 | return { allowed: false, remaining: 0, retryAfterSeconds }; |
| 85 | } |
| 86 | |
| 87 | existing.count += 1; |
| 88 | ipRateLimits.set(ip, existing); |
| 89 | return { allowed: true, remaining: maxRequests - existing.count, retryAfterSeconds: 0 }; |
| 90 | } |
| 91 | |
| 92 | function checkRepoCooldown(repoKey, now = Date.now()) { |
| 93 | cleanupExpiredEntries(now); |
no test coverage detected