express-rate-limit 5.0.0 → 5.1.1
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/LICENSE +7 -0
- package/README.md +13 -4
- package/lib/express-rate-limit.js +13 -2
- package/package.json +9 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2019 Nathan Friedly
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -109,13 +109,15 @@ A `req.rateLimit` property is added to all requests with the `limit`, `current`,
|
|
|
109
109
|
|
|
110
110
|
Max number of connections during `windowMs` milliseconds before sending a 429 response.
|
|
111
111
|
|
|
112
|
-
May be a number, or a function that returns a number or a promise.
|
|
112
|
+
May be a number, or a function that returns a number or a promise. If `max` is a function, it will be called with `req` and `res` params.
|
|
113
113
|
|
|
114
114
|
Defaults to `5`. Set to `0` to disable.
|
|
115
115
|
|
|
116
116
|
### windowMs
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
Timeframe for which requests are checked/remebered. Also used in the Retry-After header when the limit is reached.
|
|
119
|
+
|
|
120
|
+
Note: with non-default stores, you may need to configure this value twice, once here and once on the store. In some cases the units also differ (e.g. seconds vs miliseconds)
|
|
119
121
|
|
|
120
122
|
Defaults to `60000` (1 minute).
|
|
121
123
|
|
|
@@ -137,7 +139,13 @@ Defaults to `429`.
|
|
|
137
139
|
|
|
138
140
|
Enable headers for request limit (`X-RateLimit-Limit`) and current usage (`X-RateLimit-Remaining`) on all responses and time to wait before retrying (`Retry-After`) when `max` is exceeded.
|
|
139
141
|
|
|
140
|
-
Defaults to `true`.
|
|
142
|
+
Defaults to `true`. Behavior may change in the next major release.
|
|
143
|
+
|
|
144
|
+
### draft_polli_ratelimit_headers
|
|
145
|
+
|
|
146
|
+
Enable headers conforming to the [ratelimit standardization proposal](https://tools.ietf.org/id/draft-polli-ratelimit-headers-01.html): `RateLimit-Limit`, `RateLimit-Remaining`, and, if the store supports it, `RateLimit-Reset`. May be used in conjunction with, or instead of the `headers` option.
|
|
147
|
+
|
|
148
|
+
Defaults to `false`. Behavior and name will likely change in future releases.
|
|
141
149
|
|
|
142
150
|
### keyGenerator
|
|
143
151
|
|
|
@@ -200,7 +208,7 @@ Defaults to `false`.
|
|
|
200
208
|
|
|
201
209
|
### skip
|
|
202
210
|
|
|
203
|
-
Function used to skip requests. Returning `true` from the function will skip limiting for that request.
|
|
211
|
+
Function used to skip (whitelist) requests. Returning `true` from the function will skip limiting for that request.
|
|
204
212
|
|
|
205
213
|
Defaults to always `false` (count all requests):
|
|
206
214
|
|
|
@@ -221,6 +229,7 @@ Available data stores are:
|
|
|
221
229
|
- MemoryStore: _(default)_ Simple in-memory option. Does not share state when app has multiple processes or servers.
|
|
222
230
|
- [rate-limit-redis](https://npmjs.com/package/rate-limit-redis): A [Redis](http://redis.io/)-backed store, more suitable for large or demanding deployments.
|
|
223
231
|
- [rate-limit-memcached](https://npmjs.org/package/rate-limit-memcached): A [Memcached](https://memcached.org/)-backed store.
|
|
232
|
+
- [rate-limit-mongo](https://www.npmjs.com/package/rate-limit-mongo): A [MongoDB](https://www.mongodb.com/)-backed store.
|
|
224
233
|
|
|
225
234
|
You may also create your own store. It must implement the following in order to function:
|
|
226
235
|
|
|
@@ -9,6 +9,7 @@ function RateLimit(options) {
|
|
|
9
9
|
message: "Too many requests, please try again later.",
|
|
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
|
+
draft_polli_ratelimit_headers: false, //Support for the new RateLimit standardization headers
|
|
12
13
|
skipFailedRequests: false, // Do not count failed requests (status >= 400)
|
|
13
14
|
skipSuccessfulRequests: false, // Do not count successful requests (status < 400)
|
|
14
15
|
// allows to create custom keys (by default user IP is used)
|
|
@@ -73,7 +74,7 @@ function RateLimit(options) {
|
|
|
73
74
|
resetTime: resetTime
|
|
74
75
|
};
|
|
75
76
|
|
|
76
|
-
if (options.headers) {
|
|
77
|
+
if (options.headers && !res.headersSent) {
|
|
77
78
|
res.setHeader("X-RateLimit-Limit", max);
|
|
78
79
|
res.setHeader("X-RateLimit-Remaining", req.rateLimit.remaining);
|
|
79
80
|
if (resetTime instanceof Date) {
|
|
@@ -85,6 +86,16 @@ function RateLimit(options) {
|
|
|
85
86
|
);
|
|
86
87
|
}
|
|
87
88
|
}
|
|
89
|
+
if (options.draft_polli_ratelimit_headers && !res.headersSent) {
|
|
90
|
+
res.setHeader("RateLimit-Limit", max);
|
|
91
|
+
res.setHeader("RateLimit-Remaining", req.rateLimit.remaining);
|
|
92
|
+
if (resetTime) {
|
|
93
|
+
const deltaSeconds = Math.ceil(
|
|
94
|
+
(resetTime.getTime() - Date.now()) / 1000
|
|
95
|
+
);
|
|
96
|
+
res.setHeader("RateLimit-Reset", Math.max(0, deltaSeconds));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
88
99
|
|
|
89
100
|
if (options.skipFailedRequests || options.skipSuccessfulRequests) {
|
|
90
101
|
let decremented = false;
|
|
@@ -125,7 +136,7 @@ function RateLimit(options) {
|
|
|
125
136
|
}
|
|
126
137
|
|
|
127
138
|
if (max && current > max) {
|
|
128
|
-
if (options.headers) {
|
|
139
|
+
if (options.headers && !res.headersSent) {
|
|
129
140
|
res.setHeader("Retry-After", Math.ceil(options.windowMs / 1000));
|
|
130
141
|
}
|
|
131
142
|
return options.handler(req, res, next);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-rate-limit",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.1",
|
|
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": {
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"eslint": "^
|
|
37
|
-
"eslint-config-prettier": "^
|
|
38
|
-
"eslint-plugin-prettier": "^3.
|
|
39
|
-
"express": "^4.
|
|
40
|
-
"husky": "^
|
|
41
|
-
"mocha": "^
|
|
42
|
-
"prettier": "^1.
|
|
43
|
-
"pretty-quick": "^
|
|
36
|
+
"eslint": "^6.8.0",
|
|
37
|
+
"eslint-config-prettier": "^6.10.0",
|
|
38
|
+
"eslint-plugin-prettier": "^3.1.2",
|
|
39
|
+
"express": "^4.17.1",
|
|
40
|
+
"husky": "^4.2.3",
|
|
41
|
+
"mocha": "^7.0.1",
|
|
42
|
+
"prettier": "^1.19.1",
|
|
43
|
+
"pretty-quick": "^2.0.1",
|
|
44
44
|
"supertest": "^4.0.2"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|