web-gatekeeper-js 1.0.9 → 1.0.11

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-gatekeeper-js",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Redis based rate limiter and throttler using sliding window and token bucket algorithms",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -21,7 +21,7 @@ export const slidingWindowScript = `
21
21
  'currentCount', 1,
22
22
  'previousCount', 0
23
23
  )
24
- redis.call('PEXPIRE', key, math.floor(ttl))
24
+ redis.call('EXPIRE', key, math.floor(ttl))
25
25
  return { 1, 1, limit - 1, resetAfter } -- ← 4 values
26
26
  end
27
27
 
@@ -55,7 +55,7 @@ export const slidingWindowScript = `
55
55
  'currentCount', newCurrentCount,
56
56
  'previousCount', newPreviousCount
57
57
  )
58
- redis.call('PEXPIRE', key, math.floor(ttl))
58
+ redis.call('EXPIRE', key, math.floor(ttl))
59
59
 
60
60
  local remaining = math.floor(limit - effectiveCount - 1)
61
61
  return { 1, newCurrentCount, remaining, resetAfter } -- ← 4 values
@@ -14,21 +14,21 @@ export const throttlerScript = `
14
14
 
15
15
  -- reject if queue too full
16
16
  if waitTime > maxWait then
17
- return { 0, waitTime }
17
+ return { 0, math.floor(waitTime) }
18
18
  end
19
19
 
20
20
  -- calculate new nextAllowedTime
21
21
  local newNextAllowedTime
22
22
  if waitTime <= 0 then
23
- newNextAllowedTime = now + (1000 / refillRate)
23
+ newNextAllowedTime = math.floor(now + (1000 / refillRate))
24
24
  else
25
- newNextAllowedTime = nextAllowedTime + (1000 / refillRate)
25
+ newNextAllowedTime = math.floor(nextAllowedTime + (1000 / refillRate))
26
26
  end
27
27
 
28
28
  -- save and set TTL
29
29
  redis.call('HSET', key, 'nextAllowedTime', newNextAllowedTime)
30
- redis.call('PEXPIRE', key, math.floor(ttl))
30
+ redis.call('EXPIRE', key, math.floor(ttl))
31
31
 
32
32
  -- return allowed + waitTime so Node.js knows how long to delay
33
- return { 1, math.max(waitTime, 0) }
33
+ return { 1, math.floor(math.max(waitTime, 0)) }
34
34
  `
@@ -15,8 +15,8 @@ export const tokenBucketScript = `
15
15
  'time', now,
16
16
  'tokenLeft', maxToken - 1
17
17
  )
18
- redis.call('PEXPIRE', key, math.floor(ttl))
19
- return { 1, maxToken - 1 }
18
+ redis.call('EXPIRE', key, math.floor(ttl))
19
+ return { 1, math.floor(maxToken - 1) }
20
20
  end
21
21
 
22
22
  -- calculate refill (same as your timeElapsed and updatedToken)
@@ -29,10 +29,10 @@ export const tokenBucketScript = `
29
29
  'time', now,
30
30
  'tokenLeft', updatedToken
31
31
  )
32
- redis.call('PEXPIRE', key, math.floor(ttl))
32
+ redis.call('EXPIRE', key, math.floor(ttl))
33
33
 
34
34
  local retryAfter = math.ceil((1 - updatedToken) / refillRate)
35
- return { 0, 0, retryAfter }
35
+ return { 0, 0, math.floor(retryAfter) }
36
36
  end
37
37
 
38
38
  -- allow — consume 1 token and save (same as your hSet)
@@ -40,7 +40,7 @@ export const tokenBucketScript = `
40
40
  'time', now,
41
41
  'tokenLeft', updatedToken - 1
42
42
  )
43
- redis.call('PEXPIRE', key, math.floor(ttl))
43
+ redis.call('EXPIRE', key, math.floor(ttl))
44
44
 
45
- return { 1, updatedToken - 1, 0 }
45
+ return { 1, math.floor(updatedToken - 1), 0 }
46
46
  `