4.1 React Query 설치 및 Provider 구성 o

This commit is contained in:
koreacomp5
2025-10-09 15:13:12 +09:00
parent 8064837422
commit 55c0f6abb1
8 changed files with 113 additions and 4 deletions

23
src/lib/ratelimit.ts Normal file
View File

@@ -0,0 +1,23 @@
type Key = string;
const bucket: Map<Key, number[]> = new Map();
export function getClientKey(req: Request, extra?: string): string {
const ip = req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "local";
return extra ? `${ip}:${extra}` : ip;
}
export function isRateLimited(key: string, max: number, windowMs: number): boolean {
const now = Date.now();
const windowStart = now - windowMs;
const arr = bucket.get(key)?.filter((t) => t >= windowStart) ?? [];
if (arr.length >= max) {
bucket.set(key, arr); // cleanup
return true;
}
arr.push(now);
bucket.set(key, arr);
return false;
}