initial
This commit is contained in:
13
web/lib/rate-limit.ts
Normal file
13
web/lib/rate-limit.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user