All files / src/lib account-rate-limit.ts

100% Statements 3/3
100% Branches 4/4
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                      3x         3x                 21x        
import { createRateLimiter } from "@/lib/rate-limit";
 
/**
 * The one limit on creating sandbox accounts, per address and in all, per
 * hour (ADR-054). The Klarbefund app's route and the PID register route of
 * ADR-067 draw on the same budget, so a second way in is not a second
 * allowance.
 *
 * Anchored on globalThis: Next.js compiles each route on its own, and a
 * module-level instance would give each route its own count.
 */
const g = globalThis as unknown as {
  __accountLimiter?: ReturnType<typeof createRateLimiter>;
};
 
export const accountLimiter =
  g.__accountLimiter ??
  (g.__accountLimiter = createRateLimiter({
    windowMs: 60 * 60_000,
    perKey: 5,
    inTotal: 100,
  }));
 
/** The caller's address as the ingress reports it, or "unknown". */
export function clientAddress(request: Request): string {
  return (
    request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "unknown"
  );
}