Skip to content

Commit dffe966

Browse files
committed
additional checks during rate limiter construction
1 parent 225b244 commit dffe966

File tree

5 files changed

+7
-6
lines changed

5 files changed

+7
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"lint:fix": "eslint --fix src test @types",
1212
"prettier": "prettier --write .",
1313
"prepare": "husky install",
14-
"build": "tsc"
14+
"build": "tsc",
15+
"build:fix": "node node_modules/.bin/yab dist"
1516
},
1617
"repository": {
1718
"type": "git",

src/rateLimiters/fixedWindow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class FixedWindow implements RateLimiter {
3131
this.windowSize = windowSize;
3232
this.client = client;
3333
this.keyExpiry = expiry;
34-
if (windowSize <= 0 || capacity <= 0 || expiry <= 0)
34+
if (!windowSize || !capacity || windowSize <= 0 || capacity <= 0 || expiry <= 0)
3535
throw Error('FixedWindow windowSize, capacity and keyExpiry must be positive');
3636
}
3737

@@ -68,7 +68,7 @@ class FixedWindow implements RateLimiter {
6868
// attempt to get the value for the uuid from the redis cache
6969
const windowJSON = await this.client.get(uuid);
7070

71-
if (windowJSON === null) {
71+
if (!windowJSON) {
7272
const newUserWindow: Window = {
7373
currentTokens: tokens > this.capacity ? 0 : tokens,
7474
fixedWindowStart: timestamp,

src/rateLimiters/slidingWindowCounter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SlidingWindowCounter implements RateLimiter {
3636
this.capacity = capacity;
3737
this.client = client;
3838
this.keyExpiry = expiry;
39-
if (windowSize <= 0 || capacity <= 0 || expiry <= 0)
39+
if (!windowSize || !capacity || windowSize <= 0 || capacity <= 0 || expiry <= 0)
4040
throw SyntaxError(
4141
'SlidingWindowCounter window size, capacity and keyExpiry must be positive'
4242
);

src/rateLimiters/slidingWindowLog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class SlidingWindowLog implements RateLimiter {
3535
this.capacity = capacity;
3636
this.client = client;
3737
this.keyExpiry = expiry;
38-
if (windowSize <= 0 || capacity <= 0 || expiry <= 0)
38+
if (!windowSize || !capacity || windowSize <= 0 || capacity <= 0 || expiry <= 0)
3939
throw SyntaxError(
4040
'SlidingWindowLog window size, capacity and keyExpiry must be positive'
4141
);

src/rateLimiters/tokenBucket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TokenBucket implements RateLimiter {
3030
this.refillRate = refillRate;
3131
this.client = client;
3232
this.keyExpiry = expiry;
33-
if (refillRate <= 0 || capacity <= 0 || expiry <= 0)
33+
if (!refillRate || !capacity || refillRate <= 0 || capacity <= 0 || expiry <= 0)
3434
throw Error('TokenBucket refillRate, capacity and keyExpiry must be positive');
3535
}
3636

0 commit comments

Comments
 (0)