VYBECODEDDDDDDDDD

This commit is contained in:
Anaïs Saraiva
2026-08-15 00:20:45 +02:00
parent 1a2c8aaf7c
commit 5f201cf725
23 changed files with 2263 additions and 2 deletions
+29
View File
@@ -0,0 +1,29 @@
interface Bucket {
count: number;
resetAt: number;
}
export class RateLimiter {
private buckets = new Map<string, Bucket>();
constructor(
private readonly max: number,
private readonly windowMs: number,
) {}
check(ip: string): boolean {
const now = Date.now();
if (this.buckets.size > 10_000) this.prune(now);
const existing = this.buckets.get(ip);
const bucket = existing && existing.resetAt > now ? existing : { count: 0, resetAt: now + this.windowMs };
if (!existing || bucket !== existing) this.buckets.set(ip, bucket);
bucket.count++;
return bucket.count <= this.max;
}
private prune(now: number): void {
for (const [ip, bucket] of this.buckets) {
if (bucket.resetAt <= now) this.buckets.delete(ip);
}
}
}