express-rate-limit 7.1.5 → 7.2.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.cjs CHANGED
@@ -296,6 +296,20 @@ var validations = {
296
296
  );
297
297
  }
298
298
  }
299
+ },
300
+ /**
301
+ * Checks to see if the instance was created inside of a request handler, which would prevent it from working correctly.
302
+ */
303
+ creationStack() {
304
+ const { stack } = new Error(
305
+ "express-rate-limit validation check (set options.validate.creationStack=false to disable)"
306
+ );
307
+ if (stack?.includes("Layer.handle [as handle_request]")) {
308
+ throw new ValidationError(
309
+ "ERR_ERL_CREATED_IN_REQUEST_HANDLER",
310
+ `express-rate-limit instance should be created at app initialization, not when responding to a request.`
311
+ );
312
+ }
299
313
  }
300
314
  };
301
315
  var getValidations = (_enabled) => {
@@ -616,6 +630,7 @@ var handleAsyncErrors = (fn) => async (request, response, next) => {
616
630
  var rateLimit = (passedOptions) => {
617
631
  const config = parseOptions(passedOptions ?? {});
618
632
  const options = getOptionsFromConfig(config);
633
+ config.validations.creationStack();
619
634
  if (typeof config.store.init === "function")
620
635
  config.store.init(options);
621
636
  const middleware = handleAsyncErrors(
package/dist/index.d.cts CHANGED
@@ -97,6 +97,10 @@ declare const validations: {
97
97
  * If any unrecognized values are found, an error is logged that includes the list of supported vaidations.
98
98
  */
99
99
  validationsConfig(): void;
100
+ /**
101
+ * Checks to see if the instance was created inside of a request handler, which would prevent it from working correctly.
102
+ */
103
+ creationStack(): void;
100
104
  };
101
105
  export type Validations = typeof validations;
102
106
  /**
package/dist/index.d.mts CHANGED
@@ -97,6 +97,10 @@ declare const validations: {
97
97
  * If any unrecognized values are found, an error is logged that includes the list of supported vaidations.
98
98
  */
99
99
  validationsConfig(): void;
100
+ /**
101
+ * Checks to see if the instance was created inside of a request handler, which would prevent it from working correctly.
102
+ */
103
+ creationStack(): void;
100
104
  };
101
105
  export type Validations = typeof validations;
102
106
  /**
package/dist/index.d.ts CHANGED
@@ -97,6 +97,10 @@ declare const validations: {
97
97
  * If any unrecognized values are found, an error is logged that includes the list of supported vaidations.
98
98
  */
99
99
  validationsConfig(): void;
100
+ /**
101
+ * Checks to see if the instance was created inside of a request handler, which would prevent it from working correctly.
102
+ */
103
+ creationStack(): void;
100
104
  };
101
105
  export type Validations = typeof validations;
102
106
  /**
package/dist/index.mjs CHANGED
@@ -268,6 +268,20 @@ var validations = {
268
268
  );
269
269
  }
270
270
  }
271
+ },
272
+ /**
273
+ * Checks to see if the instance was created inside of a request handler, which would prevent it from working correctly.
274
+ */
275
+ creationStack() {
276
+ const { stack } = new Error(
277
+ "express-rate-limit validation check (set options.validate.creationStack=false to disable)"
278
+ );
279
+ if (stack?.includes("Layer.handle [as handle_request]")) {
280
+ throw new ValidationError(
281
+ "ERR_ERL_CREATED_IN_REQUEST_HANDLER",
282
+ `express-rate-limit instance should be created at app initialization, not when responding to a request.`
283
+ );
284
+ }
271
285
  }
272
286
  };
273
287
  var getValidations = (_enabled) => {
@@ -588,6 +602,7 @@ var handleAsyncErrors = (fn) => async (request, response, next) => {
588
602
  var rateLimit = (passedOptions) => {
589
603
  const config = parseOptions(passedOptions ?? {});
590
604
  const options = getOptionsFromConfig(config);
605
+ config.validations.creationStack();
591
606
  if (typeof config.store.init === "function")
592
607
  config.store.init(options);
593
608
  const middleware = handleAsyncErrors(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-rate-limit",
3
- "version": "7.1.5",
3
+ "version": "7.2.0",
4
4
  "description": "Basic IP rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset.",
5
5
  "author": {
6
6
  "name": "Nathan Friedly",
package/readme.md CHANGED
@@ -9,16 +9,6 @@
9
9
 
10
10
  </div>
11
11
 
12
- ---
13
-
14
- Sponsored by [Zuplo](https://zuplo.link/express-rate-limit) a fully-managed API
15
- Gateway for developers. Add
16
- [dynamic rate-limiting](https://zuplo.link/dynamic-rate-limiting),
17
- authentication and more to any API in minutes. Learn more at
18
- [zuplo.com](https://zuplo.link/express-rate-limit)
19
-
20
- ---
21
-
22
12
  Basic rate-limiting middleware for [Express](http://expressjs.com/). Use to
23
13
  limit repeated requests to public APIs and/or endpoints such as password reset.
24
14
  Plays nice with
@@ -38,13 +28,49 @@ const limiter = rateLimit({
38
28
  limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
39
29
  standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
40
30
  legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
41
- // store: ... , // Use an external store for consistency across multiple server instances.
31
+ // store: ... , // Redis, Memcached, etc. See below.
42
32
  })
43
33
 
44
34
  // Apply the rate limiting middleware to all requests.
45
35
  app.use(limiter)
46
36
  ```
47
37
 
38
+ ### Data Stores
39
+
40
+ The rate limiter comes with a built-in memory store, and supports a variety of
41
+ [external data stores](https://express-rate-limit.mintlify.app/reference/stores).
42
+
43
+ ### Configuration
44
+
45
+ All function options may be async. Click the name for additional info and
46
+ default values.
47
+
48
+ | Option | Type | Remarks |
49
+ | ------------------------------------------------------------------------------------------------------------------ | -------------------------------- | ----------------------------------------------------------------------------------------------- |
50
+ | [`windowMs`](https://express-rate-limit.mintlify.app/reference/configuration#windowms) | `number` | How long to remember requests for, in milliseconds. |
51
+ | [`limit`](https://express-rate-limit.mintlify.app/reference/configuration#limit) | `number` \| `function` | How many requests to allow. |
52
+ | [`message`](https://express-rate-limit.mintlify.app/reference/configuration#message) | `string` \| `json` \| `function` | Response to return after limit is reached. |
53
+ | [`statusCode`](https://express-rate-limit.mintlify.app/reference/configuration#statuscode) | `number` | HTTP status code after limit is reached (default is 429). |
54
+ | [`legacyHeaders`](https://express-rate-limit.mintlify.app/reference/configuration#legacyheaders) | `boolean` | Enable the `X-Rate-Limit` header. |
55
+ | [`standardHeaders`](https://express-rate-limit.mintlify.app/reference/configuration#standardheaders) | `'draft-6'` \| `'draft-7'` | Enable the `Ratelimit` header. |
56
+ | [`requestPropertyName`](https://express-rate-limit.mintlify.app/reference/configuration#requestpropertyname) | `string` | Add rate limit info to the `req` object. |
57
+ | [`skipFailedRequests`](https://express-rate-limit.mintlify.app/reference/configuration#skipfailedrequests) | `boolean` | Uncount 4xx/5xx responses. |
58
+ | [`skipSuccessfulRequests`](https://express-rate-limit.mintlify.app/reference/configuration#skipsuccessfulrequests) | `boolean` | Uncount 1xx/2xx/3xx responses. |
59
+ | [`keyGenerator`](https://express-rate-limit.mintlify.app/reference/configuration#keygenerator) | `function` | Identify users (defaults to IP address). |
60
+ | [`handler`](https://express-rate-limit.mintlify.app/reference/configuration#handler) | `function` | Function to run after limit is reached (overrides `message` and `statusCode` settings, if set). |
61
+ | [`skip`](https://express-rate-limit.mintlify.app/reference/configuration#skip) | `function` | Return `true` to bypass the limiter for the given request. |
62
+ | [`requestWasSuccessful`](https://express-rate-limit.mintlify.app/reference/configuration#requestwassuccessful) | `function` | Used by `skipFailedRequests` and `skipSuccessfulRequests`. |
63
+ | [`validate`](https://express-rate-limit.mintlify.app/reference/configuration#validate) | `boolean` \| `object` | Enable or disable built-in validation checks. |
64
+ | [`store`](https://express-rate-limit.mintlify.app/reference/configuration#store) | `Store` | Use a custom store to share hit counts across multiple nodes. |
65
+
66
+ ## Thank You
67
+
68
+ Sponsored by [Zuplo](https://zuplo.link/express-rate-limit) a fully-managed API
69
+ Gateway for developers. Add
70
+ [dynamic rate-limiting](https://zuplo.link/dynamic-rate-limiting),
71
+ authentication and more to any API in minutes. Learn more at
72
+ [zuplo.com](https://zuplo.link/express-rate-limit)
73
+
48
74
  ---
49
75
 
50
76
  Thanks to Mintlify for hosting the documentation at
@@ -58,6 +84,8 @@ Thanks to Mintlify for hosting the documentation at
58
84
 
59
85
  ---
60
86
 
87
+ Finally, thank you to everyone who's contributed to this project in any way! 🫶
88
+
61
89
  ## Issues and Contributing
62
90
 
63
91
  If you encounter a bug or want to see something added/changed, please go ahead