Download src/lib/rate-limit.ts from T2T-AI-X/copilot_work_3: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/T2T-AI-X/copilot_work_3/resolve/refs%2Fpr%2F4/src/lib/rate-limit.ts
- Command line
-
hf download hf://T2T-AI-X/copilot_work_3@refs/pr/4/src/lib/rate-limit.ts
-
curl -L -o rate-limit.ts https://huggingface.co/T2T-AI-X/copilot_work_3/resolve/refs%2Fpr%2F4/src/lib/rate-limit.ts
2.26 kB
| type Bucket = { count: number; resetAt: number }; | |
| declare global { | |
| var __chainkitRateLimits: Map<string, Bucket> | undefined; | |
| } | |
| const MAX_KEYS = 10_000; | |
| function getBuckets() { | |
| globalThis.__chainkitRateLimits ??= new Map(); | |
| return globalThis.__chainkitRateLimits; | |
| } | |
| export type RateLimitResult = { allowed: boolean; retryAfterSeconds: number }; | |
| /** | |
| * Fixed-window limiter held in process memory. On Cloud Run it is per instance | |
| * and resets on cold start, so treat it as best-effort; put Cloud Armor in front | |
| * for a hard limit. | |
| */ | |
| export function checkRateLimit( | |
| key: string, | |
| limit: number, | |
| windowMs: number, | |
| now = Date.now(), | |
| ): RateLimitResult { | |
| const buckets = getBuckets(); | |
| if (buckets.size > MAX_KEYS) { | |
| for (const [bucketKey, bucket] of buckets) { | |
| if (bucket.resetAt <= now) buckets.delete(bucketKey); | |
| } | |
| // Still full of live buckets: drop the oldest ones rather than grow unbounded. | |
| if (buckets.size > MAX_KEYS) { | |
| for (const bucketKey of buckets.keys()) { | |
| buckets.delete(bucketKey); | |
| if (buckets.size <= MAX_KEYS / 2) break; | |
| } | |
| } | |
| } | |
| const existing = buckets.get(key); | |
| if (!existing || existing.resetAt <= now) { | |
| buckets.set(key, { count: 1, resetAt: now + windowMs }); | |
| return { allowed: true, retryAfterSeconds: 0 }; | |
| } | |
| existing.count += 1; | |
| if (existing.count > limit) { | |
| return { allowed: false, retryAfterSeconds: Math.max(1, Math.ceil((existing.resetAt - now) / 1000)) }; | |
| } | |
| return { allowed: true, retryAfterSeconds: 0 }; | |
| } | |
| /** Client IP as set by Cloud Run / a proxy. Falls back to "unknown". */ | |
| export function getClientIp(request: Request) { | |
| const forwarded = request.headers.get("x-forwarded-for"); | |
| if (forwarded) { | |
| // Cloud Run appends the real client address; the last entry is the one we trust. | |
| const parts = forwarded.split(",").map((part) => part.trim()).filter(Boolean); | |
| if (parts.length) return parts[parts.length - 1]; | |
| } | |
| return request.headers.get("x-real-ip") ?? "unknown"; | |
| } | |
| export function rateLimitResponse(result: RateLimitResult) { | |
| return Response.json( | |
| { error: "Too many requests. Please try again later." }, | |
| { status: 429, headers: { "Retry-After": String(result.retryAfterSeconds) } }, | |
| ); | |
| } | |