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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sid3945/rate-limiter

v1.0.4

Published

Flexible rate limiting middleware with Redis support

Readme

Node.js Rate Limiter

A flexible and robust rate limiting middleware for Node.js applications with support for both in-memory and Redis-based storage. This rate limiter can be used in both single-server and distributed environments.

Features

  • 🚀 Simple and easy to use
  • 💾 Multiple storage options (in-memory and Redis)
  • 🔄 Configurable time windows and request limits
  • 🎯 Multiple identifier strategies (IP, auth token, cookie)
  • 🔌 Pluggable custom identifier extraction
  • 🎨 Clean middleware interface
  • 🔒 Thread-safe and distributed-safe (when using Redis)
  • ⚡ Lightweight with minimal dependencies

Installation

npm install @sid3945/rate-limiter

Quick Start

import express from 'express';
import RateLimiter from '@yourusername/rate-limiter';

const app = express();

const rateLimiter = new RateLimiter({
  window: 3000,  // 3 seconds from the first request from this source
  maxHits: 3     // 3 requests per window 
});

app.use(rateLimiter.guard());

app.get('/_healthz', (req, res) => {
  res.json({ message: 'Status Ok!' });
});

app.listen(3000);

Configuration Options

Basic Configuration

const rateLimiter = new RateLimiter({
  window: 3000,       // Time window in milliseconds
  maxHits: 3,         // Maximum number of requests per window
  identifier: 'ip',   // Identifier strategy ('ip', 'authToken', 'cookie')
  storage: 'memory'   // Storage strategy ('in-memory' or 'redis')
});

Redis Configuration

const rateLimiter = new RateLimiter({
  window: 3000,
  maxHits: 3,
  storage: 'redis',
  redisConfig: {
    host: 'localhost',
    port: 6379,
    password: 'your-password', db: 0 // Redis database number
  }
});

Custom Identifier

const rateLimiter = new RateLimiter({
  window: 3000,
  maxHits: 3,
  customIdentifierExtractor: (req) => {
    return req.headers['x-custom-id'] || req.ip;
  }
});

Storage Strategies

In-Memory Storage

  • Best for single-server deployments
  • No additional dependencies
  • Fast performance
  • Data is lost on server restart

Redis Storage

  • Best for distributed environments
  • Requires Redis server
  • Consistent rate limiting across multiple servers
  • Persists across server restarts
  • Automatic cleanup of expired records

Response Format

When rate limit is exceeded, the middleware returns:

{
  error: "Too many requests",
  retryAfter: 3
}

With HTTP status code 429 (Too Many Requests).

Example Use Cases

Basic API Rate Limiting

const apiLimiter = new RateLimiter({
  window: 60000,    // 1 minute
  maxHits: 60       // 60 requests per minute
});

app.use('/api/', apiLimiter.guard());

Different Limits for Authentication

const publicLimiter = new RateLimiter({
  window: 60000,
  maxHits: 30
});

const authenticatedLimiter = new RateLimiter({
  window: 60000,
  maxHits: 100,
  identifier: 'authToken'
});

app.use('/api/public', publicLimiter.guard());

app.use('/api/private', authenticatedLimiter.guard());

Performance Considerations

  • In-memory storage has minimal impact on request latency
  • Redis storage adds network latency but enables distributed rate limiting
  • Redis keys automatically expire after the window period
  • Careful consideration needed for high-traffic applications

Error Handling

The middleware includes built-in error handling:

  • Redis connection failures fallback to allowing requests
  • Invalid configurations throw errors during initialization
  • All errors are logged for debugging

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Author

Siddharth Upadhyay

Changelog

1.0.0

  • Initial release
  • Support for in-memory and Redis storage
  • Basic rate limiting functionality

1.0.2

  • Updated documentation (and reduced GEN AI footprint 😁)

Todo

  • [ ] Add support for sliding windows
  • [ ] Add support for multiple concurrent windows
  • [ ] Add support for rate limit headers
  • [ ] Add support for custom response formats