ez-ratelimiter
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Ez Ratelimiter logo

Ez Ratelimiter

The ez-iest ratelimiter for nodejs.

npm bundle size npm NPM npm

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.

Package Sidebar

Install

npm i ez-ratelimiter

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

13.1 kB

Total Files

5

Last publish

Collaborators

  • shadofer