@glorhythm/rate-limiter
v1.0.0-beta
Published
Glorhythm rate-limiter library
Maintainers
Readme
@glorhythm/rate-limiter
A small, Redis-backed rate-limiting utility for Node.js written in TypeScript.
It uses a fixed-window counter per key and relies on @glorhythm/redis for the Redis connection.
Installation
npm install @glorhythm/rate-limiter @glorhythm/redisFeatures
- Fixed window rate limiting with Redis
- Simple static API
- Supports custom keys
- TypeScript support
Basic Usage
import RateLimiter from "@glorhythm/rate-limiter";
const key = "login:user123";
const attempts = 5;
const windowSeconds = 60;
const allowed = await RateLimiter.isAvailable(key, attempts);
if (!allowed) {
const availableIn = await RateLimiter.availableIn(key);
throw new Error("Try again in " + availableIn + " seconds");
}
const remaining = await RateLimiter.increment(key, attempts, windowSeconds);
console.log("Remaining:", remaining);API
isAvailable(key, attempts)
Returns whether this key still has remaining attempts left.
availableIn(key)
Returns seconds until the current rate-limit window resets.
increment(key, attempts, seconds)
Consumes one attempt and returns remaining attempts.
remaining(key, attempts)
Returns remaining allowed attempts without incrementing.
License
MIT © Glorhythm
