requestsolution / lib /rate-limit.ts
Jacksuyu's picture
Claude Opus 4.6
Initial release: Community Demand Board V0.1
8e944c6
Raw History Blame Contribute Delete
651 Bytes
const rateLimitMap = new Map<
string,
{ count: number; resetTime: number }
>();
export function rateLimit(
identifier: string,
limit: number = 5,
windowMs: number = 60 * 60 * 1000 // 1 hour
): { success: boolean; remaining: number } {
const now = Date.now();
const entry = rateLimitMap.get(identifier);
if (!entry || now > entry.resetTime) {
rateLimitMap.set(identifier, { count: 1, resetTime: now + windowMs });
return { success: true, remaining: limit - 1 };
}
if (entry.count >= limit) {
return { success: false, remaining: 0 };
}
entry.count++;
return { success: true, remaining: limit - entry.count };
}