● Zion Boggan zionboggan.com ↗

Rate-limit middleware: globalThis-backed store (proxy-layer is primary; app limiter is backstop)

fae84f1   Zion Boggan committed on Jun 26, 2026 (1 month ago)
middleware.ts +4 -1
@@ -11,7 +11,10 @@ const WINDOW_MS = 60_000;
const MAX_REQ = 120; // per IP per window, across /api/*
type Bucket = { count: number; resetAt: number };
-const buckets = new Map<string, Bucket>();
+// Anchor on globalThis so the counter survives middleware module re-evaluation
+// within the same Node process (plain module-level state can be reset).
+const g = globalThis as unknown as { __docketBuckets?: Map<string, Bucket> };
+const buckets: Map<string, Bucket> = (g.__docketBuckets ??= new Map());
function clientIp(req: NextRequest): string {
const xff = req.headers.get("x-forwarded-for");