22 lines
500 B
JavaScript
22 lines
500 B
JavaScript
var requests = {};
|
|
|
|
function checkLimit(userId, res) {
|
|
var now = Date.now();
|
|
var windowMs = 60000;
|
|
if (!requests[userId]) requests[userId] = [];
|
|
var timestamps = requests[userId];
|
|
timestamps.push(now);
|
|
while (timestamps.length > 0 && timestamps[0] < now - windowMs) {
|
|
timestamps.shift();
|
|
}
|
|
if (timestamps.length > 100) {
|
|
res.status(429).send("Rate limited");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function reset() { requests = {}; }
|
|
|
|
module.exports = { checkLimit, reset };
|