node-rate-limiting
v1.0.6
Published
A lightweight, dependency-free rate limiter for Node.js using a sliding window
Maintainers
Readme
Node Rate Limiting
A lightweight, dependency-free rate limiter for Node.js using a sliding window algorithm. Supports both CommonJS and ES Modules.
Installation
npm install node-rate-limitingUsage
CommonJS
const RateLimiter = require("node-rate-limiting");
// Create a rate limiter: max 5 actions per 60 seconds
const limiter = new RateLimiter({ limit: 5, windowMs: 60 * 1000 });
console.log(limiter.check("user1")); // true
console.log(limiter.check("user1")); // true
// ... after 5 calls
console.log(limiter.check("user1")); // false
limiter.reset("user1");
console.log(limiter.check("user1")); // trueES Modules
import RateLimiter from "node-rate-limiting";
// Create a rate limiter: max 5 actions per 60 seconds
const limiter = new RateLimiter({ limit: 5, windowMs: 60 * 1000 });
console.log(limiter.check("user1")); // true
console.log(limiter.check("user1")); // true
// ... after 5 calls
console.log(limiter.check("user1")); // false
limiter.reset("user1");
console.log(limiter.check("user1")); // trueAPI
new RateLimiter({ limit, windowMs })
- limit: Maximum number of actions allowed in the time window.
- windowMs: Time window in milliseconds.
limiter.check(identifier)
- identifier: A string to identify the entity (e.g., user ID, IP address).
- Returns
trueif the action is allowed,falseif the limit is exceeded.
limiter.reset(identifier)
- Resets the rate limit for the given identifier.
limiter.resetAll()
- Resets all rate limits.
License
MIT
