File size: 611 Bytes
94193b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Shared fixed-window rate limiter for the web proxy routes.
 * In-memory, per-process. Keyed by workspace session or client IP.
 */
const WINDOW_MS = 60_000;
const LIMIT = 60;
const MAX_KEYS = 10_000;
const hits = new Map<string, number[]>();

export function checkRateLimit(key: string): boolean {
  const now = Date.now();
  if (hits.size > MAX_KEYS) hits.clear(); // crude memory bound
  const recent = (hits.get(key) ?? []).filter(t => now - t < WINDOW_MS);
  if (recent.length >= LIMIT) {
    hits.set(key, recent);
    return false;
  }
  recent.push(now);
  hits.set(key, recent);
  return true;
}