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.
Files changed (73) hide show
  1. package/.vscode/settings.json +22 -0
  2. package/LICENSE +1 -1
  3. package/PROJECT_CHARTER.md +7 -7
  4. package/README.md +24 -25
  5. package/SPONSORS.md +2 -0
  6. package/docs/Guides/Benchmarking.md +4 -4
  7. package/docs/Guides/Database.md +1 -1
  8. package/docs/Guides/Delay-Accepting-Requests.md +10 -10
  9. package/docs/Guides/Ecosystem.md +5 -1
  10. package/docs/Guides/Fluent-Schema.md +1 -1
  11. package/docs/Guides/Getting-Started.md +9 -5
  12. package/docs/Guides/Index.md +1 -1
  13. package/docs/Guides/Migration-Guide-V4.md +1 -1
  14. package/docs/Guides/Migration-Guide-V5.md +12 -2
  15. package/docs/Guides/Plugins-Guide.md +6 -6
  16. package/docs/Guides/Serverless.md +14 -48
  17. package/docs/Guides/Style-Guide.md +2 -2
  18. package/docs/Guides/Testing.md +2 -2
  19. package/docs/Guides/Write-Plugin.md +2 -3
  20. package/docs/Reference/ContentTypeParser.md +58 -78
  21. package/docs/Reference/Decorators.md +50 -60
  22. package/docs/Reference/Encapsulation.md +28 -33
  23. package/docs/Reference/Errors.md +50 -53
  24. package/docs/Reference/HTTP2.md +7 -7
  25. package/docs/Reference/Hooks.md +31 -30
  26. package/docs/Reference/LTS.md +10 -15
  27. package/docs/Reference/Lifecycle.md +19 -24
  28. package/docs/Reference/Logging.md +59 -56
  29. package/docs/Reference/Middleware.md +19 -19
  30. package/docs/Reference/Plugins.md +55 -71
  31. package/docs/Reference/Principles.md +25 -30
  32. package/docs/Reference/Reply.md +11 -10
  33. package/docs/Reference/Request.md +89 -98
  34. package/docs/Reference/Routes.md +108 -128
  35. package/docs/Reference/Server.md +18 -16
  36. package/docs/Reference/Type-Providers.md +19 -21
  37. package/docs/Reference/TypeScript.md +1 -18
  38. package/docs/Reference/Validation-and-Serialization.md +134 -159
  39. package/docs/Reference/Warnings.md +22 -25
  40. package/fastify.js +1 -1
  41. package/lib/contentTypeParser.js +7 -8
  42. package/lib/error-handler.js +14 -12
  43. package/lib/headRoute.js +4 -2
  44. package/lib/pluginUtils.js +4 -2
  45. package/lib/server.js +5 -0
  46. package/lib/validation.js +1 -1
  47. package/lib/warnings.js +9 -0
  48. package/lib/wrapThenable.js +8 -1
  49. package/package.json +10 -10
  50. package/test/build/error-serializer.test.js +2 -1
  51. package/test/bundler/esbuild/package.json +1 -1
  52. package/test/close.test.js +125 -108
  53. package/test/custom-parser-async.test.js +34 -36
  54. package/test/genReqId.test.js +125 -174
  55. package/test/has-route.test.js +1 -3
  56. package/test/internals/content-type-parser.test.js +1 -1
  57. package/test/issue-4959.test.js +84 -0
  58. package/test/listen.1.test.js +37 -34
  59. package/test/listen.2.test.js +47 -40
  60. package/test/listen.3.test.js +28 -32
  61. package/test/listen.4.test.js +61 -45
  62. package/test/listen.5.test.js +23 -0
  63. package/test/register.test.js +55 -50
  64. package/test/request-error.test.js +114 -94
  65. package/test/route-shorthand.test.js +36 -32
  66. package/test/server.test.js +0 -175
  67. package/test/stream.5.test.js +35 -33
  68. package/test/throw.test.js +87 -91
  69. package/test/toolkit.js +32 -0
  70. package/test/trust-proxy.test.js +23 -23
  71. package/test/types/instance.test-d.ts +1 -0
  72. package/test/upgrade.test.js +32 -30
  73. package/types/instance.d.ts +4 -0
@@ -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: the header's value must be properly encoded using
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
- *Note: The header `Transfer-Encoding: chunked` will be added once you use the
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
- *Note: Any error passed to `done` callback will be ignored. If you interested
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: the input URL must be properly encoded using
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
- *Note: If you are passing an error to `send` and the statusCode is less than
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
- *Note: you are responsible for logging when customizing the error handler*
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` - the parsed querystring, its format is specified by
8
- [`querystringParser`](./Server.md#querystringparser)
9
- - `body` - the request payload, see [Content-Type
10
- Parser](./ContentTypeParser.md) for details on what request payloads Fastify
11
- natively parses and how to support 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 [encapsulation
16
- 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
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` - the host of the incoming request (derived from `X-Forwarded-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
- is false, not provided with HTTP/1.0, or removed by schema validation.
28
- - `hostname` - the hostname derived from the `host` property of
29
- the incoming request
30
- - `port` - the port from the `host` property, which may refer to
31
- the port the server is listening on
32
- - `protocol` - the protocol of the incoming request (`https` or `http`)
33
- - `method` - the method of the incoming request
34
- - `url` - the URL of the incoming request
35
- - `originalUrl` - similar to `url`, this allows you to access the
36
- original `url` in case of internal re-routing
37
- - `is404` - true if request is being handled by 404 handler, false if it is not
38
- - `socket` - the underlying connection of the incoming request
39
- - `context` - Deprecated, use `request.routeOptions.config` instead.
40
- A Fastify internal object. You should not use
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` - either server limit or route limit
45
- - `config` - the [`config`](./Routes.md#routes-config) object for this route
46
- - `method` - the http method for the route
47
- - `url` - the path of the URL to match this route
48
- - `handler` - the handler for this route
49
- - `attachValidation` - attach `validationError` to request
50
- (if there is a schema defined)
51
- - `logLevel` - log level defined for this route
52
- - `schema` - the JSON schemas definition for this route
53
- - `version` - a semver compatible string that defines the version of the endpoint
54
- - `exposeHeadRoute` - creates a sibling HEAD route for any GET routes
55
- - `prefixTrailingSlash` - string used to determine how to handle passing /
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 http part,
59
- if any of either are set or cached.
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
- using the default (or customized) `ValidationCompiler`.
63
- The optional `httpPart` is forwarded to the `ValidationCompiler`
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 input by using the specified
67
- schema and returns the serialized payload. If the optional
68
- `httpPart` is provided, the function will use the serializer
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 Object with the headers of the
74
- incoming request. You can set custom headers like this:
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 will add to the request headers the new values that can be read
84
- calling `request.headers.bar`. Moreover, you can still access the standard
85
- request's headers with the `request.raw.headers` property.
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
- > Note: For performance reason on `not found` route, you may see that we will
88
- add an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
84
+ For performance reasons, `Symbol('fastify.RequestAcceptVersion')` may be added
85
+ to headers on `not found` routes.
89
86
 
90
- > Note: Using schema validation may mutate the `request.headers` and
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 using a provided `schema` or `httpPart`,
126
- it will return a `validation` function that can be used to
127
- validate diverse inputs. It returns `undefined` if no
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 property errors. Errors encountered during the last validation
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])](#compilevalidationschema)
155
- for more information on how to compile validation function.
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 will compile a validation schema and
161
- return a function that can be used to validate data.
162
- The function returned (a.k.a. _validation function_) is compiled
163
- by using the provided [`SchemaController#ValidationCompiler`](./Server.md#schema-controller).
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 directly
167
- the `ValidationCompiler`, so it can be used to compile the validation
168
- function if a custom `ValidationCompiler` is provided for the route.
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 property errors. Errors encountered during the last validation
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
- Note that you should be careful when using this function, as it will cache
202
- the compiled validation functions based on the schema provided. If the
203
- schemas provided are mutated or changed, the validation functions will not
204
- detect that the schema has been altered and for instance it will reuse the
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 there is a need to change the properties of a schema, always opt to create
209
- a totally new schema (object), otherwise the implementation will not benefit from
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 will validate the input based on the provided schema,
252
- or HTTP part passed. If both are provided, the `httpPart` parameter
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 there is not a validation function for a given `schema`, a new validation
256
- function will be compiled, forwarding the `httpPart` if provided.
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 how to compile validation schemas.
279
+ for more information on compiling validation schemas.