File size: 1,695 Bytes
cf30fac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * In-memory sliding-window rate limiter for API endpoints.
 * Minimal zero-dependency implementation.
 */

const ipRequests = new Map()

// Clean up expired entries every 5 minutes to prevent memory leaks
const CLEANUP_INTERVAL_MS = 5 * 60 * 1000
let lastCleanup = Date.now()

function cleanupExpired(windowMs) {
  const now = Date.now()
  if (now - lastCleanup < CLEANUP_INTERVAL_MS) return
  lastCleanup = now

  for (const [ip, timestamps] of ipRequests.entries()) {
    const valid = timestamps.filter((t) => now - t < windowMs)
    if (valid.length === 0) {
      ipRequests.delete(ip)
    } else {
      ipRequests.set(ip, valid)
    }
  }
}

/**
 * Check if a request from an IP should be rate limited.
 * @param {string} ip - Client IP identifier
 * @param {number} maxRequests - Maximum allowed requests in the time window
 * @param {number} windowMs - Window duration in milliseconds (default: 60,000ms = 1 minute)
 * @returns {{ allowed: boolean, remaining: number, resetMs: number }}
 */
export function checkRateLimit(ip = '127.0.0.1', maxRequests = 15, windowMs = 60_000) {
  const now = Date.now()
  cleanupExpired(windowMs)

  const timestamps = ipRequests.get(ip) || []
  const validTimestamps = timestamps.filter((t) => now - t < windowMs)

  if (validTimestamps.length >= maxRequests) {
    const oldestTimestamp = validTimestamps[0]
    const resetMs = windowMs - (now - oldestTimestamp)
    return {
      allowed: false,
      remaining: 0,
      resetMs: Math.max(0, resetMs),
    }
  }

  validTimestamps.push(now)
  ipRequests.set(ip, validTimestamps)

  return {
    allowed: true,
    remaining: maxRequests - validTimestamps.length,
    resetMs: windowMs,
  }
}