Registry / storage / sliding-window-rate-limiter

sliding-window-rate-limiter

JSON →
library6.0.1jsnpmunverified

A sliding window rate limiter supporting Redis >= 3.2 backend or in-memory backend. Current stable version 6.0.1 requires Node >= 16 and ES6. Provides check, reserve, and cancel operations with automatic cleanup. Key differentiators: supports both Redis and in-memory backends, includes safe mode with automatic reconnection, and uses a sliding window algorithm rather than fixed window. Actively maintained with frequent releases.

npm install sliding-window-rate-limiter
INSTALL
IMPORT
SIG · SLIDING-WINDOW-RAT
S
sliding-window-rate-limiter
storagejavascriptv6.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SlidingWindowRateLimiter
import { SlidingWindowRateLimiter } from 'sliding-window-rate-limiter'
const SlidingWindowRateLimiter = require('sliding-window-rate-limiter').default;
CommonJS require returns the named export directly, not via .default. Named import works for both ESM and TypeScript.
SlidingWindowRateLimiter (default export)
import SlidingWindowRateLimiter from 'sliding-window-rate-limiter'
import SlidingWindowRateLimiter from 'sliding-window-rate-limiter/dist/index.js';
Default export is available only in ESM/TypeScript. In CommonJS you must use named import { SlidingWindowRateLimiter }.
types
import type { SlidingWindowRateLimiter } from 'sliding-window-rate-limiter'
import { SlidingWindowRateLimiterType } from 'sliding-window-rate-limiter'
Use the same symbol name with 'import type' for type-only imports. No separate type export.

Shows in-memory and Redis-backed rate limiter initialization, check, reserve, and cancel operations.

import { SlidingWindowRateLimiter } from 'sliding-window-rate-limiter'; // In-memory limiter const limiter = SlidingWindowRateLimiter.createLimiter({ interval: 60000 }); async function limitCheck(key) { const result = await limiter.check(key, 10); console.log(`Usage: ${result.usage}, Reset: ${result.reset}`); return result.usage < 10; } // Redis limiter (with proper error handling) import Redis from 'ioredis'; const redis = new Redis({ host: 'redis-server', retryStrategy: t => 1000, maxRetriesPerRequest: 1 }); const redisLimiter = SlidingWindowRateLimiter.createLimiter({ interval: 60000, redis, safe: true }); async function redisLimitCheck(key) { const { token, usage, reset } = await redisLimiter.reserve(key, 10); if (usage >= 10) { console.log(`Rate limited, retry after ${reset}ms`); return false; } // ... do work ... await redisLimiter.cancel(key, token); return true; }
Debug
Known issues
breakingVersion 6.0.0+ requires Node >= 16.0.0 (dropped support for Node 14 and older).
fix
Upgrade Node.js to version 16.0.0 or higher.
affects: >=6.0.0
breakingThe `usage` method was renamed to `check` in version 5.0.0. The old name no longer exists.
fix
Replace `limiter.usage(key, limit)` with `limiter.check(key, limit)`.
affects: >=5.0.0
deprecatedThe `SafeRedis` backend class is deprecated; use `safe: true` option in `createLimiter` instead.
fix
Use `SlidingWindowRateLimiter.createLimiter({ interval, redis, safe: true })` instead of instantiating SafeRedisBackend directly.
affects: >=4.0.0
gotchaWhen using CommonJS (`require`), the module exports `{ SlidingWindowRateLimiter }` — not a default export. Many users mistakenly write `const Limiter = require('sliding-window-rate-limiter').default`.
fix
Use `const { SlidingWindowRateLimiter } = require('sliding-window-rate-limiter')`.
affects: >=6.0.0
gotchaIf using Redis backend with custom retry strategy, note that the library sets default `retryStrategy` to 1 second and `maxRetriesPerRequest` to 1 when passed a URL string. This can cause frequent disconnections under load.
fix
Pass an explicit `redis` instance with your own retry strategy rather than a URL string.
affects: >=1.0.0
deprecatedThe `operationTimeout` option default changed from 0 (no timeout) to 5000ms in version 5.0.0.
fix
Set `operationTimeout: 0` if you need the old behavior of no timeout; otherwise adjust your code for 5s timeout.
affects: >=5.0.0
Errors
Common errors & fixes
ERR Error running script (call to f_...): @user_script:1: attempt to call field 'replicate_commands' (a nil value)
Redis server version is below 3.2.0; the library uses Redis commands that require Redis >= 3.2.
fix
Upgrade Redis server to version 3.2.0 or later.
TypeError: SlidingWindowRateLimiter.createLimiter is not a function
Using incorrect import style: likely using default import in CommonJS, or importing from wrong path.
fix
In CommonJS: const { SlidingWindowRateLimiter } = require('sliding-window-rate-limiter'). In ESM: import { SlidingWindowRateLimiter } from 'sliding-window-rate-limiter'.
Error: Redis connection lost: connection is closed.
Redis connection timed out or server unavailable. With safe mode, it retries but may throw if reuseRedisAfter expires.
fix
Ensure Redis server is running and accessible. Consider setting a longer retryStrategy or increasing operationTimeout.
ReferenceError: exports is not defined in ES module scope
Trying to use require() in an ES module (e.g., package.json type: 'module').
fix
Use import { SlidingWindowRateLimiter } from 'sliding-window-rate-limiter' instead of require().
Upgrade
Version history
6.0.1latest on npm
Audit
Dependencies
ioredisoptionalRequired for Redis backend; optional if using in-memory backend only
Agent activity
28 hits · last 30 days
node
24
Amazon
1
Resources
sliding-window-rate-limiter — npm install sliding-window-rate-limiter · libregistry