fastify 5.3.2 â 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/build/build-validation.js +2 -1
- package/docs/Guides/Delay-Accepting-Requests.md +3 -3
- package/docs/Guides/Ecosystem.md +16 -7
- package/docs/Guides/Serverless.md +28 -69
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +2 -4
- package/docs/Reference/Hooks.md +14 -14
- package/docs/Reference/Logging.md +3 -3
- package/docs/Reference/Middleware.md +1 -1
- package/docs/Reference/Reply.md +8 -8
- package/docs/Reference/Request.md +1 -1
- package/docs/Reference/Routes.md +3 -3
- package/docs/Reference/Server.md +40 -10
- package/docs/Reference/Validation-and-Serialization.md +1 -1
- package/eslint.config.js +17 -9
- package/fastify.d.ts +2 -1
- package/fastify.js +20 -4
- package/lib/configValidator.js +1 -1
- package/lib/decorate.js +2 -2
- package/lib/errors.js +6 -8
- package/lib/logger-factory.js +1 -1
- package/lib/logger-pino.js +2 -2
- package/lib/pluginOverride.js +3 -1
- package/lib/reply.js +9 -13
- package/lib/request.js +4 -11
- package/lib/server.js +30 -51
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +8 -0
- package/package.json +11 -7
- package/test/404s.test.js +226 -325
- package/test/allow-unsafe-regex.test.js +19 -48
- package/test/als.test.js +28 -40
- package/test/async-await.test.js +11 -2
- package/test/body-limit.test.js +41 -65
- package/test/build-certificate.js +1 -1
- package/test/close-pipelining.test.js +5 -4
- package/test/custom-parser-async.test.js +17 -22
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +422 -341
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/helper.js +108 -70
- package/test/hooks-async.test.js +248 -218
- package/test/hooks.on-listen.test.js +255 -239
- package/test/hooks.on-ready.test.js +110 -92
- package/test/hooks.test.js +910 -769
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/proppatch.test.js +23 -29
- package/test/http-methods/report.test.js +44 -69
- package/test/http-methods/search.test.js +67 -82
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +28 -27
- package/test/https/https.test.js +56 -53
- package/test/inject.test.js +114 -97
- package/test/input-validation.js +63 -53
- package/test/internals/errors.test.js +0 -10
- package/test/internals/handle-request.test.js +49 -66
- package/test/internals/hooks.test.js +17 -0
- package/test/issue-4959.test.js +14 -5
- package/test/listen.4.test.js +31 -43
- package/test/logger/response.test.js +19 -20
- package/test/nullable-validation.test.js +33 -46
- package/test/options.error-handler.test.js +1 -1
- package/test/options.test.js +1 -1
- package/test/output-validation.test.js +49 -72
- package/test/patch.error-handler.test.js +1 -1
- package/test/patch.test.js +1 -1
- package/test/plugin.1.test.js +71 -60
- package/test/plugin.2.test.js +104 -86
- package/test/plugin.3.test.js +56 -35
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +36 -30
- package/test/proto-poisoning.test.js +78 -97
- package/test/put.error-handler.test.js +1 -1
- package/test/put.test.js +1 -1
- package/test/reply-error.test.js +169 -148
- package/test/reply-trailers.test.js +119 -108
- package/test/request-error.test.js +0 -46
- package/test/route-hooks.test.js +112 -92
- package/test/route-prefix.test.js +194 -133
- package/test/schema-feature.test.js +309 -238
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +278 -199
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +30 -27
- package/test/stream.2.test.js +20 -10
- package/test/stream.3.test.js +37 -31
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +0 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/register.test-d.ts +1 -1
- package/test/types/request.test-d.ts +1 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +1 -1
- package/types/errors.d.ts +0 -1
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
- package/test/http2/missing-http2-module.test.js +0 -17
package/README.md
CHANGED
|
@@ -306,6 +306,8 @@ listed in alphabetical order.
|
|
|
306
306
|
* [__Vincent Le Goff__](https://github.com/zekth)
|
|
307
307
|
* [__Luciano Mammino__](https://github.com/lmammino),
|
|
308
308
|
<https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
|
|
309
|
+
* [__Jean Michelet__](https://github.com/jean-michelet),
|
|
310
|
+
<https://www.npmjs.com/~jean-michelet>
|
|
309
311
|
* [__KaKa Ng__](https://github.com/climba03003),
|
|
310
312
|
<https://www.npmjs.com/~climba03003>
|
|
311
313
|
* [__Luis Orbaiceta__](https://github.com/luisorbaiceta),
|
|
@@ -42,7 +42,8 @@ const defaultInitOptions = {
|
|
|
42
42
|
requestIdLogLabel: 'reqId',
|
|
43
43
|
http2SessionTimeout: 72000, // 72 seconds
|
|
44
44
|
exposeHeadRoutes: true,
|
|
45
|
-
useSemicolonDelimiter: false
|
|
45
|
+
useSemicolonDelimiter: false,
|
|
46
|
+
allowErrorHandlerOverride: true // TODO: set to false in v6
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
const schema = {
|
|
@@ -529,9 +529,9 @@ since that was not one of the requests we asked our plugin to filter, it
|
|
|
529
529
|
succeeded. That could also be used as a means of informing an interested party
|
|
530
530
|
whether or not we were ready to serve requests (although `/ping` is more
|
|
531
531
|
commonly associated with *liveness* checks and that would be the responsibility
|
|
532
|
-
of a *readiness* check -- the curious reader can get more info on these
|
|
533
|
-
[
|
|
534
|
-
with the `ready` field. Below is the response to that request:
|
|
532
|
+
of a *readiness* check -- the curious reader can get more info on these
|
|
533
|
+
[terms](https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-setting-up-health-checks-with-readiness-and-liveness-probes))
|
|
534
|
+
here with the `ready` field. Below is the response to that request:
|
|
535
535
|
|
|
536
536
|
```sh
|
|
537
537
|
HTTP/1.1 200 OK
|
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -40,8 +40,6 @@ section.
|
|
|
40
40
|
plugin for adding
|
|
41
41
|
[CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection to
|
|
42
42
|
Fastify.
|
|
43
|
-
- [`@fastify/diagnostics-channel`](https://github.com/fastify/fastify-diagnostics-channel)
|
|
44
|
-
Plugin to deal with `diagnostics_channel` on Fastify.
|
|
45
43
|
- [`@fastify/elasticsearch`](https://github.com/fastify/fastify-elasticsearch)
|
|
46
44
|
Plugin to share the same ES client.
|
|
47
45
|
- [`@fastify/env`](https://github.com/fastify/fastify-env) Load and check
|
|
@@ -154,6 +152,15 @@ section.
|
|
|
154
152
|
|
|
155
153
|
#### [Community](#community)
|
|
156
154
|
|
|
155
|
+
> âšī¸ Note:
|
|
156
|
+
> Fastify community plugins are part of the broader community efforts,
|
|
157
|
+
> and we are thankful for these contributions. However, they are not
|
|
158
|
+
> maintained by the Fastify team.
|
|
159
|
+
> Use them at your own discretion.
|
|
160
|
+
> If you find malicious code, please
|
|
161
|
+
> [open an issue](https://github.com/fastify/fastify/issues/new/choose) or
|
|
162
|
+
> submit a PR to remove the plugin from the list.
|
|
163
|
+
|
|
157
164
|
- [`@aaroncadillac/crudify-mongo`](https://github.com/aaroncadillac/crudify-mongo)
|
|
158
165
|
A simple way to add a crud in your fastify project.
|
|
159
166
|
- [`@applicazza/fastify-nextjs`](https://github.com/applicazza/fastify-nextjs)
|
|
@@ -195,11 +202,9 @@ section.
|
|
|
195
202
|
- [`@immobiliarelabs/fastify-metrics`](https://github.com/immobiliare/fastify-metrics)
|
|
196
203
|
Minimalistic and opinionated plugin that collects usage/process metrics and
|
|
197
204
|
dispatches to [statsd](https://github.com/statsd/statsd).
|
|
198
|
-
- [`@
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
A plugin to implement [Lyra](https://github.com/nearform/lyra) search engine
|
|
202
|
-
on Fastify
|
|
205
|
+
- [`@inaiat/fastify-papr`](https://github.com/inaiat/fastify-papr)
|
|
206
|
+
A plugin to integrate [Papr](https://github.com/plexinc/papr),
|
|
207
|
+
the MongoDB ORM for TypeScript & MongoDB, with Fastify.
|
|
203
208
|
- [`@jerome1337/fastify-enforce-routes-pattern`](https://github.com/Jerome1337/fastify-enforce-routes-pattern)
|
|
204
209
|
A Fastify plugin that enforces naming pattern for routes path.
|
|
205
210
|
- [`@joggr/fastify-prisma`](https://github.com/joggrdocs/fastify-prisma)
|
|
@@ -737,9 +742,13 @@ middlewares into Fastify plugins
|
|
|
737
742
|
generator by directory structure.
|
|
738
743
|
- [`fastify-flux`](https://github.com/Jnig/fastify-flux) Tool for building
|
|
739
744
|
Fastify APIs using decorators and convert Typescript interface to JSON Schema.
|
|
745
|
+
- [`jeasx`](https://www.jeasx.dev)
|
|
746
|
+
A flexible server-rendering framework built on Fastify
|
|
747
|
+
that leverages asynchronous JSX to simplify web development.
|
|
740
748
|
- [`simple-tjscli`](https://github.com/imjuni/simple-tjscli) CLI tool to
|
|
741
749
|
generate JSON Schema from TypeScript interfaces.
|
|
742
750
|
- [`vite-plugin-fastify`](https://github.com/Vanilla-IceCream/vite-plugin-fastify)
|
|
743
751
|
Fastify plugin for Vite with Hot-module Replacement.
|
|
744
752
|
- [`vite-plugin-fastify-routes`](https://github.com/Vanilla-IceCream/vite-plugin-fastify-routes)
|
|
745
753
|
File-based routing for Fastify applications using Vite.
|
|
754
|
+
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
<h1 align="center">Serverless</h1>
|
|
2
2
|
|
|
3
3
|
Run serverless applications and REST APIs using your existing Fastify
|
|
4
|
-
application.
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
application. You may need to make code changes to work on your
|
|
5
|
+
serverless platform of choice. This document contains a small guide
|
|
6
|
+
for the most popular serverless providers and how to use
|
|
7
7
|
Fastify with them.
|
|
8
8
|
|
|
9
9
|
#### Should you use Fastify in a serverless platform?
|
|
10
10
|
|
|
11
|
-
That is up to you! Keep in mind
|
|
11
|
+
That is up to you! Keep in mind, functions as a service should always use
|
|
12
12
|
small and focused functions, but you can also run an entire web application with
|
|
13
13
|
them. It is important to remember that the bigger the application the slower the
|
|
14
14
|
initial boot will be. The best way to run Fastify applications in serverless
|
|
15
|
-
environments is to use platforms like Google Cloud Run, AWS Fargate,
|
|
16
|
-
Container Instances, where the server can handle multiple requests
|
|
17
|
-
time and make full use of Fastify's features.
|
|
15
|
+
environments is to use platforms like Google Cloud Run, AWS Fargate, Azure
|
|
16
|
+
Container Instances, and Vercel where the server can handle multiple requests
|
|
17
|
+
at the same time and make full use of Fastify's features.
|
|
18
18
|
|
|
19
19
|
One of the best features of using Fastify in serverless applications is the ease
|
|
20
20
|
of development. In your local environment, you will always run the Fastify
|
|
@@ -136,7 +136,6 @@ of serverless applications to the cloud.
|
|
|
136
136
|
|
|
137
137
|
[Genezio has a dedicated guide for deploying a Fastify application.](https://genezio.com/docs/frameworks/fastify/)
|
|
138
138
|
|
|
139
|
-
|
|
140
139
|
## Google Cloud Functions
|
|
141
140
|
|
|
142
141
|
### Creation of Fastify instance
|
|
@@ -238,14 +237,13 @@ npx @google-cloud/functions-framework --target=fastifyFunction
|
|
|
238
237
|
Or add this command to your `package.json` scripts:
|
|
239
238
|
```json
|
|
240
239
|
"scripts": {
|
|
241
|
-
...
|
|
242
|
-
"dev": "npx @google-cloud/functions-framework --target=fastifyFunction"
|
|
243
|
-
...
|
|
240
|
+
...
|
|
241
|
+
"dev": "npx @google-cloud/functions-framework --target=fastifyFunction"
|
|
242
|
+
...
|
|
244
243
|
}
|
|
245
244
|
```
|
|
246
245
|
and run it with `npm run dev`.
|
|
247
246
|
|
|
248
|
-
|
|
249
247
|
### Deploy
|
|
250
248
|
```bash
|
|
251
249
|
gcloud functions deploy fastifyFunction \
|
|
@@ -326,7 +324,7 @@ async function registerRoutes (fastify) {
|
|
|
326
324
|
})
|
|
327
325
|
|
|
328
326
|
// define your endpoints here...
|
|
329
|
-
fastify.post("/some-route-here", async (request, reply) => {}
|
|
327
|
+
fastify.post("/some-route-here", async (request, reply) => {})
|
|
330
328
|
|
|
331
329
|
fastify.get('/', async (request, reply) => {
|
|
332
330
|
reply.send({message: 'Hello World!'})
|
|
@@ -377,7 +375,6 @@ firebase functions:log
|
|
|
377
375
|
- [Fastify on Firebase Functions](https://github.com/lirantal/lemon-squeezy-firebase-webhook-fastify/blob/main/package.json)
|
|
378
376
|
- [An article about HTTP webhooks on Firebase Functions and Fastify: A Practical Case Study with Lemon Squeezy](https://lirantal.com/blog/http-webhooks-firebase-functions-fastify-practical-case-study-lemon-squeezy)
|
|
379
377
|
|
|
380
|
-
|
|
381
378
|
## Google Cloud Run
|
|
382
379
|
|
|
383
380
|
Unlike AWS Lambda or Google Cloud Functions, Google Cloud Run is a serverless
|
|
@@ -463,7 +460,7 @@ CMD [ "npm", "start" ]
|
|
|
463
460
|
To keep build artifacts out of your container (which keeps it small and improves
|
|
464
461
|
build times) add a `.dockerignore` file like the one below:
|
|
465
462
|
|
|
466
|
-
|
|
463
|
+
```dockerignore
|
|
467
464
|
Dockerfile
|
|
468
465
|
README.md
|
|
469
466
|
node_modules
|
|
@@ -490,12 +487,11 @@ gcloud beta run deploy --image gcr.io/PROJECT-ID/APP-NAME --platform managed
|
|
|
490
487
|
|
|
491
488
|
Your app will be accessible from the URL GCP provides.
|
|
492
489
|
|
|
493
|
-
|
|
494
490
|
## netlify-lambda
|
|
495
491
|
|
|
496
492
|
First, please perform all preparation steps related to **AWS Lambda**.
|
|
497
493
|
|
|
498
|
-
Create a folder called `functions`,
|
|
494
|
+
Create a folder called `functions`, then create `server.js` (and your endpoint
|
|
499
495
|
path will be `server.js`) inside the `functions` folder.
|
|
500
496
|
|
|
501
497
|
### functions/server.js
|
|
@@ -564,9 +560,9 @@ Add this command to your `package.json` *scripts*
|
|
|
564
560
|
|
|
565
561
|
```json
|
|
566
562
|
"scripts": {
|
|
567
|
-
...
|
|
568
|
-
"build:functions": "netlify-lambda build functions --config ./webpack.config.netlify.js"
|
|
569
|
-
...
|
|
563
|
+
...
|
|
564
|
+
"build:functions": "netlify-lambda build functions --config ./webpack.config.netlify.js"
|
|
565
|
+
...
|
|
570
566
|
}
|
|
571
567
|
```
|
|
572
568
|
|
|
@@ -574,54 +570,17 @@ Then it should work fine.
|
|
|
574
570
|
|
|
575
571
|
## Vercel
|
|
576
572
|
|
|
577
|
-
[Vercel](https://vercel.com)
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
{
|
|
583
|
-
"rewrites": [
|
|
584
|
-
{
|
|
585
|
-
"source": "/(.*)",
|
|
586
|
-
"destination": "/api/serverless.js"
|
|
587
|
-
}
|
|
588
|
-
]
|
|
589
|
-
}
|
|
590
|
-
```
|
|
591
|
-
|
|
592
|
-
Then, write `api/serverless.js` like so:
|
|
593
|
-
|
|
594
|
-
```js
|
|
595
|
-
"use strict";
|
|
596
|
-
|
|
597
|
-
// Read the .env file.
|
|
598
|
-
import * as dotenv from "dotenv";
|
|
599
|
-
dotenv.config();
|
|
573
|
+
[Vercel](https://vercel.com) fully supports deploying Fastify applications.
|
|
574
|
+
Additionally, with Vercel's
|
|
575
|
+
[Fluid compute](https://vercel.com/docs/functions/fluid-compute), you can combine
|
|
576
|
+
server-like concurrency with the autoscaling properties of traditional
|
|
577
|
+
serverless functions.
|
|
600
578
|
|
|
601
|
-
|
|
602
|
-
|
|
579
|
+
Get started with the
|
|
580
|
+
[Fastify Node.js template on Vercel](
|
|
581
|
+
https://vercel.com/templates/other/fastify-serverless-function).
|
|
603
582
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
// Register your application as a normal plugin.
|
|
610
|
-
app.register(import("../src/app.js"));
|
|
611
|
-
|
|
612
|
-
export default async (req, res) => {
|
|
613
|
-
await app.ready();
|
|
614
|
-
app.server.emit('request', req, res);
|
|
615
|
-
}
|
|
616
|
-
```
|
|
617
|
-
|
|
618
|
-
In `src/app.js` define the plugin.
|
|
619
|
-
```js
|
|
620
|
-
async function routes (fastify, options) {
|
|
621
|
-
fastify.get('/', async (request, reply) => {
|
|
622
|
-
return { hello: 'world' }
|
|
623
|
-
})
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
export default routes;
|
|
627
|
-
```
|
|
583
|
+
[Fluid compute](https://vercel.com/docs/functions/fluid-compute) currently
|
|
584
|
+
requires an explicit opt-in. Learn more about enabling Fluid compute
|
|
585
|
+
[here](
|
|
586
|
+
https://vercel.com/docs/functions/fluid-compute#how-to-enable-fluid-compute).
|
|
@@ -152,7 +152,7 @@ fastify.addContentTypeParser('text/xml', function (request, payload, done) {
|
|
|
152
152
|
})
|
|
153
153
|
```
|
|
154
154
|
|
|
155
|
-
>
|
|
155
|
+
> âšī¸ Note: `function(req, done)` and `async function(req)` are
|
|
156
156
|
> still supported but deprecated.
|
|
157
157
|
|
|
158
158
|
#### Body Parser
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -69,7 +69,6 @@
|
|
|
69
69
|
- [FST_ERR_SCH_VALIDATION_BUILD](#fst_err_sch_validation_build)
|
|
70
70
|
- [FST_ERR_SCH_SERIALIZATION_BUILD](#fst_err_sch_serialization_build)
|
|
71
71
|
- [FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX](#fst_err_sch_response_schema_not_nested_2xx)
|
|
72
|
-
- [FST_ERR_HTTP2_INVALID_VERSION](#fst_err_http2_invalid_version)
|
|
73
72
|
- [FST_ERR_INIT_OPTS_INVALID](#fst_err_init_opts_invalid)
|
|
74
73
|
- [FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE](#fst_err_force_close_connections_idle_not_available)
|
|
75
74
|
- [FST_ERR_DUPLICATED_ROUTE](#fst_err_duplicated_route)
|
|
@@ -98,6 +97,7 @@
|
|
|
98
97
|
- [FST_ERR_VALIDATION](#fst_err_validation)
|
|
99
98
|
- [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid)
|
|
100
99
|
- [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn)
|
|
100
|
+
- [FST_ERR_ERROR_HANDLER_ALREADY_SET](#fst_err_error_handler_already_set)
|
|
101
101
|
|
|
102
102
|
### Error Handling In Node.js
|
|
103
103
|
<a id="error-handling"></a>
|
|
@@ -340,7 +340,6 @@ Below is a table with all the error codes used by Fastify.
|
|
|
340
340
|
| <a id="fst_err_sch_validation_build">FST_ERR_SCH_VALIDATION_BUILD</a> | The JSON schema provided for validation to a route is not valid. | Fix the JSON schema. | [#2023](https://github.com/fastify/fastify/pull/2023) |
|
|
341
341
|
| <a id="fst_err_sch_serialization_build">FST_ERR_SCH_SERIALIZATION_BUILD</a> | The JSON schema provided for serialization of a route response is not valid. | Fix the JSON schema. | [#2023](https://github.com/fastify/fastify/pull/2023) |
|
|
342
342
|
| <a id="fst_err_sch_response_schema_not_nested_2xx">FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX</a> | Response schemas should be nested under a valid status code (2XX). | Use a valid status code. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
343
|
-
| <a id="fst_err_http2_invalid_version">FST_ERR_HTTP2_INVALID_VERSION</a> | HTTP2 is available only from node >= 8.8.1. | Use a higher version of node. | [#1346](https://github.com/fastify/fastify/pull/1346) |
|
|
344
343
|
| <a id="fst_err_init_opts_invalid">FST_ERR_INIT_OPTS_INVALID</a> | Invalid initialization options. | Use valid initialization options. | [#1471](https://github.com/fastify/fastify/pull/1471) |
|
|
345
344
|
| <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) |
|
|
346
345
|
| <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) |
|
|
@@ -368,5 +367,4 @@ Below is a table with all the error codes used by Fastify.
|
|
|
368
367
|
| <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) |
|
|
369
368
|
| <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) |
|
|
370
369
|
| <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) |
|
|
371
|
-
| <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) |
|
|
372
|
-
|
|
370
|
+
| <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/Hooks.md
CHANGED
|
@@ -34,7 +34,7 @@ are Request/Reply hooks and application hooks:
|
|
|
34
34
|
- [Using Hooks to Inject Custom Properties](#using-hooks-to-inject-custom-properties)
|
|
35
35
|
- [Diagnostics Channel Hooks](#diagnostics-channel-hooks)
|
|
36
36
|
|
|
37
|
-
>
|
|
37
|
+
> âšī¸ Note: The `done` callback is not available when using `async`/`await` or
|
|
38
38
|
> returning a `Promise`. If you do invoke a `done` callback in this situation
|
|
39
39
|
> unexpected behavior may occur, e.g. duplicate invocation of handlers.
|
|
40
40
|
|
|
@@ -68,7 +68,7 @@ fastify.addHook('onRequest', async (request, reply) => {
|
|
|
68
68
|
})
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
>
|
|
71
|
+
> âšī¸ Note: In the [onRequest](#onrequest) hook, `request.body` will always be
|
|
72
72
|
> `undefined`, because the body parsing happens before the
|
|
73
73
|
> [preValidation](#prevalidation) hook.
|
|
74
74
|
|
|
@@ -98,16 +98,16 @@ fastify.addHook('preParsing', async (request, reply, payload) => {
|
|
|
98
98
|
})
|
|
99
99
|
```
|
|
100
100
|
|
|
101
|
-
>
|
|
101
|
+
> âšī¸ Note: In the [preParsing](#preparsing) hook, `request.body` will always be
|
|
102
102
|
> `undefined`, because the body parsing happens before the
|
|
103
103
|
> [preValidation](#prevalidation) hook.
|
|
104
104
|
|
|
105
|
-
>
|
|
105
|
+
> âšī¸ Note: You should also add a `receivedEncodedLength` property to the
|
|
106
106
|
> returned stream. This property is used to correctly match the request payload
|
|
107
107
|
> with the `Content-Length` header value. Ideally, this property should be updated
|
|
108
108
|
> on each received chunk.
|
|
109
109
|
|
|
110
|
-
>
|
|
110
|
+
> âšī¸ Note: The size of the returned stream is checked to not exceed the limit
|
|
111
111
|
> set in [`bodyLimit`](./Server.md#bodylimit) option.
|
|
112
112
|
|
|
113
113
|
### preValidation
|
|
@@ -166,7 +166,7 @@ fastify.addHook('preSerialization', async (request, reply, payload) => {
|
|
|
166
166
|
})
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
-
>
|
|
169
|
+
> âšī¸ Note: The hook is NOT called if the payload is a `string`, a `Buffer`, a
|
|
170
170
|
> `stream`, or `null`.
|
|
171
171
|
|
|
172
172
|
### onError
|
|
@@ -196,7 +196,7 @@ user
|
|
|
196
196
|
*(Note that the default error handler always sends the error back to the
|
|
197
197
|
user)*.
|
|
198
198
|
|
|
199
|
-
>
|
|
199
|
+
> âšī¸ Note: Unlike the other hooks, passing an error to the `done` function is not
|
|
200
200
|
> supported.
|
|
201
201
|
|
|
202
202
|
### onSend
|
|
@@ -233,7 +233,7 @@ fastify.addHook('onSend', (request, reply, payload, done) => {
|
|
|
233
233
|
> to `0`, whereas the `Content-Length` header will not be set if the payload is
|
|
234
234
|
> `null`.
|
|
235
235
|
|
|
236
|
-
>
|
|
236
|
+
> âšī¸ Note: If you change the payload, you may only change it to a `string`, a
|
|
237
237
|
> `Buffer`, a `stream`, a `ReadableStream`, a `Response`, or `null`.
|
|
238
238
|
|
|
239
239
|
|
|
@@ -256,7 +256,7 @@ The `onResponse` hook is executed when a response has been sent, so you will not
|
|
|
256
256
|
be able to send more data to the client. It can however be useful for sending
|
|
257
257
|
data to external services, for example, to gather statistics.
|
|
258
258
|
|
|
259
|
-
>
|
|
259
|
+
> âšī¸ Note: Setting `disableRequestLogging` to `true` will disable any error log
|
|
260
260
|
> inside the `onResponse` hook. In this case use `try - catch` to log errors.
|
|
261
261
|
|
|
262
262
|
### onTimeout
|
|
@@ -298,7 +298,7 @@ The `onRequestAbort` hook is executed when a client closes the connection before
|
|
|
298
298
|
the entire request has been processed. Therefore, you will not be able to send
|
|
299
299
|
data to the client.
|
|
300
300
|
|
|
301
|
-
>
|
|
301
|
+
> âšī¸ Note: Client abort detection is not completely reliable.
|
|
302
302
|
> See: [`Detecting-When-Clients-Abort.md`](../Guides/Detecting-When-Clients-Abort.md)
|
|
303
303
|
|
|
304
304
|
### Manage Errors from a hook
|
|
@@ -452,7 +452,7 @@ fastify.addHook('onListen', async function () {
|
|
|
452
452
|
})
|
|
453
453
|
```
|
|
454
454
|
|
|
455
|
-
>
|
|
455
|
+
> âšī¸ Note: This hook will not run when the server is started using
|
|
456
456
|
> fastify.inject()` or `fastify.ready()`.
|
|
457
457
|
|
|
458
458
|
### onClose
|
|
@@ -576,7 +576,7 @@ This hook can be useful if you are developing a plugin that needs to know when a
|
|
|
576
576
|
plugin context is formed, and you want to operate in that specific context, thus
|
|
577
577
|
this hook is encapsulated.
|
|
578
578
|
|
|
579
|
-
>
|
|
579
|
+
> âšī¸ Note: This hook will not be called if a plugin is wrapped inside
|
|
580
580
|
> [`fastify-plugin`](https://github.com/fastify/fastify-plugin).
|
|
581
581
|
```js
|
|
582
582
|
fastify.decorate('data', [])
|
|
@@ -774,7 +774,7 @@ fastify.route({
|
|
|
774
774
|
})
|
|
775
775
|
```
|
|
776
776
|
|
|
777
|
-
>
|
|
777
|
+
> âšī¸ Note: Both options also accept an array of functions.
|
|
778
778
|
|
|
779
779
|
## Using Hooks to Inject Custom Properties
|
|
780
780
|
<a id="using-hooks-to-inject-custom-properties"></a>
|
|
@@ -861,7 +861,7 @@ channel.subscribe(function ({ fastify }) {
|
|
|
861
861
|
})
|
|
862
862
|
```
|
|
863
863
|
|
|
864
|
-
>
|
|
864
|
+
> âšī¸ Note: The TracingChannel class API is currently experimental and may undergo
|
|
865
865
|
> breaking changes even in semver-patch releases of Node.js.
|
|
866
866
|
|
|
867
867
|
Five other events are published on a per-request basis following the
|
|
@@ -157,7 +157,7 @@ const fastify = require('fastify')({
|
|
|
157
157
|
});
|
|
158
158
|
```
|
|
159
159
|
|
|
160
|
-
>
|
|
160
|
+
> âšī¸ Note: In some cases, the [`Reply`](./Reply.md) object passed to the `res`
|
|
161
161
|
> serializer cannot be fully constructed. When writing a custom `res`
|
|
162
162
|
> serializer, check for the existence of any properties on `reply` aside from
|
|
163
163
|
> `statusCode`, which is always present. For example, verify the existence of
|
|
@@ -184,7 +184,7 @@ const fastify = require('fastify')({
|
|
|
184
184
|
});
|
|
185
185
|
```
|
|
186
186
|
|
|
187
|
-
>
|
|
187
|
+
> âšī¸ Note: The body cannot be serialized inside a `req` method because the
|
|
188
188
|
request is serialized when the child logger is created. At that time, the body
|
|
189
189
|
is not yet parsed.
|
|
190
190
|
|
|
@@ -199,7 +199,7 @@ app.addHook('preHandler', function (req, reply, done) {
|
|
|
199
199
|
})
|
|
200
200
|
```
|
|
201
201
|
|
|
202
|
-
>
|
|
202
|
+
> âšī¸ Note: Ensure serializers never throw errors, as this can cause the Node
|
|
203
203
|
> process to exit. See the
|
|
204
204
|
> [Pino documentation](https://getpino.io/#/docs/api?id=opt-serializers) for more
|
|
205
205
|
> information.
|
|
@@ -50,7 +50,7 @@ that already has the Fastify [Request](./Request.md#request) and
|
|
|
50
50
|
To run middleware under certain paths, pass the path as the first parameter to
|
|
51
51
|
`use`.
|
|
52
52
|
|
|
53
|
-
>
|
|
53
|
+
> âšī¸ Note: This does not support routes with parameters
|
|
54
54
|
> (e.g. `/user/:id/comments`) and wildcards are not supported in multiple paths.
|
|
55
55
|
|
|
56
56
|
```js
|
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
|
-
>
|
|
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,10 +260,10 @@ 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
|
-
>
|
|
263
|
+
> âšī¸ Note: The header `Transfer-Encoding: chunked` will be added once you use
|
|
264
264
|
> the trailer. It is a hard requirement for using trailer in Node.js.
|
|
265
265
|
|
|
266
|
-
>
|
|
266
|
+
> âšī¸ Note: Any error passed to `done` callback will be ignored. If you interested
|
|
267
267
|
> in the error, you can turn on `debug` level logging.*
|
|
268
268
|
|
|
269
269
|
```js
|
|
@@ -279,14 +279,14 @@ const { createHash } = require('node:crypto')
|
|
|
279
279
|
reply.trailer('content-md5', function(reply, payload, done) {
|
|
280
280
|
const hash = createHash('md5')
|
|
281
281
|
hash.update(payload)
|
|
282
|
-
done(null, hash.
|
|
282
|
+
done(null, hash.digest('hex'))
|
|
283
283
|
})
|
|
284
284
|
|
|
285
285
|
// when you prefer async-await
|
|
286
286
|
reply.trailer('content-md5', async function(reply, payload) {
|
|
287
287
|
const hash = createHash('md5')
|
|
288
288
|
hash.update(payload)
|
|
289
|
-
return hash.
|
|
289
|
+
return hash.digest('hex')
|
|
290
290
|
})
|
|
291
291
|
```
|
|
292
292
|
|
|
@@ -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
|
-
>
|
|
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
|
|
@@ -823,7 +823,7 @@ automatically create an error structured as the following:
|
|
|
823
823
|
You can add custom properties to the Error object, such as `headers`, that will
|
|
824
824
|
be used to enhance the HTTP response.
|
|
825
825
|
|
|
826
|
-
>
|
|
826
|
+
> âšī¸ Note: If you are passing an error to `send` and the statusCode is less than
|
|
827
827
|
> 400, Fastify will automatically set it at 500.
|
|
828
828
|
|
|
829
829
|
Tip: you can simplify errors by using the
|
|
@@ -871,7 +871,7 @@ fastify.get('/', {
|
|
|
871
871
|
If you want to customize error handling, check out
|
|
872
872
|
[`setErrorHandler`](./Server.md#seterrorhandler) API.
|
|
873
873
|
|
|
874
|
-
>
|
|
874
|
+
> âšī¸ Note: you are responsible for logging when customizing the error handler.
|
|
875
875
|
|
|
876
876
|
API:
|
|
877
877
|
|
|
@@ -84,7 +84,7 @@ This operation adds new values to the request headers, accessible via
|
|
|
84
84
|
For performance reasons, `Symbol('fastify.RequestAcceptVersion')` may be added
|
|
85
85
|
to headers on `not found` routes.
|
|
86
86
|
|
|
87
|
-
>
|
|
87
|
+
> âšī¸ Note: Schema validation may mutate the `request.headers` and
|
|
88
88
|
> `request.raw.headers` objects, causing the headers to become empty.
|
|
89
89
|
|
|
90
90
|
```js
|
package/docs/Reference/Routes.md
CHANGED
|
@@ -137,7 +137,7 @@ fastify.route(options)
|
|
|
137
137
|
|
|
138
138
|
* `reply` is defined in [Reply](./Reply.md).
|
|
139
139
|
|
|
140
|
-
>
|
|
140
|
+
> âšī¸ Note: The documentation for `onRequest`, `preParsing`, `preValidation`,
|
|
141
141
|
> `preHandler`, `preSerialization`, `onSend`, and `onResponse` is detailed in
|
|
142
142
|
> [Hooks](./Hooks.md). To send a response before the request is handled by the
|
|
143
143
|
> `handler`, see [Respond to a request from
|
|
@@ -233,7 +233,7 @@ const opts = {
|
|
|
233
233
|
fastify.get('/', opts)
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
-
>
|
|
236
|
+
> âšī¸ Note: Specifying the handler in both `options` and as the third parameter to
|
|
237
237
|
> the shortcut method throws a duplicate `handler` error.
|
|
238
238
|
|
|
239
239
|
### Url building
|
|
@@ -402,7 +402,7 @@ This approach supports both `callback-style` and `async-await` with minimal
|
|
|
402
402
|
trade-off. However, it is recommended to use only one style for consistent
|
|
403
403
|
error handling within your application.
|
|
404
404
|
|
|
405
|
-
>
|
|
405
|
+
> âšī¸ Note: Every async function returns a promise by itself.
|
|
406
406
|
|
|
407
407
|
### Route Prefixing
|
|
408
408
|
<a id="route-prefixing"></a>
|