ratelimits
v1.0.3
Published
Let's you set rates on any action occuring. Use a string to id the thing to be limited.
Downloads
5
Readme
ratelimit
Let's you set rates on any action occuring. Use a string to id the thing to be limited.
You can set time-bounded limits for instance IP addresses on a server (to deny access):
// set a threshold and number of minutes to reset
var ipaddress = ratelimits({threshold: 5, minutes: 1})
app.get('/', function(req, res){
var okToEnter = ipaddress.check(req.ip) // true|false
if(!okToEnter) res.sendStatus(429) // Too many requests
})
ipaddress.db // shows info, ie.. { '1.1.1.1': { expire: 1485105971142, count: 5 } }
or user actions like clicking a button in a browser: https://jsfiddle.net/digplan/ztmc4y3v/
<button id=mybutton onclick='this.disabled = this.innerText = !window.clicker.check(this.id)'>
Click me 6 times!</button>
var clicker = ratelimits({threshold: 5, minutes: 1/10})
// when limited
clicker.onlimited = (id, count) => {
console.log(`${id} has count ${count} and has been limited`)
}
// when reset
clicker.onreset = id => {
console.log(`${id} has been reset`)
}