@upstash/ratelimit 0.4.1 → 0.4.3-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/dist/index.mjs CHANGED
@@ -1,27 +1,3 @@
1
- // src/duration.ts
2
- function ms(d) {
3
- const match = d.match(/^(\d+)\s?(ms|s|m|h|d)$/);
4
- if (!match) {
5
- throw new Error(`Unable to parse window size: ${d}`);
6
- }
7
- const time = parseInt(match[1]);
8
- const unit = match[2];
9
- switch (unit) {
10
- case "ms":
11
- return time;
12
- case "s":
13
- return time * 1e3;
14
- case "m":
15
- return time * 1e3 * 60;
16
- case "h":
17
- return time * 1e3 * 60 * 60;
18
- case "d":
19
- return time * 1e3 * 60 * 60 * 24;
20
- default:
21
- throw new Error(`Unable to parse window size: ${d}`);
22
- }
23
- }
24
-
25
1
  // src/analytics.ts
26
2
  import { Analytics as CoreAnalytics } from "@upstash/core-analytics";
27
3
  var Analytics = class {
@@ -29,6 +5,7 @@ var Analytics = class {
29
5
  table = "events";
30
6
  constructor(config) {
31
7
  this.analytics = new CoreAnalytics({
8
+ // @ts-expect-error we need to fix the types in core-analytics, it should only require the methods it needs, not the whole sdk
32
9
  redis: config.redis,
33
10
  window: "1h",
34
11
  prefix: config.prefix ?? "@upstash/ratelimit",
@@ -119,6 +96,30 @@ var Cache = class {
119
96
  }
120
97
  };
121
98
 
99
+ // src/duration.ts
100
+ function ms(d) {
101
+ const match = d.match(/^(\d+)\s?(ms|s|m|h|d)$/);
102
+ if (!match) {
103
+ throw new Error(`Unable to parse window size: ${d}`);
104
+ }
105
+ const time = parseInt(match[1]);
106
+ const unit = match[2];
107
+ switch (unit) {
108
+ case "ms":
109
+ return time;
110
+ case "s":
111
+ return time * 1e3;
112
+ case "m":
113
+ return time * 1e3 * 60;
114
+ case "h":
115
+ return time * 1e3 * 60 * 60;
116
+ case "d":
117
+ return time * 1e3 * 60 * 60 * 24;
118
+ default:
119
+ throw new Error(`Unable to parse window size: ${d}`);
120
+ }
121
+ }
122
+
122
123
  // src/ratelimit.ts
123
124
  var Ratelimit = class {
124
125
  limiter;
@@ -131,7 +132,7 @@ var Ratelimit = class {
131
132
  this.limiter = config.limiter;
132
133
  this.timeout = config.timeout ?? 5e3;
133
134
  this.prefix = config.prefix ?? "@upstash/ratelimit";
134
- this.analytics = config.analytics !== false ? new Analytics({
135
+ this.analytics = config.analytics ? new Analytics({
135
136
  redis: Array.isArray(this.ctx.redis) ? this.ctx.redis[0] : this.ctx.redis,
136
137
  prefix: this.prefix
137
138
  }) : void 0;
@@ -207,7 +208,7 @@ var Ratelimit = class {
207
208
  /**
208
209
  * Block until the request may pass or timeout is reached.
209
210
  *
210
- * This method returns a promsie that resolves as soon as the request may be processed
211
+ * This method returns a promise that resolves as soon as the request may be processed
211
212
  * or after the timeoue has been reached.
212
213
  *
213
214
  * Use this if you want to delay the request until it is ready to get processed.
@@ -250,6 +251,224 @@ var Ratelimit = class {
250
251
  };
251
252
  };
252
253
 
254
+ // src/multi.ts
255
+ function randomId() {
256
+ let result = "";
257
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
258
+ const charactersLength = characters.length;
259
+ for (let i = 0; i < 16; i++) {
260
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
261
+ }
262
+ return result;
263
+ }
264
+ var MultiRegionRatelimit = class extends Ratelimit {
265
+ /**
266
+ * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
267
+ */
268
+ constructor(config) {
269
+ super({
270
+ prefix: config.prefix,
271
+ limiter: config.limiter,
272
+ timeout: config.timeout,
273
+ analytics: config.analytics,
274
+ ctx: {
275
+ redis: config.redis,
276
+ cache: config.ephemeralCache ? new Cache(config.ephemeralCache) : void 0
277
+ }
278
+ });
279
+ }
280
+ /**
281
+ * Each requests inside a fixed time increases a counter.
282
+ * Once the counter reaches a maxmimum allowed number, all further requests are
283
+ * rejected.
284
+ *
285
+ * **Pro:**
286
+ *
287
+ * - Newer requests are not starved by old ones.
288
+ * - Low storage cost.
289
+ *
290
+ * **Con:**
291
+ *
292
+ * A burst of requests near the boundary of a window can result in a very
293
+ * high request rate because two windows will be filled with requests quickly.
294
+ *
295
+ * @param tokens - How many requests a user can make in each time window.
296
+ * @param window - A fixed timeframe
297
+ */
298
+ static fixedWindow(tokens, window) {
299
+ const windowDuration = ms(window);
300
+ const script = `
301
+ local key = KEYS[1]
302
+ local id = ARGV[1]
303
+ local window = ARGV[2]
304
+
305
+ redis.call("SADD", key, id)
306
+ local members = redis.call("SMEMBERS", key)
307
+ if #members == 1 then
308
+ -- The first time this key is set, the value will be 1.
309
+ -- So we only need the expire command once
310
+ redis.call("PEXPIRE", key, window)
311
+ end
312
+
313
+ return members
314
+ `;
315
+ return async function(ctx, identifier) {
316
+ if (ctx.cache) {
317
+ const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
318
+ if (blocked) {
319
+ return {
320
+ success: false,
321
+ limit: tokens,
322
+ remaining: 0,
323
+ reset: reset2,
324
+ pending: Promise.resolve()
325
+ };
326
+ }
327
+ }
328
+ const requestID = randomId();
329
+ const bucket = Math.floor(Date.now() / windowDuration);
330
+ const key = [identifier, bucket].join(":");
331
+ const dbs = ctx.redis.map((redis) => ({
332
+ redis,
333
+ request: redis.eval(script, [key], [requestID, windowDuration])
334
+ }));
335
+ const firstResponse = await Promise.any(dbs.map((s) => s.request));
336
+ const usedTokens = firstResponse.length;
337
+ const remaining = tokens - usedTokens - 1;
338
+ async function sync() {
339
+ const individualIDs = await Promise.all(dbs.map((s) => s.request));
340
+ const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
341
+ for (const db of dbs) {
342
+ const ids = await db.request;
343
+ if (ids.length >= tokens) {
344
+ continue;
345
+ }
346
+ const diff = allIDs.filter((id) => !ids.includes(id));
347
+ if (diff.length === 0) {
348
+ continue;
349
+ }
350
+ await db.redis.sadd(key, ...allIDs);
351
+ }
352
+ }
353
+ const success = remaining > 0;
354
+ const reset = (bucket + 1) * windowDuration;
355
+ if (ctx.cache && !success) {
356
+ ctx.cache.blockUntil(identifier, reset);
357
+ }
358
+ return {
359
+ success,
360
+ limit: tokens,
361
+ remaining,
362
+ reset,
363
+ pending: sync()
364
+ };
365
+ };
366
+ }
367
+ /**
368
+ * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
369
+ * costs than `slidingLogs` and improved boundary behavior by calcualting a
370
+ * weighted score between two windows.
371
+ *
372
+ * **Pro:**
373
+ *
374
+ * Good performance allows this to scale to very high loads.
375
+ *
376
+ * **Con:**
377
+ *
378
+ * Nothing major.
379
+ *
380
+ * @param tokens - How many requests a user can make in each time window.
381
+ * @param window - The duration in which the user can max X requests.
382
+ */
383
+ static slidingWindow(tokens, window) {
384
+ const windowSize = ms(window);
385
+ const script = `
386
+ local currentKey = KEYS[1] -- identifier including prefixes
387
+ local previousKey = KEYS[2] -- key of the previous bucket
388
+ local tokens = tonumber(ARGV[1]) -- tokens per window
389
+ local now = ARGV[2] -- current timestamp in milliseconds
390
+ local window = ARGV[3] -- interval in milliseconds
391
+ local requestID = ARGV[4] -- uuid for this request
392
+
393
+
394
+ local currentMembers = redis.call("SMEMBERS", currentKey)
395
+ local requestsInCurrentWindow = #currentMembers
396
+ local previousMembers = redis.call("SMEMBERS", previousKey)
397
+ local requestsInPreviousWindow = #previousMembers
398
+
399
+ local percentageInCurrent = ( now % window) / window
400
+ if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
401
+ return {currentMembers, previousMembers}
402
+ end
403
+
404
+ redis.call("SADD", currentKey, requestID)
405
+ table.insert(currentMembers, requestID)
406
+ if requestsInCurrentWindow == 0 then
407
+ -- The first time this key is set, the value will be 1.
408
+ -- So we only need the expire command once
409
+ redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
410
+ end
411
+ return {currentMembers, previousMembers}
412
+ `;
413
+ const windowDuration = ms(window);
414
+ return async function(ctx, identifier) {
415
+ if (ctx.cache) {
416
+ const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
417
+ if (blocked) {
418
+ return {
419
+ success: false,
420
+ limit: tokens,
421
+ remaining: 0,
422
+ reset: reset2,
423
+ pending: Promise.resolve()
424
+ };
425
+ }
426
+ }
427
+ const requestID = randomId();
428
+ const now = Date.now();
429
+ const currentWindow = Math.floor(now / windowSize);
430
+ const currentKey = [identifier, currentWindow].join(":");
431
+ const previousWindow = currentWindow - windowSize;
432
+ const previousKey = [identifier, previousWindow].join(":");
433
+ const dbs = ctx.redis.map((redis) => ({
434
+ redis,
435
+ request: redis.eval(script, [currentKey, previousKey], [tokens, now, windowDuration, requestID])
436
+ }));
437
+ const percentageInCurrent = now % windowDuration / windowDuration;
438
+ const [current, previous] = await Promise.any(dbs.map((s) => s.request));
439
+ const usedTokens = previous.length * (1 - percentageInCurrent) + current.length;
440
+ const remaining = tokens - usedTokens;
441
+ async function sync() {
442
+ const [individualIDs] = await Promise.all(dbs.map((s) => s.request));
443
+ const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
444
+ for (const db of dbs) {
445
+ const [ids] = await db.request;
446
+ if (ids.length >= tokens) {
447
+ continue;
448
+ }
449
+ const diff = allIDs.filter((id) => !ids.includes(id));
450
+ if (diff.length === 0) {
451
+ continue;
452
+ }
453
+ await db.redis.sadd(currentKey, ...allIDs);
454
+ }
455
+ }
456
+ const success = remaining > 0;
457
+ const reset = (currentWindow + 1) * windowDuration;
458
+ if (ctx.cache && !success) {
459
+ ctx.cache.blockUntil(identifier, reset);
460
+ }
461
+ return {
462
+ success,
463
+ limit: tokens,
464
+ remaining,
465
+ reset,
466
+ pending: sync()
467
+ };
468
+ };
469
+ }
470
+ };
471
+
253
472
  // src/single.ts
254
473
  var RegionRatelimit = class extends Ratelimit {
255
474
  /**
@@ -572,224 +791,6 @@ var RegionRatelimit = class extends Ratelimit {
572
791
  };
573
792
  }
574
793
  };
575
-
576
- // src/multi.ts
577
- function randomId() {
578
- let result = "";
579
- const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
580
- const charactersLength = characters.length;
581
- for (let i = 0; i < 16; i++) {
582
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
583
- }
584
- return result;
585
- }
586
- var MultiRegionRatelimit = class extends Ratelimit {
587
- /**
588
- * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
589
- */
590
- constructor(config) {
591
- super({
592
- prefix: config.prefix,
593
- limiter: config.limiter,
594
- timeout: config.timeout,
595
- analytics: config.analytics,
596
- ctx: {
597
- redis: config.redis,
598
- cache: config.ephemeralCache ? new Cache(config.ephemeralCache) : void 0
599
- }
600
- });
601
- }
602
- /**
603
- * Each requests inside a fixed time increases a counter.
604
- * Once the counter reaches a maxmimum allowed number, all further requests are
605
- * rejected.
606
- *
607
- * **Pro:**
608
- *
609
- * - Newer requests are not starved by old ones.
610
- * - Low storage cost.
611
- *
612
- * **Con:**
613
- *
614
- * A burst of requests near the boundary of a window can result in a very
615
- * high request rate because two windows will be filled with requests quickly.
616
- *
617
- * @param tokens - How many requests a user can make in each time window.
618
- * @param window - A fixed timeframe
619
- */
620
- static fixedWindow(tokens, window) {
621
- const windowDuration = ms(window);
622
- const script = `
623
- local key = KEYS[1]
624
- local id = ARGV[1]
625
- local window = ARGV[2]
626
-
627
- redis.call("SADD", key, id)
628
- local members = redis.call("SMEMBERS", key)
629
- if #members == 1 then
630
- -- The first time this key is set, the value will be 1.
631
- -- So we only need the expire command once
632
- redis.call("PEXPIRE", key, window)
633
- end
634
-
635
- return members
636
- `;
637
- return async function(ctx, identifier) {
638
- if (ctx.cache) {
639
- const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
640
- if (blocked) {
641
- return {
642
- success: false,
643
- limit: tokens,
644
- remaining: 0,
645
- reset: reset2,
646
- pending: Promise.resolve()
647
- };
648
- }
649
- }
650
- const requestID = randomId();
651
- const bucket = Math.floor(Date.now() / windowDuration);
652
- const key = [identifier, bucket].join(":");
653
- const dbs = ctx.redis.map((redis) => ({
654
- redis,
655
- request: redis.eval(script, [key], [requestID, windowDuration])
656
- }));
657
- const firstResponse = await Promise.any(dbs.map((s) => s.request));
658
- const usedTokens = firstResponse.length;
659
- const remaining = tokens - usedTokens - 1;
660
- async function sync() {
661
- const individualIDs = await Promise.all(dbs.map((s) => s.request));
662
- const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
663
- for (const db of dbs) {
664
- const ids = await db.request;
665
- if (ids.length >= tokens) {
666
- continue;
667
- }
668
- const diff = allIDs.filter((id) => !ids.includes(id));
669
- if (diff.length === 0) {
670
- continue;
671
- }
672
- await db.redis.sadd(key, ...allIDs);
673
- }
674
- }
675
- const success = remaining > 0;
676
- const reset = (bucket + 1) * windowDuration;
677
- if (ctx.cache && !success) {
678
- ctx.cache.blockUntil(identifier, reset);
679
- }
680
- return {
681
- success,
682
- limit: tokens,
683
- remaining,
684
- reset,
685
- pending: sync()
686
- };
687
- };
688
- }
689
- /**
690
- * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
691
- * costs than `slidingLogs` and improved boundary behavior by calcualting a
692
- * weighted score between two windows.
693
- *
694
- * **Pro:**
695
- *
696
- * Good performance allows this to scale to very high loads.
697
- *
698
- * **Con:**
699
- *
700
- * Nothing major.
701
- *
702
- * @param tokens - How many requests a user can make in each time window.
703
- * @param window - The duration in which the user can max X requests.
704
- */
705
- static slidingWindow(tokens, window) {
706
- const windowSize = ms(window);
707
- const script = `
708
- local currentKey = KEYS[1] -- identifier including prefixes
709
- local previousKey = KEYS[2] -- key of the previous bucket
710
- local tokens = tonumber(ARGV[1]) -- tokens per window
711
- local now = ARGV[2] -- current timestamp in milliseconds
712
- local window = ARGV[3] -- interval in milliseconds
713
- local requestID = ARGV[4] -- uuid for this request
714
-
715
-
716
- local currentMembers = redis.call("SMEMBERS", currentKey)
717
- local requestsInCurrentWindow = #currentMembers
718
- local previousMembers = redis.call("SMEMBERS", previousKey)
719
- local requestsInPreviousWindow = #previousMembers
720
-
721
- local percentageInCurrent = ( now % window) / window
722
- if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
723
- return {currentMembers, previousMembers}
724
- end
725
-
726
- redis.call("SADD", currentKey, requestID)
727
- table.insert(currentMembers, requestID)
728
- if requestsInCurrentWindow == 0 then
729
- -- The first time this key is set, the value will be 1.
730
- -- So we only need the expire command once
731
- redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
732
- end
733
- return {currentMembers, previousMembers}
734
- `;
735
- const windowDuration = ms(window);
736
- return async function(ctx, identifier) {
737
- if (ctx.cache) {
738
- const { blocked, reset: reset2 } = ctx.cache.isBlocked(identifier);
739
- if (blocked) {
740
- return {
741
- success: false,
742
- limit: tokens,
743
- remaining: 0,
744
- reset: reset2,
745
- pending: Promise.resolve()
746
- };
747
- }
748
- }
749
- const requestID = randomId();
750
- const now = Date.now();
751
- const currentWindow = Math.floor(now / windowSize);
752
- const currentKey = [identifier, currentWindow].join(":");
753
- const previousWindow = currentWindow - windowSize;
754
- const previousKey = [identifier, previousWindow].join(":");
755
- const dbs = ctx.redis.map((redis) => ({
756
- redis,
757
- request: redis.eval(script, [currentKey, previousKey], [tokens, now, windowDuration, requestID])
758
- }));
759
- const percentageInCurrent = now % windowDuration / windowDuration;
760
- const [current, previous] = await Promise.any(dbs.map((s) => s.request));
761
- const usedTokens = previous.length * (1 - percentageInCurrent) + current.length;
762
- const remaining = tokens - usedTokens;
763
- async function sync() {
764
- const [individualIDs] = await Promise.all(dbs.map((s) => s.request));
765
- const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
766
- for (const db of dbs) {
767
- const [ids] = await db.request;
768
- if (ids.length >= tokens) {
769
- continue;
770
- }
771
- const diff = allIDs.filter((id) => !ids.includes(id));
772
- if (diff.length === 0) {
773
- continue;
774
- }
775
- await db.redis.sadd(currentKey, ...allIDs);
776
- }
777
- }
778
- const success = remaining > 0;
779
- const reset = (currentWindow + 1) * windowDuration;
780
- if (ctx.cache && !success) {
781
- ctx.cache.blockUntil(identifier, reset);
782
- }
783
- return {
784
- success,
785
- limit: tokens,
786
- remaining,
787
- reset,
788
- pending: sync()
789
- };
790
- };
791
- }
792
- };
793
794
  export {
794
795
  Analytics,
795
796
  MultiRegionRatelimit,