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

cruise-controller

v1.1.0

Published

An Express.js middleware for handling rate limiting with multiple strategies and flexible configurations.

Downloads

1

Readme

Cruise-Controller - An Express Middleware for Rate Limiting

Introduction

Cruise-controller is a flexible rate limiting middleware for Express. It allows you to limit requests based on custom identifiers such as IP addresses or authenticated users. It also supports throttling by performing exponential backoff and can use either in-memory or Redis storage. It also allows for whitelisting and blacklisting of identifiers, customizable error responses, and has features for robust handling of store failures.

Installation

First, install the package using npm:

npm install cruise-controller

Then, require it in your project:

const RateLimiter = require('cruise-controller');

Usage

Instantiate the rate limiter with desired options:

const rateLimiter = new RateLimiter({
  max: 100, // max requests
  windowMs: 15 * 60 * 1000, // window in milliseconds
  getKey: (req) => req.ip, // function to identify the source of a request
  store: new RedisStore(), // specify the store
  whitelist: ['127.0.0.1'], // array of whitelisted identifiers
  blacklist: [], // array of blacklisted identifiers
  onExceeded: (req, res) => res.sendStatus(429), // function to execute when rate limit is exceeded
}); 

Use the rate limiter in your Express app:

app.use(rateLimiter.middleware());

Options

The rate limiter takes the following options:

  • max: The maximum number of allowed requests in the specified window. Defaults to 5.
  • windowMs: The window of time in which requests are considered for rate limiting, in milliseconds. Defaults to 1 minute.
  • getKey: A function that takes a request and returns an identifier. Defaults to the IP address from the request.
  • store: The store to use for storing rate limiting data. Defaults to an in-memory store. Can be a MemoryStore or RedisStore instance.
  • whitelist: An array of identifiers to always allow. Defaults to an empty array.
  • blacklist: An array of identifiers to always block. Defaults to an empty array.
  • onExceeded: A function that is called when the rate limit is exceeded. It receives the request and response objects and must send a response. Defaults to a function that sends a 429 status.

Exponential Backoff

The rate limiting mechanism incorporates an Exponential Backoff feature, designed to gracefully handle rate-limit exceeded responses. When a client surpasses its allowed request limit, instead of receiving an immediate error response, this feature introduces a time delay for subsequent requests from that client. The delay duration starts conservatively and gradually increases with each exceeded request, allowing the client to recover and reduce the request rate. This ensures a smoother and more user-friendly experience, preventing rapid successive requests and minimizing service disruptions due to rate limiting.

  • enableExponentialBackoff: Option to enable the feature. Defaults to false.
  • baseDelayMs: The base delay time, in milliseconds. Defaults to 1 second.
  • maxDelayMs: The maximum limit to delay a request, in milliseconds. Defaults to 1 minute;
  • delayMultiplier: The multiplier to exponentially increase the current delay for each client. Defaults to 2.

Custom Stores

You can implement your own custom store. Refer to redisStore.js and memoryStore.js for more info.

Contributing

Please feel free to open an issue or pull request if you would like to contribute to this project.