Giter Club home page Giter Club logo

ratelimit's Introduction

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.

ratelimit's People

Contributors

stamtsag avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.