-
Notifications
You must be signed in to change notification settings - Fork 0
/
rateLimiter.js
47 lines (40 loc) · 1.43 KB
/
rateLimiter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Description: This file contains the rate limiter middleware for the API.
// parameters: windowMs, maxRequests, allowInfiniteCalls, timeLimit
// windowMs: The time window for which requests are checked.
// maxRequests: The maximum number of requests allowed in the time window.
// allowInfiniteCalls: If true, the rate limiter will allow infinite calls to the API after the time limit has passed.
// timeLimit: The time limit in seconds for which the rate limiter will allow infinite calls to the API.
const rateLimit = require("express-rate-limit");
function createRateLimiter(
windowMs,
maxRequests,
allowInfiniteCalls,
timeLimit
) {
const limiter = rateLimit({
windowMs: windowMs,
max: maxRequests,
message: "Too many requests from this IP, please try again later.",
});
return (req, res, next) => {
if (allowInfiniteCalls) {
const timeLimitMs = timeLimit * 1000; // Convert time limit to milliseconds
const startTime = Date.now() - timeLimitMs;
req.rateLimit = {
...limiter,
windowMs: timeLimitMs,
max: Infinity,
store: new rateLimit.MemoryStore({
windowMs: timeLimitMs,
max: maxRequests,
}),
};
// Clear out previous requests before the time limit
req.rateLimit.store.resetKey(req.ip, startTime);
} else {
req.rateLimit = limiter;
}
return limiter(req, res, next);
};
}
module.exports = createRateLimiter;