web-gatekeeper-js 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/RateLimiter.js
CHANGED
|
@@ -11,18 +11,23 @@ export class RateLimiter {
|
|
|
11
11
|
|
|
12
12
|
constructor({ redisClient, windowSize, limit, maxToken, refillRate }) {
|
|
13
13
|
if (!redisClient) throw new Error("redisClient is required");
|
|
14
|
-
if (
|
|
15
|
-
if (
|
|
14
|
+
if (windowSize == null) throw new Error("windowSize is required");
|
|
15
|
+
if (limit == null) throw new Error("limit is required");
|
|
16
16
|
if (maxToken == null) throw new Error("maxToken is required");
|
|
17
17
|
if (refillRate == null) throw new Error("refillRate is required");
|
|
18
18
|
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
if (!Number.isFinite(windowSize) || windowSize <= 0) {
|
|
20
|
+
throw new Error("windowSize must be a positive number (ms)");
|
|
21
|
+
}
|
|
22
|
+
if (!Number.isFinite(limit) || limit <= 0) {
|
|
23
|
+
throw new Error("limit must be a positive number");
|
|
24
|
+
}
|
|
25
|
+
if (!Number.isFinite(maxToken) || maxToken <= 0) {
|
|
26
|
+
throw new Error("maxToken must be a positive number");
|
|
27
|
+
}
|
|
28
|
+
if (!Number.isFinite(refillRate) || refillRate <= 0) {
|
|
29
|
+
throw new Error("refillRate must be a positive number");
|
|
30
|
+
}
|
|
26
31
|
|
|
27
32
|
this.#store = new RedisStore(redisClient);
|
|
28
33
|
this.#windowSize = windowSize;
|
package/src/Throttler.js
CHANGED
|
@@ -10,8 +10,15 @@ export class Throttler {
|
|
|
10
10
|
|
|
11
11
|
constructor({ redisClient, refillRate, maxWait }) {
|
|
12
12
|
if (!redisClient) throw new Error('redisClient is required')
|
|
13
|
-
if (
|
|
14
|
-
if (
|
|
13
|
+
if (refillRate == null) throw new Error('refillRate is required')
|
|
14
|
+
if (maxWait == null) throw new Error('maxWait is required')
|
|
15
|
+
|
|
16
|
+
if (!Number.isFinite(refillRate) || refillRate <= 0) {
|
|
17
|
+
throw new Error('refillRate must be a positive number')
|
|
18
|
+
}
|
|
19
|
+
if (!Number.isFinite(maxWait) || maxWait < 0) {
|
|
20
|
+
throw new Error('maxWait must be a non-negative number')
|
|
21
|
+
}
|
|
15
22
|
|
|
16
23
|
this.#store = new RedisStore(redisClient)
|
|
17
24
|
this.#refillRate = refillRate
|
|
@@ -3,7 +3,9 @@ export const slidingWindowScript = `
|
|
|
3
3
|
local windowSize = math.floor(tonumber(ARGV[1]))
|
|
4
4
|
local limit = math.floor(tonumber(ARGV[2]))
|
|
5
5
|
local now = math.floor(tonumber(ARGV[3]))
|
|
6
|
-
local
|
|
6
|
+
local ttlRaw = tonumber(ARGV[4])
|
|
7
|
+
local ttl = math.floor(ttlRaw or 0)
|
|
8
|
+
if ttl < 1 then ttl = 1 end
|
|
7
9
|
|
|
8
10
|
local windowStart = math.floor(now / windowSize) * windowSize
|
|
9
11
|
local resetAfter = math.ceil((windowSize - (now - windowStart)) / 1000)
|
|
@@ -3,7 +3,9 @@ export const throttlerScript = `
|
|
|
3
3
|
local now = math.floor(tonumber(ARGV[1]))
|
|
4
4
|
local refillRate = tonumber(ARGV[2])
|
|
5
5
|
local maxWait = math.floor(tonumber(ARGV[3]))
|
|
6
|
-
local
|
|
6
|
+
local ttlRaw = tonumber(ARGV[4])
|
|
7
|
+
local ttl = math.floor(ttlRaw or 0)
|
|
8
|
+
if ttl < 1 then ttl = 1 end
|
|
7
9
|
|
|
8
10
|
-- safely read nextAllowedTime
|
|
9
11
|
local raw = redis.call('HGET', key, 'nextAllowedTime')
|
|
@@ -3,7 +3,9 @@ export const tokenBucketScript = `
|
|
|
3
3
|
local now = math.floor(tonumber(ARGV[1]))
|
|
4
4
|
local maxToken = tonumber(ARGV[2])
|
|
5
5
|
local refillRate = tonumber(ARGV[3])
|
|
6
|
-
local
|
|
6
|
+
local ttlRaw = tonumber(ARGV[4])
|
|
7
|
+
local ttl = math.floor(ttlRaw or 0)
|
|
8
|
+
if ttl < 1 then ttl = 1 end
|
|
7
9
|
|
|
8
10
|
local rawTime = redis.call('HGET', key, 'time')
|
|
9
11
|
local rawTokens = redis.call('HGET', key, 'tokenLeft')
|