express-rate-limit 5.2.6 → 5.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Express Rate Limit
2
2
 
3
- ![Node.js CI](https://github.com/nfriedly/express-rate-limit/workflows/Node.js%20CI/badge.svg)
3
+ [![Node.js CI](https://github.com/nfriedly/express-rate-limit/workflows/Node.js%20CI/badge.svg)](https://github.com/nfriedly/express-rate-limit/actions)
4
4
  [![NPM version](https://img.shields.io/npm/v/express-rate-limit.svg)](https://npmjs.org/package/express-rate-limit "View this project on NPM")
5
5
  [![npm downloads](https://img.shields.io/npm/dm/express-rate-limit)](https://www.npmjs.com/package/express-rate-limit)
6
6
 
@@ -186,6 +186,19 @@ function (req, res, options) {
186
186
  }
187
187
  ```
188
188
 
189
+ ### requestWasSuccessful
190
+
191
+ Function that is called when `skipFailedRequests` and/or `skipSuccessfulRequests` are set to `true`.
192
+ May be overridden if, for example, a service sends out a 200 status code on errors.
193
+
194
+ Defaults to
195
+
196
+ ```js
197
+ function (req, res) {
198
+ return res.statusCode < 400;
199
+ }
200
+ ```
201
+
189
202
  ### skipFailedRequests
190
203
 
191
204
  When set to `true`, failed requests won't be counted. Request considered failed when:
@@ -10,8 +10,12 @@ function RateLimit(options) {
10
10
  statusCode: 429, // 429 status = Too Many Requests (RFC 6585)
11
11
  headers: true, //Send custom rate limit header with limit and remaining
12
12
  draft_polli_ratelimit_headers: false, //Support for the new RateLimit standardization headers
13
- skipFailedRequests: false, // Do not count failed requests (status >= 400)
14
- skipSuccessfulRequests: false, // Do not count successful requests (status < 400)
13
+ // ability to manually decide if request was successful. Used when `skipSuccessfulRequests` and/or `skipFailedRequests` are set to `true`
14
+ requestWasSuccessful: function (req, res) {
15
+ return res.statusCode < 400;
16
+ },
17
+ skipFailedRequests: false, // Do not count failed requests
18
+ skipSuccessfulRequests: false, // Do not count successful requests
15
19
  // allows to create custom keys (by default user IP is used)
16
20
  keyGenerator: function (req /*, res*/) {
17
21
  return req.ip;
@@ -114,7 +118,7 @@ function RateLimit(options) {
114
118
 
115
119
  if (options.skipFailedRequests) {
116
120
  res.on("finish", function () {
117
- if (res.statusCode >= 400) {
121
+ if (!options.requestWasSuccessful(req, res)) {
118
122
  decrementKey();
119
123
  }
120
124
  });
@@ -130,7 +134,7 @@ function RateLimit(options) {
130
134
 
131
135
  if (options.skipSuccessfulRequests) {
132
136
  res.on("finish", function () {
133
- if (res.statusCode < 400) {
137
+ if (options.requestWasSuccessful(req, res)) {
134
138
  options.store.decrement(key);
135
139
  }
136
140
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-rate-limit",
3
- "version": "5.2.6",
3
+ "version": "5.3.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
  "homepage": "https://github.com/nfriedly/express-rate-limit",
6
6
  "author": {
@@ -41,6 +41,7 @@
41
41
  "mocha": "^8.2.1",
42
42
  "prettier": "^2.2.1",
43
43
  "pretty-quick": "^3.1.0",
44
+ "sinon": "^9.2.4",
44
45
  "supertest": "^6.1.3"
45
46
  },
46
47
  "scripts": {