Files
gestion-materiaux-destock/src/lib/rate-limit.ts
T

33 lines
892 B
TypeScript

const MAX_ATTEMPTS = 5
const BLOCK_DURATION_MS = 15 * 60 * 1000 // 15 minutes
type Entry = { count: number; blockedAt: number | null }
let attempts = new Map<string, Entry>()
export function checkRateLimit(key: string): boolean {
const now = Date.now()
const entry = attempts.get(key)
if (!entry) return true
if (entry.blockedAt !== null) {
if (now - entry.blockedAt < BLOCK_DURATION_MS) return false
attempts.delete(key)
}
return true
}
export function recordFailedAttempt(key: string): void {
const entry = attempts.get(key) ?? { count: 0, blockedAt: null }
entry.count += 1
if (entry.count >= MAX_ATTEMPTS) entry.blockedAt = Date.now()
attempts.set(key, entry)
}
export function clearAttempts(key: string): void {
attempts.delete(key)
}
/** Réinitialisation pour les tests uniquement */
export function _resetForTests(): void {
attempts = new Map()
}