@ezier/ratelimit
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

Ezier Ratelimiter logo

Ezier Ratelimiter

An ezier 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 @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.

/@ezier/ratelimit/

    Package Sidebar

    Install

    npm i @ezier/ratelimit

    Weekly Downloads

    1

    Version

    0.3.2

    License

    MIT

    Unpacked Size

    14.2 kB

    Total Files

    5

    Last publish

    Collaborators

    • shadofer