Spaces:
Build error
Build error
Download lib/rate-limit.ts from Jacksuyu/requestsolution: direct link, hf CLI and curl.
- Browser
- Download file 651 Bytes
-
https://huggingface.co/spaces/Jacksuyu/requestsolution/resolve/main/lib/rate-limit.ts
- Command line
-
hf download hf://spaces/Jacksuyu/requestsolution/lib/rate-limit.ts
-
curl -L -o rate-limit.ts https://huggingface.co/spaces/Jacksuyu/requestsolution/resolve/main/lib/rate-limit.ts
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 }; | |
| } | |