19 lines
431 B
TypeScript
19 lines
431 B
TypeScript
const buckets = new Map<string, number[]>();
|
|
|
|
export function assertRateLimit(
|
|
key: string,
|
|
maxRequests: number,
|
|
windowMs: number,
|
|
) {
|
|
const now = Date.now();
|
|
const start = now - windowMs;
|
|
const hits = (buckets.get(key) ?? []).filter((ts) => ts >= start);
|
|
|
|
if (hits.length >= maxRequests) {
|
|
throw new Error("Rate limit exceeded. Please try again in a minute.");
|
|
}
|
|
|
|
hits.push(now);
|
|
buckets.set(key, hits);
|
|
}
|