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

sliding-rate-limiter

v1.0.0

Published

A TypeScript sliding window rate limiter with Memory and Redis support

Readme

Sliding Rate Limiter

A production-ready TypeScript sliding window rate limiter for Node.js applications.

Supports:

  • ✅ Sliding Window Algorithm
  • ✅ Memory Store
  • ✅ Redis Store
  • ✅ Express Middleware
  • ✅ TypeScript Support
  • ✅ Custom Storage Adapters
  • ✅ Rate Limit Headers
  • ✅ ESM + CommonJS builds
  • ✅ Fully Tested

Installation

Install using npm:

npm install sliding-rate-limiter

or using yarn:

yarn add sliding-rate-limiter

or pnpm:

pnpm add sliding-rate-limiter

Basic Usage

SlidingWindow

The core limiter can be used without Express.

import { SlidingWindow } from "sliding-rate-limiter";


const limiter = new SlidingWindow({

  max: 5,

  windowMs: 60000

});


async function test(){

  const result = await limiter.consume(
    "user-123"
  );


  console.log(result);

}


test();

Output:

{
  "allowed": true,
  "remaining": 4,
  "limit": 5,
  "retryAfter": 0,
  "resetAt": 1785257675953
}

Configuration

new SlidingWindow({

  max: 100,

  windowMs: 60000

});

Options

| Option | Type | Description | | -------- | ------ | --------------------------- | | max | number | Maximum requests allowed | | windowMs | number | Time window in milliseconds | | store | Store | Custom storage adapter |

Example:

{
  max: 10,
  windowMs: 10000
}

Allows:

10 requests every 10 seconds

Express Middleware

Protect your Express routes.

Install Express:

npm install express

Example:

import express from "express";

import {
  rateLimiter
} from "sliding-rate-limiter";


const app = express();


const limiter = rateLimiter({

  max: 5,

  windowMs: 60000

});


app.use(limiter);


app.get("/", (req,res)=>{

  res.json({

    message:"Request allowed"

  });

});


app.listen(3000);

Response Headers

Every request receives rate limit headers:

RateLimit-Limit
RateLimit-Remaining
RateLimit-Reset

Example:

RateLimit-Limit: 5

RateLimit-Remaining: 2

RateLimit-Reset: 1785260130942

When limit is exceeded:

HTTP 429 Too Many Requests

Response:

{
  "success": false,
  "message": "Too many requests. Please try again later.",
  "retryAfter": 60
}

Header:

Retry-After: 60

Multiple Users

Each key has its own limit.

Example:

await limiter.consume("user-1");

await limiter.consume("user-2");

Output:

user-1
Request 1 allowed
Request 2 allowed
Request 3 blocked


user-2
Request 1 allowed

Reset Limits

You can manually reset a user's rate limit.

await limiter.reset(
  "user-123"
);

After reset:

{
 allowed:true
}

Memory Store

Default storage.

No setup required.

Example:

import {
 SlidingWindow
} from "sliding-rate-limiter";


const limiter =
new SlidingWindow({

 max:10,

 windowMs:60000

});

Data is stored in application memory.

Useful for:

  • Development
  • Small applications
  • Single server deployments

Redis Store

Redis support is available for distributed applications.

Install Redis client:

npm install ioredis

Example:

import Redis from "ioredis";

import {
 SlidingWindow,
 RedisStore
} from "sliding-rate-limiter";


const redis =
new Redis();


const limiter =
new SlidingWindow({

 max:100,

 windowMs:60000,


 store:
 new RedisStore(redis)

});

Redis allows:

  • Multiple servers
  • Horizontal scaling
  • Shared rate limits

Custom Storage

You can create your own storage adapter.

Extend:

import {
 Store
} from "sliding-rate-limiter";


class CustomStore extends Store {


 async get(key:string){

 }


 async add(
  key:string,
  timestamp:number
 ){

 }


 async removeExpired(
  key:string,
  before:number
 ){

 }


 async count(
  key:string
 ){

 }


 async delete(
  key:string
 ){

 }


}

Then:

const limiter =
new SlidingWindow({

 max:10,

 windowMs:60000,

 store:new CustomStore()

});

Algorithm

This library uses the Sliding Window algorithm.

Flow:

Request
   |
   v
Remove expired timestamps
   |
   v
Count active requests
   |
   v
Check limit
   |
   +---- Allowed
   |
   +---- Blocked (429)

Example:

Window: 60 seconds

Limit: 5 requests


00s  Request
10s  Request
20s  Request
30s  Request
40s  Request


45s  Request blocked


70s
First request expires


New request allowed

Testing

Clone repository:

git clone <repository-url>

Install:

npm install

Run tests:

npm test

Example output:

✓ SlidingWindow.test.ts
✓ MemoryStore.test.ts
✓ RedisStore.test.ts
✓ Express.test.ts

10 tests passed

Building From Source

Install dependencies:

npm install

Build:

npm run build

Creates:

dist/

├── index.js
├── index.cjs
├── index.d.ts
└── source maps

Requirements

  • Node.js >= 18
  • TypeScript >= 5

Use Cases

Perfect for:

  • API protection
  • Authentication endpoints
  • Login throttling
  • Public APIs
  • SaaS applications
  • Microservices
  • Distributed systems

Open Source

License

MIT License


Author

Hamza Ashfaq