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

@nabham/limiter

v1.0.4

Published

Express rate limiter based on request

Downloads

1

Readme

limiter

An express rate limiting library. Limits requests based on request object property.

limiter supports in-memory, mongo and redis store with atomicity among multi node processes.

Installing

npm install @nabham/limiter

Usage

var express = require('express');
var app = express();

var expressLimiter = require('@nabham/limiter');
expressLimiter.initialize({ type: 'redis' });

var bucket = expressLimiter.bucket({ limit: 10, window: 60 });

app.get('/', bucket, (req, res) => {
  res.send();
});

Configuration

limiter support these parameters for initializing configuration.

Redis store

{
  type: 'redis'
  store: {
    host: "127.0.0.1",      // Redis server hostname
    port: 6379,             // Redis server port
    db: 3                   // Redis database number
    atomic: true            // If atomicity needed among multi node process
  }
}

NOTE: Redis store is default store. If store configuration is not passed, it will fallback to default parameters shown above. In case of multi node processes set atomic property in store configuration.

Memory store

{
  type: 'memory'
}

NOTE: Memory store doesn't require any configuration. Memory store does not work well in case of multi worker, as each worker will have its own heap space.

Mongo store

{
  type: 'mongo',
  store: {
    url: 'mongodb://localhost:27017'     // Mongodb connection URI
    collectionName: 'limiter'            // Collection name
    dbName: 'ratelimit'                  // Database name
  }
}

List of all available options under store can be found here. If store configuration is not passed, it will fallback to default parameters shown above.

NOTE: MongoDB store uses TTL index ( it helps in atomicity ) to set expiry which is monitored by TTL monitor which by default runs every 60 seconds. It might affect correctness of limiter for upto 60 seconds, this might create problem when working with smaller windows. Either use redis, memory store for more granularity or decrease mongodb's TTL monitor frequency

bucket creation configuration

| Property | Default | Description | | --- | --- | --- | | id | connection.remoteAddress | Property to be taken from request object. For ex. token, username, connection.remoteAddress ( ip ) . connection.remoteAddress is the path of ip address in express request object. user.id if using passportjs. | window | 60 | Time window in seconds in which rate limit will be applied. | limit | Infinity | Number of requests allowed in window | message | Too Many Requests | Message to be sent to client with 429 error code.

Strategy

limiter works on the principle of sliding window algorithm. window in bucket configuration will start counting requests after first request and will reset once window time is over.

Any suggestion is very much appreciated

License

The MIT License