Gathering detailed insights and metrics for advance-rate-limiter
Gathering detailed insights and metrics for advance-rate-limiter
Gathering detailed insights and metrics for advance-rate-limiter
Gathering detailed insights and metrics for advance-rate-limiter
this repository contains all the code for NPM package of ARL(advance-rate-limiter)
npm install advance-rate-limiter
Typescript
Module System
Node Version
NPM Version
71.2
Supply Chain
99.4
Quality
79.9
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
789
Last Day
2
Last Week
2
Last Month
13
Last Year
789
MIT License
1 Stars
98 Commits
1 Watchers
9 Branches
1 Contributors
Updated on Oct 10, 2024
Minified
Minified + Gzipped
Latest Version
1.0.5
Package Id
advance-rate-limiter@1.0.5
Unpacked Size
33.18 kB
Size
8.85 kB
File Count
55
NPM Version
10.7.0
Node Version
20.15.1
Published on
Jul 28, 2024
Cumulative downloads
Total Downloads
Last Day
100%
2
Compared to previous day
Last Week
100%
2
Compared to previous week
Last Month
-35%
13
Compared to previous month
Last Year
0%
789
Compared to previous year
4
advance-rate-limiter
is a sophisticated middleware for Express.js applications that provides rate limiting using Redis and a sliding window counter algorithm. Configure rate limits dynamically through a JSON file and integrate seamlessly into your Express application.
Install advance-rate-limiter via npm:
1npm install advance-rate-limiter
To set up the Redis client with the advance-rate-limiter
package, use the following code:
1import { setupRedisClient } from 'advance-rate-limiter'; 2 3// Configuration object for Redis 4const redisConfig = { 5 host: 'your-redis-host', 6 port: 'your-redis-port', 7 password: 'your-redis-password', // Optional: Include if your Redis instance requires authentication 8}; 9 10// Initialize Redis client with the configuration 11setupRedisClient(redisConfig);
rateLimiter
method1{ 2 "/api/endpoint1": { "limit": 2, "windowTime": 60 }, // 2 requests per minute 3 "/api/endpoint2": { "limit": 5, "windowTime": 120 } // 5 requests per 2 minutes 4}
for globalRateLimiter
method
1{ 2 "limit": 10, // allow 10 request 3 "windowTime" : 60 // time limit is 1 minutes 4}
1import express from 'express'; 2import rateLimiter, { setupRedisClient } from 'advance-rate-limiter'; 3import * as fs from 'fs'; 4import * as path from 'path'; 5 6const app = express(); 7 8// Load rate limit configuration from JSON file 9const rateLimitConfigPath = path.join(__dirname, 'rateLimitConfig.json'); 10const rateLimitConfig = JSON.parse(fs.readFileSync(rateLimitConfigPath, 'utf8')); 11 12// Initialize Redis client 13const redisConfig = { 14 host: 'your-redis-host', 15 port: 'your-redis-port', 16 password: 'your-redis-password', 17}; 18 19setupRedisClient(redisConfig); 20 21// Apply rate limiter middleware 22app.use(rateLimiter(rateLimitConfig)); 23 24app.get('/api/endpoint1', (req, res) => { 25 res.send('API endpoint 1 is working'); 26}); 27 28app.get('/api/endpoint2', (req, res) => { 29 res.send('API endpoint 2 is working'); 30}); 31 32app.listen(3000, () => { 33 console.log('Server is running on port 3000'); 34});
1import express from 'express'; 2import { setupRedisClient, globalRateLimiter } from 'advance-rate-limiter'; 3import globalRateLimitConfig from './globalRateLimitConfig.json' assert {type : 'json'}; 4 5 6const app = express(); 7 8 9console.log("this is rate-limit- config", globalRateLimitConfig); 10 11// Initialize Redis client 12const redisConfig = { 13 host: 'your-redis-host', 14 port: 'your-redis-port', 15 password: 'your-redis-password', 16}; 17 18setupRedisClient(redisConfig); 19 20// Apply rate limiter middleware 21app.use(globalRateLimiter(globalRateLimitConfig)); 22 23app.get('/api/endpoint1', (req, res) => { 24 res.send('API endpoint 1 is working'); 25}); 26 27app.get('/api/endpoint2', (req, res) => { 28 res.send('API endpoint 2 is working'); 29}); 30 31app.listen(3000, () => { 32 console.log('Server is running on port 3000'); 33});
rateLimits: An object or JSON file defining rate limits for different routes.
redisConfig: Configuration object for Redis.
To run the tests for this package, ensure you have jest
installed and run:
npm test
This project is licensed under the MIT License.
Function | Configuration | Remark |
---|---|---|
setupRedisClient | { host: 'your-redis-host', port: 'your-redis-port', password: 'your-redis-password' } | Sets up the Redis connection. Include the password field if your Redis instance requires authentication. |
rateLimiter | { "/api/endpoint1": { "limit": 2, "windowTime": 60 }, "/api/endpoint2": { "limit": 5, "windowTime": 120 } } | 1. /api/endpoint1: Allows a maximum of 2 requests per minute (60 seconds). 2. /api/endpoint2: Allows a maximum of 5 requests per 2 minutes (120 seconds). These rate limits help prevent abuse by limiting the number of requests that can be made to each endpoint within a specified time window. |
globalRateLimiter | { "limit": 10, "windowTime" : 60 } | 1. limit: The total number of requests allowed. 2. windowTime: The time frame (in seconds) within which the specified number of requests is allowed. This global rate limit applies across your entire project if implemented at the entry point of your application (e.g., in the index.js file). |
No vulnerabilities found.
No security vulnerabilities found.
rate-limiter-advance
A flexible, lightweight rate limiting utility designed to control the number of times a function can be executed within a specific time window. This is especially useful for API calls or any operation that requires throttling to prevent overuse.
rate-limiter-advanced
A rate limiter middleware for Node.js using Redis