Skip to content

Commit 973d8b2

Browse files
committed
fix: verify number of buckets
1 parent fc0bc4f commit 973d8b2

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/ratelimiter",
3-
"version": "5.12.0",
3+
"version": "5.13.0",
44
"description": "Respect the rate limit rules of API's you need to consume.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/ratelimiter/RateLimitStore.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,27 @@ export class RateLimitStore extends Macroable {
3838
public async getOrInit(key: string, rules: RateLimitRule[]) {
3939
const cache = Cache.store(this.options.store)
4040

41-
let buckets = await cache.get(key)
41+
const buckets = await cache.get(key)
4242

4343
if (!buckets) {
44-
buckets = JSON.stringify(rules.map(() => []))
44+
const initialized = JSON.stringify(rules.map(() => []))
4545

46-
await cache.set(key, buckets)
46+
await cache.set(key, initialized)
47+
48+
return JSON.parse(initialized) as number[][]
49+
}
50+
51+
const parsed = JSON.parse(buckets) as number[][]
52+
53+
if (parsed.length !== rules.length) {
54+
const reconciled = rules.map((_, i) => parsed[i] ?? [])
55+
56+
await cache.set(key, JSON.stringify(reconciled))
57+
58+
return reconciled
4759
}
4860

49-
return JSON.parse(buckets) as number[][]
61+
return parsed
5062
}
5163

5264
/**

0 commit comments

Comments
 (0)