19 lines
529 B
JavaScript
19 lines
529 B
JavaScript
/** In-memory guard against duplicate actions within a time window (M13-T08). */
|
|
export function createCooldown(windowMs) {
|
|
const entries = new Map();
|
|
|
|
return {
|
|
/** Returns true when the key is free, and locks it for `windowMs`. */
|
|
acquire(key) {
|
|
const now = Date.now();
|
|
const expiresAt = entries.get(key);
|
|
if (expiresAt && expiresAt > now) return false;
|
|
entries.set(key, now + windowMs);
|
|
return true;
|
|
},
|
|
release(key) {
|
|
entries.delete(key);
|
|
},
|
|
};
|
|
}
|