npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-rate-limiting

v0.1.0

Published

Express API rate limiting. Uses redis

Downloads

3

Readme

api-rate-limiting

Express middleware that will limit API calls as configured.

Requirements

  • redis should be installed on deployment machine.

How to use it

  • Install it with npm.
  • Pass a configuration object to it and enjoy

An example:

var express = require('express'),
    rateLimiter = require('api-rate-limiting'),
    app = express();
    
app.use('*', rateLimiter(configuration));

How does it work

This middleware will try to distribute API calls per user to be as uniform as possible. In example if we allow 10 calls per 100 seconds middleware will allow 1 call per 10 seconds. Since this rule is too strict we introduce a burst variable that will describe how many API calls should the middleware allow even in cases that would violate uniform distribution of API calls.

So if we configure the middleware to allow 10 calls per 100 seconds with burst of 10 that means that all 10 calls can be served as soon as possible, but the remaining time of 100 seconds the user will get a 429 too many requests error.

Configuration

Configuration object should have following attributes:

calls::Integer
time::Integer
unit::String (seconds|minutes|hours|days)
burst::Integer - optional
getUserLimitations::Function - optional
redisCreateArguments::Array - optional
uniqueField::Object - optional
cookieParser::Object - optional

Object uniqueField has following attributes

section::String (header|cookie|ip)
name::String

This object will tell the middleware where to search for the field that will identify the user. In example if we provide:

"uniqueField" : {
     "section" : 'header',
     "name" : 'token'
 }

The middleware will check for token field in headers. That basically mean that any user identified by token field in header will be treated as above configured.

If unique field's section is set to be cookie then the middleware will try to parse cookies in exactly the same way as cookie-parser library.

Object cookieParser has 'section' and 'secret' fields that are used as described here

redisCreateAguments is an array of arguments that will be passed to createClient() function. Arguments will be passed as described here

Different rates per user

If different consumers of API have different rates user can pass a function to middleware that will be called when necessary. The function should asynchronously pass rates of an user back to middleware.

Arguments passed to getUserLimitations function:

  • token - user's unique identifier
  • callback - function that will be called to pass back user-specific configuration to middleware

Callback function should be called with 2 arguments:

  • error
  • configuration object

Example object

calls::Integer
time::Integer
unit::String (seconds|minutes|hours|days)
burst::Integer - optional

Configuration example

This is an example of a valid configuration object.

{
    "calls" : 10,
    "time" : 100, 
    "unit" : 'seconds', 
    "burst": 10,
    "uniqueField" : { 
        "section" : 'header',
        "name" : 'token'
    }
}

This configuration will mean that our API should handle 10 calls per 100 seconds. User will be identified by a token attribute in header section.

Logging

The middleware will inform the rest of application about important things that happen using event

rate-limiting-info

The event will bring two arguments

  • severity
  • message object

In that way user can log any events with proper importance

var events = require('events'),
    eventEmitter = new events.EventEmitter();
    
    eventEmitter.on('rate-limiting-info', function(severity, message) {
        //log your stuff
    });