video-optimizer-bot / src /security /rateLimit.js
Hugging Face Deployer
Deploy bot to Hugging Face Spaces
03b405b
Raw History Blame Contribute Delete
678 Bytes
const buckets = new Map();
function allowRate(key, limit, windowMs) {
const now = Date.now();
const bucket = buckets.get(key) || [];
const recent = bucket.filter((time) => now - time < windowMs);
if (recent.length >= limit) {
buckets.set(key, recent);
return false;
}
recent.push(now);
buckets.set(key, recent);
return true;
}
setInterval(() => {
const cutoff = Date.now() - 24 * 60 * 60 * 1000;
for (const [key, values] of buckets.entries()) {
const recent = values.filter((time) => time > cutoff);
if (recent.length) buckets.set(key, recent);
else buckets.delete(key);
}
}, 60 * 60 * 1000).unref();
module.exports = { allowRate };