@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.mjs
CHANGED
|
@@ -165,7 +165,7 @@ var fixedWindowLimitScript = `#!lua flags=allow-key-locking
|
|
|
165
165
|
|
|
166
166
|
-- Check for dynamic limit
|
|
167
167
|
local effectiveLimit = tokens
|
|
168
|
-
if dynamicLimitKey ~= "" then
|
|
168
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
169
169
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
170
170
|
if dynamicLimit then
|
|
171
171
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -188,7 +188,7 @@ var fixedWindowRemainingTokensScript = `#!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)
|
|
@@ -214,7 +214,7 @@ var slidingWindowLimitScript = `#!lua flags=allow-key-locking
|
|
|
214
214
|
|
|
215
215
|
-- Check for dynamic limit
|
|
216
216
|
local effectiveLimit = tokens
|
|
217
|
-
if dynamicLimitKey ~= "" then
|
|
217
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
218
218
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
219
219
|
if dynamicLimit then
|
|
220
220
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -257,7 +257,7 @@ var slidingWindowRemainingTokensScript = `#!lua flags=allow-key-locking
|
|
|
257
257
|
|
|
258
258
|
-- Check for dynamic limit
|
|
259
259
|
local effectiveLimit = tokens
|
|
260
|
-
if dynamicLimitKey ~= "" then
|
|
260
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
261
261
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
262
262
|
if dynamicLimit then
|
|
263
263
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -292,7 +292,7 @@ var tokenBucketLimitScript = `#!lua flags=allow-key-locking
|
|
|
292
292
|
|
|
293
293
|
-- Check for dynamic limit
|
|
294
294
|
local effectiveLimit = maxTokens
|
|
295
|
-
if dynamicLimitKey ~= "" then
|
|
295
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
296
296
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
297
297
|
if dynamicLimit then
|
|
298
298
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -342,7 +342,7 @@ var tokenBucketRemainingTokensScript = `#!lua flags=allow-key-locking
|
|
|
342
342
|
|
|
343
343
|
-- Check for dynamic limit
|
|
344
344
|
local effectiveLimit = maxTokens
|
|
345
|
-
if dynamicLimitKey ~= "" then
|
|
345
|
+
if dynamicLimitKey and dynamicLimitKey ~= "" then
|
|
346
346
|
local dynamicLimit = redis.call("GET", dynamicLimitKey)
|
|
347
347
|
if dynamicLimit then
|
|
348
348
|
effectiveLimit = tonumber(dynamicLimit)
|
|
@@ -499,31 +499,31 @@ var SCRIPTS = {
|
|
|
499
499
|
fixedWindow: {
|
|
500
500
|
limit: {
|
|
501
501
|
script: fixedWindowLimitScript,
|
|
502
|
-
hash: "
|
|
502
|
+
hash: "b2efc02798c8e9b471a10f9b1d5745db89923ff3"
|
|
503
503
|
},
|
|
504
504
|
getRemaining: {
|
|
505
505
|
script: fixedWindowRemainingTokensScript,
|
|
506
|
-
hash: "
|
|
506
|
+
hash: "823cb2a5a902d69681244f5e28e5ebb8f2a89edd"
|
|
507
507
|
}
|
|
508
508
|
},
|
|
509
509
|
slidingWindow: {
|
|
510
510
|
limit: {
|
|
511
511
|
script: slidingWindowLimitScript,
|
|
512
|
-
hash: "
|
|
512
|
+
hash: "74740bb3d7792848093cdef586b6b6037c455fd6"
|
|
513
513
|
},
|
|
514
514
|
getRemaining: {
|
|
515
515
|
script: slidingWindowRemainingTokensScript,
|
|
516
|
-
hash: "
|
|
516
|
+
hash: "7ca36169e0fc9caedd81b4eef8e99854c157f9b5"
|
|
517
517
|
}
|
|
518
518
|
},
|
|
519
519
|
tokenBucket: {
|
|
520
520
|
limit: {
|
|
521
521
|
script: tokenBucketLimitScript,
|
|
522
|
-
hash: "
|
|
522
|
+
hash: "a9873a8026b509e99a49a0e5a2049756c693c384"
|
|
523
523
|
},
|
|
524
524
|
getRemaining: {
|
|
525
525
|
script: tokenBucketRemainingTokensScript,
|
|
526
|
-
hash: "
|
|
526
|
+
hash: "40daf4a441df736f5c99f8b77bbe176c909e5de4"
|
|
527
527
|
}
|
|
528
528
|
},
|
|
529
529
|
cachedFixedWindow: {
|
|
@@ -1037,7 +1037,7 @@ var Ratelimit = class {
|
|
|
1037
1037
|
};
|
|
1038
1038
|
|
|
1039
1039
|
// src/version.ts
|
|
1040
|
-
var VERSION = "2.1.0
|
|
1040
|
+
var VERSION = "2.1.0";
|
|
1041
1041
|
|
|
1042
1042
|
// src/telemetry.ts
|
|
1043
1043
|
var taggedClients = /* @__PURE__ */ new WeakSet();
|
|
@@ -1499,11 +1499,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1499
1499
|
};
|
|
1500
1500
|
}
|
|
1501
1501
|
}
|
|
1502
|
-
const
|
|
1502
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1503
1503
|
const [usedTokensAfterUpdate, effectiveLimit] = await safeEval(
|
|
1504
1504
|
ctx,
|
|
1505
1505
|
SCRIPTS.singleRegion.fixedWindow.limit,
|
|
1506
|
-
[key,
|
|
1506
|
+
[key, ...dynamicLimitKeys],
|
|
1507
1507
|
[tokens, windowDuration, incrementBy]
|
|
1508
1508
|
);
|
|
1509
1509
|
const success = usedTokensAfterUpdate <= effectiveLimit;
|
|
@@ -1527,11 +1527,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1527
1527
|
async getRemaining(ctx, identifier) {
|
|
1528
1528
|
const bucket = Math.floor(Date.now() / windowDuration);
|
|
1529
1529
|
const key = [identifier, bucket].join(":");
|
|
1530
|
-
const
|
|
1530
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1531
1531
|
const [remaining, effectiveLimit] = await safeEval(
|
|
1532
1532
|
ctx,
|
|
1533
1533
|
SCRIPTS.singleRegion.fixedWindow.getRemaining,
|
|
1534
|
-
[key,
|
|
1534
|
+
[key, ...dynamicLimitKeys],
|
|
1535
1535
|
[tokens]
|
|
1536
1536
|
);
|
|
1537
1537
|
return {
|
|
@@ -1593,11 +1593,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1593
1593
|
};
|
|
1594
1594
|
}
|
|
1595
1595
|
}
|
|
1596
|
-
const
|
|
1596
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1597
1597
|
const [remainingTokens, effectiveLimit] = await safeEval(
|
|
1598
1598
|
ctx,
|
|
1599
1599
|
SCRIPTS.singleRegion.slidingWindow.limit,
|
|
1600
|
-
[currentKey, previousKey,
|
|
1600
|
+
[currentKey, previousKey, ...dynamicLimitKeys],
|
|
1601
1601
|
[tokens, now, windowSize, incrementBy]
|
|
1602
1602
|
);
|
|
1603
1603
|
const success = remainingTokens >= 0;
|
|
@@ -1623,11 +1623,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1623
1623
|
const currentKey = [identifier, currentWindow].join(":");
|
|
1624
1624
|
const previousWindow = currentWindow - 1;
|
|
1625
1625
|
const previousKey = [identifier, previousWindow].join(":");
|
|
1626
|
-
const
|
|
1626
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1627
1627
|
const [remaining, effectiveLimit] = await safeEval(
|
|
1628
1628
|
ctx,
|
|
1629
1629
|
SCRIPTS.singleRegion.slidingWindow.getRemaining,
|
|
1630
|
-
[currentKey, previousKey,
|
|
1630
|
+
[currentKey, previousKey, ...dynamicLimitKeys],
|
|
1631
1631
|
[tokens, now, windowSize]
|
|
1632
1632
|
);
|
|
1633
1633
|
return {
|
|
@@ -1682,11 +1682,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1682
1682
|
};
|
|
1683
1683
|
}
|
|
1684
1684
|
}
|
|
1685
|
-
const
|
|
1685
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1686
1686
|
const [remaining, reset, effectiveLimit] = await safeEval(
|
|
1687
1687
|
ctx,
|
|
1688
1688
|
SCRIPTS.singleRegion.tokenBucket.limit,
|
|
1689
|
-
[identifier,
|
|
1689
|
+
[identifier, ...dynamicLimitKeys],
|
|
1690
1690
|
[maxTokens, intervalDuration, refillRate, now, incrementBy]
|
|
1691
1691
|
);
|
|
1692
1692
|
const success = remaining >= 0;
|
|
@@ -1706,11 +1706,11 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1706
1706
|
};
|
|
1707
1707
|
},
|
|
1708
1708
|
async getRemaining(ctx, identifier) {
|
|
1709
|
-
const
|
|
1709
|
+
const dynamicLimitKeys = ctx.dynamicLimits ? [`${ctx.prefix}${DYNAMIC_LIMIT_KEY_SUFFIX}`] : [];
|
|
1710
1710
|
const [remainingTokens, refilledAt, effectiveLimit] = await safeEval(
|
|
1711
1711
|
ctx,
|
|
1712
1712
|
SCRIPTS.singleRegion.tokenBucket.getRemaining,
|
|
1713
|
-
[identifier,
|
|
1713
|
+
[identifier, ...dynamicLimitKeys],
|
|
1714
1714
|
[maxTokens]
|
|
1715
1715
|
);
|
|
1716
1716
|
const freshRefillAt = Date.now() + intervalDuration;
|