@upstash/ratelimit 0.2.0 → 0.3.0-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/script/single.js DELETED
@@ -1,379 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RegionRatelimit = void 0;
4
- const duration_js_1 = require("./duration.js");
5
- const ratelimit_js_1 = require("./ratelimit.js");
6
- /**
7
- * Ratelimiter using serverless redis from https://upstash.com/
8
- *
9
- * @example
10
- * ```ts
11
- * const { limit } = new Ratelimit({
12
- * redis: Redis.fromEnv(),
13
- * limiter: Ratelimit.slidingWindow(
14
- * "30 m", // interval of 30 minutes
15
- * 10, // Allow 10 requests per window of 30 minutes
16
- * )
17
- * })
18
- *
19
- * ```
20
- */
21
- class RegionRatelimit extends ratelimit_js_1.Ratelimit {
22
- /**
23
- * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
24
- */
25
- constructor(config) {
26
- super({
27
- prefix: config.prefix,
28
- limiter: config.limiter,
29
- timeout: config.timeout,
30
- ctx: {
31
- redis: config.redis,
32
- },
33
- ephemeralCache: config.ephemeralCache,
34
- });
35
- }
36
- /**
37
- * Each requests inside a fixed time increases a counter.
38
- * Once the counter reaches a maxmimum allowed number, all further requests are
39
- * rejected.
40
- *
41
- * **Pro:**
42
- *
43
- * - Newer requests are not starved by old ones.
44
- * - Low storage cost.
45
- *
46
- * **Con:**
47
- *
48
- * A burst of requests near the boundary of a window can result in a very
49
- * high request rate because two windows will be filled with requests quickly.
50
- *
51
- * @param tokens - How many requests a user can make in each time window.
52
- * @param window - A fixed timeframe
53
- */
54
- static fixedWindow(
55
- /**
56
- * How many requests are allowed per window.
57
- */
58
- tokens,
59
- /**
60
- * The duration in which `tokens` requests are allowed.
61
- */
62
- window) {
63
- const windowDuration = (0, duration_js_1.ms)(window);
64
- const script = `
65
- local key = KEYS[1]
66
- local window = ARGV[1]
67
-
68
- local r = redis.call("INCR", key)
69
- if r == 1 then
70
- -- The first time this key is set, the value will be 1.
71
- -- So we only need the expire command once
72
- redis.call("PEXPIRE", key, window)
73
- end
74
-
75
- return r
76
- `;
77
- return async function (ctx, identifier) {
78
- const bucket = Math.floor(Date.now() / windowDuration);
79
- const key = [identifier, bucket].join(":");
80
- if (ctx.cache) {
81
- const { blocked, reset } = ctx.cache.isBlocked(identifier);
82
- if (blocked) {
83
- return {
84
- success: false,
85
- limit: tokens,
86
- remaining: 0,
87
- reset: reset,
88
- pending: Promise.resolve(),
89
- };
90
- }
91
- }
92
- const usedTokensAfterUpdate = (await ctx.redis.eval(script, [key], [windowDuration]));
93
- const success = usedTokensAfterUpdate <= tokens;
94
- const reset = (bucket + 1) * windowDuration;
95
- if (ctx.cache && !success) {
96
- ctx.cache.blockUntil(identifier, reset);
97
- }
98
- return {
99
- success,
100
- limit: tokens,
101
- remaining: tokens - usedTokensAfterUpdate,
102
- reset,
103
- pending: Promise.resolve(),
104
- };
105
- };
106
- }
107
- /**
108
- * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
109
- * costs than `slidingLogs` and improved boundary behavior by calcualting a
110
- * weighted score between two windows.
111
- *
112
- * **Pro:**
113
- *
114
- * Good performance allows this to scale to very high loads.
115
- *
116
- * **Con:**
117
- *
118
- * Nothing major.
119
- *
120
- * @param tokens - How many requests a user can make in each time window.
121
- * @param window - The duration in which the user can max X requests.
122
- */
123
- static slidingWindow(
124
- /**
125
- * How many requests are allowed per window.
126
- */
127
- tokens,
128
- /**
129
- * The duration in which `tokens` requests are allowed.
130
- */
131
- window) {
132
- const script = `
133
- local currentKey = KEYS[1] -- identifier including prefixes
134
- local previousKey = KEYS[2] -- key of the previous bucket
135
- local tokens = tonumber(ARGV[1]) -- tokens per window
136
- local now = ARGV[2] -- current timestamp in milliseconds
137
- local window = ARGV[3] -- interval in milliseconds
138
-
139
- local requestsInCurrentWindow = redis.call("GET", currentKey)
140
- if requestsInCurrentWindow == false then
141
- requestsInCurrentWindow = 0
142
- end
143
-
144
-
145
- local requestsInPreviousWindow = redis.call("GET", previousKey)
146
- if requestsInPreviousWindow == false then
147
- requestsInPreviousWindow = 0
148
- end
149
- local percentageInCurrent = ( now % window) / window
150
- if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
151
- return 0
152
- end
153
-
154
- local newValue = redis.call("INCR", currentKey)
155
- if newValue == 1 then
156
- -- The first time this key is set, the value will be 1.
157
- -- So we only need the expire command once
158
- redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
159
- end
160
- return tokens - newValue
161
- `;
162
- const windowSize = (0, duration_js_1.ms)(window);
163
- return async function (ctx, identifier) {
164
- const now = Date.now();
165
- const currentWindow = Math.floor(now / windowSize);
166
- const currentKey = [identifier, currentWindow].join(":");
167
- const previousWindow = currentWindow - windowSize;
168
- const previousKey = [identifier, previousWindow].join(":");
169
- if (ctx.cache) {
170
- const { blocked, reset } = ctx.cache.isBlocked(identifier);
171
- if (blocked) {
172
- return {
173
- success: false,
174
- limit: tokens,
175
- remaining: 0,
176
- reset: reset,
177
- pending: Promise.resolve(),
178
- };
179
- }
180
- }
181
- const remaining = (await ctx.redis.eval(script, [currentKey, previousKey], [tokens, now, windowSize]));
182
- const success = remaining > 0;
183
- const reset = (currentWindow + 1) * windowSize;
184
- if (ctx.cache && !success) {
185
- ctx.cache.blockUntil(identifier, reset);
186
- }
187
- return {
188
- success,
189
- limit: tokens,
190
- remaining,
191
- reset,
192
- pending: Promise.resolve(),
193
- };
194
- };
195
- }
196
- /**
197
- * You have a bucket filled with `{maxTokens}` tokens that refills constantly
198
- * at `{refillRate}` per `{interval}`.
199
- * Every request will remove one token from the bucket and if there is no
200
- * token to take, the request is rejected.
201
- *
202
- * **Pro:**
203
- *
204
- * - Bursts of requests are smoothed out and you can process them at a constant
205
- * rate.
206
- * - Allows to set a higher initial burst limit by setting `maxTokens` higher
207
- * than `refillRate`
208
- */
209
- static tokenBucket(
210
- /**
211
- * How many tokens are refilled per `interval`
212
- *
213
- * An interval of `10s` and refillRate of 5 will cause a new token to be added every 2 seconds.
214
- */
215
- refillRate,
216
- /**
217
- * The interval for the `refillRate`
218
- */
219
- interval,
220
- /**
221
- * Maximum number of tokens.
222
- * A newly created bucket starts with this many tokens.
223
- * Useful to allow higher burst limits.
224
- */
225
- maxTokens) {
226
- const script = `
227
- local key = KEYS[1] -- identifier including prefixes
228
- local maxTokens = tonumber(ARGV[1]) -- maximum number of tokens
229
- local interval = tonumber(ARGV[2]) -- size of the window in milliseconds
230
- local refillRate = tonumber(ARGV[3]) -- how many tokens are refilled after each interval
231
- local now = tonumber(ARGV[4]) -- current timestamp in milliseconds
232
- local remaining = 0
233
-
234
- local bucket = redis.call("HMGET", key, "updatedAt", "tokens")
235
-
236
- if bucket[1] == false then
237
- -- The bucket does not exist yet, so we create it and add a ttl.
238
- remaining = maxTokens - 1
239
-
240
- redis.call("HMSET", key, "updatedAt", now, "tokens", remaining)
241
- redis.call("PEXPIRE", key, interval)
242
-
243
- return {remaining, now + interval}
244
- end
245
-
246
- -- The bucket does exist
247
-
248
- local updatedAt = tonumber(bucket[1])
249
- local tokens = tonumber(bucket[2])
250
-
251
- if now >= updatedAt + interval then
252
- remaining = math.min(maxTokens, tokens + refillRate) - 1
253
-
254
- redis.call("HMSET", key, "updatedAt", now, "tokens", remaining)
255
- return {remaining, now + interval}
256
- end
257
-
258
- if tokens > 0 then
259
- remaining = tokens - 1
260
- redis.call("HMSET", key, "updatedAt", now, "tokens", remaining)
261
- end
262
-
263
- return {remaining, updatedAt + interval}
264
- `;
265
- const intervalDuration = (0, duration_js_1.ms)(interval);
266
- return async function (ctx, identifier) {
267
- if (ctx.cache) {
268
- const { blocked, reset } = ctx.cache.isBlocked(identifier);
269
- if (blocked) {
270
- return {
271
- success: false,
272
- limit: maxTokens,
273
- remaining: 0,
274
- reset: reset,
275
- pending: Promise.resolve(),
276
- };
277
- }
278
- }
279
- const now = Date.now();
280
- const key = [identifier, Math.floor(now / intervalDuration)].join(":");
281
- const [remaining, reset] = (await ctx.redis.eval(script, [key], [maxTokens, intervalDuration, refillRate, now]));
282
- const success = remaining > 0;
283
- if (ctx.cache && !success) {
284
- ctx.cache.blockUntil(identifier, reset);
285
- }
286
- return {
287
- success,
288
- limit: maxTokens,
289
- remaining,
290
- reset,
291
- pending: Promise.resolve(),
292
- };
293
- };
294
- }
295
- /**
296
- * cachedFixedWindow first uses the local cache to decide if a request may pass and then updates
297
- * it asynchronously.
298
- * This is experimental and not yet recommended for production use.
299
- *
300
- * @experimental
301
- *
302
- * Each requests inside a fixed time increases a counter.
303
- * Once the counter reaches a maxmimum allowed number, all further requests are
304
- * rejected.
305
- *
306
- * **Pro:**
307
- *
308
- * - Newer requests are not starved by old ones.
309
- * - Low storage cost.
310
- *
311
- * **Con:**
312
- *
313
- * A burst of requests near the boundary of a window can result in a very
314
- * high request rate because two windows will be filled with requests quickly.
315
- *
316
- * @param tokens - How many requests a user can make in each time window.
317
- * @param window - A fixed timeframe
318
- */
319
- static cachedFixedWindow(
320
- /**
321
- * How many requests are allowed per window.
322
- */
323
- tokens,
324
- /**
325
- * The duration in which `tokens` requests are allowed.
326
- */
327
- window) {
328
- const windowDuration = (0, duration_js_1.ms)(window);
329
- const script = `
330
- local key = KEYS[1]
331
- local window = ARGV[1]
332
-
333
- local r = redis.call("INCR", key)
334
- if r == 1 then
335
- -- The first time this key is set, the value will be 1.
336
- -- So we only need the expire command once
337
- redis.call("PEXPIRE", key, window)
338
- end
339
-
340
- return r
341
- `;
342
- return async function (ctx, identifier) {
343
- if (!ctx.cache) {
344
- throw new Error("This algorithm requires a cache");
345
- }
346
- const bucket = Math.floor(Date.now() / windowDuration);
347
- const key = [identifier, bucket].join(":");
348
- const reset = (bucket + 1) * windowDuration;
349
- const hit = typeof ctx.cache.get(key) === "number";
350
- if (hit) {
351
- const cachedTokensAfterUpdate = ctx.cache.incr(key);
352
- const success = cachedTokensAfterUpdate < tokens;
353
- const pending = success
354
- ? ctx.redis.eval(script, [key], [windowDuration]).then((t) => {
355
- ctx.cache.set(key, t);
356
- })
357
- : Promise.resolve();
358
- return {
359
- success,
360
- limit: tokens,
361
- remaining: tokens - cachedTokensAfterUpdate,
362
- reset: reset,
363
- pending,
364
- };
365
- }
366
- const usedTokensAfterUpdate = (await ctx.redis.eval(script, [key], [windowDuration]));
367
- ctx.cache.set(key, usedTokensAfterUpdate);
368
- const remaining = tokens - usedTokensAfterUpdate;
369
- return {
370
- success: remaining >= 0,
371
- limit: tokens,
372
- remaining,
373
- reset: reset,
374
- pending: Promise.resolve(),
375
- };
376
- };
377
- }
378
- }
379
- exports.RegionRatelimit = RegionRatelimit;
package/script/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +0,0 @@
1
- export { crypto, type Crypto, type SubtleCrypto, type AlgorithmIdentifier, type Algorithm, type RsaOaepParams, type BufferSource, type AesCtrParams, type AesCbcParams, type AesGcmParams, type CryptoKey, type KeyAlgorithm, type KeyType, type KeyUsage, type EcdhKeyDeriveParams, type HkdfParams, type HashAlgorithmIdentifier, type Pbkdf2Params, type AesDerivedKeyParams, type HmacImportParams, type JsonWebKey, type RsaOtherPrimesInfo, type KeyFormat, type RsaHashedKeyGenParams, type RsaKeyGenParams, type BigInteger, type EcKeyGenParams, type NamedCurve, type CryptoKeyPair, type AesKeyGenParams, type HmacKeyGenParams, type RsaHashedImportParams, type EcKeyImportParams, type AesKeyAlgorithm, type RsaPssParams, type EcdsaParams } from "@deno/shim-crypto";
2
- export {} from "@types/node";
3
- export declare const dntGlobalThis: Omit<typeof globalThis, "crypto"> & {
4
- crypto: import("@deno/shim-crypto").Crypto;
5
- };
package/types/cache.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import { EphemeralCache } from "./types.js";
2
- export declare class Cache implements EphemeralCache {
3
- /**
4
- * Stores identifier -> reset (in milliseconds)
5
- */
6
- private readonly cache;
7
- constructor(cache: Map<string, number>);
8
- isBlocked(identifier: string): {
9
- blocked: boolean;
10
- reset: number;
11
- };
12
- blockUntil(identifier: string, reset: number): void;
13
- set(key: string, value: number): void;
14
- get(key: string): number | null;
15
- incr(key: string): number;
16
- }
@@ -1,7 +0,0 @@
1
- type Unit = "ms" | "s" | "m" | "h" | "d";
2
- export type Duration = `${number} ${Unit}`;
3
- /**
4
- * Convert a human readable duration to milliseconds
5
- */
6
- export declare function ms(d: Duration): number;
7
- export {};
package/types/mod.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export { RegionRatelimit as Ratelimit } from "./single.js";
2
- export type { RegionRatelimitConfig as RatelimitConfig } from "./single.js";
3
- export { MultiRegionRatelimit } from "./multi.js";
4
- export type { MultiRegionRatelimitConfig } from "./multi.js";
5
- export type { Algorithm } from "./types.js";
package/types/multi.d.ts DELETED
@@ -1,121 +0,0 @@
1
- import type { Duration } from "./duration.js";
2
- import type { Algorithm, MultiRegionContext } from "./types.js";
3
- import { Ratelimit } from "./ratelimit.js";
4
- import type { Redis } from "./types.js";
5
- export type MultiRegionRatelimitConfig = {
6
- /**
7
- * Instances of `@upstash/redis`
8
- * @see https://github.com/upstash/upstash-redis#quick-start
9
- */
10
- redis: Redis[];
11
- /**
12
- * The ratelimiter function to use.
13
- *
14
- * Choose one of the predefined ones or implement your own.
15
- * Available algorithms are exposed via static methods:
16
- * - MultiRegionRatelimit.fixedWindow
17
- */
18
- limiter: Algorithm<MultiRegionContext>;
19
- /**
20
- * All keys in redis are prefixed with this.
21
- *
22
- * @default `@upstash/ratelimit`
23
- */
24
- prefix?: string;
25
- /**
26
- * If enabled, the ratelimiter will keep a global cache of identifiers, that have
27
- * exhausted their ratelimit. In serverless environments this is only possible if
28
- * you create the ratelimiter instance outside of your handler function. While the
29
- * function is still hot, the ratelimiter can block requests without having to
30
- * request data from redis, thus saving time and money.
31
- *
32
- * Whenever an identifier has exceeded its limit, the ratelimiter will add it to an
33
- * internal list together with its reset timestamp. If the same identifier makes a
34
- * new request before it is reset, we can immediately reject it.
35
- *
36
- * Set to `false` to disable.
37
- *
38
- * If left undefined, a map is created automatically, but it can only work
39
- * if the map or th ratelimit instance is created outside your serverless function handler.
40
- */
41
- ephemeralCache?: Map<string, number> | false;
42
- /**
43
- * If set, the ratelimiter will allow requests to pass after this many milliseconds.
44
- *
45
- * Use this if you want to allow requests in case of network problems
46
- */
47
- timeout?: number;
48
- };
49
- /**
50
- * Ratelimiter using serverless redis from https://upstash.com/
51
- *
52
- * @example
53
- * ```ts
54
- * const { limit } = new MultiRegionRatelimit({
55
- * redis: Redis.fromEnv(),
56
- * limiter: MultiRegionRatelimit.fixedWindow(
57
- * 10, // Allow 10 requests per window of 30 minutes
58
- * "30 m", // interval of 30 minutes
59
- * )
60
- * })
61
- *
62
- * ```
63
- */
64
- export declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
65
- /**
66
- * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
67
- */
68
- constructor(config: MultiRegionRatelimitConfig);
69
- /**
70
- * Each requests inside a fixed time increases a counter.
71
- * Once the counter reaches a maxmimum allowed number, all further requests are
72
- * rejected.
73
- *
74
- * **Pro:**
75
- *
76
- * - Newer requests are not starved by old ones.
77
- * - Low storage cost.
78
- *
79
- * **Con:**
80
- *
81
- * A burst of requests near the boundary of a window can result in a very
82
- * high request rate because two windows will be filled with requests quickly.
83
- *
84
- * @param tokens - How many requests a user can make in each time window.
85
- * @param window - A fixed timeframe
86
- */
87
- static fixedWindow(
88
- /**
89
- * How many requests are allowed per window.
90
- */
91
- tokens: number,
92
- /**
93
- * The duration in which `tokens` requests are allowed.
94
- */
95
- window: Duration): Algorithm<MultiRegionContext>;
96
- /**
97
- * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
98
- * costs than `slidingLogs` and improved boundary behavior by calcualting a
99
- * weighted score between two windows.
100
- *
101
- * **Pro:**
102
- *
103
- * Good performance allows this to scale to very high loads.
104
- *
105
- * **Con:**
106
- *
107
- * Nothing major.
108
- *
109
- * @param tokens - How many requests a user can make in each time window.
110
- * @param window - The duration in which the user can max X requests.
111
- */
112
- static slidingWindow(
113
- /**
114
- * How many requests are allowed per window.
115
- */
116
- tokens: number,
117
- /**
118
- * The duration in which `tokens` requests are allowed.
119
- */
120
- window: Duration): Algorithm<MultiRegionContext>;
121
- }
@@ -1,112 +0,0 @@
1
- import type { Algorithm, Context, RatelimitResponse } from "./types.js";
2
- export declare class TimeoutError extends Error {
3
- constructor();
4
- }
5
- export type RatelimitConfig<TContext> = {
6
- /**
7
- * The ratelimiter function to use.
8
- *
9
- * Choose one of the predefined ones or implement your own.
10
- * Available algorithms are exposed via static methods:
11
- * - Ratelimiter.fixedWindow
12
- * - Ratelimiter.slidingLogs
13
- * - Ratelimiter.slidingWindow
14
- * - Ratelimiter.tokenBucket
15
- */
16
- limiter: Algorithm<TContext>;
17
- ctx: TContext;
18
- /**
19
- * All keys in redis are prefixed with this.
20
- *
21
- * @default `@upstash/ratelimit`
22
- */
23
- prefix?: string;
24
- /**
25
- * If enabled, the ratelimiter will keep a global cache of identifiers, that have
26
- * exhausted their ratelimit. In serverless environments this is only possible if
27
- * you create the ratelimiter instance outside of your handler function. While the
28
- * function is still hot, the ratelimiter can block requests without having to
29
- * request data from redis, thus saving time and money.
30
- *
31
- * Whenever an identifier has exceeded its limit, the ratelimiter will add it to an
32
- * internal list together with its reset timestamp. If the same identifier makes a
33
- * new request before it is reset, we can immediately reject it.
34
- *
35
- * Set to `false` to disable.
36
- *
37
- * If left undefined, a map is created automatically, but it can only work
38
- * if the map or the ratelimit instance is created outside your serverless function handler.
39
- */
40
- ephemeralCache?: Map<string, number> | false;
41
- /**
42
- * If set, the ratelimiter will allow requests to pass after this many milliseconds.
43
- *
44
- * Use this if you want to allow requests in case of network problems
45
- */
46
- timeout?: number;
47
- };
48
- /**
49
- * Ratelimiter using serverless redis from https://upstash.com/
50
- *
51
- * @example
52
- * ```ts
53
- * const { limit } = new Ratelimit({
54
- * redis: Redis.fromEnv(),
55
- * limiter: Ratelimit.slidingWindow(
56
- * 10, // Allow 10 requests per window of 30 minutes
57
- * "30 m", // interval of 30 minutes
58
- * ),
59
- * })
60
- *
61
- * ```
62
- */
63
- export declare abstract class Ratelimit<TContext extends Context> {
64
- protected readonly limiter: Algorithm<TContext>;
65
- protected readonly ctx: TContext;
66
- protected readonly prefix: string;
67
- protected readonly timeout?: number;
68
- constructor(config: RatelimitConfig<TContext>);
69
- /**
70
- * Determine if a request should pass or be rejected based on the identifier and previously chosen ratelimit.
71
- *
72
- * Use this if you want to reject all requests that you can not handle right now.
73
- *
74
- * @example
75
- * ```ts
76
- * const ratelimit = new Ratelimit({
77
- * redis: Redis.fromEnv(),
78
- * limiter: Ratelimit.slidingWindow(10, "10 s")
79
- * })
80
- *
81
- * const { success } = await ratelimit.limit(id)
82
- * if (!success){
83
- * return "Nope"
84
- * }
85
- * return "Yes"
86
- * ```
87
- */
88
- limit: (identifier: string) => Promise<RatelimitResponse>;
89
- /**
90
- * Block until the request may pass or timeout is reached.
91
- *
92
- * This method returns a promsie that resolves as soon as the request may be processed
93
- * or after the timeoue has been reached.
94
- *
95
- * Use this if you want to delay the request until it is ready to get processed.
96
- *
97
- * @example
98
- * ```ts
99
- * const ratelimit = new Ratelimit({
100
- * redis: Redis.fromEnv(),
101
- * limiter: Ratelimit.slidingWindow(10, "10 s")
102
- * })
103
- *
104
- * const { success } = await ratelimit.blockUntilReady(id, 60_000)
105
- * if (!success){
106
- * return "Nope"
107
- * }
108
- * return "Yes"
109
- * ```
110
- */
111
- blockUntilReady: (identifier: string, timeout: number) => Promise<RatelimitResponse>;
112
- }