Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
1.3 kB ยท 47 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748export const RATE_LIMIT_WINDOW = 60;export const MAX_REQUESTS_PER_WINDOW = 3;
type CdnRateLimit = | { status: "skipped" } | { status: "no-client-ip" } | { status: "ok" | "limited"; limit: number; remaining: number; reset: number; };
// dev runs without Upstash credentials and the redis client throws at module// evaluation, so the limiter is only imported (and enforced) in productionexport async function rateLimitCdnRequest( prefix: string, headers: Headers,): Promise<CdnRateLimit> { if (process.env.NODE_ENV !== "production") return { status: "skipped" };
const { getClientIP, ratelimit } = await import("@/lib/ratelimit");
const clientIP = getClientIP(headers); if (!clientIP) return { status: "no-client-ip" };
const rl = await ratelimit(`${prefix}:${clientIP}`, { window: RATE_LIMIT_WINDOW, limit: MAX_REQUESTS_PER_WINDOW, });
return { status: rl.success ? "ok" : "limited", limit: rl.limit, remaining: rl.remaining, reset: rl.reset, };}
export function rateLimitHeaders(rl: CdnRateLimit): Record<string, string> { if (rl.status !== "ok" && rl.status !== "limited") return {}; return { "X-RateLimit-Limit": rl.limit.toString(), "X-RateLimit-Remaining": rl.remaining.toString(), "X-RateLimit-Reset": rl.reset.toString(), };}