fastify 5.2.1 → 5.2.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/.vscode/settings.json +22 -0
- package/LICENSE +1 -1
- package/PROJECT_CHARTER.md +7 -7
- package/README.md +24 -25
- package/SPONSORS.md +2 -0
- package/docs/Guides/Benchmarking.md +4 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +10 -10
- package/docs/Guides/Ecosystem.md +5 -1
- package/docs/Guides/Fluent-Schema.md +1 -1
- package/docs/Guides/Getting-Started.md +9 -5
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +1 -1
- package/docs/Guides/Migration-Guide-V5.md +12 -2
- package/docs/Guides/Plugins-Guide.md +6 -6
- package/docs/Guides/Serverless.md +14 -48
- package/docs/Guides/Style-Guide.md +2 -2
- package/docs/Guides/Testing.md +2 -2
- package/docs/Guides/Write-Plugin.md +2 -3
- package/docs/Reference/ContentTypeParser.md +58 -78
- package/docs/Reference/Decorators.md +50 -60
- package/docs/Reference/Encapsulation.md +28 -33
- package/docs/Reference/Errors.md +50 -53
- package/docs/Reference/HTTP2.md +7 -7
- package/docs/Reference/Hooks.md +31 -30
- package/docs/Reference/LTS.md +10 -15
- package/docs/Reference/Lifecycle.md +19 -24
- package/docs/Reference/Logging.md +59 -56
- package/docs/Reference/Middleware.md +19 -19
- package/docs/Reference/Plugins.md +55 -71
- package/docs/Reference/Principles.md +25 -30
- package/docs/Reference/Reply.md +11 -10
- package/docs/Reference/Request.md +89 -98
- package/docs/Reference/Routes.md +108 -128
- package/docs/Reference/Server.md +18 -16
- package/docs/Reference/Type-Providers.md +19 -21
- package/docs/Reference/TypeScript.md +1 -18
- package/docs/Reference/Validation-and-Serialization.md +134 -159
- package/docs/Reference/Warnings.md +22 -25
- package/fastify.js +1 -1
- package/lib/contentTypeParser.js +7 -8
- package/lib/error-handler.js +14 -12
- package/lib/headRoute.js +4 -2
- package/lib/pluginUtils.js +4 -2
- package/lib/server.js +5 -0
- package/lib/validation.js +1 -1
- package/lib/warnings.js +9 -0
- package/lib/wrapThenable.js +8 -1
- package/package.json +10 -10
- package/test/build/error-serializer.test.js +2 -1
- package/test/bundler/esbuild/package.json +1 -1
- package/test/close.test.js +125 -108
- package/test/custom-parser-async.test.js +34 -36
- package/test/genReqId.test.js +125 -174
- package/test/has-route.test.js +1 -3
- package/test/internals/content-type-parser.test.js +1 -1
- package/test/issue-4959.test.js +84 -0
- package/test/listen.1.test.js +37 -34
- package/test/listen.2.test.js +47 -40
- package/test/listen.3.test.js +28 -32
- package/test/listen.4.test.js +61 -45
- package/test/listen.5.test.js +23 -0
- package/test/register.test.js +55 -50
- package/test/request-error.test.js +114 -94
- package/test/route-shorthand.test.js +36 -32
- package/test/server.test.js +0 -175
- package/test/stream.5.test.js +35 -33
- package/test/throw.test.js +87 -91
- package/test/toolkit.js +32 -0
- package/test/trust-proxy.test.js +23 -23
- package/test/types/instance.test-d.ts +1 -0
- package/test/upgrade.test.js +32 -30
- package/types/instance.d.ts +4 -0
package/docs/Reference/Reply.md
CHANGED
|
@@ -151,7 +151,7 @@ fastify.get('/', async function (req, rep) {
|
|
|
151
151
|
Sets a response header. If the value is omitted or undefined, it is coerced to
|
|
152
152
|
`''`.
|
|
153
153
|
|
|
154
|
-
> Note:
|
|
154
|
+
> 🛈 Note: The header's value must be properly encoded using
|
|
155
155
|
> [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
|
|
156
156
|
> or similar modules such as
|
|
157
157
|
> [`encodeurl`](https://www.npmjs.com/package/encodeurl). Invalid characters
|
|
@@ -260,11 +260,11 @@ requires heavy resources to be sent after the `data`, for example,
|
|
|
260
260
|
`Server-Timing` and `Etag`. It can ensure the client receives the response data
|
|
261
261
|
as soon as possible.
|
|
262
262
|
|
|
263
|
-
|
|
264
|
-
trailer. It is a hard requirement for using trailer in Node.js
|
|
263
|
+
> 🛈 Note: The header `Transfer-Encoding: chunked` will be added once you use
|
|
264
|
+
> the trailer. It is a hard requirement for using trailer in Node.js.
|
|
265
265
|
|
|
266
|
-
|
|
267
|
-
in the error, you can turn on `debug` level logging.*
|
|
266
|
+
> 🛈 Note: Any error passed to `done` callback will be ignored. If you interested
|
|
267
|
+
> in the error, you can turn on `debug` level logging.*
|
|
268
268
|
|
|
269
269
|
```js
|
|
270
270
|
reply.trailer('server-timing', function() {
|
|
@@ -314,7 +314,7 @@ reply.getTrailer('server-timing') // undefined
|
|
|
314
314
|
Redirects a request to the specified URL, the status code is optional, default
|
|
315
315
|
to `302` (if status code is not already set by calling `code`).
|
|
316
316
|
|
|
317
|
-
> Note:
|
|
317
|
+
> 🛈 Note: The input URL must be properly encoded using
|
|
318
318
|
> [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
|
|
319
319
|
> or similar modules such as
|
|
320
320
|
> [`encodeurl`](https://www.npmjs.com/package/encodeurl). Invalid URLs will
|
|
@@ -362,7 +362,8 @@ Sets the content type for the response. This is a shortcut for
|
|
|
362
362
|
reply.type('text/html')
|
|
363
363
|
```
|
|
364
364
|
If the `Content-Type` has a JSON subtype, and the charset parameter is not set,
|
|
365
|
-
`utf-8` will be used as the charset by default.
|
|
365
|
+
`utf-8` will be used as the charset by default. For other content types, the
|
|
366
|
+
charset must be set explicitly.
|
|
366
367
|
|
|
367
368
|
### .getSerializationFunction(schema | httpStatus, [contentType])
|
|
368
369
|
<a id="getserializationfunction"></a>
|
|
@@ -822,8 +823,8 @@ automatically create an error structured as the following:
|
|
|
822
823
|
You can add custom properties to the Error object, such as `headers`, that will
|
|
823
824
|
be used to enhance the HTTP response.
|
|
824
825
|
|
|
825
|
-
|
|
826
|
-
400, Fastify will automatically set it at 500
|
|
826
|
+
> 🛈 Note: If you are passing an error to `send` and the statusCode is less than
|
|
827
|
+
> 400, Fastify will automatically set it at 500.
|
|
827
828
|
|
|
828
829
|
Tip: you can simplify errors by using the
|
|
829
830
|
[`http-errors`](https://npm.im/http-errors) module or
|
|
@@ -870,7 +871,7 @@ fastify.get('/', {
|
|
|
870
871
|
If you want to customize error handling, check out
|
|
871
872
|
[`setErrorHandler`](./Server.md#seterrorhandler) API.
|
|
872
873
|
|
|
873
|
-
|
|
874
|
+
> 🛈 Note: you are responsible for logging when customizing the error handler.
|
|
874
875
|
|
|
875
876
|
API:
|
|
876
877
|
|
|
@@ -4,74 +4,71 @@
|
|
|
4
4
|
The first parameter of the handler function is `Request`.
|
|
5
5
|
|
|
6
6
|
Request is a core Fastify object containing the following fields:
|
|
7
|
-
- `query` -
|
|
8
|
-
[`querystringParser`](./Server.md#querystringparser)
|
|
9
|
-
- `body` -
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
- `params` -
|
|
13
|
-
- [`headers`](#headers) -
|
|
14
|
-
- `raw` -
|
|
15
|
-
- `server` - The Fastify server instance, scoped to the current
|
|
16
|
-
context](./Encapsulation.md)
|
|
17
|
-
- `id` -
|
|
18
|
-
- `log` -
|
|
19
|
-
- `ip` -
|
|
20
|
-
- `ips` -
|
|
7
|
+
- `query` - The parsed querystring, its format is specified by
|
|
8
|
+
[`querystringParser`](./Server.md#querystringparser).
|
|
9
|
+
- `body` - The request payload, see [Content-Type Parser](./ContentTypeParser.md)
|
|
10
|
+
for details on what request payloads Fastify natively parses and how to support
|
|
11
|
+
other content types.
|
|
12
|
+
- `params` - The params matching the URL.
|
|
13
|
+
- [`headers`](#headers) - The headers getter and setter.
|
|
14
|
+
- `raw` - The incoming HTTP request from Node core.
|
|
15
|
+
- `server` - The Fastify server instance, scoped to the current
|
|
16
|
+
[encapsulation context](./Encapsulation.md).
|
|
17
|
+
- `id` - The request ID.
|
|
18
|
+
- `log` - The logger instance of the incoming request.
|
|
19
|
+
- `ip` - The IP address of the incoming request.
|
|
20
|
+
- `ips` - An array of the IP addresses, ordered from closest to furthest, in the
|
|
21
21
|
`X-Forwarded-For` header of the incoming request (only when the
|
|
22
|
-
[`trustProxy`](./Server.md#factory-trust-proxy) option is enabled)
|
|
23
|
-
- `host` -
|
|
22
|
+
[`trustProxy`](./Server.md#factory-trust-proxy) option is enabled).
|
|
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
|
-
enabled). For HTTP/2 compatibility it returns `:authority` if no host header
|
|
26
|
-
exists. The host header may return an empty string if `requireHostHeader`
|
|
27
|
-
|
|
28
|
-
- `hostname` -
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- `
|
|
33
|
-
- `
|
|
34
|
-
- `
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
- `
|
|
38
|
-
- `
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
it directly or modify it. It is useful to access one special key:
|
|
25
|
+
enabled). For HTTP/2 compatibility, it returns `:authority` if no host header
|
|
26
|
+
exists. The host header may return an empty string if `requireHostHeader` is
|
|
27
|
+
`false`, not provided with HTTP/1.0, or removed by schema validation.
|
|
28
|
+
- `hostname` - The hostname derived from the `host` property of the incoming request.
|
|
29
|
+
- `port` - The port from the `host` property, which may refer to the port the
|
|
30
|
+
server is listening on.
|
|
31
|
+
- `protocol` - The protocol of the incoming request (`https` or `http`).
|
|
32
|
+
- `method` - The method of the incoming request.
|
|
33
|
+
- `url` - The URL of the incoming request.
|
|
34
|
+
- `originalUrl` - Similar to `url`, allows access to the original `url` in
|
|
35
|
+
case of internal re-routing.
|
|
36
|
+
- `is404` - `true` if request is being handled by 404 handler, `false` otherwise.
|
|
37
|
+
- `socket` - The underlying connection of the incoming request.
|
|
38
|
+
- `context` - Deprecated, use `request.routeOptions.config` instead. A Fastify
|
|
39
|
+
internal object. Do not use or modify it directly. It is useful to access one
|
|
40
|
+
special key:
|
|
42
41
|
- `context.config` - The route [`config`](./Routes.md#routes-config) object.
|
|
43
|
-
- `routeOptions` - The route [`option`](./Routes.md#routes-options) object
|
|
44
|
-
- `bodyLimit` -
|
|
45
|
-
- `config` -
|
|
46
|
-
- `method` -
|
|
47
|
-
- `url` -
|
|
48
|
-
- `handler` -
|
|
49
|
-
- `attachValidation` -
|
|
50
|
-
|
|
51
|
-
- `logLevel` -
|
|
52
|
-
- `schema` -
|
|
53
|
-
- `version` -
|
|
54
|
-
- `exposeHeadRoute` -
|
|
55
|
-
- `prefixTrailingSlash` -
|
|
42
|
+
- `routeOptions` - The route [`option`](./Routes.md#routes-options) object.
|
|
43
|
+
- `bodyLimit` - Either server limit or route limit.
|
|
44
|
+
- `config` - The [`config`](./Routes.md#routes-config) object for this route.
|
|
45
|
+
- `method` - The HTTP method for the route.
|
|
46
|
+
- `url` - The path of the URL to match this route.
|
|
47
|
+
- `handler` - The handler for this route.
|
|
48
|
+
- `attachValidation` - Attach `validationError` to request (if there is
|
|
49
|
+
a schema defined).
|
|
50
|
+
- `logLevel` - Log level defined for this route.
|
|
51
|
+
- `schema` - The JSON schemas definition for this route.
|
|
52
|
+
- `version` - A semver compatible string that defines the version of the endpoint.
|
|
53
|
+
- `exposeHeadRoute` - Creates a sibling HEAD route for any GET routes.
|
|
54
|
+
- `prefixTrailingSlash` - String used to determine how to handle passing `/`
|
|
56
55
|
as a route with a prefix.
|
|
57
56
|
- [.getValidationFunction(schema | httpPart)](#getvalidationfunction) -
|
|
58
|
-
Returns a validation function for the specified schema or
|
|
59
|
-
|
|
57
|
+
Returns a validation function for the specified schema or HTTP part, if
|
|
58
|
+
set or cached.
|
|
60
59
|
- [.compileValidationSchema(schema, [httpPart])](#compilevalidationschema) -
|
|
61
|
-
Compiles the specified schema and returns a validation function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if provided, defaults to `null`.
|
|
60
|
+
Compiles the specified schema and returns a validation function using the
|
|
61
|
+
default (or customized) `ValidationCompiler`. The optional `httpPart` is
|
|
62
|
+
forwarded to the `ValidationCompiler` if provided, defaults to `null`.
|
|
65
63
|
- [.validateInput(data, schema | httpPart, [httpPart])](#validate) -
|
|
66
|
-
Validates the specified
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
function given for that HTTP Status Code. Defaults to `null`.
|
|
64
|
+
Validates the input using the specified schema and returns the serialized
|
|
65
|
+
payload. If `httpPart` is provided, the function uses the serializer for
|
|
66
|
+
that HTTP Status Code. Defaults to `null`.
|
|
70
67
|
|
|
71
68
|
### Headers
|
|
72
69
|
|
|
73
|
-
The `request.headers` is a getter that returns an
|
|
74
|
-
incoming request.
|
|
70
|
+
The `request.headers` is a getter that returns an object with the headers of the
|
|
71
|
+
incoming request. Set custom headers as follows:
|
|
75
72
|
|
|
76
73
|
```js
|
|
77
74
|
request.headers = {
|
|
@@ -80,15 +77,15 @@ request.headers = {
|
|
|
80
77
|
}
|
|
81
78
|
```
|
|
82
79
|
|
|
83
|
-
This operation
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
This operation adds new values to the request headers, accessible via
|
|
81
|
+
`request.headers.bar`. Standard request headers remain accessible via
|
|
82
|
+
`request.raw.headers`.
|
|
86
83
|
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
For performance reasons, `Symbol('fastify.RequestAcceptVersion')` may be added
|
|
85
|
+
to headers on `not found` routes.
|
|
89
86
|
|
|
90
|
-
> Note:
|
|
91
|
-
`request.raw.headers` objects, causing the headers to become empty.
|
|
87
|
+
> 🛈 Note: Schema validation may mutate the `request.headers` and
|
|
88
|
+
> `request.raw.headers` objects, causing the headers to become empty.
|
|
92
89
|
|
|
93
90
|
```js
|
|
94
91
|
fastify.post('/:params', options, function (request, reply) {
|
|
@@ -122,13 +119,12 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
122
119
|
### .getValidationFunction(schema | httpPart)
|
|
123
120
|
<a id="getvalidationfunction"></a>
|
|
124
121
|
|
|
125
|
-
By calling this function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
serialization function was found using either of the provided inputs.
|
|
122
|
+
By calling this function with a provided `schema` or `httpPart`, it returns a
|
|
123
|
+
`validation` function to validate diverse inputs. It returns `undefined` if no
|
|
124
|
+
serialization function is found using the provided inputs.
|
|
129
125
|
|
|
130
|
-
This function has
|
|
131
|
-
are assigned to errors
|
|
126
|
+
This function has an `errors` property. Errors encountered during the last
|
|
127
|
+
validation are assigned to `errors`.
|
|
132
128
|
|
|
133
129
|
```js
|
|
134
130
|
const validate = request
|
|
@@ -151,24 +147,23 @@ console.log(validate({ foo: 0.5 })) // false
|
|
|
151
147
|
console.log(validate.errors) // validation errors
|
|
152
148
|
```
|
|
153
149
|
|
|
154
|
-
See [.compileValidationSchema(schema, [httpStatus])](#
|
|
155
|
-
for more information on
|
|
150
|
+
See [.compileValidationSchema(schema, [httpStatus])](#compileValidationSchema)
|
|
151
|
+
for more information on compiling validation schemas.
|
|
156
152
|
|
|
157
153
|
### .compileValidationSchema(schema, [httpPart])
|
|
158
154
|
<a id="compilevalidationschema"></a>
|
|
159
155
|
|
|
160
|
-
This function
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
A `WeakMap` is used to cache this, reducing compilation calls.
|
|
156
|
+
This function compiles a validation schema and returns a function to validate data.
|
|
157
|
+
The returned function (a.k.a. _validation function_) is compiled using the provided
|
|
158
|
+
[`SchemaController#ValidationCompiler`](./Server.md#schema-controller). A `WeakMap`
|
|
159
|
+
is used to cache this, reducing compilation calls.
|
|
165
160
|
|
|
166
|
-
The optional parameter `httpPart`, if provided, is forwarded
|
|
167
|
-
|
|
168
|
-
|
|
161
|
+
The optional parameter `httpPart`, if provided, is forwarded to the
|
|
162
|
+
`ValidationCompiler`, allowing it to compile the validation function if a custom
|
|
163
|
+
`ValidationCompiler` is provided for the route.
|
|
169
164
|
|
|
170
|
-
This function has
|
|
171
|
-
are assigned to errors
|
|
165
|
+
This function has an `errors` property. Errors encountered during the last
|
|
166
|
+
validation are assigned to `errors`.
|
|
172
167
|
|
|
173
168
|
```js
|
|
174
169
|
const validate = request
|
|
@@ -198,16 +193,13 @@ console.log(validate({ hello: 'world' })) // false
|
|
|
198
193
|
console.log(validate.errors) // validation errors
|
|
199
194
|
```
|
|
200
195
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
previously compiled validation function, as the cache is based on
|
|
206
|
-
the reference of the schema (Object) previously provided.
|
|
196
|
+
Be careful when using this function, as it caches compiled validation functions
|
|
197
|
+
based on the provided schema. If schemas are mutated or changed, the validation
|
|
198
|
+
functions will not detect the alterations and will reuse the previously compiled
|
|
199
|
+
validation function, as the cache is based on the schema's reference.
|
|
207
200
|
|
|
208
|
-
If
|
|
209
|
-
|
|
210
|
-
the cache mechanism.
|
|
201
|
+
If schema properties need to be changed, create a new schema object to benefit
|
|
202
|
+
from the cache mechanism.
|
|
211
203
|
|
|
212
204
|
Using the following schema as an example:
|
|
213
205
|
```js
|
|
@@ -248,12 +240,11 @@ console.log(newValidate === validate) // false
|
|
|
248
240
|
### .validateInput(data, [schema | httpStatus], [httpStatus])
|
|
249
241
|
<a id="validate"></a>
|
|
250
242
|
|
|
251
|
-
This function
|
|
252
|
-
|
|
253
|
-
will take precedence.
|
|
243
|
+
This function validates the input based on the provided schema or HTTP part. If
|
|
244
|
+
both are provided, the `httpPart` parameter takes precedence.
|
|
254
245
|
|
|
255
|
-
If
|
|
256
|
-
|
|
246
|
+
If no validation function exists for a given `schema`, a new validation function
|
|
247
|
+
will be compiled, forwarding the `httpPart` if provided.
|
|
257
248
|
|
|
258
249
|
```js
|
|
259
250
|
request
|
|
@@ -285,4 +276,4 @@ request
|
|
|
285
276
|
```
|
|
286
277
|
|
|
287
278
|
See [.compileValidationSchema(schema, [httpStatus])](#compileValidationSchema)
|
|
288
|
-
for more information on
|
|
279
|
+
for more information on compiling validation schemas.
|