@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/README.md
CHANGED
|
@@ -78,6 +78,8 @@ For more information on getting started, you can refer to [our documentation](ht
|
|
|
78
78
|
|
|
79
79
|
See [the documentation](https://upstash.com/docs/redis/sdks/ratelimit-ts/overview) for more information details about this package.
|
|
80
80
|
|
|
81
|
+
The npm package includes TypeScript source in `node_modules/@upstash/ratelimit/src/` and documentation in `node_modules/@upstash/ratelimit/docs/`. Start with `src/index.ts` to explore the source.
|
|
82
|
+
|
|
81
83
|
## Telemetry
|
|
82
84
|
|
|
83
85
|
The SDK reports its name and version to Upstash as a header on the requests made
|
package/dist/index.d.mts
CHANGED
|
@@ -133,6 +133,8 @@ type AnalyticsConfig = {
|
|
|
133
133
|
};
|
|
134
134
|
/**
|
|
135
135
|
* The Analytics package is experimental and can change at any time.
|
|
136
|
+
*
|
|
137
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
136
138
|
*/
|
|
137
139
|
declare class Analytics {
|
|
138
140
|
private readonly analytics;
|
|
@@ -172,7 +174,7 @@ declare class Analytics {
|
|
|
172
174
|
}>;
|
|
173
175
|
}
|
|
174
176
|
|
|
175
|
-
type Unit = "ms" | "s" | "m" | "h" | "d";
|
|
177
|
+
type Unit = "ms" | "s" | "m" | "h" | "d" | "w";
|
|
176
178
|
type Duration = `${number} ${Unit}` | `${number}${Unit}`;
|
|
177
179
|
|
|
178
180
|
type RatelimitConfig<TContext> = {
|
|
@@ -306,6 +308,8 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
306
308
|
* }
|
|
307
309
|
* return "Yes"
|
|
308
310
|
* ```
|
|
311
|
+
*
|
|
312
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
309
313
|
*/
|
|
310
314
|
limit: (identifier: string, req?: LimitOptions) => Promise<RatelimitResponse>;
|
|
311
315
|
/**
|
|
@@ -329,8 +333,13 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
329
333
|
* }
|
|
330
334
|
* return "Yes"
|
|
331
335
|
* ```
|
|
336
|
+
*
|
|
337
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
332
338
|
*/
|
|
333
339
|
blockUntilReady: (identifier: string, timeout: number) => Promise<RatelimitResponse>;
|
|
340
|
+
/**
|
|
341
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
342
|
+
*/
|
|
334
343
|
resetUsedTokens: (identifier: string) => Promise<void>;
|
|
335
344
|
/**
|
|
336
345
|
* Returns the remaining token count together with a reset timestamps
|
|
@@ -339,6 +348,8 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
339
348
|
* @returns object with `remaining`, `reset`, and `limit` fields. `remaining` denotes
|
|
340
349
|
* the remaining tokens, `limit` is the effective limit (considering dynamic
|
|
341
350
|
* limits if enabled), and `reset` denotes the timestamp when the tokens reset.
|
|
351
|
+
*
|
|
352
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
342
353
|
*/
|
|
343
354
|
getRemaining: (identifier: string) => Promise<{
|
|
344
355
|
remaining: number;
|
|
@@ -407,6 +418,7 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
407
418
|
* ```
|
|
408
419
|
*
|
|
409
420
|
* @param options.limit - The new rate limit to apply globally, or false to disable
|
|
421
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
410
422
|
*/
|
|
411
423
|
setDynamicLimit: (options: {
|
|
412
424
|
limit: number | false;
|
|
@@ -421,6 +433,7 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
421
433
|
* ```
|
|
422
434
|
*
|
|
423
435
|
* @returns Object containing the current global dynamic limit, or null if not set
|
|
436
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
424
437
|
*/
|
|
425
438
|
getDynamicLimit: () => Promise<{
|
|
426
439
|
dynamicLimit: number | null;
|
|
@@ -521,10 +534,14 @@ type MultiRegionRatelimitConfig = {
|
|
|
521
534
|
* })
|
|
522
535
|
*
|
|
523
536
|
* ```
|
|
537
|
+
*
|
|
538
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
524
539
|
*/
|
|
525
540
|
declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
526
541
|
/**
|
|
527
542
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
|
|
543
|
+
*
|
|
544
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
528
545
|
*/
|
|
529
546
|
constructor(config: MultiRegionRatelimitConfig);
|
|
530
547
|
/**
|
|
@@ -544,6 +561,7 @@ declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
|
544
561
|
*
|
|
545
562
|
* @param tokens - How many requests a user can make in each time window.
|
|
546
563
|
* @param window - A fixed timeframe
|
|
564
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
547
565
|
*/
|
|
548
566
|
static fixedWindow(
|
|
549
567
|
/**
|
|
@@ -569,6 +587,7 @@ declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
|
569
587
|
*
|
|
570
588
|
* @param tokens - How many requests a user can make in each time window.
|
|
571
589
|
* @param window - The duration in which the user can max X requests.
|
|
590
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
572
591
|
*/
|
|
573
592
|
static slidingWindow(
|
|
574
593
|
/**
|
|
@@ -689,10 +708,14 @@ type RegionRatelimitConfig = {
|
|
|
689
708
|
* })
|
|
690
709
|
*
|
|
691
710
|
* ```
|
|
711
|
+
*
|
|
712
|
+
* @see node_modules/@upstash/ratelimit/docs/gettingstarted.mdx
|
|
692
713
|
*/
|
|
693
714
|
declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
694
715
|
/**
|
|
695
716
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithm of your choice.
|
|
717
|
+
*
|
|
718
|
+
* @see node_modules/@upstash/ratelimit/docs/gettingstarted.mdx
|
|
696
719
|
*/
|
|
697
720
|
constructor(config: RegionRatelimitConfig);
|
|
698
721
|
/**
|
|
@@ -712,6 +735,7 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
712
735
|
*
|
|
713
736
|
* @param tokens - How many requests a user can make in each time window.
|
|
714
737
|
* @param window - A fixed timeframe
|
|
738
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
715
739
|
*/
|
|
716
740
|
static fixedWindow(
|
|
717
741
|
/**
|
|
@@ -737,6 +761,7 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
737
761
|
*
|
|
738
762
|
* @param tokens - How many requests a user can make in each time window.
|
|
739
763
|
* @param window - The duration in which the user can max X requests.
|
|
764
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
740
765
|
*/
|
|
741
766
|
static slidingWindow(
|
|
742
767
|
/**
|
|
@@ -759,6 +784,8 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
759
784
|
* rate.
|
|
760
785
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
761
786
|
* than `refillRate`
|
|
787
|
+
*
|
|
788
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
762
789
|
*/
|
|
763
790
|
static tokenBucket(
|
|
764
791
|
/**
|
|
@@ -800,6 +827,7 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
800
827
|
*
|
|
801
828
|
* @param tokens - How many requests a user can make in each time window.
|
|
802
829
|
* @param window - A fixed timeframe
|
|
830
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
803
831
|
*/
|
|
804
832
|
static cachedFixedWindow(
|
|
805
833
|
/**
|
|
@@ -834,6 +862,7 @@ declare class ThresholdError extends Error {
|
|
|
834
862
|
* @param ttl time to live in milliseconds for the status flag. Optional. If not
|
|
835
863
|
* passed, ttl is infferred from current time.
|
|
836
864
|
* @returns list of ips which are not in the deny list
|
|
865
|
+
* @see node_modules/@upstash/ratelimit/docs/traffic-protection.mdx
|
|
837
866
|
*/
|
|
838
867
|
declare const updateIpDenyList: (redis: Redis$1, prefix: string, threshold: number, ttl?: number) => Promise<unknown[]>;
|
|
839
868
|
/**
|
|
@@ -844,6 +873,7 @@ declare const updateIpDenyList: (redis: Redis$1, prefix: string, threshold: numb
|
|
|
844
873
|
* @param redis redis instance
|
|
845
874
|
* @param prefix ratelimit prefix
|
|
846
875
|
* @returns
|
|
876
|
+
* @see node_modules/@upstash/ratelimit/docs/traffic-protection.mdx
|
|
847
877
|
*/
|
|
848
878
|
declare const disableIpDenyList: (redis: Redis$1, prefix: string) => Promise<unknown[]>;
|
|
849
879
|
|
package/dist/index.d.ts
CHANGED
|
@@ -133,6 +133,8 @@ type AnalyticsConfig = {
|
|
|
133
133
|
};
|
|
134
134
|
/**
|
|
135
135
|
* The Analytics package is experimental and can change at any time.
|
|
136
|
+
*
|
|
137
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
136
138
|
*/
|
|
137
139
|
declare class Analytics {
|
|
138
140
|
private readonly analytics;
|
|
@@ -172,7 +174,7 @@ declare class Analytics {
|
|
|
172
174
|
}>;
|
|
173
175
|
}
|
|
174
176
|
|
|
175
|
-
type Unit = "ms" | "s" | "m" | "h" | "d";
|
|
177
|
+
type Unit = "ms" | "s" | "m" | "h" | "d" | "w";
|
|
176
178
|
type Duration = `${number} ${Unit}` | `${number}${Unit}`;
|
|
177
179
|
|
|
178
180
|
type RatelimitConfig<TContext> = {
|
|
@@ -306,6 +308,8 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
306
308
|
* }
|
|
307
309
|
* return "Yes"
|
|
308
310
|
* ```
|
|
311
|
+
*
|
|
312
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
309
313
|
*/
|
|
310
314
|
limit: (identifier: string, req?: LimitOptions) => Promise<RatelimitResponse>;
|
|
311
315
|
/**
|
|
@@ -329,8 +333,13 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
329
333
|
* }
|
|
330
334
|
* return "Yes"
|
|
331
335
|
* ```
|
|
336
|
+
*
|
|
337
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
332
338
|
*/
|
|
333
339
|
blockUntilReady: (identifier: string, timeout: number) => Promise<RatelimitResponse>;
|
|
340
|
+
/**
|
|
341
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
342
|
+
*/
|
|
334
343
|
resetUsedTokens: (identifier: string) => Promise<void>;
|
|
335
344
|
/**
|
|
336
345
|
* Returns the remaining token count together with a reset timestamps
|
|
@@ -339,6 +348,8 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
339
348
|
* @returns object with `remaining`, `reset`, and `limit` fields. `remaining` denotes
|
|
340
349
|
* the remaining tokens, `limit` is the effective limit (considering dynamic
|
|
341
350
|
* limits if enabled), and `reset` denotes the timestamp when the tokens reset.
|
|
351
|
+
*
|
|
352
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
342
353
|
*/
|
|
343
354
|
getRemaining: (identifier: string) => Promise<{
|
|
344
355
|
remaining: number;
|
|
@@ -407,6 +418,7 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
407
418
|
* ```
|
|
408
419
|
*
|
|
409
420
|
* @param options.limit - The new rate limit to apply globally, or false to disable
|
|
421
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
410
422
|
*/
|
|
411
423
|
setDynamicLimit: (options: {
|
|
412
424
|
limit: number | false;
|
|
@@ -421,6 +433,7 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
421
433
|
* ```
|
|
422
434
|
*
|
|
423
435
|
* @returns Object containing the current global dynamic limit, or null if not set
|
|
436
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
424
437
|
*/
|
|
425
438
|
getDynamicLimit: () => Promise<{
|
|
426
439
|
dynamicLimit: number | null;
|
|
@@ -521,10 +534,14 @@ type MultiRegionRatelimitConfig = {
|
|
|
521
534
|
* })
|
|
522
535
|
*
|
|
523
536
|
* ```
|
|
537
|
+
*
|
|
538
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
524
539
|
*/
|
|
525
540
|
declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
526
541
|
/**
|
|
527
542
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
|
|
543
|
+
*
|
|
544
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
528
545
|
*/
|
|
529
546
|
constructor(config: MultiRegionRatelimitConfig);
|
|
530
547
|
/**
|
|
@@ -544,6 +561,7 @@ declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
|
544
561
|
*
|
|
545
562
|
* @param tokens - How many requests a user can make in each time window.
|
|
546
563
|
* @param window - A fixed timeframe
|
|
564
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
547
565
|
*/
|
|
548
566
|
static fixedWindow(
|
|
549
567
|
/**
|
|
@@ -569,6 +587,7 @@ declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
|
569
587
|
*
|
|
570
588
|
* @param tokens - How many requests a user can make in each time window.
|
|
571
589
|
* @param window - The duration in which the user can max X requests.
|
|
590
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
572
591
|
*/
|
|
573
592
|
static slidingWindow(
|
|
574
593
|
/**
|
|
@@ -689,10 +708,14 @@ type RegionRatelimitConfig = {
|
|
|
689
708
|
* })
|
|
690
709
|
*
|
|
691
710
|
* ```
|
|
711
|
+
*
|
|
712
|
+
* @see node_modules/@upstash/ratelimit/docs/gettingstarted.mdx
|
|
692
713
|
*/
|
|
693
714
|
declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
694
715
|
/**
|
|
695
716
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithm of your choice.
|
|
717
|
+
*
|
|
718
|
+
* @see node_modules/@upstash/ratelimit/docs/gettingstarted.mdx
|
|
696
719
|
*/
|
|
697
720
|
constructor(config: RegionRatelimitConfig);
|
|
698
721
|
/**
|
|
@@ -712,6 +735,7 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
712
735
|
*
|
|
713
736
|
* @param tokens - How many requests a user can make in each time window.
|
|
714
737
|
* @param window - A fixed timeframe
|
|
738
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
715
739
|
*/
|
|
716
740
|
static fixedWindow(
|
|
717
741
|
/**
|
|
@@ -737,6 +761,7 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
737
761
|
*
|
|
738
762
|
* @param tokens - How many requests a user can make in each time window.
|
|
739
763
|
* @param window - The duration in which the user can max X requests.
|
|
764
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
740
765
|
*/
|
|
741
766
|
static slidingWindow(
|
|
742
767
|
/**
|
|
@@ -759,6 +784,8 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
759
784
|
* rate.
|
|
760
785
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
761
786
|
* than `refillRate`
|
|
787
|
+
*
|
|
788
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
762
789
|
*/
|
|
763
790
|
static tokenBucket(
|
|
764
791
|
/**
|
|
@@ -800,6 +827,7 @@ declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
800
827
|
*
|
|
801
828
|
* @param tokens - How many requests a user can make in each time window.
|
|
802
829
|
* @param window - A fixed timeframe
|
|
830
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
803
831
|
*/
|
|
804
832
|
static cachedFixedWindow(
|
|
805
833
|
/**
|
|
@@ -834,6 +862,7 @@ declare class ThresholdError extends Error {
|
|
|
834
862
|
* @param ttl time to live in milliseconds for the status flag. Optional. If not
|
|
835
863
|
* passed, ttl is infferred from current time.
|
|
836
864
|
* @returns list of ips which are not in the deny list
|
|
865
|
+
* @see node_modules/@upstash/ratelimit/docs/traffic-protection.mdx
|
|
837
866
|
*/
|
|
838
867
|
declare const updateIpDenyList: (redis: Redis$1, prefix: string, threshold: number, ttl?: number) => Promise<unknown[]>;
|
|
839
868
|
/**
|
|
@@ -844,6 +873,7 @@ declare const updateIpDenyList: (redis: Redis$1, prefix: string, threshold: numb
|
|
|
844
873
|
* @param redis redis instance
|
|
845
874
|
* @param prefix ratelimit prefix
|
|
846
875
|
* @returns
|
|
876
|
+
* @see node_modules/@upstash/ratelimit/docs/traffic-protection.mdx
|
|
847
877
|
*/
|
|
848
878
|
declare const disableIpDenyList: (redis: Redis$1, prefix: string) => Promise<unknown[]>;
|
|
849
879
|
|
package/dist/index.js
CHANGED
|
@@ -137,33 +137,30 @@ var DYNAMIC_LIMIT_KEY_SUFFIX = ":dynamic:global";
|
|
|
137
137
|
var DEFAULT_PREFIX = "@upstash/ratelimit";
|
|
138
138
|
|
|
139
139
|
// src/duration.ts
|
|
140
|
+
var MULTIPLIERS = {
|
|
141
|
+
ms: 1,
|
|
142
|
+
s: 1e3,
|
|
143
|
+
m: 1e3 * 60,
|
|
144
|
+
h: 1e3 * 60 * 60,
|
|
145
|
+
d: 1e3 * 60 * 60 * 24,
|
|
146
|
+
w: 1e3 * 60 * 60 * 24 * 7
|
|
147
|
+
};
|
|
140
148
|
function ms(d) {
|
|
141
|
-
const match = d.match(/^(\d+)\s?(ms|s|m|h|d)$/);
|
|
149
|
+
const match = d.match(/^(\d+(?:\.\d+)?)\s?(ms|s|m|h|d|w)$/);
|
|
142
150
|
if (!match) {
|
|
143
151
|
throw new Error(`Unable to parse window size: ${d}`);
|
|
144
152
|
}
|
|
145
|
-
const time = Number.
|
|
153
|
+
const time = Number.parseFloat(match[1]);
|
|
146
154
|
const unit = match[2];
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
case "m": {
|
|
155
|
-
return time * 1e3 * 60;
|
|
156
|
-
}
|
|
157
|
-
case "h": {
|
|
158
|
-
return time * 1e3 * 60 * 60;
|
|
159
|
-
}
|
|
160
|
-
case "d": {
|
|
161
|
-
return time * 1e3 * 60 * 60 * 24;
|
|
162
|
-
}
|
|
163
|
-
default: {
|
|
164
|
-
throw new Error(`Unable to parse window size: ${d}`);
|
|
165
|
-
}
|
|
155
|
+
const multiplier = MULTIPLIERS[unit];
|
|
156
|
+
if (multiplier === void 0) {
|
|
157
|
+
throw new Error(`Unable to parse window size: ${d}`);
|
|
158
|
+
}
|
|
159
|
+
const result = Math.round(time * multiplier);
|
|
160
|
+
if (result < 1) {
|
|
161
|
+
throw new Error(`Window size must be at least 1 ms, got: ${d}`);
|
|
166
162
|
}
|
|
163
|
+
return result;
|
|
167
164
|
}
|
|
168
165
|
|
|
169
166
|
// src/hash.ts
|
|
@@ -342,8 +339,12 @@ var tokenBucketLimitScript = `#!lua flags=allow-key-locking
|
|
|
342
339
|
refilledAt = refilledAt + numRefills * interval
|
|
343
340
|
end
|
|
344
341
|
|
|
345
|
-
-- Only reject
|
|
346
|
-
|
|
342
|
+
-- Only reject when consuming (not refunding) and there are not enough tokens
|
|
343
|
+
-- for this request. Checking tokens == 0 only covered incrementBy == 1: with
|
|
344
|
+
-- a larger rate (e.g. 2 tokens left, incrementBy 5) the request fell through,
|
|
345
|
+
-- drove tokens negative and still wrote it back, so a rejected request
|
|
346
|
+
-- consumed tokens and a single large rate could lock the identifier out.
|
|
347
|
+
if incrementBy > 0 and tokens < incrementBy then
|
|
347
348
|
return {-1, refilledAt + interval, effectiveLimit}
|
|
348
349
|
end
|
|
349
350
|
|
|
@@ -542,7 +543,7 @@ var SCRIPTS = {
|
|
|
542
543
|
tokenBucket: {
|
|
543
544
|
limit: {
|
|
544
545
|
script: tokenBucketLimitScript,
|
|
545
|
-
hash: "
|
|
546
|
+
hash: "d60be5bd3d83482ae229309f1f7425424f891204"
|
|
546
547
|
},
|
|
547
548
|
getRemaining: {
|
|
548
549
|
script: tokenBucketRemainingTokensScript,
|
|
@@ -818,6 +819,8 @@ var Ratelimit = class {
|
|
|
818
819
|
* }
|
|
819
820
|
* return "Yes"
|
|
820
821
|
* ```
|
|
822
|
+
*
|
|
823
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
821
824
|
*/
|
|
822
825
|
limit = async (identifier, req) => {
|
|
823
826
|
let timeoutId = null;
|
|
@@ -855,6 +858,8 @@ var Ratelimit = class {
|
|
|
855
858
|
* }
|
|
856
859
|
* return "Yes"
|
|
857
860
|
* ```
|
|
861
|
+
*
|
|
862
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
858
863
|
*/
|
|
859
864
|
blockUntilReady = async (identifier, timeout) => {
|
|
860
865
|
if (timeout <= 0) {
|
|
@@ -871,13 +876,16 @@ var Ratelimit = class {
|
|
|
871
876
|
throw new Error("This should not happen");
|
|
872
877
|
}
|
|
873
878
|
const wait = Math.min(res.reset, deadline) - Date.now();
|
|
874
|
-
await new Promise((r) => setTimeout(r, wait));
|
|
879
|
+
await new Promise((r) => setTimeout(r, wait > 0 ? wait : 100));
|
|
875
880
|
if (Date.now() > deadline) {
|
|
876
881
|
break;
|
|
877
882
|
}
|
|
878
883
|
}
|
|
879
884
|
return res;
|
|
880
885
|
};
|
|
886
|
+
/**
|
|
887
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
888
|
+
*/
|
|
881
889
|
resetUsedTokens = async (identifier) => {
|
|
882
890
|
const pattern = [this.prefix, identifier].join(":");
|
|
883
891
|
await this.limiter().resetTokens(this.ctx, pattern);
|
|
@@ -889,6 +897,8 @@ var Ratelimit = class {
|
|
|
889
897
|
* @returns object with `remaining`, `reset`, and `limit` fields. `remaining` denotes
|
|
890
898
|
* the remaining tokens, `limit` is the effective limit (considering dynamic
|
|
891
899
|
* limits if enabled), and `reset` denotes the timestamp when the tokens reset.
|
|
900
|
+
*
|
|
901
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
892
902
|
*/
|
|
893
903
|
getRemaining = async (identifier) => {
|
|
894
904
|
const pattern = [this.prefix, identifier].join(":");
|
|
@@ -1026,6 +1036,7 @@ var Ratelimit = class {
|
|
|
1026
1036
|
* ```
|
|
1027
1037
|
*
|
|
1028
1038
|
* @param options.limit - The new rate limit to apply globally, or false to disable
|
|
1039
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
1029
1040
|
*/
|
|
1030
1041
|
setDynamicLimit = async (options) => {
|
|
1031
1042
|
if (!this.dynamicLimits) {
|
|
@@ -1046,6 +1057,7 @@ var Ratelimit = class {
|
|
|
1046
1057
|
* ```
|
|
1047
1058
|
*
|
|
1048
1059
|
* @returns Object containing the current global dynamic limit, or null if not set
|
|
1060
|
+
* @see node_modules/@upstash/ratelimit/docs/methods.mdx
|
|
1049
1061
|
*/
|
|
1050
1062
|
getDynamicLimit = async () => {
|
|
1051
1063
|
if (!this.dynamicLimits) {
|
|
@@ -1060,7 +1072,7 @@ var Ratelimit = class {
|
|
|
1060
1072
|
};
|
|
1061
1073
|
|
|
1062
1074
|
// src/version.ts
|
|
1063
|
-
var VERSION = "2.1.0";
|
|
1075
|
+
var VERSION = "2.2.1-canary.0";
|
|
1064
1076
|
|
|
1065
1077
|
// src/telemetry.ts
|
|
1066
1078
|
var taggedClients = /* @__PURE__ */ new WeakSet();
|
|
@@ -1093,6 +1105,8 @@ function randomId() {
|
|
|
1093
1105
|
var MultiRegionRatelimit = class extends Ratelimit {
|
|
1094
1106
|
/**
|
|
1095
1107
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
|
|
1108
|
+
*
|
|
1109
|
+
* @see node_modules/@upstash/ratelimit/docs/features.mdx
|
|
1096
1110
|
*/
|
|
1097
1111
|
constructor(config) {
|
|
1098
1112
|
super({
|
|
@@ -1135,6 +1149,7 @@ var MultiRegionRatelimit = class extends Ratelimit {
|
|
|
1135
1149
|
*
|
|
1136
1150
|
* @param tokens - How many requests a user can make in each time window.
|
|
1137
1151
|
* @param window - A fixed timeframe
|
|
1152
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1138
1153
|
*/
|
|
1139
1154
|
static fixedWindow(tokens, window) {
|
|
1140
1155
|
const windowDuration = ms(window);
|
|
@@ -1298,6 +1313,7 @@ var MultiRegionRatelimit = class extends Ratelimit {
|
|
|
1298
1313
|
*
|
|
1299
1314
|
* @param tokens - How many requests a user can make in each time window.
|
|
1300
1315
|
* @param window - The duration in which the user can max X requests.
|
|
1316
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1301
1317
|
*/
|
|
1302
1318
|
static slidingWindow(tokens, window) {
|
|
1303
1319
|
const windowSize = ms(window);
|
|
@@ -1466,6 +1482,8 @@ var MultiRegionRatelimit = class extends Ratelimit {
|
|
|
1466
1482
|
var RegionRatelimit = class extends Ratelimit {
|
|
1467
1483
|
/**
|
|
1468
1484
|
* Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithm of your choice.
|
|
1485
|
+
*
|
|
1486
|
+
* @see node_modules/@upstash/ratelimit/docs/gettingstarted.mdx
|
|
1469
1487
|
*/
|
|
1470
1488
|
constructor(config) {
|
|
1471
1489
|
super({
|
|
@@ -1501,6 +1519,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1501
1519
|
*
|
|
1502
1520
|
* @param tokens - How many requests a user can make in each time window.
|
|
1503
1521
|
* @param window - A fixed timeframe
|
|
1522
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1504
1523
|
*/
|
|
1505
1524
|
static fixedWindow(tokens, window) {
|
|
1506
1525
|
const windowDuration = ms(window);
|
|
@@ -1592,6 +1611,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1592
1611
|
*
|
|
1593
1612
|
* @param tokens - How many requests a user can make in each time window.
|
|
1594
1613
|
* @param window - The duration in which the user can max X requests.
|
|
1614
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1595
1615
|
*/
|
|
1596
1616
|
static slidingWindow(tokens, window) {
|
|
1597
1617
|
const windowSize = ms(window);
|
|
@@ -1685,6 +1705,8 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1685
1705
|
* rate.
|
|
1686
1706
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
1687
1707
|
* than `refillRate`
|
|
1708
|
+
*
|
|
1709
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1688
1710
|
*/
|
|
1689
1711
|
static tokenBucket(refillRate, interval, maxTokens) {
|
|
1690
1712
|
const intervalDuration = ms(interval);
|
|
@@ -1781,6 +1803,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1781
1803
|
*
|
|
1782
1804
|
* @param tokens - How many requests a user can make in each time window.
|
|
1783
1805
|
* @param window - A fixed timeframe
|
|
1806
|
+
* @see node_modules/@upstash/ratelimit/docs/algorithms.mdx
|
|
1784
1807
|
*/
|
|
1785
1808
|
static cachedFixedWindow(tokens, window) {
|
|
1786
1809
|
const windowDuration = ms(window);
|
|
@@ -1801,7 +1824,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1801
1824
|
const hit = typeof ctx.cache.get(key) === "number";
|
|
1802
1825
|
if (hit) {
|
|
1803
1826
|
const cachedTokensAfterUpdate = ctx.cache.incr(key, incrementBy);
|
|
1804
|
-
const success = cachedTokensAfterUpdate
|
|
1827
|
+
const success = cachedTokensAfterUpdate <= tokens;
|
|
1805
1828
|
const pending = success ? safeEval(
|
|
1806
1829
|
ctx,
|
|
1807
1830
|
SCRIPTS.singleRegion.cachedFixedWindow.limit,
|