ci-electrical/web/lib/rate-limit.ts

13 lines
421 B
TypeScript

const hits = new Map<string, { count: number; time: number }>();
export function allow(bucket: string, key: string, limit = 10, windowMs = 60_000) {
const k = `${bucket}:${key}`;
const now = Date.now();
const entry = hits.get(k);
if (!entry || now - entry.time > windowMs) {
hits.set(k, { count: 1, time: now });
return true;
}
if (entry.count >= limit) return false;
entry.count++;
return true;
}