feat(ai): simpler In-Memory-Ratelimiter pro IP
This commit is contained in:
21
src/lib/server/ai/rate-limit.ts
Normal file
21
src/lib/server/ai/rate-limit.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export type RateLimiter = { check: (key: string) => boolean };
|
||||
|
||||
export function createRateLimiter(opts: {
|
||||
windowMs: number;
|
||||
max: number;
|
||||
}): RateLimiter {
|
||||
const store = new Map<string, { count: number; resetAt: number }>();
|
||||
return {
|
||||
check(key: string): boolean {
|
||||
const now = Date.now();
|
||||
const entry = store.get(key);
|
||||
if (!entry || entry.resetAt <= now) {
|
||||
store.set(key, { count: 1, resetAt: now + opts.windowMs });
|
||||
return true;
|
||||
}
|
||||
if (entry.count >= opts.max) return false;
|
||||
entry.count += 1;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user