lib/limit.js - git - Webernet Explorer _X
git 🏠 Home 👤 Sign up Self hosted git, retro shell
★ otgit, code hosting from the year 2000 ★
📄 octuna / lib / limit.js
19 lines · 0.5 KB · Raw
1function make(max, windowMs) {
2 const hits = new Map();
3 return function check(key) {
4 const now = Date.now();
5 const arr = (hits.get(key) || []).filter(t => now - t < windowMs);
6 if (arr.length >= max) { hits.set(key, arr); return false; }
7 arr.push(now);
8 hits.set(key, arr);
9 if (hits.size > 5000) {
10 for (const [k, v] of hits) {
11 if (!v.length || now - v[v.length - 1] > windowMs) hits.delete(k);
12 }
13 }
14 return true;
15 };
16}
17 
18module.exports = { make };
19 
Done 🔒 Internet