-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathratelimit.ts
410 lines (368 loc) · 12.4 KB
/
ratelimit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { Analytics } from "./analytics";
import { Cache } from "./cache";
import type { Algorithm, Context, LimitOptions, LimitPayload, RatelimitResponse, Redis } from "./types";
import { checkDenyList, checkDenyListCache, defaultDeniedResponse, resolveLimitPayload } from "./deny-list/index";
export class TimeoutError extends Error {
constructor() {
super("Timeout");
this.name = "TimeoutError";
}
}
export type RatelimitConfig<TContext> = {
/**
* The ratelimiter function to use.
*
* Choose one of the predefined ones or implement your own.
* Available algorithms are exposed via static methods:
* - Ratelimiter.fixedWindow
* - Ratelimiter.slidingWindow
* - Ratelimiter.tokenBucket
*/
limiter: Algorithm<TContext>;
ctx: TContext;
/**
* All keys in redis are prefixed with this.
*
* @default `@upstash/ratelimit`
*/
prefix?: string;
/**
* If enabled, the ratelimiter will keep a global cache of identifiers, that have
* exhausted their ratelimit. In serverless environments this is only possible if
* you create the ratelimiter instance outside of your handler function. While the
* function is still hot, the ratelimiter can block requests without having to
* request data from redis, thus saving time and money.
*
* Whenever an identifier has exceeded its limit, the ratelimiter will add it to an
* internal list together with its reset timestamp. If the same identifier makes a
* new request before it is reset, we can immediately reject it.
*
* Set to `false` to disable.
*
* If left undefined, a map is created automatically, but it can only work
* if the map or the ratelimit instance is created outside your serverless function handler.
*/
ephemeralCache?: Map<string, number> | false;
/**
* If set, the ratelimiter will allow requests to pass after this many milliseconds.
*
* Use this if you want to allow requests in case of network problems
*
* @default 5000
*/
timeout?: number;
/**
* If enabled, the ratelimiter will store analytics data in redis, which you can check out at
* https://console.upstash.com/ratelimit
*
* @default false
*/
analytics?: boolean;
/**
* Enables deny list. If set to true, requests with identifier or ip/user-agent/countrie
* in the deny list will be rejected automatically. To edit the deny list, check out the
* ratelimit dashboard at https://console.upstash.com/ratelimit
*
* @default false
*/
enableProtection?: boolean
denyListThreshold?: number
};
/**
* Ratelimiter using serverless redis from https://upstash.com/
*
* @example
* ```ts
* const { limit } = new Ratelimit({
* redis: Redis.fromEnv(),
* limiter: Ratelimit.slidingWindow(
* 10, // Allow 10 requests per window of 30 minutes
* "30 m", // interval of 30 minutes
* ),
* })
*
* ```
*/
export abstract class Ratelimit<TContext extends Context> {
protected readonly limiter: Algorithm<TContext>;
protected readonly ctx: TContext;
protected readonly prefix: string;
protected readonly timeout: number;
protected readonly primaryRedis: Redis;
protected readonly analytics?: Analytics;
protected readonly enableProtection: boolean;
protected readonly denyListThreshold: number
constructor(config: RatelimitConfig<TContext>) {
this.ctx = config.ctx;
this.limiter = config.limiter;
this.timeout = config.timeout ?? 5000;
this.prefix = config.prefix ?? "@upstash/ratelimit";
this.enableProtection = config.enableProtection ?? false;
this.denyListThreshold = config.denyListThreshold ?? 6;
this.primaryRedis = ("redis" in this.ctx) ? this.ctx.redis : this.ctx.regionContexts[0].redis
this.analytics = config.analytics
? new Analytics({
redis: this.primaryRedis,
prefix: this.prefix,
})
: undefined;
if (config.ephemeralCache instanceof Map) {
this.ctx.cache = new Cache(config.ephemeralCache);
} else if (config.ephemeralCache === undefined) {
this.ctx.cache = new Cache(new Map());
}
}
/**
* Determine if a request should pass or be rejected based on the identifier and previously chosen ratelimit.
*
* Use this if you want to reject all requests that you can not handle right now.
*
* @example
* ```ts
* const ratelimit = new Ratelimit({
* redis: Redis.fromEnv(),
* limiter: Ratelimit.slidingWindow(10, "10 s")
* })
*
* const { success } = await ratelimit.limit(id)
* if (!success){
* return "Nope"
* }
* return "Yes"
* ```
*
* @param req.rate - The rate at which tokens will be added or consumed from the token bucket. A higher rate allows for more requests to be processed. Defaults to 1 token per interval if not specified.
*
* Usage with `req.rate`
* @example
* ```ts
* const ratelimit = new Ratelimit({
* redis: Redis.fromEnv(),
* limiter: Ratelimit.slidingWindow(100, "10 s")
* })
*
* const { success } = await ratelimit.limit(id, {rate: 10})
* if (!success){
* return "Nope"
* }
* return "Yes"
* ```
*/
public limit = async (
identifier: string,
req?: LimitOptions,
): Promise<RatelimitResponse> => {
let timeoutId: any = null;
try {
const response = this.getRatelimitResponse(identifier, req);
const { responseArray, newTimeoutId } = this.applyTimeout(response);
timeoutId = newTimeoutId;
const timedResponse = await Promise.race(responseArray);
const finalResponse = this.submitAnalytics(timedResponse, identifier, req);
return finalResponse;
} finally {
if (timeoutId) {
clearTimeout(timeoutId);
}
}
};
/**
* Block until the request may pass or timeout is reached.
*
* This method returns a promise that resolves as soon as the request may be processed
* or after the timeout has been reached.
*
* Use this if you want to delay the request until it is ready to get processed.
*
* @example
* ```ts
* const ratelimit = new Ratelimit({
* redis: Redis.fromEnv(),
* limiter: Ratelimit.slidingWindow(10, "10 s")
* })
*
* const { success } = await ratelimit.blockUntilReady(id, 60_000)
* if (!success){
* return "Nope"
* }
* return "Yes"
* ```
*/
public blockUntilReady = async (
/**
* An identifier per user or api.
* Choose a userID, or api token, or ip address.
*
* If you want to limit your api across all users, you can set a constant string.
*/
identifier: string,
/**
* Maximum duration to wait in milliseconds.
* After this time the request will be denied.
*/
timeout: number,
): Promise<RatelimitResponse> => {
if (timeout <= 0) {
throw new Error("timeout must be positive");
}
let res: RatelimitResponse;
const deadline = Date.now() + timeout;
while (true) {
res = await this.limit(identifier);
if (res.success) {
break;
}
if (res.reset === 0) {
throw new Error("This should not happen");
}
const wait = Math.min(res.reset, deadline) - Date.now();
await new Promise((r) => setTimeout(r, wait));
if (Date.now() > deadline) {
break;
}
}
return res!;
};
public resetUsedTokens = async (identifier: string) => {
const pattern = [this.prefix, identifier].join(":");
await this.limiter().resetTokens(this.ctx, pattern);
};
/**
* Returns the remaining token count together with a reset timestamps
*
* @param identifier identifir to check
* @returns object with `remaining` and reset fields. `remaining` denotes
* the remaining tokens and reset denotes the timestamp when the
* tokens reset.
*/
public getRemaining = async (identifier: string): Promise<{
remaining: number;
reset: number;
}> => {
const pattern = [this.prefix, identifier].join(":");
return await this.limiter().getRemaining(this.ctx, pattern);
};
/**
* Checks if the identifier or the values in req are in the deny list cache.
* If so, returns the default denied response.
*
* Otherwise, calls redis to check the rate limit and deny list. Returns after
* resolving the result. Resolving is overriding the rate limit result if
* the some value is in deny list.
*
* @param identifier identifier to block
* @param req options with ip, user agent, country, rate and geo info
* @returns rate limit response
*/
private getRatelimitResponse = async (
identifier: string,
req?: LimitOptions
): Promise<RatelimitResponse> => {
const key = this.getKey(identifier);
const definedMembers = this.getDefinedMembers(identifier, req);
const deniedValue = checkDenyListCache(definedMembers)
const result: LimitPayload = deniedValue ? [defaultDeniedResponse(deniedValue), { deniedValue, invalidIpDenyList: false }] : (await Promise.all([
this.limiter().limit(this.ctx, key, req?.rate),
this.enableProtection
? checkDenyList(this.primaryRedis, this.prefix, definedMembers)
: { deniedValue: undefined, invalidIpDenyList: false }
]));
return resolveLimitPayload(this.primaryRedis, this.prefix, result, this.denyListThreshold)
};
/**
* Creates an array with the original response promise and a timeout promise
* if this.timeout > 0.
*
* @param response Ratelimit response promise
* @returns array with the response and timeout promise. also includes the timeout id
*/
private applyTimeout = (response: Promise<RatelimitResponse>) => {
let newTimeoutId: any = null;
const responseArray: Array<Promise<RatelimitResponse>> = [response];
if (this.timeout > 0) {
const timeoutResponse = new Promise<RatelimitResponse>((resolve) => {
newTimeoutId = setTimeout(() => {
resolve({
success: true,
limit: 0,
remaining: 0,
reset: 0,
pending: Promise.resolve(),
reason: "timeout"
});
}, this.timeout);
})
responseArray.push(timeoutResponse);
}
return {
responseArray,
newTimeoutId,
}
}
/**
* submits analytics if this.analytics is set
*
* @param ratelimitResponse final rate limit response
* @param identifier identifier to submit
* @param req limit options
* @returns rate limit response after updating the .pending field
*/
private submitAnalytics = (
ratelimitResponse: RatelimitResponse,
identifier: string,
req?: Pick<LimitOptions, "geo">,
) => {
if (this.analytics) {
try {
const geo = req ? this.analytics.extractGeo(req) : undefined;
const analyticsP = this.analytics
.record({
identifier: ratelimitResponse.reason === "denyList" // if in denyList, use denied value as identifier
? ratelimitResponse.deniedValue!
: identifier,
time: Date.now(),
success: ratelimitResponse.reason === "denyList" // if in denyList, label success as "denied"
? "denied"
: ratelimitResponse.success,
...geo,
})
.catch((error) => {
let errorMessage = "Failed to record analytics"
if (`${error}`.includes("WRONGTYPE")) {
errorMessage = `
Failed to record analytics. See the information below:
This can occur when you uprade to Ratelimit version 1.1.2
or later from an earlier version.
This occurs simply because the way we store analytics data
has changed. To avoid getting this error, disable analytics
for *an hour*, then simply enable it back.\n
`
}
console.warn(errorMessage, error);
});
ratelimitResponse.pending = Promise.all([ratelimitResponse.pending, analyticsP]);
} catch (error) {
console.warn("Failed to record analytics", error);
};
};
return ratelimitResponse;
}
private getKey = (identifier: string): string => {
return [this.prefix, identifier].join(":");
}
/**
* returns a list of defined values from
* [identifier, req.ip, req.userAgent, req.country]
*
* @param identifier identifier
* @param req limit options
* @returns list of defined values
*/
private getDefinedMembers = (
identifier: string,
req?: Pick<LimitOptions, "ip" | "userAgent" | "country">
): string[] => {
const members = [identifier, req?.ip, req?.userAgent, req?.country];
return (members as string[]).filter(Boolean);
}
}