@upstash/ratelimit 2.1.0-rc → 2.1.0
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/dist/index.js +25 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -188,7 +188,7 @@ var fixedWindowLimitScript = `#!lua flags=allow-key-locking
|
|
|
188
188
|
|
|
189
189
|
-- Check for dynamic limit
|
|
190
190
|
local effectiveLimit = tokens
|
|
191
|
-
if dynamicLimitKey ~= "" then
|
|
191
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
192
192
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
193
193
|
if dynamicLimit then
|
|
194
194
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -211,7 +211,7 @@ var fixedWindowRemainingTokensScript = `#!lua flags=allow-key-locking
|
|
|
211
211
|
|
|
212
212
|
-- Check for dynamic limit
|
|
213
213
|
local effectiveLimit = tokens
|
|
214
|
-
if dynamicLimitKey ~= "" then
|
|
214
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
215
215
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
216
216
|
if dynamicLimit then
|
|
217
217
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -237,7 +237,7 @@ var slidingWindowLimitScript = `#!lua flags=allow-key-locking
|
|
|
237
237
|
|
|
238
238
|
-- Check for dynamic limit
|
|
239
239
|
local effectiveLimit = tokens
|
|
240
|
-
if dynamicLimitKey ~= "" then
|
|
240
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
241
241
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
242
242
|
if dynamicLimit then
|
|
243
243
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -280,7 +280,7 @@ var slidingWindowRemainingTokensScript = `#!lua flags=allow-key-locking
|
|
|
280
280
|
|
|
281
281
|
-- Check for dynamic limit
|
|
282
282
|
local effectiveLimit = tokens
|
|
283
|
-
if dynamicLimitKey ~= "" then
|
|
283
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
284
284
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
285
285
|
if dynamicLimit then
|
|
286
286
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -315,7 +315,7 @@ var tokenBucketLimitScript = `#!lua flags=allow-key-locking
|
|
|
315
315
|
|
|
316
316
|
-- Check for dynamic limit
|
|
317
317
|
local effectiveLimit = maxTokens
|
|
318
|
-
if dynamicLimitKey ~= "" then
|
|
318
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
319
319
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
320
320
|
if dynamicLimit then
|
|
321
321
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -365,7 +365,7 @@ var tokenBucketRemainingTokensScript = `#!lua flags=allow-key-locking
|
|
|
365
365
|
|
|
366
366
|
-- Check for dynamic limit
|
|
367
367
|
local effectiveLimit = maxTokens
|
|
368
|
-
if dynamicLimitKey ~= "" then
|
|
368
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
369
369
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
370
370
|
if dynamicLimit then
|
|
371
371
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -522,31 +522,31 @@ var SCRIPTS = {
|
|
|
522
522
|
fixedWindow: {
|
|
523
523
|
limit: {
|
|
524
524
|
script: fixedWindowLimitScript,
|
|
525
|
-
hash: "
|
|
525
|
+
hash: "b2efc02798c8e9b471a10f9b1d5745db89923ff3"
|
|
526
526
|
},
|
|
527
527
|
getRemaining: {
|
|
528
528
|
script: fixedWindowRemainingTokensScript,
|
|
529
|
-
hash: "
|
|
529
|
+
hash: "823cb2a5a902d69681244f5e28e5ebb8f2a89edd"
|
|
530
530
|
}
|
|
531
531
|
},
|
|
532
532
|
slidingWindow: {
|
|
533
533
|
limit: {
|
|
534
534
|
script: slidingWindowLimitScript,
|
|
535
|
-
hash: "
|
|
535
|
+
hash: "74740bb3d7792848093cdef586b6b6037c455fd6"
|
|
536
536
|
},
|
|
537
537
|
getRemaining: {
|
|
538
538
|
script: slidingWindowRemainingTokensScript,
|
|
539
|
-
hash: "
|
|
539
|
+
hash: "7ca36169e0fc9caedd81b4eef8e99854c157f9b5"
|
|
540
540
|
}
|
|
541
541
|
},
|
|
542
542
|
tokenBucket: {
|
|
543
543
|
limit: {
|
|
544
544
|
script: tokenBucketLimitScript,
|
|
545
|
-
hash: "
|
|
545
|
+
hash: "a9873a8026b509e99a49a0e5a2049756c693c384"
|
|
546
546
|
},
|
|
547
547
|
getRemaining: {
|
|
548
548
|
script: tokenBucketRemainingTokensScript,
|
|
549
|
-
hash: "
|
|
549
|
+
hash: "40daf4a441df736f5c99f8b77bbe176c909e5de4"
|
|
550
550
|
}
|
|
551
551
|
},
|
|
552
552
|
cachedFixedWindow: {
|
|
@@ -1060,7 +1060,7 @@ var Ratelimit = class {
|
|
|
1060
1060
|
};
|
|
1061
1061
|
|
|
1062
1062
|
// src/version.ts
|
|
1063
|
-
var VERSION = "2.1.0
|
|
1063
|
+
var VERSION = "2.1.0";
|
|
1064
1064
|
|
|
1065
1065
|
// src/telemetry.ts
|
|
1066
1066
|
var taggedClients = /* @__PURE__ */ new WeakSet();
|
|
@@ -1522,11 +1522,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1522
1522
|
};
|
|
1523
1523
|
}
|
|
1524
1524
|
}
|
|
1525
|
-
const
|
|
1525
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1526
1526
|
const [usedTokensAfterUpdate, effectiveLimit] = await safeEval(
|
|
1527
1527
|
ctx,
|
|
1528
1528
|
SCRIPTS.singleRegion.fixedWindow.limit,
|
|
1529
|
-
[key,
|
|
1529
|
+
[key, ...dynamicLimitKeys],
|
|
1530
1530
|
[tokens, windowDuration, incrementBy]
|
|
1531
1531
|
);
|
|
1532
1532
|
const success = usedTokensAfterUpdate <= effectiveLimit;
|
|
@@ -1550,11 +1550,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1550
1550
|
async getRemaining(ctx, identifier) {
|
|
1551
1551
|
const bucket = Math.floor(Date.now() / windowDuration);
|
|
1552
1552
|
const key = [identifier, bucket].join(":");
|
|
1553
|
-
const
|
|
1553
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1554
1554
|
const [remaining, effectiveLimit] = await safeEval(
|
|
1555
1555
|
ctx,
|
|
1556
1556
|
SCRIPTS.singleRegion.fixedWindow.getRemaining,
|
|
1557
|
-
[key,
|
|
1557
|
+
[key, ...dynamicLimitKeys],
|
|
1558
1558
|
[tokens]
|
|
1559
1559
|
);
|
|
1560
1560
|
return {
|
|
@@ -1616,11 +1616,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1616
1616
|
};
|
|
1617
1617
|
}
|
|
1618
1618
|
}
|
|
1619
|
-
const
|
|
1619
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1620
1620
|
const [remainingTokens, effectiveLimit] = await safeEval(
|
|
1621
1621
|
ctx,
|
|
1622
1622
|
SCRIPTS.singleRegion.slidingWindow.limit,
|
|
1623
|
-
[currentKey, previousKey,
|
|
1623
|
+
[currentKey, previousKey, ...dynamicLimitKeys],
|
|
1624
1624
|
[tokens, now, windowSize, incrementBy]
|
|
1625
1625
|
);
|
|
1626
1626
|
const success = remainingTokens >= 0;
|
|
@@ -1646,11 +1646,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1646
1646
|
const currentKey = [identifier, currentWindow].join(":");
|
|
1647
1647
|
const previousWindow = currentWindow - 1;
|
|
1648
1648
|
const previousKey = [identifier, previousWindow].join(":");
|
|
1649
|
-
const
|
|
1649
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1650
1650
|
const [remaining, effectiveLimit] = await safeEval(
|
|
1651
1651
|
ctx,
|
|
1652
1652
|
SCRIPTS.singleRegion.slidingWindow.getRemaining,
|
|
1653
|
-
[currentKey, previousKey,
|
|
1653
|
+
[currentKey, previousKey, ...dynamicLimitKeys],
|
|
1654
1654
|
[tokens, now, windowSize]
|
|
1655
1655
|
);
|
|
1656
1656
|
return {
|
|
@@ -1705,11 +1705,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1705
1705
|
};
|
|
1706
1706
|
}
|
|
1707
1707
|
}
|
|
1708
|
-
const
|
|
1708
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1709
1709
|
const [remaining, reset, effectiveLimit] = await safeEval(
|
|
1710
1710
|
ctx,
|
|
1711
1711
|
SCRIPTS.singleRegion.tokenBucket.limit,
|
|
1712
|
-
[identifier,
|
|
1712
|
+
[identifier, ...dynamicLimitKeys],
|
|
1713
1713
|
[maxTokens, intervalDuration, refillRate, now, incrementBy]
|
|
1714
1714
|
);
|
|
1715
1715
|
const success = remaining >= 0;
|
|
@@ -1729,11 +1729,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1729
1729
|
};
|
|
1730
1730
|
},
|
|
1731
1731
|
async getRemaining(ctx, identifier) {
|
|
1732
|
-
const
|
|
1732
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1733
1733
|
const [remainingTokens, refilledAt, effectiveLimit] = await safeEval(
|
|
1734
1734
|
ctx,
|
|
1735
1735
|
SCRIPTS.singleRegion.tokenBucket.getRemaining,
|
|
1736
|
-
[identifier,
|
|
1736
|
+
[identifier, ...dynamicLimitKeys],
|
|
1737
1737
|
[maxTokens]
|
|
1738
1738
|
);
|
|
1739
1739
|
const freshRefillAt = Date.now() + intervalDuration;
|