fastify 5.0.0 → 5.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/.borp.yaml +3 -0
- package/.vscode/settings.json +22 -0
- package/README.md +12 -7
- package/docs/Guides/Database.md +15 -15
- package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
- package/docs/Guides/Ecosystem.md +14 -15
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +11 -11
- package/docs/Guides/Migration-Guide-V5.md +133 -9
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Guides/Recommendations.md +9 -9
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +58 -57
- package/docs/Guides/Write-Plugin.md +2 -2
- package/docs/Guides/Write-Type-Provider.md +3 -3
- package/docs/Reference/ContentTypeParser.md +4 -4
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Errors.md +3 -3
- package/docs/Reference/Hooks.md +7 -7
- package/docs/Reference/LTS.md +8 -0
- package/docs/Reference/Logging.md +5 -4
- package/docs/Reference/Reply.md +55 -58
- package/docs/Reference/Request.md +49 -42
- package/docs/Reference/Routes.md +16 -13
- package/docs/Reference/Server.md +32 -28
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +5 -5
- package/examples/typescript-server.ts +1 -1
- package/fastify.d.ts +14 -5
- package/fastify.js +8 -6
- package/lib/contentTypeParser.js +9 -7
- package/lib/context.js +1 -2
- package/lib/error-handler.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/fourOhFour.js +1 -1
- package/lib/hooks.js +4 -1
- package/lib/{logger.js → logger-factory.js} +70 -122
- package/lib/logger-pino.js +68 -0
- package/lib/pluginOverride.js +1 -1
- package/lib/pluginUtils.js +2 -2
- package/lib/reply.js +4 -5
- package/lib/request.js +16 -9
- package/lib/route.js +23 -22
- package/lib/validation.js +2 -2
- package/package.json +13 -15
- package/test/404s.test.js +675 -629
- package/test/500s.test.js +72 -63
- package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
- package/test/als.test.js +48 -45
- package/test/async-await.test.js +148 -134
- package/test/async-dispose.test.js +4 -5
- package/test/async_hooks.test.js +30 -28
- package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
- package/test/buffer.test.js +9 -10
- package/test/build/error-serializer.test.js +3 -4
- package/test/build/version.test.js +2 -3
- package/test/build-certificate.js +1 -1
- package/test/bundler/README.md +5 -5
- package/test/bundler/esbuild/bundler-test.js +10 -9
- package/test/bundler/webpack/bundler-test.js +10 -9
- package/test/case-insensitive.test.js +31 -28
- package/test/chainable.test.js +4 -5
- package/test/check.test.js +8 -10
- package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
- package/test/client-timeout.test.js +5 -5
- package/test/close-pipelining.test.js +6 -8
- package/test/conditional-pino.test.js +47 -0
- package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
- package/test/constrained-routes.test.js +243 -236
- package/test/content-length.test.js +53 -68
- package/test/content-parser.test.js +186 -158
- package/test/content-type.test.js +8 -9
- package/test/context-config.test.js +44 -54
- package/test/custom-http-server.test.js +16 -20
- package/test/custom-parser.5.test.js +32 -32
- package/test/diagnostics-channel/404.test.js +15 -15
- package/test/diagnostics-channel/async-delay-request.test.js +25 -25
- package/test/diagnostics-channel/async-request.test.js +24 -24
- package/test/diagnostics-channel/error-before-handler.test.js +4 -5
- package/test/diagnostics-channel/error-request.test.js +19 -19
- package/test/diagnostics-channel/error-status.test.js +8 -8
- package/test/diagnostics-channel/init.test.js +6 -7
- package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
- package/test/diagnostics-channel/sync-request.test.js +19 -19
- package/test/encapsulated-child-logger-factory.test.js +8 -8
- package/test/encapsulated-error-handler.test.js +20 -20
- package/test/esm/errorCodes.test.mjs +5 -5
- package/test/esm/esm.test.mjs +3 -3
- package/test/esm/named-exports.mjs +3 -3
- package/test/esm/other.mjs +2 -2
- package/test/fastify-instance.test.js +33 -34
- package/test/{findRoute.test.js → find-route.test.js} +11 -10
- package/test/fluent-schema.test.js +33 -36
- package/test/handler-context.test.js +11 -11
- package/test/has-route.test.js +12 -15
- package/test/header-overflow.test.js +13 -12
- package/test/hooks.on-ready.test.js +2 -2
- package/test/hooks.test.js +25 -25
- package/test/http-methods/copy.test.js +22 -24
- package/test/http-methods/custom-http-methods.test.js +24 -21
- package/test/http-methods/get.test.js +97 -84
- package/test/http-methods/head.test.js +63 -57
- package/test/http-methods/lock.test.js +21 -20
- package/test/http-methods/mkcalendar.test.js +31 -27
- package/test/http-methods/mkcol.test.js +10 -10
- package/test/http-methods/move.test.js +11 -11
- package/test/http-methods/propfind.test.js +32 -27
- package/test/http-methods/proppatch.test.js +21 -19
- package/test/http-methods/report.test.js +32 -27
- package/test/http-methods/search.test.js +52 -47
- package/test/http-methods/trace.test.js +3 -4
- package/test/http-methods/unlock.test.js +10 -10
- package/test/http2/closing.test.js +50 -58
- package/test/http2/constraint.test.js +47 -50
- package/test/http2/head.test.js +18 -19
- package/test/http2/missing-http2-module.test.js +4 -5
- package/test/http2/plain.test.js +31 -31
- package/test/http2/secure-with-fallback.test.js +61 -61
- package/test/http2/secure.test.js +28 -31
- package/test/http2/unknown-http-method.test.js +13 -14
- package/test/https/custom-https-server.test.js +6 -7
- package/test/https/https.test.js +78 -78
- package/test/imports.test.js +5 -6
- package/test/internals/all.test.js +8 -11
- package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
- package/test/internals/context.test.js +9 -11
- package/test/internals/decorator.test.js +20 -21
- package/test/internals/errors.test.js +427 -427
- package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
- package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
- package/test/internals/hooks.test.js +31 -35
- package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
- package/test/internals/logger.test.js +28 -28
- package/test/internals/plugin.test.js +17 -18
- package/test/internals/reply-serialize.test.js +106 -106
- package/test/internals/reply.test.js +620 -585
- package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
- package/test/internals/request-validate.test.js +218 -221
- package/test/internals/request.test.js +225 -107
- package/test/internals/server.test.js +15 -12
- package/test/internals/validation.test.js +35 -36
- package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
- package/test/listen.5.test.js +9 -9
- package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
- package/test/middleware.test.js +4 -5
- package/test/noop-set.test.js +5 -5
- package/test/post-empty-body.test.js +18 -11
- package/test/pretty-print.test.js +59 -49
- package/test/proto-poisoning.test.js +42 -37
- package/test/reply-code.test.js +34 -32
- package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
- package/test/request-error.test.js +122 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +31 -25
- package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
- package/test/route.1.test.js +79 -72
- package/test/route.2.test.js +17 -16
- package/test/route.3.test.js +32 -27
- package/test/route.4.test.js +21 -25
- package/test/route.5.test.js +45 -64
- package/test/route.6.test.js +70 -89
- package/test/route.7.test.js +61 -65
- package/test/route.8.test.js +80 -18
- package/test/router-options.test.js +80 -77
- package/test/same-shape.test.js +5 -5
- package/test/schema-examples.test.js +72 -38
- package/test/serialize-response.test.js +9 -10
- package/test/server.test.js +75 -78
- package/test/set-error-handler.test.js +2 -3
- package/test/stream-serializers.test.js +10 -7
- package/test/sync-routes.test.js +18 -18
- package/test/test-reporter.mjs +68 -0
- package/test/trust-proxy.test.js +51 -45
- package/test/type-provider.test.js +8 -6
- package/test/types/content-type-parser.test-d.ts +1 -1
- package/test/types/fastify.test-d.ts +16 -4
- package/test/types/hooks.test-d.ts +2 -1
- package/test/types/instance.test-d.ts +13 -13
- package/test/types/logger.test-d.ts +2 -2
- package/test/types/plugin.test-d.ts +17 -9
- package/test/types/register.test-d.ts +22 -6
- package/test/types/reply.test-d.ts +1 -1
- package/test/types/route.test-d.ts +34 -4
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +1 -1
- package/test/url-rewriting.test.js +35 -38
- package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
- package/test/validation-error-handling.test.js +259 -285
- package/test/versioned-routes.test.js +126 -113
- package/test/web-api.test.js +48 -37
- package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
- package/types/hooks.d.ts +2 -1
- package/types/instance.d.ts +9 -2
- package/types/register.d.ts +12 -3
- package/types/reply.d.ts +1 -1
- package/types/request.d.ts +2 -6
- package/types/serverFactory.d.ts +3 -3
- package/types/utils.d.ts +13 -5
- package/test/types/import.js +0 -2
package/docs/Reference/Reply.md
CHANGED
|
@@ -11,15 +11,14 @@
|
|
|
11
11
|
- [.headers(object)](#headersobject)
|
|
12
12
|
- [.getHeader(key)](#getheaderkey)
|
|
13
13
|
- [.getHeaders()](#getheaders)
|
|
14
|
-
- [set-cookie](#set-cookie)
|
|
15
14
|
- [.removeHeader(key)](#removeheaderkey)
|
|
16
15
|
- [.hasHeader(key)](#hasheaderkey)
|
|
16
|
+
- [.writeEarlyHints(hints, callback)](#writeearlyhintshints-callback)
|
|
17
17
|
- [.trailer(key, function)](#trailerkey-function)
|
|
18
18
|
- [.hasTrailer(key)](#hastrailerkey)
|
|
19
19
|
- [.removeTrailer(key)](#removetrailerkey)
|
|
20
20
|
- [.redirect(dest, [code ,])](#redirectdest--code)
|
|
21
21
|
- [.callNotFound()](#callnotfound)
|
|
22
|
-
- [.getResponseTime()](#getresponsetime)
|
|
23
22
|
- [.type(contentType)](#typecontenttype)
|
|
24
23
|
- [.getSerializationFunction(schema | httpStatus, [contentType])](#getserializationfunctionschema--httpstatus)
|
|
25
24
|
- [.compileSerializationSchema(schema, [httpStatus], [contentType])](#compileserializationschemaschema-httpstatus)
|
|
@@ -33,8 +32,9 @@
|
|
|
33
32
|
- [Strings](#strings)
|
|
34
33
|
- [Streams](#streams)
|
|
35
34
|
- [Buffers](#buffers)
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
35
|
+
- [TypedArrays](#typedarrays)
|
|
36
|
+
- [ReadableStream](#readablestream)
|
|
37
|
+
- [Response](#response)
|
|
38
38
|
- [Errors](#errors)
|
|
39
39
|
- [Type of the final payload](#type-of-the-final-payload)
|
|
40
40
|
- [Async-Await and Promises](#async-await-and-promises)
|
|
@@ -72,14 +72,14 @@ since the request was received by Fastify.
|
|
|
72
72
|
serialized payload.
|
|
73
73
|
- `.getSerializationFunction(schema | httpStatus, [contentType])` - Returns the serialization
|
|
74
74
|
function for the specified schema or http status, if any of either are set.
|
|
75
|
-
- `.compileSerializationSchema(schema, [httpStatus], [contentType])` - Compiles
|
|
76
|
-
the specified schema and returns a serialization function using the default
|
|
77
|
-
(or customized) `SerializerCompiler`. The optional `httpStatus` is forwarded
|
|
75
|
+
- `.compileSerializationSchema(schema, [httpStatus], [contentType])` - Compiles
|
|
76
|
+
the specified schema and returns a serialization function using the default
|
|
77
|
+
(or customized) `SerializerCompiler`. The optional `httpStatus` is forwarded
|
|
78
78
|
to the `SerializerCompiler` if provided, default to `undefined`.
|
|
79
|
-
- `.serializeInput(data, schema, [,httpStatus], [contentType])` - Serializes
|
|
79
|
+
- `.serializeInput(data, schema, [,httpStatus], [contentType])` - Serializes
|
|
80
80
|
the specified data using the specified schema and returns the serialized payload.
|
|
81
|
-
If the optional `httpStatus`, and `contentType` are provided, the function
|
|
82
|
-
will use the serializer function given for that specific content type and
|
|
81
|
+
If the optional `httpStatus`, and `contentType` are provided, the function
|
|
82
|
+
will use the serializer function given for that specific content type and
|
|
83
83
|
HTTP Status Code. Default to `undefined`.
|
|
84
84
|
- `.serializer(function)` - Sets a custom serializer for the payload.
|
|
85
85
|
- `.send(payload)` - Sends the payload to the user, could be a plain text, a
|
|
@@ -88,7 +88,7 @@ since the request was received by Fastify.
|
|
|
88
88
|
already been called.
|
|
89
89
|
- `.hijack()` - interrupt the normal request lifecycle.
|
|
90
90
|
- `.raw` - The
|
|
91
|
-
[`http.ServerResponse`](https://nodejs.org/dist/latest-
|
|
91
|
+
[`http.ServerResponse`](https://nodejs.org/dist/latest-v20.x/docs/api/http.html#http_class_http_serverresponse)
|
|
92
92
|
from Node core.
|
|
93
93
|
- `.log` - The logger instance of the incoming request.
|
|
94
94
|
- `.request` - The incoming request.
|
|
@@ -114,9 +114,6 @@ If not set via `reply.code`, the resulting `statusCode` will be `200`.
|
|
|
114
114
|
Invokes the custom response time getter to calculate the amount of time passed
|
|
115
115
|
since the request was received by Fastify.
|
|
116
116
|
|
|
117
|
-
Note that unless this function is called in the [`onResponse`
|
|
118
|
-
hook](./Hooks.md#onresponse) it will always return `0`.
|
|
119
|
-
|
|
120
117
|
```js
|
|
121
118
|
const milliseconds = reply.elapsedTime
|
|
122
119
|
```
|
|
@@ -161,7 +158,7 @@ Sets a response header. If the value is omitted or undefined, it is coerced to
|
|
|
161
158
|
> will result in a 500 `TypeError` response.
|
|
162
159
|
|
|
163
160
|
For more information, see
|
|
164
|
-
[`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest-
|
|
161
|
+
[`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest-v20.x/docs/api/http.html#http_response_setheader_name_value).
|
|
165
162
|
|
|
166
163
|
- ### set-cookie
|
|
167
164
|
<a id="set-cookie"></a>
|
|
@@ -247,7 +244,7 @@ The hints parameter is an object containing the early hint key-value pairs.
|
|
|
247
244
|
Example:
|
|
248
245
|
|
|
249
246
|
```js
|
|
250
|
-
reply.writeEarlyHints({
|
|
247
|
+
reply.writeEarlyHints({
|
|
251
248
|
Link: '</styles.css>; rel=preload; as=style'
|
|
252
249
|
});
|
|
253
250
|
```
|
|
@@ -370,8 +367,8 @@ If the `Content-Type` has a JSON subtype, and the charset parameter is not set,
|
|
|
370
367
|
### .getSerializationFunction(schema | httpStatus, [contentType])
|
|
371
368
|
<a id="getserializationfunction"></a>
|
|
372
369
|
|
|
373
|
-
By calling this function using a provided `schema` or `httpStatus`,
|
|
374
|
-
and the optional `contentType`, it will return a `serialzation` function
|
|
370
|
+
By calling this function using a provided `schema` or `httpStatus`,
|
|
371
|
+
and the optional `contentType`, it will return a `serialzation` function
|
|
375
372
|
that can be used to serialize diverse inputs. It returns `undefined` if no
|
|
376
373
|
serialization function was found using either of the provided inputs.
|
|
377
374
|
|
|
@@ -381,12 +378,12 @@ the serialization functions compiled by using `compileSerializationSchema`.
|
|
|
381
378
|
```js
|
|
382
379
|
const serialize = reply
|
|
383
380
|
.getSerializationFunction({
|
|
384
|
-
type: 'object',
|
|
385
|
-
properties: {
|
|
386
|
-
foo: {
|
|
387
|
-
type: 'string'
|
|
388
|
-
}
|
|
389
|
-
}
|
|
381
|
+
type: 'object',
|
|
382
|
+
properties: {
|
|
383
|
+
foo: {
|
|
384
|
+
type: 'string'
|
|
385
|
+
}
|
|
386
|
+
}
|
|
390
387
|
})
|
|
391
388
|
serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
392
389
|
|
|
@@ -415,8 +412,8 @@ The function returned (a.k.a. _serialization function_) returned is compiled
|
|
|
415
412
|
by using the provided `SerializerCompiler`. Also this is cached by using
|
|
416
413
|
a `WeakMap` for reducing compilation calls.
|
|
417
414
|
|
|
418
|
-
The optional parameters `httpStatus` and `contentType`, if provided,
|
|
419
|
-
are forwarded directly to the `SerializerCompiler`, so it can be used
|
|
415
|
+
The optional parameters `httpStatus` and `contentType`, if provided,
|
|
416
|
+
are forwarded directly to the `SerializerCompiler`, so it can be used
|
|
420
417
|
to compile the serialization function if a custom `SerializerCompiler` is used.
|
|
421
418
|
|
|
422
419
|
This heavily depends of the `schema#responses` attached to the route, or
|
|
@@ -425,12 +422,12 @@ the serialization functions compiled by using `compileSerializationSchema`.
|
|
|
425
422
|
```js
|
|
426
423
|
const serialize = reply
|
|
427
424
|
.compileSerializationSchema({
|
|
428
|
-
type: 'object',
|
|
429
|
-
properties: {
|
|
430
|
-
foo: {
|
|
431
|
-
type: 'string'
|
|
432
|
-
}
|
|
433
|
-
}
|
|
425
|
+
type: 'object',
|
|
426
|
+
properties: {
|
|
427
|
+
foo: {
|
|
428
|
+
type: 'string'
|
|
429
|
+
}
|
|
430
|
+
}
|
|
434
431
|
})
|
|
435
432
|
serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
436
433
|
|
|
@@ -438,12 +435,12 @@ serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
|
438
435
|
|
|
439
436
|
const serialize = reply
|
|
440
437
|
.compileSerializationSchema({
|
|
441
|
-
type: 'object',
|
|
442
|
-
properties: {
|
|
443
|
-
foo: {
|
|
444
|
-
type: 'string'
|
|
445
|
-
}
|
|
446
|
-
}
|
|
438
|
+
type: 'object',
|
|
439
|
+
properties: {
|
|
440
|
+
foo: {
|
|
441
|
+
type: 'string'
|
|
442
|
+
}
|
|
443
|
+
}
|
|
447
444
|
}, 200)
|
|
448
445
|
serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
449
446
|
|
|
@@ -489,7 +486,7 @@ const schema1 = {
|
|
|
489
486
|
```
|
|
490
487
|
|
|
491
488
|
*Not*
|
|
492
|
-
```js
|
|
489
|
+
```js
|
|
493
490
|
const serialize = reply.compileSerializationSchema(schema1)
|
|
494
491
|
|
|
495
492
|
// Later on...
|
|
@@ -523,25 +520,25 @@ function will be compiled, forwarding the `httpStatus` and `contentType` if prov
|
|
|
523
520
|
|
|
524
521
|
```js
|
|
525
522
|
reply
|
|
526
|
-
.serializeInput({ foo: 'bar'}, {
|
|
527
|
-
type: 'object',
|
|
528
|
-
properties: {
|
|
529
|
-
foo: {
|
|
530
|
-
type: 'string'
|
|
531
|
-
}
|
|
532
|
-
}
|
|
523
|
+
.serializeInput({ foo: 'bar'}, {
|
|
524
|
+
type: 'object',
|
|
525
|
+
properties: {
|
|
526
|
+
foo: {
|
|
527
|
+
type: 'string'
|
|
528
|
+
}
|
|
529
|
+
}
|
|
533
530
|
}) // '{"foo":"bar"}'
|
|
534
531
|
|
|
535
532
|
// or
|
|
536
533
|
|
|
537
534
|
reply
|
|
538
535
|
.serializeInput({ foo: 'bar'}, {
|
|
539
|
-
type: 'object',
|
|
540
|
-
properties: {
|
|
541
|
-
foo: {
|
|
542
|
-
type: 'string'
|
|
543
|
-
}
|
|
544
|
-
}
|
|
536
|
+
type: 'object',
|
|
537
|
+
properties: {
|
|
538
|
+
foo: {
|
|
539
|
+
type: 'string'
|
|
540
|
+
}
|
|
541
|
+
}
|
|
545
542
|
}, 200) // '{"foo":"bar"}'
|
|
546
543
|
|
|
547
544
|
// or
|
|
@@ -590,7 +587,7 @@ values.
|
|
|
590
587
|
<a id="raw"></a>
|
|
591
588
|
|
|
592
589
|
This is the
|
|
593
|
-
[`http.ServerResponse`](https://nodejs.org/dist/latest-
|
|
590
|
+
[`http.ServerResponse`](https://nodejs.org/dist/latest-v20.x/docs/api/http.html#http_class_http_serverresponse)
|
|
594
591
|
from Node core. Whilst you are using the Fastify `Reply` object, the use of
|
|
595
592
|
`Reply.raw` functions is at your own risk as you are skipping all the Fastify
|
|
596
593
|
logic of handling the HTTP response. e.g.:
|
|
@@ -716,7 +713,7 @@ fastify.get('/streams', async function (request, reply) {
|
|
|
716
713
|
If you are sending a buffer and you have not set a `'Content-Type'` header,
|
|
717
714
|
*send* will set it to `'application/octet-stream'`.
|
|
718
715
|
|
|
719
|
-
As noted above, Buffers are considered to be pre-serialized, so they will be
|
|
716
|
+
As noted above, Buffers are considered to be pre-serialized, so they will be
|
|
720
717
|
sent unmodified without response validation.
|
|
721
718
|
|
|
722
719
|
```js
|
|
@@ -747,7 +744,7 @@ fastify.get('/streams', async function (request, reply) {
|
|
|
747
744
|
`send` manages TypedArray like a Buffer, and sets the `'Content-Type'`
|
|
748
745
|
header to `'application/octet-stream'` if not already set.
|
|
749
746
|
|
|
750
|
-
As noted above, TypedArray/Buffers are considered to be pre-serialized, so they
|
|
747
|
+
As noted above, TypedArray/Buffers are considered to be pre-serialized, so they
|
|
751
748
|
will be sent unmodified without response validation.
|
|
752
749
|
|
|
753
750
|
```js
|
|
@@ -763,7 +760,7 @@ fastify.get('/streams', function (request, reply) {
|
|
|
763
760
|
<a id="send-readablestream"></a>
|
|
764
761
|
|
|
765
762
|
`ReadableStream` will be treated as a node stream mentioned above,
|
|
766
|
-
the content is considered to be pre-serialized, so they will be
|
|
763
|
+
the content is considered to be pre-serialized, so they will be
|
|
767
764
|
sent unmodified without response validation.
|
|
768
765
|
|
|
769
766
|
```js
|
|
@@ -782,7 +779,7 @@ fastify.get('/streams', function (request, reply) {
|
|
|
782
779
|
|
|
783
780
|
`Response` allows to manage the reply payload, status code and
|
|
784
781
|
headers in one place. The payload provided inside `Response` is
|
|
785
|
-
considered to be pre-serialized, so they will be sent unmodified
|
|
782
|
+
considered to be pre-serialized, so they will be sent unmodified
|
|
786
783
|
without response validation.
|
|
787
784
|
|
|
788
785
|
Please be aware when using `Response`, the status code and headers
|
|
@@ -880,7 +877,7 @@ API:
|
|
|
880
877
|
```js
|
|
881
878
|
fastify.setErrorHandler(function (error, request, reply) {
|
|
882
879
|
request.log.warn(error)
|
|
883
|
-
|
|
880
|
+
const statusCode = error.statusCode >= 400 ? error.statusCode : 500
|
|
884
881
|
reply
|
|
885
882
|
.code(statusCode)
|
|
886
883
|
.type('text/plain')
|
|
@@ -23,19 +23,23 @@ Request is a core Fastify object containing the following fields:
|
|
|
23
23
|
- `host` - the host of the incoming request (derived from `X-Forwarded-Host`
|
|
24
24
|
header when the [`trustProxy`](./Server.md#factory-trust-proxy) option is
|
|
25
25
|
enabled). For HTTP/2 compatibility it returns `:authority` if no host header
|
|
26
|
-
exists.
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
exists. The host header may return empty string when using
|
|
27
|
+
`requireHostHeader = false`, not suppied when connected with `HTTP/1.0` or
|
|
28
|
+
using schema validation that remove the extra headers.
|
|
29
|
+
- `hostname` - the host of the `host` property, it may refers the incoming
|
|
30
|
+
request hostname
|
|
31
|
+
- `port` - the port of the `host` property, it may refers the port thats
|
|
32
|
+
the server is listening on
|
|
29
33
|
- `protocol` - the protocol of the incoming request (`https` or `http`)
|
|
30
34
|
- `method` - the method of the incoming request
|
|
31
35
|
- `url` - the URL of the incoming request
|
|
32
|
-
- `originalUrl` - similar to `url`, this allows you to access the
|
|
33
|
-
original `url` in case of internal re-routing
|
|
36
|
+
- `originalUrl` - similar to `url`, this allows you to access the
|
|
37
|
+
original `url` in case of internal re-routing
|
|
34
38
|
- `is404` - true if request is being handled by 404 handler, false if it is not
|
|
35
39
|
- `socket` - the underlying connection of the incoming request
|
|
36
40
|
- `context` - Deprecated, use `request.routeOptions.config` instead.
|
|
37
41
|
A Fastify internal object. You should not use
|
|
38
|
-
it directly or modify it. It is useful to access one special key:
|
|
42
|
+
it directly or modify it. It is useful to access one special key:
|
|
39
43
|
- `context.config` - The route [`config`](./Routes.md#routes-config) object.
|
|
40
44
|
- `routeOptions` - The route [`option`](./Routes.md#routes-options) object
|
|
41
45
|
- `bodyLimit` - either server limit or route limit
|
|
@@ -43,15 +47,15 @@ it directly or modify it. It is useful to access one special key:
|
|
|
43
47
|
- `method` - the http method for the route
|
|
44
48
|
- `url` - the path of the URL to match this route
|
|
45
49
|
- `handler` - the handler for this route
|
|
46
|
-
- `attachValidation` - attach `validationError` to request
|
|
50
|
+
- `attachValidation` - attach `validationError` to request
|
|
47
51
|
(if there is a schema defined)
|
|
48
52
|
- `logLevel` - log level defined for this route
|
|
49
53
|
- `schema` - the JSON schemas definition for this route
|
|
50
54
|
- `version` - a semver compatible string that defines the version of the endpoint
|
|
51
55
|
- `exposeHeadRoute` - creates a sibling HEAD route for any GET routes
|
|
52
|
-
- `prefixTrailingSlash` - string used to determine how to handle passing /
|
|
56
|
+
- `prefixTrailingSlash` - string used to determine how to handle passing /
|
|
53
57
|
as a route with a prefix.
|
|
54
|
-
- [.getValidationFunction(schema | httpPart)](#getvalidationfunction) -
|
|
58
|
+
- [.getValidationFunction(schema | httpPart)](#getvalidationfunction) -
|
|
55
59
|
Returns a validation function for the specified schema or http part,
|
|
56
60
|
if any of either are set or cached.
|
|
57
61
|
- [.compileValidationSchema(schema, [httpPart])](#compilevalidationschema) -
|
|
@@ -84,6 +88,9 @@ request's headers with the `request.raw.headers` property.
|
|
|
84
88
|
> Note: For performance reason on `not found` route, you may see that we will
|
|
85
89
|
add an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
|
|
86
90
|
|
|
91
|
+
> Note: When using schema, it may mutate the `request.headers` and
|
|
92
|
+
`request.raw.headers` object. So, you may found the headers becomes empty.
|
|
93
|
+
|
|
87
94
|
```js
|
|
88
95
|
fastify.post('/:params', options, function (request, reply) {
|
|
89
96
|
console.log(request.body)
|
|
@@ -116,7 +123,7 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
116
123
|
### .getValidationFunction(schema | httpPart)
|
|
117
124
|
<a id="getvalidationfunction"></a>
|
|
118
125
|
|
|
119
|
-
By calling this function using a provided `schema` or `httpPart`,
|
|
126
|
+
By calling this function using a provided `schema` or `httpPart`,
|
|
120
127
|
it will return a `validation` function that can be used to
|
|
121
128
|
validate diverse inputs. It returns `undefined` if no
|
|
122
129
|
serialization function was found using either of the provided inputs.
|
|
@@ -127,12 +134,12 @@ are assigned to errors
|
|
|
127
134
|
```js
|
|
128
135
|
const validate = request
|
|
129
136
|
.getValidationFunction({
|
|
130
|
-
type: 'object',
|
|
131
|
-
properties: {
|
|
132
|
-
foo: {
|
|
133
|
-
type: 'string'
|
|
134
|
-
}
|
|
135
|
-
}
|
|
137
|
+
type: 'object',
|
|
138
|
+
properties: {
|
|
139
|
+
foo: {
|
|
140
|
+
type: 'string'
|
|
141
|
+
}
|
|
142
|
+
}
|
|
136
143
|
})
|
|
137
144
|
console.log(validate({ foo: 'bar' })) // true
|
|
138
145
|
console.log(validate.errors) // null
|
|
@@ -167,12 +174,12 @@ are assigned to errors
|
|
|
167
174
|
```js
|
|
168
175
|
const validate = request
|
|
169
176
|
.compileValidationSchema({
|
|
170
|
-
type: 'object',
|
|
171
|
-
properties: {
|
|
172
|
-
foo: {
|
|
173
|
-
type: 'string'
|
|
174
|
-
}
|
|
175
|
-
}
|
|
177
|
+
type: 'object',
|
|
178
|
+
properties: {
|
|
179
|
+
foo: {
|
|
180
|
+
type: 'string'
|
|
181
|
+
}
|
|
182
|
+
}
|
|
176
183
|
})
|
|
177
184
|
console.log(validate({ foo: 'bar' })) // true
|
|
178
185
|
console.log(validate.errors) // null
|
|
@@ -181,12 +188,12 @@ console.log(validate.errors) // null
|
|
|
181
188
|
|
|
182
189
|
const validate = request
|
|
183
190
|
.compileValidationSchema({
|
|
184
|
-
type: 'object',
|
|
185
|
-
properties: {
|
|
186
|
-
foo: {
|
|
187
|
-
type: 'string'
|
|
188
|
-
}
|
|
189
|
-
}
|
|
191
|
+
type: 'object',
|
|
192
|
+
properties: {
|
|
193
|
+
foo: {
|
|
194
|
+
type: 'string'
|
|
195
|
+
}
|
|
196
|
+
}
|
|
190
197
|
}, 200)
|
|
191
198
|
console.log(validate({ hello: 'world' })) // false
|
|
192
199
|
console.log(validate.errors) // validation errors
|
|
@@ -216,7 +223,7 @@ const schema1 = {
|
|
|
216
223
|
```
|
|
217
224
|
|
|
218
225
|
*Not*
|
|
219
|
-
```js
|
|
226
|
+
```js
|
|
220
227
|
const validate = request.compileValidationSchema(schema1)
|
|
221
228
|
|
|
222
229
|
// Later on...
|
|
@@ -251,25 +258,25 @@ function will be compiled, forwarding the `httpPart` if provided.
|
|
|
251
258
|
|
|
252
259
|
```js
|
|
253
260
|
request
|
|
254
|
-
.validateInput({ foo: 'bar'}, {
|
|
255
|
-
type: 'object',
|
|
256
|
-
properties: {
|
|
257
|
-
foo: {
|
|
258
|
-
type: 'string'
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
+
.validateInput({ foo: 'bar'}, {
|
|
262
|
+
type: 'object',
|
|
263
|
+
properties: {
|
|
264
|
+
foo: {
|
|
265
|
+
type: 'string'
|
|
266
|
+
}
|
|
267
|
+
}
|
|
261
268
|
}) // true
|
|
262
269
|
|
|
263
270
|
// or
|
|
264
271
|
|
|
265
272
|
request
|
|
266
273
|
.validateInput({ foo: 'bar'}, {
|
|
267
|
-
type: 'object',
|
|
268
|
-
properties: {
|
|
269
|
-
foo: {
|
|
270
|
-
type: 'string'
|
|
271
|
-
}
|
|
272
|
-
}
|
|
274
|
+
type: 'object',
|
|
275
|
+
properties: {
|
|
276
|
+
foo: {
|
|
277
|
+
type: 'string'
|
|
278
|
+
}
|
|
279
|
+
}
|
|
273
280
|
}, 'body') // true
|
|
274
281
|
|
|
275
282
|
// or
|
package/docs/Reference/Routes.md
CHANGED
|
@@ -93,16 +93,16 @@ fastify.route(options)
|
|
|
93
93
|
* `childLoggerFactory(logger, binding, opts, rawReq)`: a custom factory function
|
|
94
94
|
that will be called to produce a child logger instance for every request.
|
|
95
95
|
See [`childLoggerFactory`](./Server.md#childloggerfactory) for more info.
|
|
96
|
-
Overrides the default logger factory, and anything set by
|
|
96
|
+
Overrides the default logger factory, and anything set by
|
|
97
97
|
[`setChildLoggerFactory`](./Server.md#setchildloggerfactory), for requests to
|
|
98
|
-
the route. To access the default factory, you can access
|
|
98
|
+
the route. To access the default factory, you can access
|
|
99
99
|
`instance.childLoggerFactory`. Note that this will point to Fastify's default
|
|
100
100
|
`childLoggerFactory` only if a plugin hasn't overridden it already.
|
|
101
101
|
* `validatorCompiler({ schema, method, url, httpPart })`: function that builds
|
|
102
102
|
schemas for request validations. See the [Validation and
|
|
103
103
|
Serialization](./Validation-and-Serialization.md#schema-validator)
|
|
104
104
|
documentation.
|
|
105
|
-
* `serializerCompiler({ { schema, method, url, httpStatus, contentType } })`:
|
|
105
|
+
* `serializerCompiler({ { schema, method, url, httpStatus, contentType } })`:
|
|
106
106
|
function that builds schemas for response serialization. See the [Validation and
|
|
107
107
|
Serialization](./Validation-and-Serialization.md#schema-serializer)
|
|
108
108
|
documentation.
|
|
@@ -121,8 +121,8 @@ fastify.route(options)
|
|
|
121
121
|
* `version`: a [semver](https://semver.org/) compatible string that defined the
|
|
122
122
|
version of the endpoint. [Example](#version-constraints).
|
|
123
123
|
* `constraints`: defines route restrictions based on request properties or
|
|
124
|
-
values, enabling customized matching using
|
|
125
|
-
[find-my-way](https://github.com/delvedor/find-my-way) constraints. Includes
|
|
124
|
+
values, enabling customized matching using
|
|
125
|
+
[find-my-way](https://github.com/delvedor/find-my-way) constraints. Includes
|
|
126
126
|
built-in `version` and `host` constraints, with support for custom constraint
|
|
127
127
|
strategies.
|
|
128
128
|
* `prefixTrailingSlash`: string used to determine how to handle passing `/` as a
|
|
@@ -151,8 +151,11 @@ fastify.route({
|
|
|
151
151
|
url: '/',
|
|
152
152
|
schema: {
|
|
153
153
|
querystring: {
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
type: 'object',
|
|
155
|
+
properties: {
|
|
156
|
+
name: { type: 'string' },
|
|
157
|
+
excitement: { type: 'integer' }
|
|
158
|
+
}
|
|
156
159
|
},
|
|
157
160
|
response: {
|
|
158
161
|
200: {
|
|
@@ -331,8 +334,8 @@ fastify.post('/name::verb') // will be interpreted as /name:verb
|
|
|
331
334
|
Are you an `async/await` user? We have you covered!
|
|
332
335
|
```js
|
|
333
336
|
fastify.get('/', options, async function (request, reply) {
|
|
334
|
-
|
|
335
|
-
|
|
337
|
+
const data = await getData()
|
|
338
|
+
const processed = await processData(data)
|
|
336
339
|
return processed
|
|
337
340
|
})
|
|
338
341
|
```
|
|
@@ -346,8 +349,8 @@ handler or you will introduce a race condition in certain situations.
|
|
|
346
349
|
|
|
347
350
|
```js
|
|
348
351
|
fastify.get('/', options, async function (request, reply) {
|
|
349
|
-
|
|
350
|
-
|
|
352
|
+
const data = await getData()
|
|
353
|
+
const processed = await processData(data)
|
|
351
354
|
return reply.send(processed)
|
|
352
355
|
})
|
|
353
356
|
```
|
|
@@ -793,10 +796,10 @@ const secret = {
|
|
|
793
796
|
> inside the callback. If the error is not preventable, it is recommended to provide
|
|
794
797
|
> a custom `frameworkErrors` handler to deal with it. Otherwise, you route selection
|
|
795
798
|
> may break or expose sensitive information to attackers.
|
|
796
|
-
>
|
|
799
|
+
>
|
|
797
800
|
> ```js
|
|
798
801
|
> const Fastify = require('fastify')
|
|
799
|
-
>
|
|
802
|
+
>
|
|
800
803
|
> const fastify = Fastify({
|
|
801
804
|
> frameworkErrors: function (err, res, res) {
|
|
802
805
|
> if (err instanceof Fastify.errorCodes.FST_ERR_ASYNC_CONSTRAINT) {
|