fastify 5.8.5 → 5.9.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/SECURITY.md +1 -1
- package/SPONSORS.md +6 -4
- package/docs/Guides/Database.md +0 -28
- package/docs/Guides/Ecosystem.md +13 -2
- package/docs/Guides/Serverless.md +2 -2
- package/docs/Guides/Write-Plugin.md +1 -1
- package/docs/Reference/Encapsulation.md +27 -26
- package/docs/Reference/Errors.md +10 -4
- package/docs/Reference/HTTP2.md +10 -10
- package/docs/Reference/Hooks.md +4 -4
- package/docs/Reference/Index.md +14 -16
- package/docs/Reference/LTS.md +12 -13
- package/docs/Reference/Lifecycle.md +9 -8
- package/docs/Reference/Logging.md +44 -39
- package/docs/Reference/Middleware.md +21 -25
- package/docs/Reference/Principles.md +2 -2
- package/docs/Reference/Reply.md +6 -1
- package/docs/Reference/Request.md +27 -16
- package/docs/Reference/Routes.md +5 -2
- package/docs/Reference/Server.md +31 -3
- package/docs/Reference/Type-Providers.md +29 -5
- package/docs/Reference/Validation-and-Serialization.md +15 -2
- package/docs/Reference/Warnings.md +7 -6
- package/eslint.config.js +7 -2
- package/fastify.d.ts +8 -3
- package/fastify.js +43 -14
- package/lib/content-type-parser.js +13 -1
- package/lib/decorate.js +11 -3
- package/lib/error-handler.js +4 -3
- package/lib/error-serializer.js +59 -59
- package/lib/errors.js +16 -1
- package/lib/four-oh-four.js +14 -9
- package/lib/handle-request.js +11 -5
- package/lib/plugin-override.js +2 -1
- package/lib/plugin-utils.js +5 -5
- package/lib/reply.js +63 -8
- package/lib/request.js +14 -4
- package/lib/route.js +20 -6
- package/lib/schema-controller.js +1 -1
- package/lib/schemas.js +37 -30
- package/lib/symbols.js +3 -1
- package/lib/validation.js +1 -13
- package/lib/warnings.js +3 -3
- package/package.json +13 -15
- package/scripts/validate-ecosystem-links.js +1 -0
- package/test/bundler/esbuild/package.json +1 -1
- package/test/close-pipelining.test.js +1 -2
- package/test/custom-http-server.test.js +38 -0
- package/test/decorator-instance-properties.test.js +63 -0
- package/test/diagnostics-channel/async-error-handler.test.js +74 -0
- package/test/hooks.test.js +23 -0
- package/test/http-methods/get.test.js +1 -1
- package/test/http2/plain.test.js +135 -0
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/https/https.test.js +1 -2
- package/test/internals/errors.test.js +31 -1
- package/test/internals/plugin.test.js +3 -1
- package/test/internals/request.test.js +27 -3
- package/test/internals/schema-controller-perf.test.js +33 -0
- package/test/logger/logging.test.js +18 -1
- package/test/logger/options.test.js +38 -1
- package/test/reply-error.test.js +1 -1
- package/test/reply-trailers.test.js +70 -0
- package/test/request-media-type.test.js +105 -0
- package/test/route-prefix.test.js +34 -0
- package/test/router-options.test.js +222 -11
- package/test/schema-serialization.test.js +108 -0
- package/test/schema-validation.test.js +24 -0
- package/test/scripts/validate-ecosystem-links.test.js +40 -57
- package/test/throw.test.js +14 -0
- package/test/trust-proxy.test.js +21 -0
- package/test/types/content-type-parser.tst.ts +70 -0
- package/test/types/{decorate-request-reply.test-d.ts → decorate-request-reply.tst.ts} +4 -4
- package/test/types/{dummy-plugin.ts → dummy-plugin.mts} +1 -1
- package/test/types/errors.tst.ts +91 -0
- package/test/types/fastify.tst.ts +351 -0
- package/test/types/hooks.tst.ts +578 -0
- package/test/types/instance.tst.ts +597 -0
- package/test/types/{logger.test-d.ts → logger.tst.ts} +61 -62
- package/test/types/plugin.tst.ts +96 -0
- package/test/types/register.tst.ts +245 -0
- package/test/types/reply.tst.ts +297 -0
- package/test/types/request.tst.ts +199 -0
- package/test/types/route.tst.ts +576 -0
- package/test/types/{schema.test-d.ts → schema.tst.ts} +22 -22
- package/test/types/{serverFactory.test-d.ts → serverFactory.tst.ts} +4 -4
- package/test/types/tsconfig.json +9 -0
- package/test/types/{type-provider.test-d.ts → type-provider.tst.ts} +256 -250
- package/test/types/using.tst.ts +14 -0
- package/types/errors.d.ts +3 -0
- package/types/request.d.ts +23 -2
- package/test/types/content-type-parser.test-d.ts +0 -72
- package/test/types/errors.test-d.ts +0 -90
- package/test/types/fastify.test-d.ts +0 -352
- package/test/types/hooks.test-d.ts +0 -550
- package/test/types/import.ts +0 -2
- package/test/types/instance.test-d.ts +0 -588
- package/test/types/plugin.test-d.ts +0 -97
- package/test/types/register.test-d.ts +0 -237
- package/test/types/reply.test-d.ts +0 -254
- package/test/types/request.test-d.ts +0 -188
- package/test/types/route.test-d.ts +0 -553
- package/test/types/using.test-d.ts +0 -17
package/SECURITY.md
CHANGED
|
@@ -71,7 +71,7 @@ Fastify falls under the [OpenJS CNA](https://cna.openjsf.org/).
|
|
|
71
71
|
A CVE will be assigned as part of our responsible disclosure process.
|
|
72
72
|
|
|
73
73
|
> ℹ️ Note:
|
|
74
|
-
> Fastify's
|
|
74
|
+
> Fastify's HackerOne program is closed.
|
|
75
75
|
|
|
76
76
|
### Strict measures when reporting vulnerabilities
|
|
77
77
|
|
package/SPONSORS.md
CHANGED
|
@@ -13,12 +13,14 @@ or [GitHub Sponsors](https://github.com/sponsors/fastify)!
|
|
|
13
13
|
|
|
14
14
|
## Tier 3
|
|
15
15
|
|
|
16
|
-
- [Mercedes-Benz Group](https://github.com/mercedes-benz)
|
|
17
|
-
- [Val Town, Inc.](https://opencollective.com/valtown)
|
|
18
16
|
- [Handsontable - JavaScript Data Grid](https://handsontable.com/docs/react-data-grid/?utm_source=Fastify_GH&utm_medium=sponsorship&utm_campaign=library_sponsorship_2024)
|
|
19
17
|
- [Lokalise - A Localization and Translation Software Tool](https://lokalise.com/?utm_source=Fastify_GH&utm_medium=sponsorship)
|
|
20
|
-
- [
|
|
18
|
+
- [Val Town](https://www.val.town/)
|
|
19
|
+
- [atagon GmbH](https://github.com/atagon-GmbH)
|
|
20
|
+
- [Photon](https://github.com/photon-hq)
|
|
21
21
|
|
|
22
22
|
## Tier 2
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
- [Sent.dm](https://github.com/sentdm)
|
|
25
|
+
- [Süddeutsche Zeitung](https://github.com/sueddeutsche)
|
|
26
|
+
- [openclaw](https://github.com/openclaw)
|
package/docs/Guides/Database.md
CHANGED
|
@@ -149,34 +149,6 @@ fastify.listen({ port: 3000 }, err => {
|
|
|
149
149
|
})
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
-
### [LevelDB](https://github.com/fastify/fastify-leveldb)
|
|
153
|
-
Install the plugin by running `npm i @fastify/leveldb`
|
|
154
|
-
|
|
155
|
-
*Usage:*
|
|
156
|
-
```javascript
|
|
157
|
-
const fastify = require('fastify')()
|
|
158
|
-
|
|
159
|
-
fastify.register(
|
|
160
|
-
require('@fastify/leveldb'),
|
|
161
|
-
{ name: 'db' }
|
|
162
|
-
)
|
|
163
|
-
|
|
164
|
-
fastify.get('/foo', async function (req, reply) {
|
|
165
|
-
const val = await this.level.db.get(req.query.key)
|
|
166
|
-
return val
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
fastify.post('/foo', async function (req, reply) {
|
|
170
|
-
await this.level.db.put(req.body.key, req.body.value)
|
|
171
|
-
return { status: 'ok' }
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
fastify.listen({ port: 3000 }, err => {
|
|
175
|
-
if (err) throw err
|
|
176
|
-
console.log(`server listening on ${fastify.server.address().port}`)
|
|
177
|
-
})
|
|
178
|
-
```
|
|
179
|
-
|
|
180
152
|
### Writing plugin for a database library
|
|
181
153
|
We could write a plugin for a database
|
|
182
154
|
library too (e.g. Knex, Prisma, or TypeORM).
|
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -66,8 +66,6 @@ section.
|
|
|
66
66
|
Fastify, internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
67
67
|
- [`@fastify/kafka`](https://github.com/fastify/fastify-kafka) Plugin to interact
|
|
68
68
|
with Apache Kafka.
|
|
69
|
-
- [`@fastify/leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to
|
|
70
|
-
share a common LevelDB connection across Fastify.
|
|
71
69
|
- [`@fastify/middie`](https://github.com/fastify/middie) Middleware engine for
|
|
72
70
|
Fastify.
|
|
73
71
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
@@ -206,6 +204,8 @@ section.
|
|
|
206
204
|
- [`@inaiat/fastify-papr`](https://github.com/inaiat/fastify-papr)
|
|
207
205
|
A plugin to integrate [Papr](https://github.com/plexinc/papr),
|
|
208
206
|
the MongoDB ORM for TypeScript & MongoDB, with Fastify.
|
|
207
|
+
- [`@inferdi/fastify`](https://github.com/inferdi/inferdi/tree/main/packages/fastify)
|
|
208
|
+
Type-safe dependency injection support for Fastify, powered by [InferDI](https://github.com/inferdi/inferdi).
|
|
209
209
|
- [`@jerome1337/fastify-enforce-routes-pattern`](https://github.com/Jerome1337/fastify-enforce-routes-pattern)
|
|
210
210
|
A Fastify plugin that enforces naming pattern for routes path.
|
|
211
211
|
- [`@joggr/fastify-prisma`](https://github.com/joggrdocs/fastify-prisma)
|
|
@@ -232,6 +232,9 @@ section.
|
|
|
232
232
|
Plugin to generate routes automatically with valid json content
|
|
233
233
|
- [`@scalar/fastify-api-reference`](https://github.com/scalar/scalar/tree/main/integrations/fastify)
|
|
234
234
|
Beautiful OpenAPI/Swagger API references for Fastify
|
|
235
|
+
- [`@thecodepace/fastify-http-query`](https://github.com/TheCodePace/fastify-http-query)
|
|
236
|
+
Fastify plugin enabling the HTTP `QUERY` method (a safe, idempotent, cacheable
|
|
237
|
+
method with a body).
|
|
235
238
|
- [`@trubavuong/fastify-seaweedfs`](https://github.com/trubavuong/fastify-seaweedfs)
|
|
236
239
|
SeaweedFS for Fastify
|
|
237
240
|
- [`@yeliex/fastify-problem-details`](https://github.com/yeliex/fastify-problem-details)
|
|
@@ -281,6 +284,9 @@ section.
|
|
|
281
284
|
MySQL plugin with auto SQL injection attack prevention.
|
|
282
285
|
- [`fastify-at-postgres`](https://github.com/mateonunez/fastify-at-postgres) Fastify
|
|
283
286
|
Postgres plugin with auto SQL injection attack prevention.
|
|
287
|
+
- [`fastify-ata`](https://github.com/ata-core/fastify-ata) Use
|
|
288
|
+
[`ata-validator`](https://github.com/ata-core/ata-validator) as the JSON Schema
|
|
289
|
+
validator, keeping Fastify's default error shape.
|
|
284
290
|
- [`fastify-auth0-verify`](https://github.com/nearform/fastify-auth0-verify):
|
|
285
291
|
Auth0 verification plugin for Fastify, internally uses
|
|
286
292
|
[fastify-jwt](https://npm.im/fastify-jwt) and
|
|
@@ -539,6 +545,9 @@ middlewares into Fastify plugins
|
|
|
539
545
|
OSM plugin to run overpass queries by OpenStreetMap.
|
|
540
546
|
- [`fastify-override`](https://github.com/matthyk/fastify-override)
|
|
541
547
|
Fastify plugin to override decorators, plugins and hooks for testing purposes
|
|
548
|
+
- [`fastify-param-schema-validation`](https://github.com/Player1205/fastify-param-schema-validation)
|
|
549
|
+
Enforce strict parameter definitions in route validation schemas
|
|
550
|
+
to prevent missing parameter validation.
|
|
542
551
|
- [`fastify-passkit-webservice`](https://github.com/alexandercerutti/fastify-passkit-webservice)
|
|
543
552
|
A set of Fastify plugins to integrate Apple Wallet Web Service specification
|
|
544
553
|
- [`fastify-peekaboo`](https://github.com/simone-sanfratello/fastify-peekaboo)
|
|
@@ -718,6 +727,8 @@ middlewares into Fastify plugins
|
|
|
718
727
|
generator by directory structure.
|
|
719
728
|
- [`fastify-flux`](https://github.com/Jnig/fastify-flux) Tool for building
|
|
720
729
|
Fastify APIs using decorators and convert Typescript interface to JSON Schema.
|
|
730
|
+
- [`fastify-intlayer`](https://intlayer.org/doc/environment/fastify)
|
|
731
|
+
i18n solution for error handling, email template
|
|
721
732
|
- [`jeasx`](https://www.jeasx.dev)
|
|
722
733
|
A flexible server-rendering framework built on Fastify
|
|
723
734
|
that leverages asynchronous JSX to simplify web development.
|
|
@@ -451,9 +451,9 @@ You can add any valid `Dockerfile` that packages and runs a Node app. A basic
|
|
|
451
451
|
docs](https://github.com/knative/docs/blob/2d654d1fd6311750cc57187a86253c52f273d924/docs/serving/samples/hello-world/helloworld-nodejs/Dockerfile).
|
|
452
452
|
|
|
453
453
|
```Dockerfile
|
|
454
|
-
# Use the official Node.js
|
|
454
|
+
# Use the official Node.js LTS image.
|
|
455
455
|
# https://hub.docker.com/_/node
|
|
456
|
-
FROM node:
|
|
456
|
+
FROM node:lts
|
|
457
457
|
|
|
458
458
|
# Create and change to the app directory.
|
|
459
459
|
WORKDIR /usr/src/app
|
|
@@ -53,7 +53,7 @@ Always put an example file in your repository. Examples are very helpful for
|
|
|
53
53
|
users and give a very fast way to test your plugin. Your users will be grateful.
|
|
54
54
|
|
|
55
55
|
## Test
|
|
56
|
-
A plugin **must** be thoroughly tested to verify that is working properly.
|
|
56
|
+
A plugin **must** be thoroughly tested to verify that it is working properly.
|
|
57
57
|
|
|
58
58
|
A plugin without tests will not be accepted to the ecosystem list. A lack of
|
|
59
59
|
tests does not inspire trust nor guarantee that the code will continue to work
|
|
@@ -51,8 +51,8 @@ fastify.register(async function authenticatedContext (childServer) {
|
|
|
51
51
|
childServer.route({
|
|
52
52
|
path: '/one',
|
|
53
53
|
method: 'GET',
|
|
54
|
-
handler (request,
|
|
55
|
-
|
|
54
|
+
handler (request, reply) {
|
|
55
|
+
reply.send({
|
|
56
56
|
answer: request.answer,
|
|
57
57
|
// request.foo will be undefined as it is only defined in publicContext
|
|
58
58
|
foo: request.foo,
|
|
@@ -69,8 +69,8 @@ fastify.register(async function publicContext (childServer) {
|
|
|
69
69
|
childServer.route({
|
|
70
70
|
path: '/two',
|
|
71
71
|
method: 'GET',
|
|
72
|
-
handler (request,
|
|
73
|
-
|
|
72
|
+
handler (request, reply) {
|
|
73
|
+
reply.send({
|
|
74
74
|
answer: request.answer,
|
|
75
75
|
foo: request.foo,
|
|
76
76
|
// request.bar will be undefined as it is only defined in grandchildContext
|
|
@@ -85,8 +85,8 @@ fastify.register(async function publicContext (childServer) {
|
|
|
85
85
|
grandchildServer.route({
|
|
86
86
|
path: '/three',
|
|
87
87
|
method: 'GET',
|
|
88
|
-
handler (request,
|
|
89
|
-
|
|
88
|
+
handler (request, reply) {
|
|
89
|
+
reply.send({
|
|
90
90
|
answer: request.answer,
|
|
91
91
|
foo: request.foo,
|
|
92
92
|
bar: request.bar
|
|
@@ -114,12 +114,12 @@ original diagram:
|
|
|
114
114
|
To see this, start the server and issue requests:
|
|
115
115
|
|
|
116
116
|
```sh
|
|
117
|
-
|
|
118
|
-
{"answer":42}
|
|
119
|
-
|
|
120
|
-
{"answer":42,"foo":"foo"}
|
|
121
|
-
|
|
122
|
-
{"answer":42,"foo":"foo","bar":"bar"}
|
|
117
|
+
curl -H 'authorization: Bearer abc123' http://127.0.0.1:8000/one
|
|
118
|
+
# {"answer":42}
|
|
119
|
+
curl http://127.0.0.1:8000/two
|
|
120
|
+
# {"answer":42,"foo":"foo"}
|
|
121
|
+
curl http://127.0.0.1:8000/three
|
|
122
|
+
# {"answer":42,"foo":"foo","bar":"bar"}
|
|
123
123
|
```
|
|
124
124
|
|
|
125
125
|
[bearer]: https://github.com/fastify/fastify-bearer-auth
|
|
@@ -127,13 +127,14 @@ To see this, start the server and issue requests:
|
|
|
127
127
|
## Sharing Between Contexts
|
|
128
128
|
<a id="shared-context"></a>
|
|
129
129
|
|
|
130
|
-
Each context in the
|
|
131
|
-
contexts cannot access entities within their descendant contexts.
|
|
132
|
-
encapsulation can be broken using [fastify-plugin][fastify-plugin],
|
|
133
|
-
anything registered in a descendant context available
|
|
130
|
+
Each context in the previous example inherits _only_ from its parent contexts.
|
|
131
|
+
Parent contexts cannot access entities within their descendant contexts.
|
|
132
|
+
If needed, encapsulation can be broken using [fastify-plugin][fastify-plugin],
|
|
133
|
+
making anything registered in a descendant context available
|
|
134
|
+
to the parent context.
|
|
134
135
|
|
|
135
|
-
To allow `publicContext` access
|
|
136
|
-
|
|
136
|
+
To allow `publicContext` to access the `bar` decorator from `grandchildContext`,
|
|
137
|
+
update the code as follows:
|
|
137
138
|
|
|
138
139
|
```js
|
|
139
140
|
'use strict'
|
|
@@ -151,8 +152,8 @@ fastify.register(async function publicContext (childServer) {
|
|
|
151
152
|
childServer.route({
|
|
152
153
|
path: '/two',
|
|
153
154
|
method: 'GET',
|
|
154
|
-
handler (request,
|
|
155
|
-
|
|
155
|
+
handler (request, reply) {
|
|
156
|
+
reply.send({
|
|
156
157
|
answer: request.answer,
|
|
157
158
|
foo: request.foo,
|
|
158
159
|
bar: request.bar
|
|
@@ -168,8 +169,8 @@ fastify.register(async function publicContext (childServer) {
|
|
|
168
169
|
grandchildServer.route({
|
|
169
170
|
path: '/three',
|
|
170
171
|
method: 'GET',
|
|
171
|
-
handler (request,
|
|
172
|
-
|
|
172
|
+
handler (request, reply) {
|
|
173
|
+
reply.send({
|
|
173
174
|
answer: request.answer,
|
|
174
175
|
foo: request.foo,
|
|
175
176
|
bar: request.bar
|
|
@@ -185,10 +186,10 @@ fastify.listen({ port: 8000 })
|
|
|
185
186
|
Restarting the server and re-issuing the requests for `/two` and `/three`:
|
|
186
187
|
|
|
187
188
|
```sh
|
|
188
|
-
|
|
189
|
-
{"answer":42,"foo":"foo","bar":"bar"}
|
|
190
|
-
|
|
191
|
-
{"answer":42,"foo":"foo","bar":"bar"}
|
|
189
|
+
curl http://127.0.0.1:8000/two
|
|
190
|
+
# {"answer":42,"foo":"foo","bar":"bar"}
|
|
191
|
+
curl http://127.0.0.1:8000/three
|
|
192
|
+
# {"answer":42,"foo":"foo","bar":"bar"}
|
|
192
193
|
```
|
|
193
194
|
|
|
194
195
|
[fastify-plugin]: https://github.com/fastify/fastify-plugin
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -74,6 +74,7 @@
|
|
|
74
74
|
- [FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE](#fst_err_force_close_connections_idle_not_available)
|
|
75
75
|
- [FST_ERR_DUPLICATED_ROUTE](#fst_err_duplicated_route)
|
|
76
76
|
- [FST_ERR_BAD_URL](#fst_err_bad_url)
|
|
77
|
+
- [FST_ERR_MAX_PARAM_LENGTH](#fst_err_max_param_length)
|
|
77
78
|
- [FST_ERR_ASYNC_CONSTRAINT](#fst_err_async_constraint)
|
|
78
79
|
- [FST_ERR_INVALID_URL](#fst_err_invalid_url)
|
|
79
80
|
- [FST_ERR_ROUTE_OPTIONS_NOT_OBJ](#fst_err_route_options_not_obj)
|
|
@@ -82,6 +83,7 @@
|
|
|
82
83
|
- [FST_ERR_ROUTE_MISSING_HANDLER](#fst_err_route_missing_handler)
|
|
83
84
|
- [FST_ERR_ROUTE_METHOD_INVALID](#fst_err_route_method_invalid)
|
|
84
85
|
- [FST_ERR_ROUTE_METHOD_NOT_SUPPORTED](#fst_err_route_method_not_supported)
|
|
86
|
+
- [FST_ERR_ROUTE_LOG_LEVEL_INVALID](#fst_err_route_log_level_invalid)
|
|
85
87
|
- [FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED](#fst_err_route_body_validation_schema_not_supported)
|
|
86
88
|
- [FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT](#fst_err_route_body_limit_option_not_int)
|
|
87
89
|
- [FST_ERR_HANDLER_TIMEOUT](#fst_err_handler_timeout)
|
|
@@ -98,6 +100,7 @@
|
|
|
98
100
|
- [FST_ERR_PLUGIN_TIMEOUT](#fst_err_plugin_timeout)
|
|
99
101
|
- [FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE](#fst_err_plugin_not_present_in_instance)
|
|
100
102
|
- [FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER](#fst_err_plugin_invalid_async_handler)
|
|
103
|
+
- [FST_ERR_PLUGIN_DEPENDENCY_NOT_REGISTERED](#fst_err_plugin_dependency_not_registered)
|
|
101
104
|
- [FST_ERR_VALIDATION](#fst_err_validation)
|
|
102
105
|
- [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid)
|
|
103
106
|
- [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn)
|
|
@@ -209,16 +212,16 @@ fastify.setErrorHandler((error, request, reply) => {
|
|
|
209
212
|
|
|
210
213
|
fastify.register((app, options, next) => {
|
|
211
214
|
// Register child error handler
|
|
212
|
-
|
|
215
|
+
app.setErrorHandler((error, request, reply) => {
|
|
213
216
|
throw error
|
|
214
217
|
})
|
|
215
218
|
|
|
216
|
-
|
|
217
|
-
// Throws a non-Error type, '
|
|
219
|
+
app.get('/bad', async () => {
|
|
220
|
+
// Throws a non-Error type, 'foo'
|
|
218
221
|
throw 'foo'
|
|
219
222
|
})
|
|
220
223
|
|
|
221
|
-
|
|
224
|
+
app.get('/good', async () => {
|
|
222
225
|
// Throws an Error instance, 'bar'
|
|
223
226
|
throw new Error('bar')
|
|
224
227
|
})
|
|
@@ -349,6 +352,7 @@ Below is a table with all the error codes used by Fastify.
|
|
|
349
352
|
| <a id="fst_err_force_close_connections_idle_not_available">FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE</a> | Cannot set forceCloseConnections to `idle` as your HTTP server does not support `closeIdleConnections` method. | Use a different value for `forceCloseConnections`. | [#3925](https://github.com/fastify/fastify/pull/3925) |
|
|
350
353
|
| <a id="fst_err_duplicated_route">FST_ERR_DUPLICATED_ROUTE</a> | The HTTP method already has a registered controller for that URL. | Use a different URL or register the controller for another HTTP method. | [#2954](https://github.com/fastify/fastify/pull/2954) |
|
|
351
354
|
| <a id="fst_err_bad_url">FST_ERR_BAD_URL</a> | The router received an invalid URL. | Use a valid URL. | [#2106](https://github.com/fastify/fastify/pull/2106) |
|
|
355
|
+
| <a id="fst_err_max_param_length">FST_ERR_MAX_PARAM_LENGTH</a> | The router received an URL that exceed max param length. | Adjust the param length or increase the max param length to meet your needs. | [#2106](https://github.com/fastify/fastify/pull/6716) |
|
|
352
356
|
| <a id="fst_err_async_constraint">FST_ERR_ASYNC_CONSTRAINT</a> | The router received an error when using asynchronous constraints. | - | [#4323](https://github.com/fastify/fastify/pull/4323) |
|
|
353
357
|
| <a id="fst_err_invalid_url">FST_ERR_INVALID_URL</a> | URL must be a string. | Use a string for the URL. | [#3653](https://github.com/fastify/fastify/pull/3653) |
|
|
354
358
|
| <a id="fst_err_route_options_not_obj">FST_ERR_ROUTE_OPTIONS_NOT_OBJ</a> | Options for the route must be an object. | Use an object for the route options. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
@@ -357,6 +361,7 @@ Below is a table with all the error codes used by Fastify.
|
|
|
357
361
|
| <a id="fst_err_route_missing_handler">FST_ERR_ROUTE_MISSING_HANDLER</a> | Missing handler function for the route. | Add a handler function. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
358
362
|
| <a id="fst_err_route_method_invalid">FST_ERR_ROUTE_METHOD_INVALID</a> | Method is not a valid value. | Use a valid value for the method. | [#4750](https://github.com/fastify/fastify/pull/4750) |
|
|
359
363
|
| <a id="fst_err_route_method_not_supported">FST_ERR_ROUTE_METHOD_NOT_SUPPORTED</a> | Method is not supported for the route. | Use a supported method. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
364
|
+
| <a id="fst_err_route_log_level_invalid">FST_ERR_ROUTE_LOG_LEVEL_INVALID</a> | `logLevel` must match a configured logger level. | Use one of the configured logger levels for the route. | [#6523](https://github.com/fastify/fastify/pull/6523) |
|
|
360
365
|
| <a id="fst_err_route_body_validation_schema_not_supported">FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED</a> | Body validation schema route is not supported. | Use a different different method for the route. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
361
366
|
| <a id="fst_err_route_body_limit_option_not_int">FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT</a> | `bodyLimit` option must be an integer. | Use an integer for the `bodyLimit` option. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
362
367
|
| <a id="fst_err_handler_timeout">FST_ERR_HANDLER_TIMEOUT</a> | Request timed out. | Increase the `handlerTimeout` option or optimize the handler. | - |
|
|
@@ -372,6 +377,7 @@ Below is a table with all the error codes used by Fastify.
|
|
|
372
377
|
| <a id="fst_err_plugin_timeout">FST_ERR_PLUGIN_TIMEOUT</a> | Plugin did not start in time. | Increase the timeout for the plugin. | [#3106](https://github.com/fastify/fastify/pull/3106) |
|
|
373
378
|
| <a id="fst_err_plugin_not_present_in_instance">FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE</a> | The decorator is not present in the instance. | - | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
374
379
|
| <a id="fst_err_plugin_invalid_async_handler">FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER</a> | The plugin being registered mixes async and callback styles. | - | [#5141](https://github.com/fastify/fastify/pull/5141) |
|
|
380
|
+
| <a id="fst_err_plugin_dependency_not_registered">FST_ERR_PLUGIN_DEPENDENCY_NOT_REGISTERED</a> | The dependency of a plugin is not registered. | Register the missing dependency before registering this plugin. | [#6774](https://github.com/fastify/fastify/pull/6774) |
|
|
375
381
|
| <a id="fst_err_validation">FST_ERR_VALIDATION</a> | The Request failed the payload validation. | Check the request payload. | [#4824](https://github.com/fastify/fastify/pull/4824) |
|
|
376
382
|
| <a id="fst_err_listen_options_invalid">FST_ERR_LISTEN_OPTIONS_INVALID</a> | Invalid listen options. | Check the listen options. | [#4886](https://github.com/fastify/fastify/pull/4886) |
|
|
377
383
|
| <a id="fst_err_error_handler_not_fn">FST_ERR_ERROR_HANDLER_NOT_FN</a> | Error Handler must be a function | Provide a function to `setErrorHandler`. | [#5317](https://github.com/fastify/fastify/pull/5317) | <a id="fst_err_error_handler_already_set">FST_ERR_ERROR_HANDLER_ALREADY_SET</a> | Error Handler already set in this scope. Set `allowErrorHandlerOverride: true` to allow overriding. | By default, `setErrorHandler` can only be called once per encapsulation context. | [#6097](https://github.com/fastify/fastify/pull/6098) |
|
package/docs/Reference/HTTP2.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
<h1 align="center">Fastify</h1>
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## HTTP/2
|
|
4
4
|
|
|
5
|
-
_Fastify_ supports
|
|
5
|
+
_Fastify_ supports HTTP/2 over HTTPS (h2) or plaintext (h2c).
|
|
6
6
|
|
|
7
|
-
Currently, none of the
|
|
7
|
+
Currently, none of the HTTP/2-specific APIs are available through _Fastify_, but
|
|
8
8
|
Node's `req` and `res` can be accessed through the `Request` and `Reply`
|
|
9
9
|
interfaces. PRs are welcome.
|
|
10
10
|
|
|
11
11
|
### Secure (HTTPS)
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
HTTP/2 is supported in all modern browsers __only over a secure connection__:
|
|
14
14
|
|
|
15
15
|
```js
|
|
16
16
|
'use strict'
|
|
@@ -32,11 +32,11 @@ fastify.get('/', function (request, reply) {
|
|
|
32
32
|
fastify.listen({ port: 3000 })
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
[ALPN negotiation](https://datatracker.ietf.org/doc/html/rfc7301)
|
|
36
|
-
|
|
35
|
+
[ALPN negotiation](https://datatracker.ietf.org/doc/html/rfc7301) enables
|
|
36
|
+
both HTTPS and HTTP/2 over the same socket.
|
|
37
37
|
Node core `req` and `res` objects can be either
|
|
38
38
|
[HTTP/1](https://nodejs.org/api/http.html) or
|
|
39
|
-
[HTTP/2](https://nodejs.org/api/http2.html). _Fastify_ supports
|
|
39
|
+
[HTTP/2](https://nodejs.org/api/http2.html). _Fastify_ supports both out of the
|
|
40
40
|
box:
|
|
41
41
|
|
|
42
42
|
```js
|
|
@@ -53,7 +53,7 @@ const fastify = require('fastify')({
|
|
|
53
53
|
}
|
|
54
54
|
})
|
|
55
55
|
|
|
56
|
-
//
|
|
56
|
+
// This route can be accessed through both protocols
|
|
57
57
|
fastify.get('/', function (request, reply) {
|
|
58
58
|
reply.code(200).send({ hello: 'world' })
|
|
59
59
|
})
|
|
@@ -61,7 +61,7 @@ fastify.get('/', function (request, reply) {
|
|
|
61
61
|
fastify.listen({ port: 3000 })
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
Test the
|
|
64
|
+
Test the server with:
|
|
65
65
|
|
|
66
66
|
```
|
|
67
67
|
$ npx h2url https://localhost:3000
|
|
@@ -69,7 +69,7 @@ $ npx h2url https://localhost:3000
|
|
|
69
69
|
|
|
70
70
|
### Plain or insecure
|
|
71
71
|
|
|
72
|
-
For microservices,
|
|
72
|
+
For microservices, HTTP/2 can connect in plain text, but this is not
|
|
73
73
|
supported by browsers.
|
|
74
74
|
|
|
75
75
|
```js
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -737,9 +737,9 @@ fastify.addHook('onResponse', (request, reply, done) => {
|
|
|
737
737
|
done()
|
|
738
738
|
})
|
|
739
739
|
|
|
740
|
-
fastify.addHook('preParsing', (request, reply, done) => {
|
|
740
|
+
fastify.addHook('preParsing', (request, reply, payload, done) => {
|
|
741
741
|
// Your code
|
|
742
|
-
done()
|
|
742
|
+
done(null, payload)
|
|
743
743
|
})
|
|
744
744
|
|
|
745
745
|
fastify.addHook('preValidation', (request, reply, done) => {
|
|
@@ -790,9 +790,9 @@ fastify.route({
|
|
|
790
790
|
// this hook will always be executed after the shared `onResponse` hooks
|
|
791
791
|
done()
|
|
792
792
|
},
|
|
793
|
-
preParsing: function (request, reply, done) {
|
|
793
|
+
preParsing: function (request, reply, payload, done) {
|
|
794
794
|
// This hook will always be executed after the shared `preParsing` hooks
|
|
795
|
-
done()
|
|
795
|
+
done(null, payload)
|
|
796
796
|
},
|
|
797
797
|
preValidation: function (request, reply, done) {
|
|
798
798
|
// This hook will always be executed after the shared `preValidation` hooks
|
package/docs/Reference/Index.md
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
## Core Documents
|
|
4
4
|
<a id="reference-core-docs"></a>
|
|
5
5
|
|
|
6
|
-
For the full table of contents
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
For the full table of contents, see [below](#reference-toc). The following list
|
|
7
|
+
is a subset of the full table of contents that details core Fastify APIs and
|
|
8
|
+
concepts, ordered by likely importance to the reader:
|
|
9
9
|
|
|
10
10
|
+ [Server](./Server.md): Documents the core Fastify API. Includes documentation
|
|
11
|
-
for the factory function and the
|
|
11
|
+
for the factory function and the resulting server instance.
|
|
12
12
|
+ [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
|
|
13
13
|
illustrates where [Hooks](./Hooks.md) are available for integrating with it.
|
|
14
14
|
+ [Routes](./Routes.md): Details how to register routes with Fastify and how
|
|
@@ -25,8 +25,8 @@ order of most likely importance to the reader:
|
|
|
25
25
|
Fastify plugins are built.
|
|
26
26
|
+ [Decorators](./Decorators.md): Explains the server, request, and response
|
|
27
27
|
decorator APIs.
|
|
28
|
-
+ [Hooks](./Hooks.md): Details the API
|
|
29
|
-
|
|
28
|
+
+ [Hooks](./Hooks.md): Details the API that allows plugins to integrate with
|
|
29
|
+
the request lifecycle.
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
## Reference Documentation Table Of Contents
|
|
@@ -42,18 +42,17 @@ This table of contents is in alphabetical order.
|
|
|
42
42
|
Fastify plugins are built.
|
|
43
43
|
+ [Errors](./Errors.md): Details how Fastify handles errors and lists the
|
|
44
44
|
standard set of errors Fastify generates.
|
|
45
|
-
+ [Hooks](./Hooks.md): Details the API
|
|
46
|
-
|
|
47
|
-
+ [
|
|
45
|
+
+ [Hooks](./Hooks.md): Details the API that allows plugins to integrate with
|
|
46
|
+
the request lifecycle.
|
|
47
|
+
+ [HTTP/2](./HTTP2.md): Details Fastify's HTTP/2 support.
|
|
48
48
|
+ [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
|
|
49
49
|
illustrates where [Hooks](./Hooks.md) are available for integrating with it.
|
|
50
50
|
+ [Logging](./Logging.md): Details Fastify's included logging and how to
|
|
51
51
|
customize it.
|
|
52
|
-
+ [Long Term Support](./LTS.md): Explains Fastify's long
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
middleware.
|
|
52
|
+
+ [Long Term Support](./LTS.md): Explains Fastify's long-term support guarantee
|
|
53
|
+
and the possible exceptions to the [semver](https://semver.org) contract.
|
|
54
|
+
+ [Middleware](./Middleware.md): Details Fastify's support for
|
|
55
|
+
Express.js-style middleware.
|
|
57
56
|
+ [Plugins](./Plugins.md): Explains Fastify's plugin architecture and API.
|
|
58
57
|
+ [Reply](./Reply.md): Details Fastify's response object available to each
|
|
59
58
|
request handler.
|
|
@@ -64,8 +63,7 @@ This table of contents is in alphabetical order.
|
|
|
64
63
|
+ [Server](./Server.md): Documents the core Fastify API. Includes documentation
|
|
65
64
|
for the factory function and the object returned by the factory function.
|
|
66
65
|
+ [TypeScript](./TypeScript.md): Documents Fastify's TypeScript support and
|
|
67
|
-
provides recommendations for
|
|
68
|
-
Fastify.
|
|
66
|
+
provides recommendations for TypeScript application development.
|
|
69
67
|
+ [Validation and Serialization](./Validation-and-Serialization.md): Details
|
|
70
68
|
Fastify's support for validating incoming data and how Fastify serializes data
|
|
71
69
|
for responses.
|
package/docs/Reference/LTS.md
CHANGED
|
@@ -11,10 +11,10 @@ in this document:
|
|
|
11
11
|
date. The release date of any specific version can be found at
|
|
12
12
|
[https://github.com/fastify/fastify/releases](https://github.com/fastify/fastify/releases).
|
|
13
13
|
2. Major releases will receive security updates for an additional six months
|
|
14
|
-
from the release of the next major release. After this period
|
|
15
|
-
review and release security fixes as long as they are provided
|
|
16
|
-
community and they do not violate other constraints,
|
|
17
|
-
Node.js version.
|
|
14
|
+
from the release of the next major release. After this period, Fastify maintainers
|
|
15
|
+
will still review and release security fixes as long as they are provided
|
|
16
|
+
by the community and they do not violate other constraints,
|
|
17
|
+
e.g., minimum supported Node.js version.
|
|
18
18
|
3. Major releases will be tested and verified against all Node.js release lines
|
|
19
19
|
that are supported by the [Node.js LTS
|
|
20
20
|
policy](https://github.com/nodejs/Release) within the LTS period of that
|
|
@@ -24,7 +24,7 @@ in this document:
|
|
|
24
24
|
and verified against alternative runtimes that are compatible with Node.js.
|
|
25
25
|
The maintenance teams of these alternative runtimes are responsible for ensuring
|
|
26
26
|
and guaranteeing these tests work properly.
|
|
27
|
-
1. [N|Solid](https://docs.nodesource.com/docs/product_suite) tests and
|
|
27
|
+
1. [N|Solid](https://docs.nodesource.com/docs/product_suite/) tests and
|
|
28
28
|
verifies each Fastify major release against current N|Solid LTS versions.
|
|
29
29
|
NodeSource ensures Fastify compatibility with N|Solid, aligning with the
|
|
30
30
|
support scope of N|Solid LTS versions at the time of the Fastify release.
|
|
@@ -35,21 +35,21 @@ A "month" is defined as 30 consecutive days.
|
|
|
35
35
|
> ## Security Releases and Semver
|
|
36
36
|
>
|
|
37
37
|
> As a consequence of providing long-term support for major releases, there are
|
|
38
|
-
> occasions
|
|
39
|
-
> release. Such changes will _always_ be
|
|
38
|
+
> occasions when breaking changes must be released as a _minor_ version
|
|
39
|
+
> release. Such changes will _always_ be documented in the [release
|
|
40
40
|
> notes](https://github.com/fastify/fastify/releases).
|
|
41
41
|
>
|
|
42
|
-
> To avoid automatically receiving breaking security updates it is possible to
|
|
42
|
+
> To avoid automatically receiving breaking security updates, it is possible to
|
|
43
43
|
> use the tilde (`~`) range qualifier. For example, to get patches for the 3.15
|
|
44
44
|
> release, and avoid automatically updating to the 3.16 release, specify the
|
|
45
45
|
> dependency as `"fastify": "~3.15.x"`. This will leave your application
|
|
46
|
-
> vulnerable
|
|
46
|
+
> vulnerable. Use this approach with caution.
|
|
47
47
|
|
|
48
48
|
### Security Support Beyond LTS
|
|
49
49
|
|
|
50
50
|
Fastify's partner, HeroDevs, provides commercial security support through the
|
|
51
51
|
OpenJS Ecosystem Sustainability Program for versions of Fastify that are EOL.
|
|
52
|
-
For more information, see
|
|
52
|
+
For more information, see the [Never Ending Support][hd-link] service offered.
|
|
53
53
|
|
|
54
54
|
### Schedule
|
|
55
55
|
<a id="lts-schedule"></a>
|
|
@@ -62,7 +62,7 @@ For more information, see their [Never Ending Support][hd-link] service.
|
|
|
62
62
|
| 4.0.0 | 2022-06-08 | 2025-06-30 | 14, 16, 18, 20, 22 | v5(18), v5(20) |
|
|
63
63
|
| 5.0.0 | 2024-09-17 | TBD | 20, 22 | v5(20) |
|
|
64
64
|
|
|
65
|
-
### CI
|
|
65
|
+
### CI Tested Operating Systems
|
|
66
66
|
<a id="supported-os"></a>
|
|
67
67
|
|
|
68
68
|
Fastify uses GitHub Actions for CI testing, please refer to [GitHub's
|
|
@@ -78,8 +78,7 @@ YAML workflow labels below:
|
|
|
78
78
|
| Windows | `windows-latest` | npm | 20 | v5(20) |
|
|
79
79
|
| MacOS | `macos-latest` | npm | 20 | v5(20) |
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
flag.
|
|
81
|
+
When using [yarn](https://yarnpkg.com/), the `--ignore-engines` flag may be required.
|
|
83
82
|
|
|
84
83
|
[semver]: https://semver.org/
|
|
85
84
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
## Lifecycle
|
|
4
4
|
<a id="lifecycle"></a>
|
|
5
5
|
|
|
6
|
-
This
|
|
6
|
+
This diagram shows the internal lifecycle of Fastify.
|
|
7
7
|
|
|
8
8
|
The right branch of each section shows the next phase of the lifecycle. The left
|
|
9
9
|
branch shows the corresponding error code generated if the parent throws an
|
|
@@ -41,10 +41,11 @@ Incoming Request
|
|
|
41
41
|
└─▶ onResponse Hook
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
When `handlerTimeout` is configured, a
|
|
45
|
-
response is not sent within the allowed time,
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
When [`handlerTimeout`](./Server.md#factory-handler-timeout) is configured, a
|
|
45
|
+
timer starts after routing. If the response is not sent within the allowed time,
|
|
46
|
+
`request.signal` is aborted and a `503 Service Unavailable` error is sent.
|
|
47
|
+
The timer is cancelled when the response completes
|
|
48
|
+
or when `reply.hijack()` is called.
|
|
48
49
|
|
|
49
50
|
Before or during the `User Handler`, `reply.hijack()` can be called to:
|
|
50
51
|
- Prevent Fastify from running subsequent hooks and the user handler
|
|
@@ -61,8 +62,8 @@ When the user handles the request, the result may be:
|
|
|
61
62
|
- In an async handler: it returns a payload or throws an `Error`
|
|
62
63
|
- In a sync handler: it sends a payload or an `Error` instance
|
|
63
64
|
|
|
64
|
-
If the reply
|
|
65
|
-
|
|
65
|
+
If the reply is hijacked, all subsequent steps are skipped.
|
|
66
|
+
Otherwise, the data flows as follows:
|
|
66
67
|
|
|
67
68
|
```
|
|
68
69
|
★ schema validation Error
|
|
@@ -96,4 +97,4 @@ graceful shutdown sequence involving
|
|
|
96
97
|
[`preClose`](./Hooks.md#pre-close) hooks, connection draining, and
|
|
97
98
|
[`onClose`](./Hooks.md#on-close) hooks. See the
|
|
98
99
|
[`close`](./Server.md#close) method documentation for the full step-by-step
|
|
99
|
-
lifecycle.
|
|
100
|
+
lifecycle.
|