@upstash/ratelimit 2.1.0 → 2.2.1-canary.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/README.md +2 -0
- package/dist/index.d.mts +31 -1
- package/dist/index.d.ts +31 -1
- package/dist/index.js +50 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +50 -27
- package/dist/index.mjs.map +1 -1
- package/docs/algorithms.mdx +169 -0
- package/docs/costs.mdx +112 -0
- package/docs/features.mdx +262 -0
- package/docs/gettingstarted.mdx +220 -0
- package/docs/methods.mdx +206 -0
- package/docs/overview.mdx +159 -0
- package/docs/traffic-protection.mdx +110 -0
- package/package.json +1 -1
- package/src/analytics.ts +110 -0
- package/src/cache.ts +55 -0
- package/src/constants.ts +14 -0
- package/src/deny-list/deny-list.ts +124 -0
- package/src/deny-list/index.ts +1 -0
- package/src/deny-list/ip-deny-list.ts +131 -0
- package/src/deny-list/scripts.ts +26 -0
- package/src/deny-list/time.ts +20 -0
- package/src/duration.ts +37 -0
- package/src/hash.ts +29 -0
- package/src/index.ts +18 -0
- package/src/lua-scripts/hash.ts +95 -0
- package/src/lua-scripts/multi.ts +86 -0
- package/src/lua-scripts/reset.ts +23 -0
- package/src/lua-scripts/single.ts +237 -0
- package/src/multi.ts +610 -0
- package/src/ratelimit.ts +506 -0
- package/src/single.ts +676 -0
- package/src/telemetry.ts +43 -0
- package/src/types.ts +132 -0
- package/src/version.ts +1 -0
package/dist/index.mjs
CHANGED
|
@@ -114,33 +114,30 @@ var DYNAMIC_LIMIT_KEY_SUFFIX = ":dynamic:global";
|
|
|
114
114
|
var DEFAULT_PREFIX = "@upstash/ratelimit";
|
|
115
115
|
|
|
116
116
|
// src/duration.ts
|
|
117
|
+
var MULTIPLIERS = {
|
|
118
|
+
ms: 1,
|
|
119
|
+
s: 1e3,
|
|
120
|
+
m: 1e3 * 60,
|
|
121
|
+
h: 1e3 * 60 * 60,
|
|
122
|
+
d: 1e3 * 60 * 60 * 24,
|
|
123
|
+
w: 1e3 * 60 * 60 * 24 * 7
|
|
124
|
+
};
|
|
117
125
|
function ms(d) {
|
|
118
|
-
const match = d.match(/^(\d+)\s?(ms|s|m|h|d)$/);
|
|
126
|
+
const match = d.match(/^(\d+(?:\.\d+)?)\s?(ms|s|m|h|d|w)$/);
|
|
119
127
|
if (!match) {
|
|
120
128
|
throw new Error(`Unable to parse window size: ${d}`);
|
|
121
129
|
}
|
|
122
|
-
const time = Number.
|
|
130
|
+
const time = Number.parseFloat(match[1]);
|
|
123
131
|
const unit = match[2];
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
case "m": {
|
|
132
|
-
return time * 1e3 * 60;
|
|
133
|
-
}
|
|
134
|
-
case "h": {
|
|
135
|
-
return time * 1e3 * 60 * 60;
|
|
136
|
-
}
|
|
137
|
-
case "d": {
|
|
138
|
-
return time * 1e3 * 60 * 60 * 24;
|
|
139
|
-
}
|
|
140
|
-
default: {
|
|
141
|
-
throw new Error(`Unable to parse window size: ${d}`);
|
|
142
|
-
}
|
|
132
|
+
const multiplier = MULTIPLIERS[unit];
|
|
133
|
+
if (multiplier === void 0) {
|
|
134
|
+
throw new Error(`Unable to parse window size: ${d}`);
|
|
135
|
+
}
|
|
136
|
+
const result = Math.round(time * multiplier);
|
|
137
|
+
if (result < 1) {
|
|
138
|
+
throw new Error(`Window size must be at least 1 ms, got: ${d}`);
|
|
143
139
|
}
|
|
140
|
+
return result;
|
|
144
141
|
}
|
|
145
142
|
|
|
146
143
|
// src/hash.ts
|
|
@@ -319,8 +316,12 @@ var tokenBucketLimitScript = `#!lua flags=allow-key-locking
|
|
|
319
316
|
refilledAt = refilledAt + numRefills * interval
|
|
320
317
|
end
|
|
321
318
|
|
|
322
|
-
-- Only reject
|
|
323
|
-
|
|
319
|
+
-- Only reject when consuming (not refunding) and there are not enough tokens
|
|
320
|
+
-- for this request. Checking tokens == 0 only covered incrementBy == 1: with
|
|
321
|
+
-- a larger rate (e.g. 2 tokens left, incrementBy 5) the request fell through,
|
|
322
|
+
-- drove tokens negative and still wrote it back, so a rejected request
|
|
323
|
+
-- consumed tokens and a single large rate could lock the identifier out.
|
|
324
|
+
if incrementBy > 0 and tokens < incrementBy then
|
|
324
325
|
return {-1, refilledAt + interval, effectiveLimit}
|
|
325
326
|
end
|
|
326
327
|
|
|
@@ -519,7 +520,7 @@ var SCRIPTS = {
|
|
|
519
520
|
tokenBucket: {
|
|
520
521
|
limit: {
|
|
521
522
|
script: tokenBucketLimitScript,
|
|
522
|
-
hash: "
|
|
523
|
+
hash: "d60be5bd3d83482ae229309f1f7425424f891204"
|
|
523
524
|
},
|
|
524
525
|
getRemaining: {
|
|
525
526
|
script: tokenBucketRemainingTokensScript,
|
|
@@ -795,6 +796,8 @@ var Ratelimit = class {
|
|
|
795
796
|
* }
|
|
796
797
|
* return "Yes"
|
|
797
798
|
* ```
|
|
799
|
+
*
|
|
800
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
798
801
|
*/
|
|
799
802
|
limit = async (identifier, req) => {
|
|
800
803
|
let timeoutId = null;
|
|
@@ -832,6 +835,8 @@ var Ratelimit = class {
|
|
|
832
835
|
* }
|
|
833
836
|
* return "Yes"
|
|
834
837
|
* ```
|
|
838
|
+
*
|
|
839
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
835
840
|
*/
|
|
836
841
|
blockUntilReady = async (identifier, timeout) => {
|
|
837
842
|
if (timeout <= 0) {
|
|
@@ -848,13 +853,16 @@ var Ratelimit = class {
|
|
|
848
853
|
throw new Error("This should not happen");
|
|
849
854
|
}
|
|
850
855
|
const wait = Math.min(res.reset, deadline) - Date.now();
|
|
851
|
-
await new Promise((r) => setTimeout(r, wait));
|
|
856
|
+
await new Promise((r) => setTimeout(r, wait > 0 ? wait : 100));
|
|
852
857
|
if (Date.now() > deadline) {
|
|
853
858
|
break;
|
|
854
859
|
}
|
|
855
860
|
}
|
|
856
861
|
return res;
|
|
857
862
|
};
|
|
863
|
+
/**
|
|
864
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
865
|
+
*/
|
|
858
866
|
resetUsedTokens = async (identifier) => {
|
|
859
867
|
const pattern = [this.prefix, identifier].join(":");
|
|
860
868
|
await this.limiter().resetTokens(this.ctx, pattern);
|
|
@@ -866,6 +874,8 @@ var Ratelimit = class {
|
|
|
866
874
|
* @returns object with `remaining`, `reset`, and `limit` fields. `remaining` denotes
|
|
867
875
|
* the remaining tokens, `limit` is the effective limit (considering dynamic
|
|
868
876
|
* limits if enabled), and `reset` denotes the timestamp when the tokens reset.
|
|
877
|
+
*
|
|
878
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
869
879
|
*/
|
|
870
880
|
getRemaining = async (identifier) => {
|
|
871
881
|
const pattern = [this.prefix, identifier].join(":");
|
|
@@ -1003,6 +1013,7 @@ var Ratelimit = class {
|
|
|
1003
1013
|
* ```
|
|
1004
1014
|
*
|
|
1005
1015
|
* @param options.limit - The new rate limit to apply globally, or false to disable
|
|
1016
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
1006
1017
|
*/
|
|
1007
1018
|
setDynamicLimit = async (options) => {
|
|
1008
1019
|
if (!this.dynamicLimits) {
|
|
@@ -1023,6 +1034,7 @@ var Ratelimit = class {
|
|
|
1023
1034
|
* ```
|
|
1024
1035
|
*
|
|
1025
1036
|
* @returns Object containing the current global dynamic limit, or null if not set
|
|
1037
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
1026
1038
|
*/
|
|
1027
1039
|
getDynamicLimit = async () => {
|
|
1028
1040
|
if (!this.dynamicLimits) {
|
|
@@ -1037,7 +1049,7 @@ var Ratelimit = class {
|
|
|
1037
1049
|
};
|
|
1038
1050
|
|
|
1039
1051
|
// src/version.ts
|
|
1040
|
-
var VERSION = "2.1.0";
|
|
1052
|
+
var VERSION = "2.2.1-canary.0";
|
|
1041
1053
|
|
|
1042
1054
|
// src/telemetry.ts
|
|
1043
1055
|
var taggedClients = /* @__PURE__ */ new WeakSet();
|
|
@@ -1070,6 +1082,8 @@ function randomId() {
|
|
|
1070
1082
|
var MultiRegionRatelimit = class extends Ratelimit {
|
|
1071
1083
|
/**
|
|
1072
1084
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
|
|
1085
|
+
*
|
|
1086
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
1073
1087
|
*/
|
|
1074
1088
|
constructor(config) {
|
|
1075
1089
|
super({
|
|
@@ -1112,6 +1126,7 @@ var MultiRegionRatelimit = class extends Ratelimit {
|
|
|
1112
1126
|
*
|
|
1113
1127
|
* @param tokens - How many requests a user can make in each time window.
|
|
1114
1128
|
* @param window - A fixed timeframe
|
|
1129
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1115
1130
|
*/
|
|
1116
1131
|
static fixedWindow(tokens, window) {
|
|
1117
1132
|
const windowDuration = ms(window);
|
|
@@ -1275,6 +1290,7 @@ var MultiRegionRatelimit = class extends Ratelimit {
|
|
|
1275
1290
|
*
|
|
1276
1291
|
* @param tokens - How many requests a user can make in each time window.
|
|
1277
1292
|
* @param window - The duration in which the user can max X requests.
|
|
1293
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1278
1294
|
*/
|
|
1279
1295
|
static slidingWindow(tokens, window) {
|
|
1280
1296
|
const windowSize = ms(window);
|
|
@@ -1443,6 +1459,8 @@ var MultiRegionRatelimit = class extends Ratelimit {
|
|
|
1443
1459
|
var RegionRatelimit = class extends Ratelimit {
|
|
1444
1460
|
/**
|
|
1445
1461
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithm of your choice.
|
|
1462
|
+
*
|
|
1463
|
+
* @see node_modules/@upstash/ratelimit/docs/gettingstarted.mdx
|
|
1446
1464
|
*/
|
|
1447
1465
|
constructor(config) {
|
|
1448
1466
|
super({
|
|
@@ -1478,6 +1496,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1478
1496
|
*
|
|
1479
1497
|
* @param tokens - How many requests a user can make in each time window.
|
|
1480
1498
|
* @param window - A fixed timeframe
|
|
1499
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1481
1500
|
*/
|
|
1482
1501
|
static fixedWindow(tokens, window) {
|
|
1483
1502
|
const windowDuration = ms(window);
|
|
@@ -1569,6 +1588,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1569
1588
|
*
|
|
1570
1589
|
* @param tokens - How many requests a user can make in each time window.
|
|
1571
1590
|
* @param window - The duration in which the user can max X requests.
|
|
1591
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1572
1592
|
*/
|
|
1573
1593
|
static slidingWindow(tokens, window) {
|
|
1574
1594
|
const windowSize = ms(window);
|
|
@@ -1662,6 +1682,8 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1662
1682
|
* rate.
|
|
1663
1683
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
1664
1684
|
* than `refillRate`
|
|
1685
|
+
*
|
|
1686
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1665
1687
|
*/
|
|
1666
1688
|
static tokenBucket(refillRate, interval, maxTokens) {
|
|
1667
1689
|
const intervalDuration = ms(interval);
|
|
@@ -1758,6 +1780,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1758
1780
|
*
|
|
1759
1781
|
* @param tokens - How many requests a user can make in each time window.
|
|
1760
1782
|
* @param window - A fixed timeframe
|
|
1783
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1761
1784
|
*/
|
|
1762
1785
|
static cachedFixedWindow(tokens, window) {
|
|
1763
1786
|
const windowDuration = ms(window);
|
|
@@ -1778,7 +1801,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1778
1801
|
const hit = typeof ctx.cache.get(key) === "number";
|
|
1779
1802
|
if (hit) {
|
|
1780
1803
|
const cachedTokensAfterUpdate = ctx.cache.incr(key, incrementBy);
|
|
1781
|
-
const success = cachedTokensAfterUpdate
|
|
1804
|
+
const success = cachedTokensAfterUpdate <= tokens;
|
|
1782
1805
|
const pending = success ? safeEval(
|
|
1783
1806
|
ctx,
|
|
1784
1807
|
SCRIPTS.singleRegion.cachedFixedWindow.limit,
|