express-rate-limit 6.11.2 → 7.0.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.d.cts CHANGED
@@ -1,7 +1,104 @@
1
- // Generated by dts-bundle-generator v7.0.0
1
+ // Generated by dts-bundle-generator v8.0.1
2
2
 
3
3
  import { NextFunction, Request, RequestHandler, Response } from 'express';
4
4
 
5
+ declare const validations: {
6
+ enabled: {
7
+ [key: string]: boolean;
8
+ };
9
+ disable(): void;
10
+ /**
11
+ * Checks whether the IP address is valid, and that it does not have a port
12
+ * number in it.
13
+ *
14
+ * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_invalid_ip_address.
15
+ *
16
+ * @param ip {string | undefined} - The IP address provided by Express as request.ip.
17
+ *
18
+ * @returns {void}
19
+ */
20
+ ip(ip: string | undefined): void;
21
+ /**
22
+ * Makes sure the trust proxy setting is not set to `true`.
23
+ *
24
+ * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_permissive_trust_proxy.
25
+ *
26
+ * @param request {Request} - The Express request object.
27
+ *
28
+ * @returns {void}
29
+ */
30
+ trustProxy(request: Request): void;
31
+ /**
32
+ * Makes sure the trust proxy setting is set in case the `X-Forwarded-For`
33
+ * header is present.
34
+ *
35
+ * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_unset_trust_proxy.
36
+ *
37
+ * @param request {Request} - The Express request object.
38
+ *
39
+ * @returns {void}
40
+ */
41
+ xForwardedForHeader(request: Request): void;
42
+ /**
43
+ * Ensures totalHits value from store is a positive integer.
44
+ *
45
+ * @param hits {any} - The `totalHits` returned by the store.
46
+ */
47
+ positiveHits(hits: any): void;
48
+ /**
49
+ * Ensures a given key is incremented only once per request.
50
+ *
51
+ * @param request {Request} - The Express request object.
52
+ * @param store {Store} - The store class.
53
+ * @param key {string} - The key used to store the client's hit count.
54
+ *
55
+ * @returns {void}
56
+ */
57
+ singleCount(request: Request, store: Store, key: string): void;
58
+ /**
59
+ * Warns the user that the behaviour for `max: 0` / `limit: 0` is changing in the next
60
+ * major release.
61
+ *
62
+ * @param limit {number} - The maximum number of hits per client.
63
+ *
64
+ * @returns {void}
65
+ */
66
+ limit(limit: number): void;
67
+ /**
68
+ * Warns the user that the `draft_polli_ratelimit_headers` option is deprecated
69
+ * and will be removed in the next major release.
70
+ *
71
+ * @param draft_polli_ratelimit_headers {any | undefined} - The now-deprecated setting that was used to enable standard headers.
72
+ *
73
+ * @returns {void}
74
+ */
75
+ draftPolliHeaders(draft_polli_ratelimit_headers?: any): void;
76
+ /**
77
+ * Warns the user that the `onLimitReached` option is deprecated and will be removed in the next
78
+ * major release.
79
+ *
80
+ * @param onLimitReached {any | undefined} - The maximum number of hits per client.
81
+ *
82
+ * @returns {void}
83
+ */
84
+ onLimitReached(onLimitReached?: any): void;
85
+ /**
86
+ * Warns the user when the selected headers option requires a reset time but
87
+ * the store does not provide one.
88
+ *
89
+ * @param resetTime {Date | undefined} - The timestamp when the client's hit count will be reset.
90
+ *
91
+ * @returns {void}
92
+ */
93
+ headersResetTime(resetTime?: Date): void;
94
+ /**
95
+ * Checks the options.validate setting to ensure that only recognized validations are enabled or disabled.
96
+ *
97
+ * If any unrecognized values are found, an error is logged that includes the list of supported vaidations.
98
+ */
99
+ validationsConfig(): void;
100
+ };
101
+ export type Validations = typeof validations;
5
102
  /**
6
103
  * Callback that fires when a client's hit counter is incremented.
7
104
  *
@@ -163,6 +260,15 @@ export type Store = {
163
260
  prefix?: string;
164
261
  };
165
262
  export type DraftHeadersVersion = "draft-6" | "draft-7";
263
+ /**
264
+ * Validate configuration object for enabling or disabling specific validations.
265
+ *
266
+ * The keys must also be keys in the validations object, except `enable`, `disable`,
267
+ * and `default`.
268
+ */
269
+ export type EnabledValidations = {
270
+ [key in keyof Omit<Validations, "enabled" | "disable"> | "default"]?: boolean;
271
+ };
166
272
  /**
167
273
  * The configuration options for the rate limiter.
168
274
  */
@@ -182,7 +288,7 @@ export type Options = {
182
288
  *
183
289
  * Defaults to `5`.
184
290
  */
185
- max: number | ValueDeterminingMiddleware<number>;
291
+ limit: number | ValueDeterminingMiddleware<number>;
186
292
  /**
187
293
  * The response body to send back when a client is rate limited.
188
294
  *
@@ -241,14 +347,6 @@ export type Options = {
241
347
  * By default, sends back the `statusCode` and `message` set via the options.
242
348
  */
243
349
  handler: RateLimitExceededEventHandler;
244
- /**
245
- * Express request handler that sends back a response when a client has
246
- * reached their rate limit, and will be rate limited on their next request.
247
- *
248
- * @deprecated 6.x - Please use a custom `handler` that checks the number of
249
- * hits instead.
250
- */
251
- onLimitReached: RateLimitReachedEventHandler;
252
350
  /**
253
351
  * Method (in the form of middleware) to determine whether or not this request
254
352
  * counts towards a client's quota.
@@ -271,9 +369,9 @@ export type Options = {
271
369
  */
272
370
  store: Store | LegacyStore;
273
371
  /**
274
- * Whether or not the validation checks should run.
372
+ * The list of validation checks that should run.
275
373
  */
276
- validate: boolean;
374
+ validate: boolean | EnabledValidations;
277
375
  /**
278
376
  * Whether to send `X-RateLimit-*` headers with the rate limit and the number
279
377
  * of requests.
@@ -282,12 +380,16 @@ export type Options = {
282
380
  */
283
381
  headers?: boolean;
284
382
  /**
285
- * Whether to send `RateLimit-*` headers with the rate limit and the number
286
- * of requests.
383
+ * The maximum number of connections to allow during the `window` before
384
+ * rate limiting the client.
385
+ *
386
+ * Can be the limit itself as a number or express middleware that parses
387
+ * the request and then figures out the limit.
287
388
  *
288
- * @deprecated 6.x - This option was renamed to `standardHeaders`.
389
+ * @deprecated 7.x - This option was renamed to `limit`. However, it will not
390
+ * be removed from the library in the foreseeable future.
289
391
  */
290
- draft_polli_ratelimit_headers?: boolean;
392
+ max?: number | ValueDeterminingMiddleware<number>;
291
393
  };
292
394
  /**
293
395
  * The extended request object that includes information about the client's
@@ -302,7 +404,7 @@ export type AugmentedRequest = Request & {
302
404
  */
303
405
  export type RateLimitInfo = {
304
406
  limit: number;
305
- current: number;
407
+ used: number;
306
408
  remaining: number;
307
409
  resetTime: Date | undefined;
308
410
  };
@@ -317,6 +419,16 @@ export type RateLimitInfo = {
317
419
  * @public
318
420
  */
319
421
  export declare const rateLimit: (passedOptions?: Partial<Options>) => RateLimitRequestHandler;
422
+ /**
423
+ * The record that stores information about a client - namely, how many times
424
+ * they have hit the endpoint, and when their hit count resets.
425
+ *
426
+ * Similar to `ClientRateLimitInfo`, except `resetTime` is a compulsory field.
427
+ */
428
+ export type Client = {
429
+ totalHits: number;
430
+ resetTime: Date;
431
+ };
320
432
  /**
321
433
  * A `Store` that stores the hit count for each client in memory.
322
434
  *
@@ -328,19 +440,21 @@ export declare class MemoryStore implements Store {
328
440
  */
329
441
  windowMs: number;
330
442
  /**
331
- * The map that stores the number of hits for each client in memory.
332
- */
333
- hits: {
334
- [key: string]: number | undefined;
335
- };
336
- /**
337
- * The time at which all hit counts will be reset.
443
+ * These two maps store usage (requests) and reset time by key (for example, IP
444
+ * addresses or API keys).
445
+ *
446
+ * They are split into two to avoid having to iterate through the entire set to
447
+ * determine which ones need reset. Instead, `Client`s are moved from `previous`
448
+ * to `current` as they hit the endpoint. Once `windowMs` has elapsed, all clients
449
+ * left in `previous`, i.e., those that have not made any recent requests, are
450
+ * known to be expired and can be deleted in bulk.
338
451
  */
339
- resetTime: Date;
452
+ previous: Map<string, Client>;
453
+ current: Map<string, Client>;
340
454
  /**
341
- * Reference to the active timer.
455
+ * A reference to the active timer.
342
456
  */
343
- interval?: NodeJS.Timer;
457
+ interval?: NodeJS.Timeout;
344
458
  /**
345
459
  * Confirmation that the keys incremented in once instance of MemoryStore
346
460
  * cannot affect other instances.
@@ -401,6 +515,34 @@ export declare class MemoryStore implements Store {
401
515
  * @public
402
516
  */
403
517
  shutdown(): void;
518
+ /**
519
+ * Recycles a client by setting its hit count to zero, and reset time to
520
+ * `windowMs` milliseconds from now.
521
+ *
522
+ * NOT to be confused with `#resetKey()`, which removes a client from both the
523
+ * `current` and `previous` maps.
524
+ *
525
+ * @param client {Client} - The client to recycle.
526
+ * @param now {number} - The current time, to which the `windowMs` is added to get the `resetTime` for the client.
527
+ *
528
+ * @return {Client} - The modified client that was passed in, to allow for chaining.
529
+ */
530
+ private resetClient;
531
+ /**
532
+ * Retrieves or creates a client, given a key. Also ensures that the client being
533
+ * returned is in the `current` map.
534
+ *
535
+ * @param key {string} - The key under which the client is (or is to be) stored.
536
+ *
537
+ * @returns {Client} - The requested client.
538
+ */
539
+ private getClient;
540
+ /**
541
+ * Move current clients to previous, create a new map for current.
542
+ *
543
+ * This function is called every `windowMs`.
544
+ */
545
+ private clearExpired;
404
546
  }
405
547
 
406
548
  export {
package/dist/index.d.mts CHANGED
@@ -1,7 +1,104 @@
1
- // Generated by dts-bundle-generator v7.0.0
1
+ // Generated by dts-bundle-generator v8.0.1
2
2
 
3
3
  import { NextFunction, Request, RequestHandler, Response } from 'express';
4
4
 
5
+ declare const validations: {
6
+ enabled: {
7
+ [key: string]: boolean;
8
+ };
9
+ disable(): void;
10
+ /**
11
+ * Checks whether the IP address is valid, and that it does not have a port
12
+ * number in it.
13
+ *
14
+ * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_invalid_ip_address.
15
+ *
16
+ * @param ip {string | undefined} - The IP address provided by Express as request.ip.
17
+ *
18
+ * @returns {void}
19
+ */
20
+ ip(ip: string | undefined): void;
21
+ /**
22
+ * Makes sure the trust proxy setting is not set to `true`.
23
+ *
24
+ * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_permissive_trust_proxy.
25
+ *
26
+ * @param request {Request} - The Express request object.
27
+ *
28
+ * @returns {void}
29
+ */
30
+ trustProxy(request: Request): void;
31
+ /**
32
+ * Makes sure the trust proxy setting is set in case the `X-Forwarded-For`
33
+ * header is present.
34
+ *
35
+ * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_unset_trust_proxy.
36
+ *
37
+ * @param request {Request} - The Express request object.
38
+ *
39
+ * @returns {void}
40
+ */
41
+ xForwardedForHeader(request: Request): void;
42
+ /**
43
+ * Ensures totalHits value from store is a positive integer.
44
+ *
45
+ * @param hits {any} - The `totalHits` returned by the store.
46
+ */
47
+ positiveHits(hits: any): void;
48
+ /**
49
+ * Ensures a given key is incremented only once per request.
50
+ *
51
+ * @param request {Request} - The Express request object.
52
+ * @param store {Store} - The store class.
53
+ * @param key {string} - The key used to store the client's hit count.
54
+ *
55
+ * @returns {void}
56
+ */
57
+ singleCount(request: Request, store: Store, key: string): void;
58
+ /**
59
+ * Warns the user that the behaviour for `max: 0` / `limit: 0` is changing in the next
60
+ * major release.
61
+ *
62
+ * @param limit {number} - The maximum number of hits per client.
63
+ *
64
+ * @returns {void}
65
+ */
66
+ limit(limit: number): void;
67
+ /**
68
+ * Warns the user that the `draft_polli_ratelimit_headers` option is deprecated
69
+ * and will be removed in the next major release.
70
+ *
71
+ * @param draft_polli_ratelimit_headers {any | undefined} - The now-deprecated setting that was used to enable standard headers.
72
+ *
73
+ * @returns {void}
74
+ */
75
+ draftPolliHeaders(draft_polli_ratelimit_headers?: any): void;
76
+ /**
77
+ * Warns the user that the `onLimitReached` option is deprecated and will be removed in the next
78
+ * major release.
79
+ *
80
+ * @param onLimitReached {any | undefined} - The maximum number of hits per client.
81
+ *
82
+ * @returns {void}
83
+ */
84
+ onLimitReached(onLimitReached?: any): void;
85
+ /**
86
+ * Warns the user when the selected headers option requires a reset time but
87
+ * the store does not provide one.
88
+ *
89
+ * @param resetTime {Date | undefined} - The timestamp when the client's hit count will be reset.
90
+ *
91
+ * @returns {void}
92
+ */
93
+ headersResetTime(resetTime?: Date): void;
94
+ /**
95
+ * Checks the options.validate setting to ensure that only recognized validations are enabled or disabled.
96
+ *
97
+ * If any unrecognized values are found, an error is logged that includes the list of supported vaidations.
98
+ */
99
+ validationsConfig(): void;
100
+ };
101
+ export type Validations = typeof validations;
5
102
  /**
6
103
  * Callback that fires when a client's hit counter is incremented.
7
104
  *
@@ -163,6 +260,15 @@ export type Store = {
163
260
  prefix?: string;
164
261
  };
165
262
  export type DraftHeadersVersion = "draft-6" | "draft-7";
263
+ /**
264
+ * Validate configuration object for enabling or disabling specific validations.
265
+ *
266
+ * The keys must also be keys in the validations object, except `enable`, `disable`,
267
+ * and `default`.
268
+ */
269
+ export type EnabledValidations = {
270
+ [key in keyof Omit<Validations, "enabled" | "disable"> | "default"]?: boolean;
271
+ };
166
272
  /**
167
273
  * The configuration options for the rate limiter.
168
274
  */
@@ -182,7 +288,7 @@ export type Options = {
182
288
  *
183
289
  * Defaults to `5`.
184
290
  */
185
- max: number | ValueDeterminingMiddleware<number>;
291
+ limit: number | ValueDeterminingMiddleware<number>;
186
292
  /**
187
293
  * The response body to send back when a client is rate limited.
188
294
  *
@@ -241,14 +347,6 @@ export type Options = {
241
347
  * By default, sends back the `statusCode` and `message` set via the options.
242
348
  */
243
349
  handler: RateLimitExceededEventHandler;
244
- /**
245
- * Express request handler that sends back a response when a client has
246
- * reached their rate limit, and will be rate limited on their next request.
247
- *
248
- * @deprecated 6.x - Please use a custom `handler` that checks the number of
249
- * hits instead.
250
- */
251
- onLimitReached: RateLimitReachedEventHandler;
252
350
  /**
253
351
  * Method (in the form of middleware) to determine whether or not this request
254
352
  * counts towards a client's quota.
@@ -271,9 +369,9 @@ export type Options = {
271
369
  */
272
370
  store: Store | LegacyStore;
273
371
  /**
274
- * Whether or not the validation checks should run.
372
+ * The list of validation checks that should run.
275
373
  */
276
- validate: boolean;
374
+ validate: boolean | EnabledValidations;
277
375
  /**
278
376
  * Whether to send `X-RateLimit-*` headers with the rate limit and the number
279
377
  * of requests.
@@ -282,12 +380,16 @@ export type Options = {
282
380
  */
283
381
  headers?: boolean;
284
382
  /**
285
- * Whether to send `RateLimit-*` headers with the rate limit and the number
286
- * of requests.
383
+ * The maximum number of connections to allow during the `window` before
384
+ * rate limiting the client.
385
+ *
386
+ * Can be the limit itself as a number or express middleware that parses
387
+ * the request and then figures out the limit.
287
388
  *
288
- * @deprecated 6.x - This option was renamed to `standardHeaders`.
389
+ * @deprecated 7.x - This option was renamed to `limit`. However, it will not
390
+ * be removed from the library in the foreseeable future.
289
391
  */
290
- draft_polli_ratelimit_headers?: boolean;
392
+ max?: number | ValueDeterminingMiddleware<number>;
291
393
  };
292
394
  /**
293
395
  * The extended request object that includes information about the client's
@@ -302,7 +404,7 @@ export type AugmentedRequest = Request & {
302
404
  */
303
405
  export type RateLimitInfo = {
304
406
  limit: number;
305
- current: number;
407
+ used: number;
306
408
  remaining: number;
307
409
  resetTime: Date | undefined;
308
410
  };
@@ -317,6 +419,16 @@ export type RateLimitInfo = {
317
419
  * @public
318
420
  */
319
421
  export declare const rateLimit: (passedOptions?: Partial<Options>) => RateLimitRequestHandler;
422
+ /**
423
+ * The record that stores information about a client - namely, how many times
424
+ * they have hit the endpoint, and when their hit count resets.
425
+ *
426
+ * Similar to `ClientRateLimitInfo`, except `resetTime` is a compulsory field.
427
+ */
428
+ export type Client = {
429
+ totalHits: number;
430
+ resetTime: Date;
431
+ };
320
432
  /**
321
433
  * A `Store` that stores the hit count for each client in memory.
322
434
  *
@@ -328,19 +440,21 @@ export declare class MemoryStore implements Store {
328
440
  */
329
441
  windowMs: number;
330
442
  /**
331
- * The map that stores the number of hits for each client in memory.
332
- */
333
- hits: {
334
- [key: string]: number | undefined;
335
- };
336
- /**
337
- * The time at which all hit counts will be reset.
443
+ * These two maps store usage (requests) and reset time by key (for example, IP
444
+ * addresses or API keys).
445
+ *
446
+ * They are split into two to avoid having to iterate through the entire set to
447
+ * determine which ones need reset. Instead, `Client`s are moved from `previous`
448
+ * to `current` as they hit the endpoint. Once `windowMs` has elapsed, all clients
449
+ * left in `previous`, i.e., those that have not made any recent requests, are
450
+ * known to be expired and can be deleted in bulk.
338
451
  */
339
- resetTime: Date;
452
+ previous: Map<string, Client>;
453
+ current: Map<string, Client>;
340
454
  /**
341
- * Reference to the active timer.
455
+ * A reference to the active timer.
342
456
  */
343
- interval?: NodeJS.Timer;
457
+ interval?: NodeJS.Timeout;
344
458
  /**
345
459
  * Confirmation that the keys incremented in once instance of MemoryStore
346
460
  * cannot affect other instances.
@@ -401,6 +515,34 @@ export declare class MemoryStore implements Store {
401
515
  * @public
402
516
  */
403
517
  shutdown(): void;
518
+ /**
519
+ * Recycles a client by setting its hit count to zero, and reset time to
520
+ * `windowMs` milliseconds from now.
521
+ *
522
+ * NOT to be confused with `#resetKey()`, which removes a client from both the
523
+ * `current` and `previous` maps.
524
+ *
525
+ * @param client {Client} - The client to recycle.
526
+ * @param now {number} - The current time, to which the `windowMs` is added to get the `resetTime` for the client.
527
+ *
528
+ * @return {Client} - The modified client that was passed in, to allow for chaining.
529
+ */
530
+ private resetClient;
531
+ /**
532
+ * Retrieves or creates a client, given a key. Also ensures that the client being
533
+ * returned is in the `current` map.
534
+ *
535
+ * @param key {string} - The key under which the client is (or is to be) stored.
536
+ *
537
+ * @returns {Client} - The requested client.
538
+ */
539
+ private getClient;
540
+ /**
541
+ * Move current clients to previous, create a new map for current.
542
+ *
543
+ * This function is called every `windowMs`.
544
+ */
545
+ private clearExpired;
404
546
  }
405
547
 
406
548
  export {