Spaces:
Configuration error
Configuration error
Download src/security/rateLimit.js from UUUZCX/video-optimizer-bot: direct link, hf CLI and curl.
- Browser
- Download file 678 Bytes
-
https://huggingface.co/spaces/UUUZCX/video-optimizer-bot/resolve/main/src/security/rateLimit.js
- Command line
-
hf download hf://spaces/UUUZCX/video-optimizer-bot/src/security/rateLimit.js
-
curl -L -o rateLimit.js https://huggingface.co/spaces/UUUZCX/video-optimizer-bot/resolve/main/src/security/rateLimit.js
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 }; | |