express-rate-limit 7.1.0 → 7.1.2
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/changelog.md +14 -0
- package/package.json +12 -11
- package/readme.md +29 -529
package/changelog.md
CHANGED
|
@@ -6,6 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
|
6
6
|
and this project adheres to
|
|
7
7
|
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
8
8
|
|
|
9
|
+
## [7.1.2](https://github.com/express-rate-limit/express-rate-limit/releases/tag/v7.1.2)
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- Re-organized documentation from readme into docs/ folder and added
|
|
14
|
+
documentation website.
|
|
15
|
+
|
|
16
|
+
## [v7.1.1](https://github.com/express-rate-limit/express-rate-limit/releases/tag/v7.1.1)
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- Enabled provenance statement generation, see
|
|
21
|
+
https://github.com/express-rate-limit/express-rate-limit#406.
|
|
22
|
+
|
|
9
23
|
## [7.1.0](https://github.com/express-rate-limit/express-rate-limit/releases/tag/v7.1.0)
|
|
10
24
|
|
|
11
25
|
### Changed
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-rate-limit",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.2",
|
|
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",
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"build:esm": "esbuild --platform=node --bundle --target=es2022 --format=esm --outfile=dist/index.mjs source/index.ts",
|
|
61
61
|
"build:types": "dts-bundle-generator --out-file=dist/index.d.ts source/index.ts && cp dist/index.d.ts dist/index.d.cts && cp dist/index.d.ts dist/index.d.mts",
|
|
62
62
|
"compile": "run-s clean build:*",
|
|
63
|
+
"docs": "cd docs && mintlify dev",
|
|
63
64
|
"lint:code": "xo",
|
|
64
65
|
"lint:rest": "prettier --check .",
|
|
65
66
|
"lint": "run-s lint:*",
|
|
@@ -76,20 +77,20 @@
|
|
|
76
77
|
"express": "^4 || ^5"
|
|
77
78
|
},
|
|
78
79
|
"devDependencies": {
|
|
79
|
-
"@express-rate-limit/prettier": "1.1.
|
|
80
|
-
"@express-rate-limit/tsconfig": "1.0.
|
|
81
|
-
"@jest/globals": "29.
|
|
82
|
-
"@types/express": "4.17.
|
|
83
|
-
"@types/jest": "29.5.
|
|
84
|
-
"@types/node": "20.
|
|
85
|
-
"@types/supertest": "2.0.
|
|
80
|
+
"@express-rate-limit/prettier": "1.1.1",
|
|
81
|
+
"@express-rate-limit/tsconfig": "1.0.2",
|
|
82
|
+
"@jest/globals": "29.7.0",
|
|
83
|
+
"@types/express": "4.17.20",
|
|
84
|
+
"@types/jest": "29.5.6",
|
|
85
|
+
"@types/node": "20.8.7",
|
|
86
|
+
"@types/supertest": "2.0.15",
|
|
86
87
|
"del-cli": "5.1.0",
|
|
87
88
|
"dts-bundle-generator": "8.0.1",
|
|
88
|
-
"esbuild": "0.19.
|
|
89
|
+
"esbuild": "0.19.5",
|
|
89
90
|
"express": "4.18.2",
|
|
90
91
|
"husky": "8.0.3",
|
|
91
|
-
"jest": "29.
|
|
92
|
-
"lint-staged": "
|
|
92
|
+
"jest": "29.7.0",
|
|
93
|
+
"lint-staged": "15.0.2",
|
|
93
94
|
"npm-run-all": "4.1.5",
|
|
94
95
|
"ratelimit-header-parser": "0.1.0",
|
|
95
96
|
"supertest": "6.3.3",
|
package/readme.md
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center"> <code>express-rate-limit</code> </h1>
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
[](https://github.com/express-rate-limit/express-rate-limit/actions/workflows/ci.yaml)
|
|
6
|
+
[](https://npmjs.org/package/express-rate-limit 'View this project on NPM')
|
|
7
|
+
[](https://www.npmjs.com/package/express-rate-limit)
|
|
8
|
+
[](license.md)
|
|
9
|
+
|
|
10
|
+
</div>
|
|
2
11
|
|
|
3
12
|
---
|
|
4
13
|
|
|
@@ -10,553 +19,44 @@ authentication and more to any API in minutes. Learn more at
|
|
|
10
19
|
|
|
11
20
|
---
|
|
12
21
|
|
|
13
|
-
<div align="center">
|
|
14
|
-
|
|
15
|
-
[](https://github.com/express-rate-limit/express-rate-limit/actions/workflows/ci.yaml)
|
|
16
|
-
[](https://npmjs.org/package/express-rate-limit 'View this project on NPM')
|
|
17
|
-
[](https://www.npmjs.com/package/express-rate-limit)
|
|
18
|
-
|
|
19
22
|
Basic rate-limiting middleware for [Express](http://expressjs.com/). Use to
|
|
20
23
|
limit repeated requests to public APIs and/or endpoints such as password reset.
|
|
21
24
|
Plays nice with
|
|
22
|
-
[express-slow-down](https://www.npmjs.com/package/express-slow-down)
|
|
23
|
-
|
|
24
|
-
</div>
|
|
25
|
-
|
|
26
|
-
## Use Cases
|
|
27
|
-
|
|
28
|
-
Depending on your use case, you may want to switch to a different
|
|
29
|
-
[store](#store).
|
|
30
|
-
|
|
31
|
-
### Abuse Prevention
|
|
32
|
-
|
|
33
|
-
The default `MemoryStore` is probably fine.
|
|
34
|
-
|
|
35
|
-
### API Rate Limit Enforcement
|
|
36
|
-
|
|
37
|
-
The default `MemoryStore` stores the hit counts for clients in memory, and is
|
|
38
|
-
thus unsuitable for use when running multiple servers or processes.
|
|
39
|
-
|
|
40
|
-
If you have multiple processes on a single server (via the
|
|
41
|
-
[node:cluster](https://nodejs.org/api/cluster.html) module), the
|
|
42
|
-
[`cluster-memory-store`](https://npmjs.com/package/@express-rate-limit/cluster-memory-store)
|
|
43
|
-
will keep them all in sync without needing an external data store.
|
|
44
|
-
|
|
45
|
-
If you have multiple servers, or want to maintain state across app restarts, use
|
|
46
|
-
an external data store such as
|
|
47
|
-
[redis](https://npmjs.com/package/rate-limit-redis),
|
|
48
|
-
[memcached](https://npmjs.org/package/rate-limit-memcached), [etc.](#store).
|
|
49
|
-
|
|
50
|
-
### Alternate Rate Limiters
|
|
51
|
-
|
|
52
|
-
This module was designed to only handle the basics and didn't even support
|
|
53
|
-
external stores initially. These other options all are excellent pieces of
|
|
54
|
-
software and may be more appropriate for some situations:
|
|
55
|
-
|
|
56
|
-
- [`rate-limiter-flexible`](https://www.npmjs.com/package/rate-limiter-flexible)
|
|
57
|
-
- [`express-brute`](https://www.npmjs.com/package/express-brute)
|
|
58
|
-
- [`rate-limiter`](https://www.npmjs.com/package/express-limiter)
|
|
59
|
-
|
|
60
|
-
## Installation
|
|
61
|
-
|
|
62
|
-
From the npm registry:
|
|
63
|
-
|
|
64
|
-
```sh
|
|
65
|
-
# Using npm
|
|
66
|
-
> npm install express-rate-limit
|
|
67
|
-
# Using yarn or pnpm
|
|
68
|
-
> yarn/pnpm add express-rate-limit
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
From Github Releases:
|
|
72
|
-
|
|
73
|
-
```sh
|
|
74
|
-
# Using npm
|
|
75
|
-
> npm install https://github.com/express-rate-limit/express-rate-limit/releases/download/v{version}/express-rate-limit.tgz
|
|
76
|
-
# Using yarn or pnpm
|
|
77
|
-
> yarn/pnpm add https://github.com/express-rate-limit/express-rate-limit/releases/download/v{version}/express-rate-limit.tgz
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
Replace `{version}` with the version of the package that you want to your, e.g.:
|
|
81
|
-
`6.0.0`.
|
|
25
|
+
[express-slow-down](https://www.npmjs.com/package/express-slow-down) and
|
|
26
|
+
[ratelimit-header-parser](https://www.npmjs.com/package/ratelimit-header-parser).
|
|
82
27
|
|
|
83
28
|
## Usage
|
|
84
29
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
This library is provided in ESM as well as CJS forms, and works with both
|
|
88
|
-
Javascript and Typescript projects.
|
|
89
|
-
|
|
90
|
-
**This package requires you to use Node 16 or above.**
|
|
91
|
-
|
|
92
|
-
Import it in a CommonJS project (`type: commonjs` or no `type` field in
|
|
93
|
-
`package.json`) as follows:
|
|
94
|
-
|
|
95
|
-
```ts
|
|
96
|
-
const { rateLimit } = require('express-rate-limit')
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Import it in a ESM project (`type: module` in `package.json`) as follows:
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
import { rateLimit } from 'express-rate-limit'
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### Examples
|
|
106
|
-
|
|
107
|
-
To use it in an API-only server where the rate-limiter should be applied to all
|
|
108
|
-
requests:
|
|
30
|
+
The [full documentation](https://express-rate-limit.mintlify.app/overview) is
|
|
31
|
+
available on-line.
|
|
109
32
|
|
|
110
33
|
```ts
|
|
111
34
|
import { rateLimit } from 'express-rate-limit'
|
|
112
35
|
|
|
113
36
|
const limiter = rateLimit({
|
|
114
37
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
115
|
-
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
|
|
38
|
+
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
|
|
116
39
|
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
|
117
|
-
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
|
118
|
-
// store: ... , // Use an external store for
|
|
40
|
+
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
|
|
41
|
+
// store: ... , // Use an external store for consistency across multiple server instances.
|
|
119
42
|
})
|
|
120
43
|
|
|
121
|
-
// Apply the rate limiting middleware to all requests
|
|
44
|
+
// Apply the rate limiting middleware to all requests.
|
|
122
45
|
app.use(limiter)
|
|
123
46
|
```
|
|
124
47
|
|
|
125
|
-
|
|
126
|
-
`express.static()`), where the rate-limiter should only apply to certain
|
|
127
|
-
requests:
|
|
128
|
-
|
|
129
|
-
```ts
|
|
130
|
-
import { rateLimit } from 'express-rate-limit'
|
|
131
|
-
|
|
132
|
-
const apiLimiter = rateLimit({
|
|
133
|
-
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
134
|
-
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
|
|
135
|
-
standardHeaders: 'draft-7', // Set `RateLimit` and `RateLimit-Policy` headers
|
|
136
|
-
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
|
137
|
-
// store: ... , // Use an external store for more precise rate limiting
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
// Apply the rate limiting middleware to API calls only
|
|
141
|
-
app.use('/api', apiLimiter)
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
To create multiple instances to apply different rules to different endpoints:
|
|
145
|
-
|
|
146
|
-
```ts
|
|
147
|
-
import { rateLimit } from 'express-rate-limit'
|
|
148
|
-
|
|
149
|
-
const apiLimiter = rateLimit({
|
|
150
|
-
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
151
|
-
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
|
|
152
|
-
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
|
153
|
-
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
|
154
|
-
// store: ... , // Use an external store for more precise rate limiting
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
app.use('/api/', apiLimiter)
|
|
158
|
-
|
|
159
|
-
const createAccountLimiter = rateLimit({
|
|
160
|
-
windowMs: 60 * 60 * 1000, // 1 hour
|
|
161
|
-
limit: 5, // Limit each IP to 5 create account requests per `window` (here, per hour)
|
|
162
|
-
message:
|
|
163
|
-
'Too many accounts created from this IP, please try again after an hour',
|
|
164
|
-
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
|
165
|
-
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
app.post('/create-account', createAccountLimiter, (req, res) => {
|
|
169
|
-
//...
|
|
170
|
-
})
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
To use a custom store:
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
import { rateLimit } from 'express-rate-limit'
|
|
177
|
-
import RedisStore from 'rate-limit-redis'
|
|
178
|
-
import RedisClient from 'ioredis'
|
|
179
|
-
|
|
180
|
-
const redisClient = new RedisClient()
|
|
181
|
-
const rateLimiter = rateLimit({
|
|
182
|
-
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
183
|
-
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
|
|
184
|
-
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
|
|
185
|
-
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
|
186
|
-
store: new RedisStore({
|
|
187
|
-
/* ... */
|
|
188
|
-
}), // Use the external store
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
// Apply the rate limiting middleware to all requests
|
|
192
|
-
app.use(rateLimiter)
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
> **Note:** most stores will require additional configuration, such as custom
|
|
196
|
-
> prefixes, when using multiple instances. The default built-in memory store is
|
|
197
|
-
> an exception to this rule.
|
|
198
|
-
|
|
199
|
-
### Troubleshooting Proxy Issues
|
|
200
|
-
|
|
201
|
-
Please take a look at
|
|
202
|
-
[the wiki page](https://github.com/express-rate-limit/express-rate-limit/wiki/Troubleshooting-Proxy-Issues)
|
|
203
|
-
on this issue.
|
|
204
|
-
|
|
205
|
-
## Configuration
|
|
206
|
-
|
|
207
|
-
### `windowMs`
|
|
208
|
-
|
|
209
|
-
> `number`
|
|
210
|
-
|
|
211
|
-
Time frame for which requests are checked/remembered. Also used in the
|
|
212
|
-
`Retry-After` header when the limit is reached.
|
|
213
|
-
|
|
214
|
-
Note: with stores that do not implement the `init` function (see the table in
|
|
215
|
-
the [`stores` section below](#stores)), you may need to configure this value
|
|
216
|
-
twice, once here and once on the store. In some cases the units also differ
|
|
217
|
-
(e.g. seconds vs miliseconds).
|
|
218
|
-
|
|
219
|
-
Defaults to `60000` ms (= 1 minute).
|
|
220
|
-
|
|
221
|
-
### `limit`
|
|
222
|
-
|
|
223
|
-
> `number | function`
|
|
224
|
-
|
|
225
|
-
The maximum number of connections to allow during the `window` before rate
|
|
226
|
-
limiting the client.
|
|
227
|
-
|
|
228
|
-
Can be the limit itself as a number or a (sync/async) function that accepts the
|
|
229
|
-
Express `req` and `res` objects and then returns a number.
|
|
230
|
-
|
|
231
|
-
~Set it to `0` to disable the rate limiter.~ As of version 7.0.0, setting `max`
|
|
232
|
-
to zero will no longer disable the rate limiter - instead, it will 'block' all
|
|
233
|
-
requests to that endpoint.
|
|
234
|
-
|
|
235
|
-
Defaults to `5`.
|
|
236
|
-
|
|
237
|
-
> Renamed in v7.x from `max` to `limit`. However, `max` will still be supported
|
|
238
|
-
> for backwards-compatibility.
|
|
239
|
-
|
|
240
|
-
An example of using a function:
|
|
241
|
-
|
|
242
|
-
```ts
|
|
243
|
-
const isPremium = async (user) => {
|
|
244
|
-
// ...
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const limiter = rateLimit({
|
|
248
|
-
// ...
|
|
249
|
-
limit: async (req, res) => {
|
|
250
|
-
if (await isPremium(req.user)) return 10
|
|
251
|
-
else return 5
|
|
252
|
-
},
|
|
253
|
-
})
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
### `message`
|
|
257
|
-
|
|
258
|
-
> `any`
|
|
259
|
-
|
|
260
|
-
The response body to send back when a client is rate limited.
|
|
261
|
-
|
|
262
|
-
May be a `string`, JSON object, or any other value that Express's
|
|
263
|
-
[`res.send`](https://expressjs.com/en/4x/api.html#res.send) method supports. It
|
|
264
|
-
can also be a (sync/async) function that accepts the Express request and
|
|
265
|
-
response objects and then returns a `string`, JSON object or any other value the
|
|
266
|
-
Express `res.send` function accepts.
|
|
267
|
-
|
|
268
|
-
Defaults to `'Too many requests, please try again later.'`
|
|
269
|
-
|
|
270
|
-
An example of using a function:
|
|
271
|
-
|
|
272
|
-
```ts
|
|
273
|
-
const isPremium = async (user) => {
|
|
274
|
-
// ...
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const limiter = rateLimit({
|
|
278
|
-
// ...
|
|
279
|
-
message: async (req, res) => {
|
|
280
|
-
if (await isPremium(req.user))
|
|
281
|
-
return 'You can only make 10 requests every hour.'
|
|
282
|
-
else return 'You can only make 5 requests every hour.'
|
|
283
|
-
},
|
|
284
|
-
})
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
### `statusCode`
|
|
288
|
-
|
|
289
|
-
> `number`
|
|
290
|
-
|
|
291
|
-
The HTTP status code to send back when a client is rate limited.
|
|
292
|
-
|
|
293
|
-
Defaults to `429` (HTTP 429 Too Many Requests - RFC 6585).
|
|
294
|
-
|
|
295
|
-
### `legacyHeaders`
|
|
296
|
-
|
|
297
|
-
> `boolean`
|
|
298
|
-
|
|
299
|
-
Whether to send the legacy rate limit headers for the limit
|
|
300
|
-
(`X-RateLimit-Limit`), current usage (`X-RateLimit-Remaining`) and reset time
|
|
301
|
-
(if the store provides it) (`X-RateLimit-Reset`) on all responses. If set to
|
|
302
|
-
`true`, the middleware also sends the `Retry-After` header on all blocked
|
|
303
|
-
requests.
|
|
304
|
-
|
|
305
|
-
Defaults to `true` (for backward compatibility).
|
|
306
|
-
|
|
307
|
-
> Renamed in `6.x` from `headers` to `legacyHeaders`.
|
|
308
|
-
|
|
309
|
-
### `standardHeaders`
|
|
310
|
-
|
|
311
|
-
> `boolean` | `'draft-6'` | `'draft-7'`
|
|
312
|
-
|
|
313
|
-
Whether to enable support for headers conforming to the
|
|
314
|
-
[RateLimit header fields for HTTP standardization draft](https://github.com/ietf-wg-httpapi/ratelimit-headers)
|
|
315
|
-
adopted by the IETF.
|
|
316
|
-
|
|
317
|
-
If set to `draft-6`, separate `RateLimit-Policy` `RateLimit-Limit`,
|
|
318
|
-
`RateLimit-Remaining`, and, if the store supports it, `RateLimit-Reset` headers
|
|
319
|
-
are set on the response, in accordance with
|
|
320
|
-
[draft-ietf-httpapi-ratelimit-headers-06](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-06).
|
|
321
|
-
|
|
322
|
-
If set to `draft-7`, a combined `RateLimit` header is set containing limit,
|
|
323
|
-
remaining, and reset values, and a `RateLimit-Policy` header is set, in
|
|
324
|
-
accordiance with
|
|
325
|
-
[draft-ietf-httpapi-ratelimit-headers-07](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-07).
|
|
326
|
-
`windowMs` is used for the reset value if the store does not provide a reset
|
|
327
|
-
time.
|
|
328
|
-
|
|
329
|
-
If set to `true`, it is treated as `draft-6`, however this behavior may change
|
|
330
|
-
in a future semver major release.
|
|
331
|
-
|
|
332
|
-
If set to any truthy value, the middleware also sends the `Retry-After` header
|
|
333
|
-
on all blocked requests.
|
|
334
|
-
|
|
335
|
-
The `standardHeaders` option may be used in conjunction with, or instead of the
|
|
336
|
-
`legacyHeaders` option.
|
|
337
|
-
|
|
338
|
-
ℹ️ Tip: use
|
|
339
|
-
[ratelimit-header-parser](https://www.npmjs.com/package/ratelimit-header-parser)
|
|
340
|
-
in clients to read/parse any form of express-rate-limit's headers.
|
|
341
|
-
|
|
342
|
-
Defaults to `false`.
|
|
343
|
-
|
|
344
|
-
> Renamed in `6.x` from `draft_polli_ratelimit_headers` to `standardHeaders`.
|
|
345
|
-
|
|
346
|
-
### `requestPropertyName`
|
|
347
|
-
|
|
348
|
-
> `string`
|
|
349
|
-
|
|
350
|
-
The name of the property on the Express `request` object to store the rate limit
|
|
351
|
-
info.
|
|
352
|
-
|
|
353
|
-
Defaults to `'rateLimit'`.
|
|
354
|
-
|
|
355
|
-
### `skipFailedRequests`
|
|
356
|
-
|
|
357
|
-
> `boolean`
|
|
358
|
-
|
|
359
|
-
When set to `true`, failed requests won't be counted. Request considered failed
|
|
360
|
-
when the `requestWasSuccessful` option returns `false`. By default, this means
|
|
361
|
-
requests fail when:
|
|
362
|
-
|
|
363
|
-
- the response status >= 400
|
|
364
|
-
- the request was cancelled before last chunk of data was sent (response `close`
|
|
365
|
-
event triggered)
|
|
366
|
-
- the response `error` event was triggered by response
|
|
367
|
-
|
|
368
|
-
(Technically they are counted and then un-counted, so a large number of slow
|
|
369
|
-
requests all at once could still trigger a rate-limit. This may be fixed in a
|
|
370
|
-
future release. PRs welcome!)
|
|
371
|
-
|
|
372
|
-
Defaults to `false`.
|
|
373
|
-
|
|
374
|
-
### `skipSuccessfulRequests`
|
|
375
|
-
|
|
376
|
-
> `boolean`
|
|
377
|
-
|
|
378
|
-
If `true`, the library will (by default) skip all requests that are considered
|
|
379
|
-
'failed' by the `requestWasSuccessful` function. By default, this means requests
|
|
380
|
-
succeed when the response status code < 400.
|
|
381
|
-
|
|
382
|
-
(Technically they are counted and then un-counted, so a large number of slow
|
|
383
|
-
requests all at once could still trigger a rate-limit. This may be fixed in a
|
|
384
|
-
future release. PRs welcome!)
|
|
385
|
-
|
|
386
|
-
Defaults to `false`.
|
|
387
|
-
|
|
388
|
-
### `keyGenerator`
|
|
389
|
-
|
|
390
|
-
> `function`
|
|
391
|
-
|
|
392
|
-
Method to retrieve custom identifiers for clients, such as their IP address,
|
|
393
|
-
username, or API Key.
|
|
394
|
-
|
|
395
|
-
Should be a (sync/async) function that accepts the Express `request` and
|
|
396
|
-
`response` objects and then returns a string.
|
|
397
|
-
|
|
398
|
-
By default, the client's IP address is used:
|
|
399
|
-
|
|
400
|
-
```ts
|
|
401
|
-
const limiter = rateLimit({
|
|
402
|
-
// ...
|
|
403
|
-
keyGenerator: (req, res) => req.ip,
|
|
404
|
-
})
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
> **Note** If a `keyGenerator` returns the same value for every user, it becomes
|
|
408
|
-
> a global rate limiter. This could be combined with a second instance of
|
|
409
|
-
> `express-rate-limit` to have both global and per-user limits.
|
|
410
|
-
|
|
411
|
-
### `handler`
|
|
412
|
-
|
|
413
|
-
> `function`
|
|
414
|
-
|
|
415
|
-
Express request handler that sends back a response when a client is
|
|
416
|
-
rate-limited.
|
|
417
|
-
|
|
418
|
-
By default, sends back the `statusCode` and `message` set via the `options`,
|
|
419
|
-
similar to this:
|
|
420
|
-
|
|
421
|
-
```ts
|
|
422
|
-
const limiter = rateLimit({
|
|
423
|
-
// ...
|
|
424
|
-
handler: (req, res, next, options) =>
|
|
425
|
-
res.status(options.statusCode).send(options.message),
|
|
426
|
-
})
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
### `skip`
|
|
430
|
-
|
|
431
|
-
> `function`
|
|
432
|
-
|
|
433
|
-
Function to determine whether or not this request counts towards a client's
|
|
434
|
-
quota. Should be a (sync/async) function that accepts the Express `request` and
|
|
435
|
-
`response` objects and then returns `true` or `false`.
|
|
436
|
-
|
|
437
|
-
Could also act as an allowlist for certain keys:
|
|
438
|
-
|
|
439
|
-
```ts
|
|
440
|
-
const allowlist = ['192.168.0.56', '192.168.0.21']
|
|
441
|
-
|
|
442
|
-
const limiter = rateLimit({
|
|
443
|
-
// ...
|
|
444
|
-
skip: (req, res) => allowlist.includes(req.ip),
|
|
445
|
-
})
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
By default, it skips no requests:
|
|
449
|
-
|
|
450
|
-
```ts
|
|
451
|
-
const limiter = rateLimit({
|
|
452
|
-
// ...
|
|
453
|
-
skip: (req, res) => false,
|
|
454
|
-
})
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
### `requestWasSuccessful`
|
|
458
|
-
|
|
459
|
-
> `function`
|
|
460
|
-
|
|
461
|
-
Method to determine whether or not the request counts as 'succesful'. Used when
|
|
462
|
-
either `skipSuccessfulRequests` or `skipFailedRequests` is set to true. Should
|
|
463
|
-
be a (sync/async) function that accepts the Express `req` and `res` objects and
|
|
464
|
-
then returns `true` or `false`.
|
|
465
|
-
|
|
466
|
-
By default, requests with a response status code less than 400 are considered
|
|
467
|
-
successful:
|
|
468
|
-
|
|
469
|
-
```ts
|
|
470
|
-
const limiter = rateLimit({
|
|
471
|
-
// ...
|
|
472
|
-
requestWasSuccessful: (req, res) => res.statusCode < 400,
|
|
473
|
-
})
|
|
474
|
-
```
|
|
475
|
-
|
|
476
|
-
### `validate`
|
|
477
|
-
|
|
478
|
-
> `boolean | Object`
|
|
479
|
-
|
|
480
|
-
When enabled, a set of validation checks are run at creation and on the first
|
|
481
|
-
request to detect common misconfigurations with proxies, etc. Prints an error to
|
|
482
|
-
the console if any issue is detected.
|
|
483
|
-
|
|
484
|
-
Automatically disables after the first request is processed.
|
|
485
|
-
|
|
486
|
-
If set to `true` or `false`, all validations are enabled or disabled.
|
|
487
|
-
|
|
488
|
-
If set to an object, individual validations can be enabled or disabled by name,
|
|
489
|
-
and the key `default` controls all unspecified validations. For example:
|
|
490
|
-
|
|
491
|
-
```js
|
|
492
|
-
const limiter = rateLimit({
|
|
493
|
-
validate: {
|
|
494
|
-
xForwardedForHeader: false,
|
|
495
|
-
default: true,
|
|
496
|
-
},
|
|
497
|
-
// ...
|
|
498
|
-
})
|
|
499
|
-
```
|
|
500
|
-
|
|
501
|
-
Supported options are `ip`, `trustProxy`, `xForwardedForHeader`, `positiveHits`,
|
|
502
|
-
`singleCount`, `limit`, `draftPolliHeaders`, `onLimitReached`,
|
|
503
|
-
`headersResetTime`, `validationsConfig`, and `default`.
|
|
504
|
-
|
|
505
|
-
See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes
|
|
506
|
-
for more info.
|
|
507
|
-
|
|
508
|
-
Defaults to `true`.
|
|
509
|
-
|
|
510
|
-
### `store`
|
|
511
|
-
|
|
512
|
-
> `Store`
|
|
513
|
-
|
|
514
|
-
The `Store` to use to store the hit count for each client.
|
|
515
|
-
|
|
516
|
-
By default, the [`memory-store`](source/memory-store.ts) is used.
|
|
517
|
-
|
|
518
|
-
Here is a list of external stores:
|
|
519
|
-
|
|
520
|
-
| Name | Description | Legacy/Modern |
|
|
521
|
-
| -------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- |
|
|
522
|
-
| [`memory-store`](source/memory-store.ts) | _(default)_ Simple in-memory option. Does not share state when app has multiple processes or servers. | Modern as of v6.0.0 |
|
|
523
|
-
| [`cluster-memory-store`](https://npmjs.com/package/@express-rate-limit/cluster-memory-store) | A memory-store wrapper that shares state across all processes on a server via the [node:cluster](https://nodejs.org/api/cluster.html) module. | Modern |
|
|
524
|
-
| [`rate-limit-redis`](https://npmjs.com/package/rate-limit-redis) | A [Redis](http://redis.io/)-backed store, more suitable for large or demanding deployments. | Modern as of v3.0.0 |
|
|
525
|
-
| [`rate-limit-memcached`](https://npmjs.org/package/rate-limit-memcached) | A [Memcached](https://memcached.org/)-backed store. | Modern as of v1.0.0 |
|
|
526
|
-
| [`rate-limit-mongo`](https://www.npmjs.com/package/rate-limit-mongo) | A [MongoDB](https://www.mongodb.com/)-backed store. | Legacy |
|
|
527
|
-
| [`precise-memory-rate-limit`](https://www.npmjs.com/package/precise-memory-rate-limit) | A memory store similar to the built-in one, except that it stores a distinct timestamp for each key. | Modern as of v2.0.0 |
|
|
528
|
-
| [`rate-limit-postgresql`](https://www.npmjs.com/package/@acpr/rate-limit-postgresql) | A [PostgreSQL](https://www.postgresql.org/)-backed store. | Modern as of v1.1.0 |
|
|
529
|
-
|
|
530
|
-
Take a look at
|
|
531
|
-
[this guide](https://github.com/express-rate-limit/express-rate-limit/wiki/Creating-Your-Own-Store)
|
|
532
|
-
if you wish to create your own store.
|
|
533
|
-
|
|
534
|
-
## Request API
|
|
535
|
-
|
|
536
|
-
A `req.rateLimit` property is added to all requests with the `limit`, `used`,
|
|
537
|
-
and `remaining` number of requests and, if the store provides it, a `resetTime`
|
|
538
|
-
Date object. These may be used in your application code to take additional
|
|
539
|
-
actions or inform the user of their status.
|
|
540
|
-
|
|
541
|
-
Note that `used` includes the current request, so it should always be > 0.
|
|
542
|
-
|
|
543
|
-
The property name can be configured with the configuration option
|
|
544
|
-
`requestPropertyName`.
|
|
545
|
-
|
|
546
|
-
## Instance API
|
|
547
|
-
|
|
548
|
-
### `resetKey(key)`
|
|
549
|
-
|
|
550
|
-
Resets the rate limiting for a given key. An example use case is to allow users
|
|
551
|
-
to complete a captcha to reset their rate limit, then call this function.
|
|
48
|
+
---
|
|
552
49
|
|
|
553
|
-
|
|
50
|
+
Thanks to Mintlify for hosting the documentation at
|
|
51
|
+
[express-rate-limit.mintlify.app](https://express-rate-limit.mintlify.app)
|
|
554
52
|
|
|
555
|
-
|
|
53
|
+
<p align="center">
|
|
54
|
+
<a href="https://mintlify.com/?utm_campaign=devmark&utm_medium=readme&utm_source=express-rate-limit">
|
|
55
|
+
<img height="75" src="https://devmark-public-assets.s3.us-west-2.amazonaws.com/sponsorships/mintlify.svg" alt="Create your docs today">
|
|
56
|
+
</a>
|
|
57
|
+
</p>
|
|
556
58
|
|
|
557
|
-
|
|
558
|
-
not work with other stores. Calling it will throw an error if the store does not
|
|
559
|
-
have a `get` method.
|
|
59
|
+
---
|
|
560
60
|
|
|
561
61
|
## Issues and Contributing
|
|
562
62
|
|
|
@@ -567,8 +67,8 @@ If you need help with something, feel free to
|
|
|
567
67
|
[start a discussion](https://github.com/express-rate-limit/express-rate-limit/discussions/new)!
|
|
568
68
|
|
|
569
69
|
If you wish to contribute to the library, thanks! First, please read
|
|
570
|
-
[the contributing guide](contributing.
|
|
571
|
-
fix/implement it!
|
|
70
|
+
[the contributing guide](https://express-rate-limit.mintlify.app/docs/guides/contributing.mdx).
|
|
71
|
+
Then you can pick up any issue and fix/implement it!
|
|
572
72
|
|
|
573
73
|
## License
|
|
574
74
|
|