Ezier Ratelimiter
An ezier ratelimiter for nodejs.
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 @ezier/ratelimit
Documentation
Documentation for the Ezier Ratelimiter can be found at https://ezier-project.github.io/ratelimit/.
Examples
Setup an instance of EzierLimiter
:
import { EzierLimiter } from '@ezier/ratelimit';
const ezierLimiter = new EzierLimiter({
maxPoints: 10,
clearDelay: 1000
});
Consume points for a client:
ezierLimiter.consumePoints('client-uid', 5);
Handle a consumption error:
import { EzierLimiterError } from '@ezier/ratelimit';
ezierLimiter.consumePoints('client-uid', 11)
.catch((err: EzierLimiterError) => {
// EzierLimiterError 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:
ezierLimiter.$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:
ezierLimiter.stop()
.then(() => {
console.log('The Ezier Ratelimiter has been stopped.');
});
Made by Shadofer with joy.