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

ratesentry

v1.0.7

Published

Distributed Rate Limiter Middleware for Express

Readme

RateSentry 🛡️

A simple, fast, and production-ready Express middleware for Distributed Rate Limiting using Redis.


🏗️ Architecture

graph TD
    ClientApp[Client Express App] -->|Import Middleware| LimiterMW(rateLimiter Middleware)
    LimiterMW -->|Execute Atomically| Redis[(Redis Counter Store)]

🧠 Core Algorithms

The Sliding Window Log Explained Simply

Standard rate limiting algorithms (like Fixed Window) have a flaw: if your limit is 100 requests per minute, a user can spam 100 requests at 12:00:59, and another 100 at 12:01:00. This results in 200 requests within just 2 seconds, despite the "100/min" rule!

The Sliding Window Log solves this by tracking the exact timestamp of every request.

  1. When a request comes in, we log its timestamp in a Redis Sorted Set.
  2. We then look exactly 60 seconds back in time from right now.
  3. We delete any timestamps older than that line.
  4. If the number of timestamps remaining is less than the limit, the request is allowed.

This creates a perfectly smooth, sliding window that cannot be "tricked" at the edge of arbitrary minute barriers.


🚀 Installation

npm install ratesentry

Note: You will need a running Redis server.


🔌 Usage

This package exports an Express middleware that you can plug into your application to easily implement rate limiting.

Basic Setup

You can directly provide your Redis connection string to configure the limiter:

const express = require('express');
const { rateLimiter } = require('ratesentry');

const app = express();

app.use('/api', rateLimiter({ 
  redisUrl: 'redis://localhost:6379',
  algorithm: 'sliding-window', // 'sliding-window', 'fixed-window', or 'token-bucket'
  standalone: true,  
  limit: 100,        // Allow 100 requests
  windowMs: 60000    // Per 60,000 milliseconds (1 minute)
}));

app.get('/api/data', (req, res) => res.send('Protected Data'));

app.listen(3000, () => console.log('Server running on port 3000'));

📖 API Documentation

Configuration Options

  • redisUrl: Your Redis connection string (e.g., redis://localhost:6379).
  • algorithm: The rate limiting algorithm to use ('sliding-window', 'fixed-window', or 'token-bucket').
  • standalone: Must be true for simple Redis usage (disables external DB checks).
  • limit: Maximum number of requests permitted within the window.
  • windowMs: The timeframe in milliseconds.

Middleware Responses

When a client goes over the limit, the middleware automatically rejects the request with HTTP status 429 Too Many Requests.

It also appends the following headers to help clients manage their usage:

  • X-RateLimit-Limit: Maximum requests permitted.
  • X-RateLimit-Remaining: Remaining requests for the window.
  • X-RateLimit-Reset: Timestamp of when the limit resets.
  • Retry-After: Seconds to wait before retrying (Required by HTTP spec).