Gathering detailed insights and metrics for rolling-rate-limiter-forked
Gathering detailed insights and metrics for rolling-rate-limiter-forked
npm install rolling-rate-limiter-forked
Typescript
Module System
NPM Version
TypeScript (98.86%)
JavaScript (1.14%)
Total Downloads
1,926
Last Day
1
Last Week
2
Last Month
15
Last Year
70
348 Stars
107 Commits
53 Forks
29 Watching
2 Branches
16 Contributors
Minified
Minified + Gzipped
Latest Version
0.2.0
Package Id
rolling-rate-limiter-forked@0.2.0
Size
5.18 kB
NPM Version
1.4.18
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-60%
2
Compared to previous week
Last month
400%
15
Compared to previous month
Last year
-27.1%
70
Compared to previous year
This is an implementation of a rate limiter in node.js that allows for rate limiting with a rolling window.
This means that if a user is allowed 5 actions per 60 seconds, any action will be blocked if 5 actions have already occured in the preceeding 60 seconds, without any set points at which this interval resets. This contrasts with many existing implementations, in which a user could make 5 requests at 0:59 and another 5 requests at 1:01.
It can use either in-memory storage or Redis as a backend. If Redis is used, multiple rate limiters can share one instance with different namespaces, and multiple processes can share rate limiter state safely without race conditions. The implementation uses what I believe to be a novel algorithm, with sorted sets.
1 2 /* 3 Setup: 4 */ 5 6 var RateLimiter = require("rolling-rate-limiter"); 7 8 var limiter = RateLimiter({ 9 interval: 1000 // in miliseconds 10 maxInInterval: 10 11 minDifference: 100 // optional: the minimum time (in miliseconds) between any two actions 12 }); 13 14 /* 15 Action: 16 */ 17 18 function attemptAction(userId) { 19 20 // Argument should be a unique identifier for a user if one exists. 21 // If none is provided, the limiter will not differentiate between users. 22 var timeLeft = limiter(userId) 23 24 if (timeLeft > 0) { 25 26 // limit was exceeded, action should not be allowed 27 // timeLeft is the number of ms until the next action will be allowed 28 // note that this can be treated as a boolean, since 0 is falsy 29 30 } else { 31 32 // limit was not exceeded, action should be allowed 33 34 } 35 36 } 37 38 /* 39 Note that the in-memory version can also operate asynchronously. 40 The syntax is identical to the redis implementation below. 41 */
This allows multiple processes (e.g. multiple instances of a server application) to use a single redis to share rate limiter state. Make sure that the limiters have identical configurations in each instance.
1 2 /* 3 Setup: 4 */ 5 6 var RateLimiter = require("rolling-rate-limiter"); 7 var Redis = require("redis"); 8 var client = Redis.createClient(config); 9 10 var limiter = RateLimiter({ 11 redis: client, 12 namespace: "UserLoginLimiter" // optional: allows one redis instance to handle multiple types of rate limiters. defaults to "rate-limiter-{string of 8 random characters}" 13 interval: 1000 14 maxInInterval: 10 15 minDifference: 100 16 }); 17 18 /* 19 Action: 20 */ 21 22 function attemptAction(userId, cb) { 23 limiter(userId, function(err, timeLeft) { 24 if (err) { 25 // redis failed or similar. 26 } else if (timeLeft) { 27 // limit was exceeded, action should not be allowed 28 } else { 29 // limit was not exceeded, action should be allowed 30 } 31 }); 32 } 33
You can easily use this module to set up a request rate limiter middleware in Express.
1 var limiter = RateLimiter({ 2 redis: redisClient, 3 namespace: "requestRateLimiter", 4 interval: 60000, 5 maxInInterval: 100, 6 minDifference: 100 7 }); 8 9 app.use(function(req, res, next) { 10 11 // "req.ipAddress" could be replaced with any unique user identifier 12 // Note that the limiter returns the number of miliseconds until an action 13 // will be allowed. Since 0 is falsey, this can be treated as a boolean. 14 limiter(req.ipAddress, function(err, timeLeft) { 15 if (err) { 16 return res.status(500).send(); 17 } else if (timeLeft) { 18 return res.status(429).send("You must wait " + timeLeft + " ms before you can make requests."); 19 } else { 20 return next(); 21 } 22 }); 23 24 });
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/20 approved changesets -- score normalized to 1
Reason
9 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More