fastify 4.20.0 → 4.22.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 +2 -1
- package/docs/Guides/Ecosystem.md +5 -3
- package/docs/Guides/Getting-Started.md +1 -1
- package/docs/Reference/Hooks.md +3 -0
- package/docs/Reference/Server.md +199 -181
- package/docs/Reference/TypeScript.md +1 -1
- package/docs/Reference/Validation-and-Serialization.md +1 -1
- package/fastify.d.ts +7 -10
- package/fastify.js +3 -3
- package/lib/contentTypeParser.js +5 -2
- package/lib/error-serializer.js +31 -29
- package/lib/errors.js +1 -1
- package/lib/pluginOverride.js +10 -3
- package/lib/pluginUtils.js +13 -10
- package/lib/reply.js +16 -4
- package/lib/wrapThenable.js +4 -1
- package/package.json +7 -9
- package/test/async-await.test.js +1 -1
- package/test/bodyLimit.test.js +69 -0
- package/test/custom-http-server.test.js +2 -1
- package/test/https/custom-https-server.test.js +2 -1
- package/test/internals/errors.test.js +2 -2
- package/test/internals/plugin.test.js +17 -2
- package/test/internals/reply.test.js +33 -2
- package/test/plugin.test.js +26 -0
- package/test/serial/logger.0.test.js +6 -1
- package/test/stream.test.js +4 -4
- package/test/types/fastify.test-d.ts +8 -3
- package/test/types/hooks.test-d.ts +13 -0
- package/test/types/instance.test-d.ts +7 -2
- package/test/types/reply.test-d.ts +25 -0
- package/test/types/request.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +82 -1
- package/test/wrapThenable.test.js +22 -0
- package/types/hooks.d.ts +104 -4
- package/types/instance.d.ts +52 -143
- package/types/reply.d.ts +8 -6
- package/types/request.d.ts +1 -1
- package/types/route.d.ts +6 -1
- package/types/schema.d.ts +1 -1
- package/types/tsconfig.eslint.json +2 -2
- package/types/type-provider.d.ts +2 -1
- package/types/utils.d.ts +9 -0
package/docs/Reference/Server.md
CHANGED
|
@@ -98,6 +98,8 @@ describes the properties available in that options object.
|
|
|
98
98
|
### `http`
|
|
99
99
|
<a id="factory-http"></a>
|
|
100
100
|
|
|
101
|
+
+ Default: `null`
|
|
102
|
+
|
|
101
103
|
An object used to configure the server's listening socket. The options
|
|
102
104
|
are the same as the Node.js core [`createServer`
|
|
103
105
|
method](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_http_createserver_options_requestlistener).
|
|
@@ -105,20 +107,20 @@ method](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_http_create
|
|
|
105
107
|
This option is ignored if options [`http2`](#factory-http2) or
|
|
106
108
|
[`https`](#factory-https) are set.
|
|
107
109
|
|
|
108
|
-
+ Default: `null`
|
|
109
|
-
|
|
110
110
|
### `http2`
|
|
111
111
|
<a id="factory-http2"></a>
|
|
112
112
|
|
|
113
|
+
+ Default: `false`
|
|
114
|
+
|
|
113
115
|
If `true` Node.js core's
|
|
114
116
|
[HTTP/2](https://nodejs.org/dist/latest-v14.x/docs/api/http2.html) module is
|
|
115
117
|
used for binding the socket.
|
|
116
118
|
|
|
117
|
-
+ Default: `false`
|
|
118
|
-
|
|
119
119
|
### `https`
|
|
120
120
|
<a id="factory-https"></a>
|
|
121
121
|
|
|
122
|
+
+ Default: `null`
|
|
123
|
+
|
|
122
124
|
An object used to configure the server's listening socket for TLS. The options
|
|
123
125
|
are the same as the Node.js core [`createServer`
|
|
124
126
|
method](https://nodejs.org/dist/latest-v14.x/docs/api/https.html#https_https_createserver_options_requestlistener).
|
|
@@ -126,96 +128,100 @@ When this property is `null`, the socket will not be configured for TLS.
|
|
|
126
128
|
|
|
127
129
|
This option also applies when the [`http2`](#factory-http2) option is set.
|
|
128
130
|
|
|
129
|
-
+ Default: `null`
|
|
130
|
-
|
|
131
131
|
### `connectionTimeout`
|
|
132
132
|
<a id="factory-connection-timeout"></a>
|
|
133
133
|
|
|
134
|
+
+ Default: `0` (no timeout)
|
|
135
|
+
|
|
134
136
|
Defines the server timeout in milliseconds. See documentation for
|
|
135
137
|
[`server.timeout`
|
|
136
138
|
property](https://nodejs.org/api/http.html#http_server_timeout) to understand
|
|
137
|
-
the effect of this option.
|
|
138
|
-
is ignored.
|
|
139
|
+
the effect of this option.
|
|
139
140
|
|
|
140
|
-
|
|
141
|
+
When `serverFactory` option is specified this option is ignored.
|
|
141
142
|
|
|
142
143
|
### `keepAliveTimeout`
|
|
143
144
|
<a id="factory-keep-alive-timeout"></a>
|
|
144
145
|
|
|
146
|
+
+ Default: `72000` (72 seconds)
|
|
147
|
+
|
|
145
148
|
Defines the server keep-alive timeout in milliseconds. See documentation for
|
|
146
149
|
[`server.keepAliveTimeout`
|
|
147
150
|
property](https://nodejs.org/api/http.html#http_server_keepalivetimeout) to
|
|
148
151
|
understand the effect of this option. This option only applies when HTTP/1 is in
|
|
149
|
-
use.
|
|
152
|
+
use.
|
|
150
153
|
|
|
151
|
-
|
|
154
|
+
When `serverFactory` option is specified this option is ignored.
|
|
152
155
|
|
|
153
156
|
### `forceCloseConnections`
|
|
154
157
|
<a id="forcecloseconnections"></a>
|
|
155
158
|
|
|
159
|
+
+ Default: `"idle"` if the HTTP server allows it, `false` otherwise
|
|
160
|
+
|
|
156
161
|
When set to `true`, upon [`close`](#close) the server will iterate the current
|
|
157
162
|
persistent connections and [destroy their
|
|
158
163
|
sockets](https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketdestroyerror).
|
|
159
164
|
|
|
160
|
-
>
|
|
161
|
-
>
|
|
165
|
+
> **Warning**
|
|
166
|
+
> Connections are not inspected to determine if requests have
|
|
167
|
+
> been completed.
|
|
162
168
|
|
|
163
169
|
Fastify will prefer the HTTP server's
|
|
164
170
|
[`closeAllConnections`](https://nodejs.org/dist/latest-v18.x/docs/api/http.html#servercloseallconnections)
|
|
165
|
-
method if supported, otherwise it will use internal connection tracking.
|
|
171
|
+
method if supported, otherwise, it will use internal connection tracking.
|
|
166
172
|
|
|
167
173
|
When set to `"idle"`, upon [`close`](#close) the server will iterate the current
|
|
168
174
|
persistent connections which are not sending a request or waiting for a response
|
|
169
|
-
and destroy their sockets. The value is supported
|
|
175
|
+
and destroy their sockets. The value is only supported if the HTTP server
|
|
170
176
|
supports the
|
|
171
177
|
[`closeIdleConnections`](https://nodejs.org/dist/latest-v18.x/docs/api/http.html#servercloseidleconnections)
|
|
172
178
|
method, otherwise attempting to set it will throw an exception.
|
|
173
179
|
|
|
174
|
-
+ Default: `"idle"` if the HTTP server allows it, `false` otherwise
|
|
175
|
-
|
|
176
180
|
### `maxRequestsPerSocket`
|
|
177
181
|
<a id="factory-max-requests-per-socket"></a>
|
|
178
182
|
|
|
179
|
-
|
|
180
|
-
|
|
183
|
+
+ Default: `0` (no limit)
|
|
184
|
+
|
|
185
|
+
Defines the maximum number of requests a socket can handle before closing keep
|
|
186
|
+
alive connection. See [`server.maxRequestsPerSocket`
|
|
181
187
|
property](https://nodejs.org/dist/latest/docs/api/http.html#http_server_maxrequestspersocket)
|
|
182
188
|
to understand the effect of this option. This option only applies when HTTP/1.1
|
|
183
189
|
is in use. Also, when `serverFactory` option is specified, this option is
|
|
184
190
|
ignored.
|
|
185
|
-
> At the time of this writing, only node version greater or equal to 16.10.0
|
|
186
|
-
> support this option. Check the Node.js documentation for availability in the
|
|
187
|
-
> version you are running.
|
|
188
191
|
|
|
189
|
-
|
|
192
|
+
> **Note**
|
|
193
|
+
> At the time of writing, only node >= v16.10.0 supports this option.
|
|
190
194
|
|
|
191
195
|
### `requestTimeout`
|
|
192
196
|
<a id="factory-request-timeout"></a>
|
|
193
197
|
|
|
198
|
+
+ Default: `0` (no limit)
|
|
199
|
+
|
|
194
200
|
Defines the maximum number of milliseconds for receiving the entire request from
|
|
195
|
-
the client. [`server.requestTimeout`
|
|
201
|
+
the client. See [`server.requestTimeout`
|
|
196
202
|
property](https://nodejs.org/dist/latest/docs/api/http.html#http_server_requesttimeout)
|
|
197
|
-
to understand the effect of this option.
|
|
198
|
-
specified, this option is ignored. It must be set to a non-zero value (e.g. 120
|
|
199
|
-
seconds) to protect against potential Denial-of-Service attacks in case the
|
|
200
|
-
server is deployed without a reverse proxy in front.
|
|
201
|
-
> At the time of this writing, only node version greater or equal to 14.11.0
|
|
202
|
-
> support this option. Check the Node.js documentation for availability in the
|
|
203
|
-
> version you are running.
|
|
203
|
+
to understand the effect of this option.
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
When `serverFactory` option is specified, this option is ignored.
|
|
206
|
+
It must be set to a non-zero value (e.g. 120 seconds) to protect against potential
|
|
207
|
+
Denial-of-Service attacks in case the server is deployed without a reverse proxy
|
|
208
|
+
in front.
|
|
209
|
+
|
|
210
|
+
> **Note**
|
|
211
|
+
> At the time of writing, only node >= v14.11.0 supports this option
|
|
206
212
|
|
|
207
213
|
### `ignoreTrailingSlash`
|
|
208
214
|
<a id="factory-ignore-slash"></a>
|
|
209
215
|
|
|
216
|
+
+ Default: `false`
|
|
217
|
+
|
|
210
218
|
Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) to handle
|
|
211
|
-
routing. By default, Fastify
|
|
212
|
-
Paths like `/foo` and `/foo/`
|
|
219
|
+
routing. By default, Fastify will take into account the trailing slashes.
|
|
220
|
+
Paths like `/foo` and `/foo/` are treated as different paths. If you want to
|
|
213
221
|
change this, set this flag to `true`. That way, both `/foo` and `/foo/` will
|
|
214
222
|
point to the same route. This option applies to *all* route registrations for
|
|
215
223
|
the resulting server instance.
|
|
216
224
|
|
|
217
|
-
+ Default: `false`
|
|
218
|
-
|
|
219
225
|
```js
|
|
220
226
|
const fastify = require('fastify')({
|
|
221
227
|
ignoreTrailingSlash: true
|
|
@@ -235,17 +241,17 @@ fastify.get('/bar', function (req, reply) {
|
|
|
235
241
|
### `ignoreDuplicateSlashes`
|
|
236
242
|
<a id="factory-ignore-duplicate-slashes"></a>
|
|
237
243
|
|
|
244
|
+
+ Default: `false`
|
|
245
|
+
|
|
238
246
|
Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) to handle
|
|
239
247
|
routing. You can use `ignoreDuplicateSlashes` option to remove duplicate slashes
|
|
240
|
-
from the path. It removes duplicate slashes in the route path and
|
|
248
|
+
from the path. It removes duplicate slashes in the route path and the request
|
|
241
249
|
URL. This option applies to *all* route registrations for the resulting server
|
|
242
250
|
instance.
|
|
243
251
|
|
|
244
|
-
|
|
245
|
-
to true
|
|
246
|
-
meaning
|
|
247
|
-
|
|
248
|
-
+ Default: `false`
|
|
252
|
+
When `ignoreTrailingSlash` and `ignoreDuplicateSlashes` are both set
|
|
253
|
+
to `true` Fastify will remove duplicate slashes, and then trailing slashes,
|
|
254
|
+
meaning `//a//b//c//` will be converted to `/a/b/c`.
|
|
249
255
|
|
|
250
256
|
```js
|
|
251
257
|
const fastify = require('fastify')({
|
|
@@ -263,46 +269,49 @@ fastify.get('///foo//bar//', function (req, reply) {
|
|
|
263
269
|
|
|
264
270
|
You can set a custom length for parameters in parametric (standard, regex, and
|
|
265
271
|
multi) routes by using `maxParamLength` option; the default value is 100
|
|
266
|
-
characters.
|
|
272
|
+
characters. If the maximum length limit is reached, the not found route will
|
|
273
|
+
be invoked.
|
|
267
274
|
|
|
268
275
|
This can be useful especially if you have a regex-based route, protecting you
|
|
269
|
-
against [
|
|
276
|
+
against [ReDoS
|
|
270
277
|
attacks](https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS).
|
|
271
278
|
|
|
272
|
-
*If the maximum length limit is reached, the not found route will be invoked.*
|
|
273
|
-
|
|
274
279
|
### `bodyLimit`
|
|
275
280
|
<a id="factory-body-limit"></a>
|
|
276
281
|
|
|
277
|
-
Defines the maximum payload, in bytes, the server is allowed to accept.
|
|
278
|
-
|
|
279
282
|
+ Default: `1048576` (1MiB)
|
|
280
283
|
|
|
284
|
+
Defines the maximum payload, in bytes, the server is allowed to accept.
|
|
285
|
+
The default body reader sends [`FST_ERR_CTP_BODY_TOO_LARGE`](./Errors.md#fst_err_ctp_body_too_large)
|
|
286
|
+
reply, if the size of the body exceeds this limit.
|
|
287
|
+
If [`preParsing` hook](./Hooks.md#preparsing) is provided, this limit is applied
|
|
288
|
+
to the size of the stream the hook returns (i.e. the size of "decoded" body).
|
|
289
|
+
|
|
281
290
|
### `onProtoPoisoning`
|
|
282
291
|
<a id="factory-on-proto-poisoning"></a>
|
|
283
292
|
|
|
293
|
+
+ Default: `'error'`
|
|
294
|
+
|
|
284
295
|
Defines what action the framework must take when parsing a JSON object with
|
|
285
296
|
`__proto__`. This functionality is provided by
|
|
286
297
|
[secure-json-parse](https://github.com/fastify/secure-json-parse). See
|
|
287
298
|
[Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more details about
|
|
288
299
|
prototype poisoning attacks.
|
|
289
300
|
|
|
290
|
-
Possible values are `'error'`, `'remove'
|
|
291
|
-
|
|
292
|
-
+ Default: `'error'`
|
|
301
|
+
Possible values are `'error'`, `'remove'`, or `'ignore'`.
|
|
293
302
|
|
|
294
303
|
### `onConstructorPoisoning`
|
|
295
304
|
<a id="factory-on-constructor-poisoning"></a>
|
|
296
305
|
|
|
306
|
+
+ Default: `'error'`
|
|
307
|
+
|
|
297
308
|
Defines what action the framework must take when parsing a JSON object with
|
|
298
309
|
`constructor`. This functionality is provided by
|
|
299
310
|
[secure-json-parse](https://github.com/fastify/secure-json-parse). See
|
|
300
311
|
[Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more details about
|
|
301
312
|
prototype poisoning attacks.
|
|
302
313
|
|
|
303
|
-
Possible values are `'error'`, `'remove'
|
|
304
|
-
|
|
305
|
-
+ Default: `'error'`
|
|
314
|
+
Possible values are `'error'`, `'remove'`, or `'ignore'`.
|
|
306
315
|
|
|
307
316
|
### `logger`
|
|
308
317
|
<a id="factory-logger"></a>
|
|
@@ -363,13 +372,18 @@ The possible values this property may have are:
|
|
|
363
372
|
### `disableRequestLogging`
|
|
364
373
|
<a id="factory-disable-request-logging"></a>
|
|
365
374
|
|
|
366
|
-
|
|
375
|
+
+ Default: `false`
|
|
376
|
+
|
|
377
|
+
When logging is enabled, Fastify will issue an `info` level log
|
|
367
378
|
message when a request is received and when the response for that request has
|
|
368
379
|
been sent. By setting this option to `true`, these log messages will be
|
|
369
380
|
disabled. This allows for more flexible request start and end logging by
|
|
370
381
|
attaching custom `onRequest` and `onResponse` hooks.
|
|
371
382
|
|
|
372
|
-
|
|
383
|
+
Please note that this option will also disable an error log written by the
|
|
384
|
+
default `onResponse` hook on reply callback errors. Other log messages
|
|
385
|
+
emitted by Fastify will stay enabled, like deprecation warnings and messages
|
|
386
|
+
emitted when requests are received while the server is closing.
|
|
373
387
|
|
|
374
388
|
```js
|
|
375
389
|
// Examples of hooks to replicate the disabled functionality.
|
|
@@ -384,11 +398,6 @@ fastify.addHook('onResponse', (req, reply, done) => {
|
|
|
384
398
|
})
|
|
385
399
|
```
|
|
386
400
|
|
|
387
|
-
Please note that this setting will also disable an error log written by the
|
|
388
|
-
default `onResponse` hook on reply callback errors. Other log messages
|
|
389
|
-
emitted by Fastify will stay enabled, like deprecation warnings and messages
|
|
390
|
-
emitted when requests are received while the server is closing.
|
|
391
|
-
|
|
392
401
|
### `serverFactory`
|
|
393
402
|
<a id="custom-http-server"></a>
|
|
394
403
|
|
|
@@ -428,13 +437,17 @@ enhance the server instance inside the `serverFactory` function before the
|
|
|
428
437
|
|
|
429
438
|
+ Default: `true`
|
|
430
439
|
|
|
431
|
-
|
|
440
|
+
By default, Fastify will automatically infer the root properties
|
|
432
441
|
of JSON Schemas if it does not find valid root properties according to the JSON
|
|
433
|
-
Schema spec. If you wish to implement your own schema validation compiler,
|
|
434
|
-
|
|
442
|
+
Schema spec. If you wish to implement your own schema validation compiler, to
|
|
443
|
+
parse schemas as JTD instead of JSON Schema for example, then you can explicitly
|
|
435
444
|
set this option to `false` to make sure the schemas you receive are unmodified
|
|
436
445
|
and are not being treated internally as JSON Schema.
|
|
437
446
|
|
|
447
|
+
Fastify does not throw on invalid schemas so if this option is set to `false`
|
|
448
|
+
in an existing project, check that none of your existing schemas become
|
|
449
|
+
invalid as a result, as they will be treated as catch-alls.
|
|
450
|
+
|
|
438
451
|
```js
|
|
439
452
|
const AjvJTD = require('ajv/dist/jtd'/* only valid for AJV v7+ */)
|
|
440
453
|
const ajv = new AjvJTD({
|
|
@@ -457,21 +470,23 @@ fastify.post('/', {
|
|
|
457
470
|
})
|
|
458
471
|
```
|
|
459
472
|
|
|
460
|
-
**Note: Fastify does not currently throw on invalid schemas, so if you turn this
|
|
461
|
-
off in an existing project, you need to be careful that none of your existing
|
|
462
|
-
schemas become invalid as a result, since they will be treated as a catch-all.**
|
|
463
|
-
|
|
464
473
|
### `caseSensitive`
|
|
465
474
|
<a id="factory-case-sensitive"></a>
|
|
466
475
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
476
|
+
+ Default: `true`
|
|
477
|
+
|
|
478
|
+
When `true` routes are registered as case-sensitive. That is, `/foo`
|
|
479
|
+
is not equal to `/Foo`.
|
|
480
|
+
When `false` then routes are case-insensitive.
|
|
481
|
+
|
|
482
|
+
Please note that setting this option to `false` goes against
|
|
483
|
+
[RFC3986](https://tools.ietf.org/html/rfc3986#section-6.2.2.1).
|
|
471
484
|
|
|
472
485
|
By setting `caseSensitive` to `false`, all paths will be matched as lowercase,
|
|
473
486
|
but the route parameters or wildcards will maintain their original letter
|
|
474
|
-
casing.
|
|
487
|
+
casing.
|
|
488
|
+
This option does not affect query strings, please refer to
|
|
489
|
+
[`querystringParser`](#querystringparser) to change their handling.
|
|
475
490
|
|
|
476
491
|
```js
|
|
477
492
|
fastify.get('/user/:username', (request, reply) => {
|
|
@@ -480,19 +495,13 @@ fastify.get('/user/:username', (request, reply) => {
|
|
|
480
495
|
})
|
|
481
496
|
```
|
|
482
497
|
|
|
483
|
-
Please note that setting this option to `false` goes against
|
|
484
|
-
[RFC3986](https://tools.ietf.org/html/rfc3986#section-6.2.2.1).
|
|
485
|
-
|
|
486
|
-
Also note, this setting will not affect query strings. If you want to change the
|
|
487
|
-
way query strings are handled take a look at
|
|
488
|
-
[`querystringParser`](#querystringparser).
|
|
489
|
-
|
|
490
|
-
|
|
491
498
|
### `allowUnsafeRegex`
|
|
492
499
|
<a id="factory-allow-unsafe-regex"></a>
|
|
493
500
|
|
|
494
|
-
|
|
495
|
-
|
|
501
|
+
+ Default `false`
|
|
502
|
+
|
|
503
|
+
Disabled by default, so routes only allow safe regular expressions. To use
|
|
504
|
+
unsafe expressions, set `allowUnsafeRegex` to `true`.
|
|
496
505
|
|
|
497
506
|
```js
|
|
498
507
|
fastify.get('/user/:id(^([0-9]+){4}$)', (request, reply) => {
|
|
@@ -500,18 +509,14 @@ fastify.get('/user/:id(^([0-9]+){4}$)', (request, reply) => {
|
|
|
500
509
|
})
|
|
501
510
|
```
|
|
502
511
|
|
|
503
|
-
Under the hood: [FindMyWay](https://github.com/delvedor/find-my-way) More info
|
|
504
|
-
about safe regexp: [Safe-regex2](https://www.npmjs.com/package/safe-regex2)
|
|
505
|
-
|
|
506
|
-
|
|
507
512
|
### `requestIdHeader`
|
|
508
513
|
<a id="factory-request-id-header"></a>
|
|
509
514
|
|
|
515
|
+
+ Default: `'request-id'`
|
|
516
|
+
|
|
510
517
|
The header name used to set the request-id. See [the
|
|
511
518
|
request-id](./Logging.md#logging-request-id) section.
|
|
512
|
-
Setting `requestIdHeader` to `false` will always use [genReqId](#genreqid)
|
|
513
|
-
|
|
514
|
-
+ Default: `'request-id'`
|
|
519
|
+
Setting `requestIdHeader` to `false` will always use [genReqId](#genreqid).
|
|
515
520
|
|
|
516
521
|
```js
|
|
517
522
|
const fastify = require('fastify')({
|
|
@@ -519,25 +524,31 @@ const fastify = require('fastify')({
|
|
|
519
524
|
//requestIdHeader: false, // -> always use genReqId
|
|
520
525
|
})
|
|
521
526
|
```
|
|
527
|
+
|
|
522
528
|
### `requestIdLogLabel`
|
|
523
529
|
<a id="factory-request-id-log-label"></a>
|
|
524
530
|
|
|
525
|
-
Defines the label used for the request identifier when logging the request.
|
|
526
|
-
|
|
527
531
|
+ Default: `'reqId'`
|
|
528
532
|
|
|
533
|
+
Defines the label used for the request identifier when logging the request.
|
|
534
|
+
|
|
529
535
|
### `genReqId`
|
|
530
536
|
<a id="factory-gen-request-id"></a>
|
|
531
537
|
|
|
532
|
-
Function for generating the request-id. It will receive the _raw_ incoming
|
|
533
|
-
request as a parameter. This function is expected to be error-free.
|
|
534
|
-
|
|
535
538
|
+ Default: `value of 'request-id' header if provided or monotonically increasing
|
|
536
539
|
integers`
|
|
537
540
|
|
|
541
|
+
Function for generating the request-id. It will receive the _raw_ incoming
|
|
542
|
+
request as a parameter. This function is expected to be error-free.
|
|
543
|
+
|
|
538
544
|
Especially in distributed systems, you may want to override the default ID
|
|
539
545
|
generation behavior as shown below. For generating `UUID`s you may want to check
|
|
540
|
-
out [hyperid](https://github.com/mcollina/hyperid)
|
|
546
|
+
out [hyperid](https://github.com/mcollina/hyperid).
|
|
547
|
+
|
|
548
|
+
> **Note**
|
|
549
|
+
> `genReqId` will be not called if the header set in
|
|
550
|
+
> <code>[requestIdHeader](#requestidheader)</code> is available (defaults to
|
|
551
|
+
> 'request-id').
|
|
541
552
|
|
|
542
553
|
```js
|
|
543
554
|
let i = 0
|
|
@@ -546,21 +557,9 @@ const fastify = require('fastify')({
|
|
|
546
557
|
})
|
|
547
558
|
```
|
|
548
559
|
|
|
549
|
-
**Note: genReqId will _not_ be called if the header set in
|
|
550
|
-
<code>[requestIdHeader](#requestidheader)</code> is available (defaults to
|
|
551
|
-
'request-id').**
|
|
552
|
-
|
|
553
560
|
### `trustProxy`
|
|
554
561
|
<a id="factory-trust-proxy"></a>
|
|
555
562
|
|
|
556
|
-
By enabling the `trustProxy` option, Fastify will know that it is sitting behind
|
|
557
|
-
a proxy and that the `X-Forwarded-*` header fields may be trusted, which
|
|
558
|
-
otherwise may be easily spoofed.
|
|
559
|
-
|
|
560
|
-
```js
|
|
561
|
-
const fastify = Fastify({ trustProxy: true })
|
|
562
|
-
```
|
|
563
|
-
|
|
564
563
|
+ Default: `false`
|
|
565
564
|
+ `true/false`: Trust all proxies (`true`) or do not trust any proxies
|
|
566
565
|
(`false`).
|
|
@@ -575,6 +574,14 @@ const fastify = Fastify({ trustProxy: true })
|
|
|
575
574
|
}
|
|
576
575
|
```
|
|
577
576
|
|
|
577
|
+
By enabling the `trustProxy` option, Fastify will know that it is sitting behind
|
|
578
|
+
a proxy and that the `X-Forwarded-*` header fields may be trusted, which
|
|
579
|
+
otherwise may be easily spoofed.
|
|
580
|
+
|
|
581
|
+
```js
|
|
582
|
+
const fastify = Fastify({ trustProxy: true })
|
|
583
|
+
```
|
|
584
|
+
|
|
578
585
|
For more examples, refer to the
|
|
579
586
|
[`proxy-addr`](https://www.npmjs.com/package/proxy-addr) package.
|
|
580
587
|
|
|
@@ -590,28 +597,32 @@ fastify.get('/', (request, reply) => {
|
|
|
590
597
|
})
|
|
591
598
|
```
|
|
592
599
|
|
|
593
|
-
**Note
|
|
594
|
-
|
|
595
|
-
derive
|
|
600
|
+
> **Note**
|
|
601
|
+
> If a request contains multiple `x-forwarded-host` or `x-forwarded-proto`
|
|
602
|
+
> headers, it is only the last one that is used to derive `request.hostname`
|
|
603
|
+
> and `request.protocol`.
|
|
596
604
|
|
|
597
605
|
### `pluginTimeout`
|
|
598
606
|
<a id="plugin-timeout"></a>
|
|
599
607
|
|
|
608
|
+
+ Default: `10000`
|
|
609
|
+
|
|
600
610
|
The maximum amount of time in *milliseconds* in which a plugin can load. If not,
|
|
601
611
|
[`ready`](#ready) will complete with an `Error` with code
|
|
602
612
|
`'ERR_AVVIO_PLUGIN_TIMEOUT'`. When set to `0`, disables this check. This
|
|
603
613
|
controls [avvio](https://www.npmjs.com/package/avvio) 's `timeout` parameter.
|
|
604
614
|
|
|
605
|
-
+ Default: `10000`
|
|
606
|
-
|
|
607
615
|
### `querystringParser`
|
|
608
616
|
<a id="factory-querystring-parser"></a>
|
|
609
617
|
|
|
610
618
|
The default query string parser that Fastify uses is the Node.js's core
|
|
611
619
|
`querystring` module.
|
|
612
620
|
|
|
613
|
-
You can
|
|
614
|
-
|
|
621
|
+
You can use this option to use a custom parser, such as
|
|
622
|
+
[`qs`](https://www.npmjs.com/package/qs).
|
|
623
|
+
|
|
624
|
+
If you only want the keys (and not the values) to be case insensitive we
|
|
625
|
+
recommend using a custom parser to convert only the keys to lowercase.
|
|
615
626
|
|
|
616
627
|
```js
|
|
617
628
|
const qs = require('qs')
|
|
@@ -620,7 +631,7 @@ const fastify = require('fastify')({
|
|
|
620
631
|
})
|
|
621
632
|
```
|
|
622
633
|
|
|
623
|
-
You can also use Fastify's default parser but change some handling
|
|
634
|
+
You can also use Fastify's default parser but change some handling behavior,
|
|
624
635
|
like the example below for case insensitive keys and values:
|
|
625
636
|
|
|
626
637
|
```js
|
|
@@ -630,24 +641,21 @@ const fastify = require('fastify')({
|
|
|
630
641
|
})
|
|
631
642
|
```
|
|
632
643
|
|
|
633
|
-
Note, if you only want the keys (and not the values) to be case insensitive we
|
|
634
|
-
recommend using a custom parser to convert only the keys to lowercase.
|
|
635
|
-
|
|
636
644
|
### `exposeHeadRoutes`
|
|
637
645
|
<a id="exposeHeadRoutes"></a>
|
|
638
646
|
|
|
647
|
+
+ Default: `true`
|
|
648
|
+
|
|
639
649
|
Automatically creates a sibling `HEAD` route for each `GET` route defined. If
|
|
640
650
|
you want a custom `HEAD` handler without disabling this option, make sure to
|
|
641
651
|
define it before the `GET` route.
|
|
642
652
|
|
|
643
|
-
+ Default: `true`
|
|
644
|
-
|
|
645
653
|
### `constraints`
|
|
646
654
|
<a id="constraints"></a>
|
|
647
655
|
|
|
648
|
-
Fastify's built
|
|
649
|
-
constraining routes by `version` or `host`. You
|
|
650
|
-
strategies, or override the built
|
|
656
|
+
Fastify's built-in route constraints are provided by `find-my-way`, which
|
|
657
|
+
allows constraining routes by `version` or `host`. You can add new constraint
|
|
658
|
+
strategies, or override the built-in strategies, by providing a `constraints`
|
|
651
659
|
object with strategies for `find-my-way`. You can find more information on
|
|
652
660
|
constraint strategies in the
|
|
653
661
|
[find-my-way](https://github.com/delvedor/find-my-way) documentation.
|
|
@@ -676,11 +684,11 @@ const fastify = require('fastify')({
|
|
|
676
684
|
### `return503OnClosing`
|
|
677
685
|
<a id="factory-return-503-on-closing"></a>
|
|
678
686
|
|
|
687
|
+
+ Default: `true`
|
|
688
|
+
|
|
679
689
|
Returns 503 after calling `close` server method. If `false`, the server routes
|
|
680
690
|
the incoming request as usual.
|
|
681
691
|
|
|
682
|
-
+ Default: `true`
|
|
683
|
-
|
|
684
692
|
### `ajv`
|
|
685
693
|
<a id="factory-ajv"></a>
|
|
686
694
|
|
|
@@ -709,7 +717,7 @@ const fastify = require('fastify')({
|
|
|
709
717
|
|
|
710
718
|
Customize the options of the default
|
|
711
719
|
[`fast-json-stringify`](https://github.com/fastify/fast-json-stringify#options)
|
|
712
|
-
instance that
|
|
720
|
+
instance that serializes the response's payload:
|
|
713
721
|
|
|
714
722
|
```js
|
|
715
723
|
const fastify = require('fastify')({
|
|
@@ -722,15 +730,17 @@ const fastify = require('fastify')({
|
|
|
722
730
|
### `http2SessionTimeout`
|
|
723
731
|
<a id="http2-session-timeout"></a>
|
|
724
732
|
|
|
733
|
+
+ Default: `72000`
|
|
734
|
+
|
|
725
735
|
Set a default
|
|
726
|
-
[timeout](https://nodejs.org/api/http2.html#
|
|
727
|
-
to every incoming HTTP/2 session. The session will be closed on
|
|
728
|
-
|
|
736
|
+
[timeout](https://nodejs.org/api/http2.html#http2sessionsettimeoutmsecs-callback)
|
|
737
|
+
to every incoming HTTP/2 session in milliseconds. The session will be closed on
|
|
738
|
+
the timeout.
|
|
729
739
|
|
|
730
|
-
|
|
740
|
+
This option is needed to offer a graceful "close" experience when using
|
|
731
741
|
HTTP/2. The low default has been chosen to mitigate denial of service attacks.
|
|
732
742
|
When the server is behind a load balancer or can scale automatically this value
|
|
733
|
-
can be increased to fit the use case. Node core defaults this to `0`.
|
|
743
|
+
can be increased to fit the use case. Node core defaults this to `0`.
|
|
734
744
|
|
|
735
745
|
### `frameworkErrors`
|
|
736
746
|
<a id="framework-errors"></a>
|
|
@@ -741,8 +751,8 @@ Fastify provides default error handlers for the most common use cases. It is
|
|
|
741
751
|
possible to override one or more of those handlers with custom code using this
|
|
742
752
|
option.
|
|
743
753
|
|
|
744
|
-
|
|
745
|
-
|
|
754
|
+
> **Note**
|
|
755
|
+
> Only `FST_ERR_BAD_URL` and `FST_ERR_ASYNC_CONSTRAINT` are implemented at present.
|
|
746
756
|
|
|
747
757
|
```js
|
|
748
758
|
const fastify = require('fastify')({
|
|
@@ -794,10 +804,11 @@ function defaultClientErrorHandler (err, socket) {
|
|
|
794
804
|
}
|
|
795
805
|
```
|
|
796
806
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
807
|
+
> **Note**
|
|
808
|
+
> `clientErrorHandler` operates with raw sockets. The handler is expected to
|
|
809
|
+
> return a properly formed HTTP response that includes a status line, HTTP headers
|
|
810
|
+
> and a message body. Before attempting to write the socket, the handler should
|
|
811
|
+
> check if the socket is still writable as it may have already been destroyed.
|
|
801
812
|
|
|
802
813
|
```js
|
|
803
814
|
const fastify = require('fastify')({
|
|
@@ -826,9 +837,11 @@ const fastify = require('fastify')({
|
|
|
826
837
|
<a id="rewrite-url"></a>
|
|
827
838
|
|
|
828
839
|
Set a sync callback function that must return a string that allows rewriting
|
|
829
|
-
URLs.
|
|
840
|
+
URLs. This is useful when you are behind a proxy that changes the URL.
|
|
841
|
+
Rewriting a URL will modify the `url` property of the `req` object.
|
|
830
842
|
|
|
831
|
-
|
|
843
|
+
Note that `rewriteUrl` is called _before_ routing, it is not encapsulated and it
|
|
844
|
+
is an instance-wide configuration.
|
|
832
845
|
|
|
833
846
|
```js
|
|
834
847
|
// @param {object} req The raw Node.js HTTP request, not the `FastifyRequest` object.
|
|
@@ -844,9 +857,6 @@ function rewriteUrl (req) {
|
|
|
844
857
|
}
|
|
845
858
|
```
|
|
846
859
|
|
|
847
|
-
Note that `rewriteUrl` is called _before_ routing, it is not encapsulated and it
|
|
848
|
-
is an instance-wide configuration.
|
|
849
|
-
|
|
850
860
|
## Instance
|
|
851
861
|
|
|
852
862
|
### Server Methods
|
|
@@ -858,8 +868,9 @@ is an instance-wide configuration.
|
|
|
858
868
|
[server](https://nodejs.org/api/http.html#http_class_http_server) object as
|
|
859
869
|
returned by the [**`Fastify factory function`**](#factory).
|
|
860
870
|
|
|
861
|
-
>
|
|
862
|
-
>
|
|
871
|
+
> **Warning**
|
|
872
|
+
> If utilized improperly, certain Fastify features could be disrupted.
|
|
873
|
+
> It is recommended to only use it for attaching listeners.
|
|
863
874
|
|
|
864
875
|
#### after
|
|
865
876
|
<a id="after"></a>
|
|
@@ -1057,8 +1068,9 @@ Note that the array contains the `fastify.server.address()` too.
|
|
|
1057
1068
|
#### getDefaultRoute
|
|
1058
1069
|
<a id="getDefaultRoute"></a>
|
|
1059
1070
|
|
|
1060
|
-
**
|
|
1061
|
-
|
|
1071
|
+
> **Warning**
|
|
1072
|
+
> This method is deprecated and will be removed in the next Fastify
|
|
1073
|
+
> major version.
|
|
1062
1074
|
|
|
1063
1075
|
The `defaultRoute` handler handles requests that do not match any URL specified
|
|
1064
1076
|
by your Fastify application. This defaults to the 404 handler, but can be
|
|
@@ -1072,9 +1084,10 @@ const defaultRoute = fastify.getDefaultRoute()
|
|
|
1072
1084
|
#### setDefaultRoute
|
|
1073
1085
|
<a id="setDefaultRoute"></a>
|
|
1074
1086
|
|
|
1075
|
-
**
|
|
1076
|
-
|
|
1077
|
-
|
|
1087
|
+
> **Warning**
|
|
1088
|
+
> This method is deprecated and will be removed in the next Fastify
|
|
1089
|
+
> major version. Please, consider using `setNotFoundHandler` or a wildcard
|
|
1090
|
+
> matching route.
|
|
1078
1091
|
|
|
1079
1092
|
The default 404 handler, or one set using `setNotFoundHandler`, will
|
|
1080
1093
|
never trigger if the default route is overridden. This sets the handler for the
|
|
@@ -1117,9 +1130,9 @@ Method to add routes to the server, it also has shorthand functions, check
|
|
|
1117
1130
|
<a id="hasRoute"></a>
|
|
1118
1131
|
|
|
1119
1132
|
Method to check if a route is already registered to the internal router. It
|
|
1120
|
-
expects an object as payload. `url` and `method` are mandatory fields. It
|
|
1121
|
-
possible to also specify `constraints`. The method returns true if the
|
|
1122
|
-
registered
|
|
1133
|
+
expects an object as the payload. `url` and `method` are mandatory fields. It
|
|
1134
|
+
is possible to also specify `constraints`. The method returns `true` if the
|
|
1135
|
+
route is registered or `false` if not.
|
|
1123
1136
|
|
|
1124
1137
|
```js
|
|
1125
1138
|
const routeExists = fastify.hasRoute({
|
|
@@ -1220,11 +1233,12 @@ different ways to define a name (in order).
|
|
|
1220
1233
|
Newlines are replaced by ` -- `. This will help to identify the root cause when
|
|
1221
1234
|
you deal with many plugins.
|
|
1222
1235
|
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1236
|
+
> **Warning**
|
|
1237
|
+
> If you have to deal with nested plugins, the name differs with the usage of
|
|
1238
|
+
> the [fastify-plugin](https://github.com/fastify/fastify-plugin) because
|
|
1239
|
+
> no new scope is created and therefore we have no place to attach contextual
|
|
1240
|
+
> data. In that case, the plugin name will represent the boot order of all
|
|
1241
|
+
> involved plugins in the format of `fastify -> plugin-A -> plugin-B`.
|
|
1228
1242
|
|
|
1229
1243
|
#### hasPlugin
|
|
1230
1244
|
<a id="hasPlugin"></a>
|
|
@@ -1327,7 +1341,9 @@ Set the schema error formatter for all routes. See
|
|
|
1327
1341
|
|
|
1328
1342
|
Set the schema serializer compiler for all routes. See
|
|
1329
1343
|
[#schema-serializer](./Validation-and-Serialization.md#schema-serializer).
|
|
1330
|
-
|
|
1344
|
+
|
|
1345
|
+
> **Note**
|
|
1346
|
+
> [`setReplySerializer`](#set-reply-serializer) has priority if set!
|
|
1331
1347
|
|
|
1332
1348
|
#### validatorCompiler
|
|
1333
1349
|
<a id="validator-compiler"></a>
|
|
@@ -1362,9 +1378,7 @@ This property can be used to fully manage:
|
|
|
1362
1378
|
- `compilersFactory`: what module must compile the JSON schemas
|
|
1363
1379
|
|
|
1364
1380
|
It can be useful when your schemas are stored in another data structure that is
|
|
1365
|
-
unknown to Fastify.
|
|
1366
|
-
#2446](https://github.com/fastify/fastify/issues/2446) for an example of what
|
|
1367
|
-
this property helps to resolve.
|
|
1381
|
+
unknown to Fastify.
|
|
1368
1382
|
|
|
1369
1383
|
Another use case is to tweak all the schemas processing. Doing so it is possible
|
|
1370
1384
|
to use Ajv v8 JTD or Standalone feature. To use such as JTD or the Standalone
|
|
@@ -1404,7 +1418,7 @@ const fastify = Fastify({
|
|
|
1404
1418
|
},
|
|
1405
1419
|
|
|
1406
1420
|
/**
|
|
1407
|
-
* The compilers factory
|
|
1421
|
+
* The compilers factory lets you fully control the validator and serializer
|
|
1408
1422
|
* in the Fastify's lifecycle, providing the encapsulation to your compilers.
|
|
1409
1423
|
*/
|
|
1410
1424
|
compilersFactory: {
|
|
@@ -1461,10 +1475,12 @@ lifecycle](./Lifecycle.md#lifecycle). *async-await* is supported as well.
|
|
|
1461
1475
|
You can also register [`preValidation`](./Hooks.md#route-hooks) and
|
|
1462
1476
|
[`preHandler`](./Hooks.md#route-hooks) hooks for the 404 handler.
|
|
1463
1477
|
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1478
|
+
> **Note**
|
|
1479
|
+
> The `preValidation` hook registered using this method will run for a
|
|
1480
|
+
> route that Fastify does not recognize and **not** when a route handler manually
|
|
1481
|
+
> calls [`reply.callNotFound`](./Reply.md#call-not-found). In which case, only
|
|
1482
|
+
> preHandler will be run.
|
|
1483
|
+
|
|
1468
1484
|
|
|
1469
1485
|
```js
|
|
1470
1486
|
fastify.setNotFoundHandler({
|
|
@@ -1495,8 +1511,9 @@ plugins are registered. If you would like to augment the behavior of the default
|
|
|
1495
1511
|
arguments `fastify.setNotFoundHandler()` within the context of these registered
|
|
1496
1512
|
plugins.
|
|
1497
1513
|
|
|
1498
|
-
> Note
|
|
1499
|
-
>
|
|
1514
|
+
> **Note**
|
|
1515
|
+
> Some config properties from the request object will be
|
|
1516
|
+
> undefined inside the custom not found handler. E.g.:
|
|
1500
1517
|
> `request.routerPath`, `routerMethod` and `context.config`.
|
|
1501
1518
|
> This method design goal is to allow calling the common not found route.
|
|
1502
1519
|
> To return a per-route customized 404 response, you can do it in
|
|
@@ -1510,11 +1527,12 @@ will be called whenever an error happens. The handler is bound to the Fastify
|
|
|
1510
1527
|
instance and is fully encapsulated, so different plugins can set different error
|
|
1511
1528
|
handlers. *async-await* is supported as well.
|
|
1512
1529
|
|
|
1513
|
-
|
|
1514
|
-
set it
|
|
1530
|
+
If the error `statusCode` is less than 400, Fastify will automatically
|
|
1531
|
+
set it to 500 before calling the error handler.
|
|
1515
1532
|
|
|
1516
|
-
|
|
1517
|
-
|
|
1533
|
+
> **Note**
|
|
1534
|
+
> `setErrorHandler` will ***not*** catch any error inside
|
|
1535
|
+
> an `onResponse` hook because the response has already been sent to the client.
|
|
1518
1536
|
|
|
1519
1537
|
```js
|
|
1520
1538
|
fastify.setErrorHandler(function (error, request, reply) {
|
|
@@ -1545,10 +1563,10 @@ if (statusCode >= 500) {
|
|
|
1545
1563
|
`fastify.setChildLoggerFactory(factory(logger, bindings, opts, rawReq))`: Set a
|
|
1546
1564
|
function that will be called when creating a child logger instance for each request
|
|
1547
1565
|
which allows for modifying or adding child logger bindings and logger options, or
|
|
1548
|
-
returning a
|
|
1566
|
+
returning a custom child logger implementation.
|
|
1549
1567
|
|
|
1550
|
-
Child logger bindings have a performance advantage over per-log bindings
|
|
1551
|
-
they are pre-
|
|
1568
|
+
Child logger bindings have a performance advantage over per-log bindings because
|
|
1569
|
+
they are pre-serialized by Pino when the child logger is created.
|
|
1552
1570
|
|
|
1553
1571
|
The first parameter is the parent logger instance, followed by the default bindings
|
|
1554
1572
|
and logger options which should be passed to the child logger, and finally
|
|
@@ -1616,7 +1634,7 @@ a custom constraint strategy with the same name.
|
|
|
1616
1634
|
`fastify.printRoutes()`: Fastify router builds a tree of routes for each HTTP
|
|
1617
1635
|
method. If you call the prettyPrint without specifying an HTTP method, it will
|
|
1618
1636
|
merge all the trees into one and print it. The merged tree doesn't represent the
|
|
1619
|
-
internal router structure. **
|
|
1637
|
+
internal router structure. **Do not use it for debugging.**
|
|
1620
1638
|
|
|
1621
1639
|
*Remember to call it inside or after a `ready` call.*
|
|
1622
1640
|
|
|
@@ -1658,8 +1676,8 @@ param. Printed tree will represent the internal router structure.
|
|
|
1658
1676
|
```
|
|
1659
1677
|
|
|
1660
1678
|
`fastify.printRoutes({ commonPrefix: false })` will print compressed trees. This
|
|
1661
|
-
|
|
1662
|
-
It doesn't represent the internal router structure. **
|
|
1679
|
+
may be useful when you have a large number of routes with common prefixes.
|
|
1680
|
+
It doesn't represent the internal router structure. **Do not use it for debugging.**
|
|
1663
1681
|
|
|
1664
1682
|
```js
|
|
1665
1683
|
console.log(fastify.printRoutes({ commonPrefix: false }))
|
|
@@ -1752,7 +1770,7 @@ fastify.ready(() => {
|
|
|
1752
1770
|
<a id="addContentTypeParser"></a>
|
|
1753
1771
|
|
|
1754
1772
|
`fastify.addContentTypeParser(content-type, options, parser)` is used to pass
|
|
1755
|
-
custom parser for a given content type. Useful for adding parsers for custom
|
|
1773
|
+
a custom parser for a given content type. Useful for adding parsers for custom
|
|
1756
1774
|
content types, e.g. `text/json, application/vnd.oasis.opendocument.text`.
|
|
1757
1775
|
`content-type` can be a string, string array or RegExp.
|
|
1758
1776
|
|
|
@@ -1854,7 +1872,7 @@ for more info.
|
|
|
1854
1872
|
`fastify.initialConfig`: Exposes a frozen read-only object registering the
|
|
1855
1873
|
initial options passed down by the user to the Fastify instance.
|
|
1856
1874
|
|
|
1857
|
-
|
|
1875
|
+
The properties that can currently be exposed are:
|
|
1858
1876
|
- connectionTimeout
|
|
1859
1877
|
- keepAliveTimeout
|
|
1860
1878
|
- bodyLimit
|