export interface RateLimitEntry { /** Timestamps (epoch ms) of recent failed attempts inside the window. */
attempts: number[]; /** If set, requests from this IP are blocked until this epoch-ms instant. */
lockedUntil?: number;
}
export interface RateLimitCheckResult { /** Whether the request is allowed to proceed. */
allowed: boolean; /** Number of remaining attempts before the limit is reached. */
remaining: number; /** Milliseconds until the lockout expires (0 when not locked). */
retryAfterMs: number;
}
export interface AuthRateLimiter { /** Check whether `ip` is currently allowed to attempt authentication. */
check(ip: string | undefined, scope?: string): RateLimitCheckResult; /** Record a failed authentication attempt for `ip`. */
recordFailure(ip: string | undefined, scope?: string): void; /** Reset the rate-limit state for `ip` (e.g. after a successful login). */
reset(ip: string | undefined, scope?: string): void; /** Return the current number of tracked IPs (useful for diagnostics). */
size(): number; /** Remove expired entries and release memory. */
prune(): void; /** Dispose the limiter and cancel periodic cleanup timers. */
dispose(): void;
}
const entries = new Map<string, RateLimitEntry>();
// Periodic cleanup to avoid unbounded map growth. const pruneTimer = pruneIntervalMs > 0 ? setInterval(() => prune(), pruneIntervalMs) : null; // Allow the Node.js process to exit even if the timer is still active. if (pruneTimer?.unref) {
pruneTimer.unref();
}
function prune(): void { const now = Date.now(); for (const [key, entry] of entries) { // If locked out, keep the entry until the lockout expires. if (entry.lockedUntil && now < entry.lockedUntil) { continue;
}
slideWindow(entry, now); if (entry.attempts.length === 0) {
entries.delete(key);
}
}
}
function size(): number { return entries.size;
}
function dispose(): void { if (pruneTimer) {
clearInterval(pruneTimer);
}
entries.clear();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.