Library that provides different algorithms to perform rate limiting
Installations
npm install rate-limiter-algorithms
Developer Guide
Typescript
No
Module System
ESM
Node Version
20.18.1
NPM Version
10.8.2
Score
71.2
Supply Chain
99.3
Quality
83.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
Download Statistics
Total Downloads
1,982
Last Day
3
Last Week
67
Last Month
341
Last Year
1,982
GitHub Statistics
2 Stars
25 Commits
1 Watching
1 Branches
1 Contributors
Package Meta Information
Latest Version
2.1.5
Package Id
rate-limiter-algorithms@2.1.5
Unpacked Size
31.91 kB
Size
5.98 kB
File Count
7
NPM Version
10.8.2
Node Version
20.18.1
Publised On
09 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
1,982
Last day
-76.9%
3
Compared to previous day
Last week
31.4%
67
Compared to previous week
Last month
34.8%
341
Compared to previous month
Last year
0%
1,982
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
Rate Limiter Algorithms
Library that provides different algorithms to perform rate limiting.
Example with Node.js
1import { createServer } from "node:http"; 2import { RateLimiter } from "rate-limiter-algorithms"; 3 4const limiter = new RateLimiter({ 5 algorithm: "token-bucket", 6 limit: 5, 7 windowMs: 5000, 8}); 9 10const server = createServer(async (req, res) => { 11 const ip = req.socket.remoteAddress || "any unique key"; 12 13 try { 14 const { isAllowed, clientData } = await limiter.consume(ip); 15 16 // set rate limiting headers 17 const headers = limiter.getHeaders(clientData); 18 for (const header of headers) { 19 res.setHeader(header[0], header[1]); 20 } 21 22 if (!isAllowed) { 23 res.writeHead(429, "Too many requests"); 24 res.end("Failure"); 25 return; 26 } 27 res.end("Success"); 28 } catch (error) { 29 console.error("Error in rate limiting:", error); 30 } 31}); 32 33server.listen(3000, "127.0.0.1", () => { 34 console.log("Listening on 127.0.0.1:3000"); 35});
Config
Option | Type | Explanation |
---|---|---|
algorithm | string | Values: token-bucket , fixed-window-counter , sliding-window-logs , sliding-window-counter |
windowMs | number | Duration of time in milliseconds when algorithm updates its counter. |
limit | number | Maximum amount of points that client can consume |
store | Store | Store which contains clients data based on chosen algorithm. Defaults to Memory Store |
Date Stores
Memory Store
Default in-memory option. Example:
1import { RateLimiter, MemoryStore } from "rate-limiter-algorithms";
2
3const limiter = new RateLimiter({
4 algorithm: "token-bucket",
5 limit: 5,
6 windowMs: 5000,
7 store: new MemoryStore(),
8});
Redis Store
Uses rawCall function to send raw commands to Redis. Example for node-redis
:
1import { RateLimiter, RedisStore } from "rate-limiter-algorithms";
2import { createClient } from "redis";
3
4const client = createClient();
5client.connect();
6
7const limiter = new RateLimiter({
8 algorithm: "token-bucket",
9 limit: 5,
10 windowMs: 5000,
11 store: new RedisStore({
12 prefix: "rla:", // it's default prefix
13 rawCall: (...args: string[]) => client.sendCommand(args),
14 }),,
15});
Raw command list:
Library | Function |
---|---|
node-redis | (...args: string[]) => client.sendCommand(args) |
ioredis | (command: string, ...args: string[]) => client.call(command, ...args) |
License
All code and documentation are (c) 2024 Eugene Shumilin and released under the MIT License
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No security vulnerabilities found.
Gathering detailed insights and metrics for rate-limiter-algorithms