ez-ratelimiter
v0.3.0
Published
The ez-iest ratelimiter in existance for nodejs.
Downloads
2
Maintainers
Readme
Ez Ratelimiter
Why?
This rate-limiter attempts to keep it e-z, with no additional functions.
All in all a basic package to rate-limit your clients.
Also used in the Fronvo server
Installing
npm i ez-ratelimiter
Documentation
Documentation for the Ez Ratelimiter can be found at https://ez-packages.github.io/ez-ratelimiter/.
Examples
Setup an instance of EzRateLimiter
:
import { EzRateLimiter } from 'ez-ratelimiter';
const ezLimiter = new EzRateLimiter({
maxPoints: 10,
clearDelay: 1000
});
Consume points for a client:
ezLimiter.consumePoints('client-uid', 5);
Handle a consumption error:
import { EzError } from 'ez-ratelimiter';
ezLimiter.consumePoints('client-uid', 11)
.catch((err: EzError) => {
// EzError check
if(err.currentPoints) {
console.log(`Client requested ${err.requestedPoints} points when it has ${err.currentPoints} points and maxPoints are ${err.maxPoints}.`);
} else {
console.log(`[${err.name}]: ${err.message}`);
}
});
Register middleware:
ezLimiter.$use({
beforeConsumption: ({consumerKey, requestedPoints}) => {
console.log(`Attempting to consume ${requestedPoints} points for ${consumerKey}...`)
},
afterConsumption: ({consumerKey, remainingPoints}) => {
console.log(`[${consumerKey}]: ${remainingPoints} points remaining.`);
}
});
Stop the rate-limiter:
ezLimiter.stop()
.then(() => {
console.log('The Ez ratelimiter has been stopped.');
});
Made by Shadofer with joy.