fastify 4.11.0 → 4.13.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/LICENSE +1 -1
- package/docs/Guides/Detecting-When-Clients-Abort.md +170 -0
- package/docs/Guides/Ecosystem.md +24 -3
- package/docs/Guides/Index.md +2 -0
- package/docs/Guides/Migration-Guide-V4.md +6 -1
- package/docs/Reference/Errors.md +110 -3
- package/docs/Reference/Server.md +16 -3
- package/docs/Reference/Validation-and-Serialization.md +3 -1
- package/fastify.d.ts +198 -176
- package/fastify.js +48 -29
- package/lib/contentTypeParser.js +11 -20
- package/lib/error-handler.js +1 -1
- package/lib/error-serializer.js +8 -0
- package/lib/errors.js +100 -0
- package/lib/fourOhFour.js +4 -1
- package/lib/hooks.js +10 -5
- package/lib/pluginUtils.js +5 -2
- package/lib/route.js +18 -11
- package/lib/schemas.js +3 -3
- package/lib/server.js +6 -2
- package/lib/validation.js +5 -1
- package/package.json +3 -3
- package/test/client-timeout.test.js +38 -0
- package/test/close.test.js +6 -6
- package/test/content-parser.test.js +51 -11
- package/test/hooks.on-ready.test.js +13 -0
- package/test/hooks.test.js +40 -1
- package/test/http2/unknown-http-method.test.js +1 -0
- package/test/internals/reply.test.js +1 -1
- package/test/maxRequestsPerSocket.test.js +6 -6
- package/test/skip-reply-send.test.js +1 -1
- package/test/types/fastify.test-d.ts +1 -0
- package/test/types/instance.test-d.ts +4 -0
- package/test/unsupported-httpversion.test.js +1 -1
- package/test/validation-error-handling.test.js +3 -3
- package/types/errors.d.ts +21 -1
- package/types/instance.d.ts +3 -0
package/LICENSE
CHANGED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
|
+
|
|
3
|
+
# Detecting When Clients Abort
|
|
4
|
+
|
|
5
|
+
## Introduction
|
|
6
|
+
|
|
7
|
+
Fastify provides request events to trigger at certain points in a request's
|
|
8
|
+
lifecycle. However, there isn't a built-in mechanism to
|
|
9
|
+
detect unintentional client disconnection scenarios such as when the client's
|
|
10
|
+
internet connection is interrupted. This guide covers methods to detect if
|
|
11
|
+
and when a client intentionally aborts a request.
|
|
12
|
+
|
|
13
|
+
Keep in mind, Fastify's `clientErrorHandler` is not designed to detect when a
|
|
14
|
+
client aborts a request. This works in the same way as the standard Node HTTP
|
|
15
|
+
module, which triggers the `clientError` event when there is a bad request or
|
|
16
|
+
exceedingly large header data. When a client aborts a request, there is no
|
|
17
|
+
error on the socket and the `clientErrorHandler` will not be triggered.
|
|
18
|
+
|
|
19
|
+
## Solution
|
|
20
|
+
|
|
21
|
+
### Overview
|
|
22
|
+
|
|
23
|
+
The proposed solution is a possible way of detecting when a client
|
|
24
|
+
intentionally aborts a request, such as when a browser is closed or the HTTP
|
|
25
|
+
request is aborted from your client application. If there is an error in your
|
|
26
|
+
application code that results in the server crashing, you may require
|
|
27
|
+
additional logic to avoid a false abort detection.
|
|
28
|
+
|
|
29
|
+
The goal here is to detect when a client intentionally aborts a connection
|
|
30
|
+
so your application logic can proceed accordingly. This can be useful for
|
|
31
|
+
logging purposes or halting business logic.
|
|
32
|
+
|
|
33
|
+
### Hands-on
|
|
34
|
+
|
|
35
|
+
Say we have the following base server set up:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import Fastify from 'fastify';
|
|
39
|
+
|
|
40
|
+
const sleep = async (time) => {
|
|
41
|
+
return await new Promise(resolve => setTimeout(resolve, time || 1000));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const app = Fastify({
|
|
45
|
+
logger: {
|
|
46
|
+
transport: {
|
|
47
|
+
target: 'pino-pretty',
|
|
48
|
+
options: {
|
|
49
|
+
translateTime: 'HH:MM:ss Z',
|
|
50
|
+
ignore: 'pid,hostname',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
app.addHook('onRequest', async (request, reply) => {
|
|
57
|
+
request.raw.on('close', () => {
|
|
58
|
+
if (request.raw.aborted) {
|
|
59
|
+
app.log.info('request closed')
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
app.get('/', async (request, reply) => {
|
|
65
|
+
await sleep(3000)
|
|
66
|
+
reply.code(200).send({ ok: true })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const start = async () => {
|
|
70
|
+
try {
|
|
71
|
+
await app.listen({ port: 3000 })
|
|
72
|
+
} catch (err) {
|
|
73
|
+
app.log.error(err)
|
|
74
|
+
process.exit(1)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
start()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Our code is setting up a Fastify server which includes the following
|
|
82
|
+
functionality:
|
|
83
|
+
|
|
84
|
+
- Accepting requests at http://localhost:3000, with a 3 second delayed response
|
|
85
|
+
of `{ ok: true }`.
|
|
86
|
+
- An onRequest hook that triggers when every request is received.
|
|
87
|
+
- Logic that triggers in the hook when the request is closed.
|
|
88
|
+
- Logging that occurs when the closed request property `aborted` is true.
|
|
89
|
+
|
|
90
|
+
In the request close event, you should examine the diff between a successful
|
|
91
|
+
request and one aborted by the client to determine the best property for your
|
|
92
|
+
use case. You can find details about request properties in the
|
|
93
|
+
[NodeJS documentation](https://nodejs.org/api/http.html).
|
|
94
|
+
|
|
95
|
+
You can also perform this logic outside of a hook, directly in a specific route.
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
app.get('/', async (request, reply) => {
|
|
99
|
+
request.raw.on('close', () => {
|
|
100
|
+
if (request.raw.aborted) {
|
|
101
|
+
app.log.info('request closed')
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
await sleep(3000)
|
|
105
|
+
reply.code(200).send({ ok: true })
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
At any point in your business logic, you can check if the request has been
|
|
110
|
+
aborted and perform alternative actions.
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
app.get('/', async (request, reply) => {
|
|
114
|
+
await sleep(3000)
|
|
115
|
+
if (request.raw.aborted) {
|
|
116
|
+
// do something here
|
|
117
|
+
}
|
|
118
|
+
await sleep(3000)
|
|
119
|
+
reply.code(200).send({ ok: true })
|
|
120
|
+
})
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
A benefit to adding this in your application code is that you can log Fastify
|
|
124
|
+
details such as the reqId, which may be unavailable in lower-level code that
|
|
125
|
+
only has access to the raw request information.
|
|
126
|
+
|
|
127
|
+
### Testing
|
|
128
|
+
|
|
129
|
+
To test this functionality you can use an app like Postman and cancel your
|
|
130
|
+
request within 3 seconds. Alternatively, you can use Node to send an HTTP
|
|
131
|
+
request with logic to abort the request before 3 seconds. Example:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
const controller = new AbortController();
|
|
135
|
+
const signal = controller.signal;
|
|
136
|
+
|
|
137
|
+
(async () => {
|
|
138
|
+
try {
|
|
139
|
+
const response = await fetch('http://localhost:3000', { signal });
|
|
140
|
+
const body = await response.text();
|
|
141
|
+
console.log(body);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error(error);
|
|
144
|
+
}
|
|
145
|
+
})();
|
|
146
|
+
|
|
147
|
+
setTimeout(() => {
|
|
148
|
+
controller.abort()
|
|
149
|
+
}, 1000);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
With either approach, you should see the Fastify log appear at the moment the
|
|
153
|
+
request is aborted.
|
|
154
|
+
|
|
155
|
+
## Conclusion
|
|
156
|
+
|
|
157
|
+
Specifics of the implementation will vary from one problem to another, but the
|
|
158
|
+
main goal of this guide was to show a very specific use case of an issue that
|
|
159
|
+
could be solved within Fastify's ecosystem.
|
|
160
|
+
|
|
161
|
+
You can listen to the request close event and determine if the request was
|
|
162
|
+
aborted or if it was successfully delivered. You can implement this solution
|
|
163
|
+
in an onRequest hook or directly in an individual route.
|
|
164
|
+
|
|
165
|
+
This approach will not trigger in the event of internet disruption, and such
|
|
166
|
+
detection would require additional business logic. If you have flawed backend
|
|
167
|
+
application logic that results in a server crash, then you could trigger a
|
|
168
|
+
false detection. The `clientErrorHandler`, either by default or with custom
|
|
169
|
+
logic, is not intended to handle this scenario and will not trigger when the
|
|
170
|
+
client aborts a request.
|
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -140,6 +140,8 @@ section.
|
|
|
140
140
|
|
|
141
141
|
- [`@applicazza/fastify-nextjs`](https://github.com/applicazza/fastify-nextjs)
|
|
142
142
|
Alternate Fastify and Next.js integration.
|
|
143
|
+
- [`@clerk/fastify`](https://github.com/clerkinc/javascript/tree/main/packages/fastify)
|
|
144
|
+
Add authentication and user management to your Fastify application with Clerk.
|
|
143
145
|
- [`@coobaha/typed-fastify`](https://github.com/Coobaha/typed-fastify) Strongly
|
|
144
146
|
typed routes with a runtime validation using JSON schema generated from types.
|
|
145
147
|
- [`@dnlup/fastify-doc`](https://github.com/dnlup/fastify-doc) A plugin for
|
|
@@ -201,6 +203,8 @@ section.
|
|
|
201
203
|
using Fastify without the need of consuming a port on Electron apps.
|
|
202
204
|
- [`fast-water`](https://github.com/tswayne/fast-water) A Fastify plugin for
|
|
203
205
|
waterline. Decorates Fastify with waterline models.
|
|
206
|
+
- [`fastify-204`](https://github.com/Shiva127/fastify-204) Fastify plugin that
|
|
207
|
+
return 204 status on empty response.
|
|
204
208
|
- [`fastify-405`](https://github.com/Eomm/fastify-405) Fastify plugin that adds
|
|
205
209
|
405 HTTP status to your routes
|
|
206
210
|
- [`fastify-allow`](https://github.com/mattbishop/fastify-allow) Fastify plugin
|
|
@@ -266,6 +270,10 @@ section.
|
|
|
266
270
|
- [`fastify-cloudevents`](https://github.com/smartiniOnGitHub/fastify-cloudevents)
|
|
267
271
|
Fastify plugin to generate and forward Fastify events in the Cloudevents
|
|
268
272
|
format.
|
|
273
|
+
- [`fastify-cloudinary`](https://github.com/Vanilla-IceCream/fastify-cloudinary)
|
|
274
|
+
The Cloudinary Fastify SDK allows you to quickly and easily integrate your
|
|
275
|
+
application with Cloudinary. Effortlessly optimize and transform your cloud's
|
|
276
|
+
assets.
|
|
269
277
|
- [`fastify-cockroachdb`](https://github.com/alex-ppg/fastify-cockroachdb)
|
|
270
278
|
Fastify plugin to connect to a CockroachDB PostgreSQL instance via the
|
|
271
279
|
Sequelize ORM.
|
|
@@ -280,6 +288,9 @@ section.
|
|
|
280
288
|
functions.
|
|
281
289
|
- [`fastify-decorators`](https://github.com/L2jLiga/fastify-decorators) Fastify
|
|
282
290
|
plugin that provides the set of TypeScript decorators.
|
|
291
|
+
- [`fastify-delay-request`](https://github.com/climba03003/fastify-delay-request)
|
|
292
|
+
Fastify plugin that allows requests to be delayed whilst a task the response is
|
|
293
|
+
dependent on is run, such as a resource intensive process.
|
|
283
294
|
- [`fastify-disablecache`](https://github.com/Fdawgs/fastify-disablecache)
|
|
284
295
|
Fastify plugin to disable client-side caching, inspired by
|
|
285
296
|
[nocache](https://github.com/helmetjs/nocache).
|
|
@@ -366,6 +377,11 @@ section.
|
|
|
366
377
|
- [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify
|
|
367
378
|
InfluxDB plugin connecting to an InfluxDB instance via the Influx default
|
|
368
379
|
package.
|
|
380
|
+
- [`fastify-ip`](https://github.com/metcoder95/fastify-ip) A plugin
|
|
381
|
+
for Fastify that allows you to infer a request ID by a
|
|
382
|
+
given set of custom Request headers.
|
|
383
|
+
- [`fastify-json-to-xml`](https://github.com/Fdawgs/fastify-json-to-xml) Fastify
|
|
384
|
+
plugin to serialize JSON responses into XML.
|
|
369
385
|
- [`fastify-jwt-authz`](https://github.com/Ethan-Arrowood/fastify-jwt-authz) JWT
|
|
370
386
|
user scope verifier.
|
|
371
387
|
- [`fastify-jwt-webapp`](https://github.com/charlesread/fastify-jwt-webapp) JWT
|
|
@@ -384,7 +400,7 @@ section.
|
|
|
384
400
|
Fastify plugin to parse request language.
|
|
385
401
|
- [`fastify-lcache`](https://github.com/denbon05/fastify-lcache)
|
|
386
402
|
Lightweight cache plugin
|
|
387
|
-
- [`fastify-list-routes`](https://github.com/chuongtrh/fastify-list-routes)
|
|
403
|
+
- [`fastify-list-routes`](https://github.com/chuongtrh/fastify-list-routes)
|
|
388
404
|
A simple plugin for Fastify list all available routes.
|
|
389
405
|
- [`fastify-loader`](https://github.com/TheNoim/fastify-loader) Load routes from
|
|
390
406
|
a directory and inject the Fastify instance in each file.
|
|
@@ -474,6 +490,8 @@ section.
|
|
|
474
490
|
- [`fastify-postgraphile`](https://github.com/alemagio/fastify-postgraphile)
|
|
475
491
|
Plugin to integrate [PostGraphile](https://www.graphile.org/postgraphile/) in
|
|
476
492
|
a Fastify project.
|
|
493
|
+
- [`fastify-postgres-dot-js`](https://github.com/kylerush/fastify-postgresjs) Fastify
|
|
494
|
+
PostgreSQL connection plugin that uses [Postgres.js](https://github.com/porsager/postgres).
|
|
477
495
|
- [`fastify-prettier`](https://github.com/hsynlms/fastify-prettier) A Fastify
|
|
478
496
|
plugin that uses [prettier](https://github.com/prettier/prettier) under the
|
|
479
497
|
hood to beautify outgoing responses and/or other things in the Fastify server.
|
|
@@ -532,9 +550,11 @@ section.
|
|
|
532
550
|
- [`fastify-server-session`](https://github.com/jsumners/fastify-server-session)
|
|
533
551
|
A session plugin with support for arbitrary backing caches via
|
|
534
552
|
`fastify-caching`.
|
|
553
|
+
- [`fastify-shared-schema`](https://github.com/Adibla/fastify-shared-schema) Plugin
|
|
554
|
+
for sharing schemas between different routes.
|
|
535
555
|
- [`fastify-slonik`](https://github.com/Unbuttun/fastify-slonik) Fastify Slonik
|
|
536
556
|
plugin, with this you can use slonik in every part of your server.
|
|
537
|
-
- [`fastify-slow-down`](https://github.com/nearform/fastify-slow-down) A plugin
|
|
557
|
+
- [`fastify-slow-down`](https://github.com/nearform/fastify-slow-down) A plugin
|
|
538
558
|
to delay the response from the server.
|
|
539
559
|
- [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) a
|
|
540
560
|
Socket.io plugin for Fastify.
|
|
@@ -585,6 +605,8 @@ section.
|
|
|
585
605
|
should use.
|
|
586
606
|
- [`fastify-wamp-router`](https://github.com/lependu/fastify-wamp-router) Web
|
|
587
607
|
Application Messaging Protocol router for Fastify.
|
|
608
|
+
- [`fastify-web-response`](https://github.com/erfanium/fastify-web-response)
|
|
609
|
+
Enables returning web streams objects `Response` and `ReadableStream` in routes.
|
|
588
610
|
- [`fastify-webpack-hmr`](https://github.com/lependu/fastify-webpack-hmr)
|
|
589
611
|
Webpack hot module reloading plugin for Fastify.
|
|
590
612
|
- [`fastify-webpack-hot`](https://github.com/gajus/fastify-webpack-hot) Webpack
|
|
@@ -621,7 +643,6 @@ section.
|
|
|
621
643
|
and lightweight Sequelize plugin for Fastify.
|
|
622
644
|
- [`typeorm-fastify-plugin`](https://github.com/jclemens24/fastify-typeorm) A simple
|
|
623
645
|
and updated Typeorm plugin for use with Fastify.
|
|
624
|
-
|
|
625
646
|
#### [Community Tools](#community-tools)
|
|
626
647
|
- [`@fastify-userland/workflows`](https://github.com/fastify-userland/workflows)
|
|
627
648
|
Reusable workflows for use in the Fastify plugin
|
package/docs/Guides/Index.md
CHANGED
|
@@ -15,6 +15,8 @@ This table of contents is in alphabetical order.
|
|
|
15
15
|
met in your application. This guide focuses on solving the problem using
|
|
16
16
|
[`Hooks`](../Reference/Hooks.md), [`Decorators`](../Reference/Decorators.md),
|
|
17
17
|
and [`Plugins`](../Reference/Plugins.md).
|
|
18
|
+
+ [Detecting When Clients Abort](./Detecting-When-Clients-Abort.md): A
|
|
19
|
+
practical guide on detecting if and when a client aborts a request.
|
|
18
20
|
+ [Ecosystem](./Ecosystem.md): Lists all core plugins and many known community
|
|
19
21
|
plugins.
|
|
20
22
|
+ [Fluent Schema](./Fluent-Schema.md): Shows how writing JSON Schema can be
|
|
@@ -39,6 +39,11 @@ const res = await fastify.inject('/encapsulated')
|
|
|
39
39
|
console.log(res.json().message) // 'wrapped'
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
+
>The root error handler is Fastify’s generic error handler.
|
|
43
|
+
>This error handler will use the headers and status code in the Error object,
|
|
44
|
+
>if they exist. **The headers and status code will not be automatically set if
|
|
45
|
+
>a custom error handler is provided**.
|
|
46
|
+
|
|
42
47
|
### Removed `app.use()` ([#3506](https://github.com/fastify/fastify/pull/3506))
|
|
43
48
|
|
|
44
49
|
With v4 of Fastify, `app.use()` has been removed and the use of middleware is
|
|
@@ -116,7 +121,7 @@ As a result, if you specify an `onRoute` hook in a plugin you should now either:
|
|
|
116
121
|
|
|
117
122
|
Into this:
|
|
118
123
|
```js
|
|
119
|
-
await fastify.register((instance, opts) => {
|
|
124
|
+
await fastify.register((instance, opts, done) => {
|
|
120
125
|
instance.addHook('onRoute', (routeOptions) => {
|
|
121
126
|
const { path, method } = routeOptions;
|
|
122
127
|
console.log({ path, method });
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -133,7 +133,43 @@ fastify.listen({ port: 3000 }, function (err, address) {
|
|
|
133
133
|
|
|
134
134
|
404 Not Found.
|
|
135
135
|
|
|
136
|
+
#### FST_ERR_OPTIONS_NOT_OBJ
|
|
137
|
+
<a id="FST_ERR_OPTIONS_NOT_OBJ"></a>
|
|
138
|
+
|
|
139
|
+
Fastify options must be an object.
|
|
140
|
+
|
|
141
|
+
#### FST_ERR_QSP_NOT_FN
|
|
142
|
+
<a id="FST_ERR_QSP_NOT_FN"></a>
|
|
143
|
+
|
|
144
|
+
QueryStringParser option should be a function.
|
|
145
|
+
|
|
146
|
+
#### FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN
|
|
147
|
+
<a id="FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN"></a>
|
|
148
|
+
|
|
149
|
+
SchemaController.bucket option should be a function.
|
|
150
|
+
|
|
151
|
+
#### FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN
|
|
152
|
+
<a id="FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN"></a>
|
|
153
|
+
|
|
154
|
+
SchemaErrorFormatter option should be a non async function.
|
|
155
|
+
|
|
156
|
+
#### FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ
|
|
157
|
+
<a id="FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ"></a>
|
|
158
|
+
|
|
159
|
+
ajv.customOptions option should be an object.
|
|
160
|
+
|
|
161
|
+
#### FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR
|
|
162
|
+
<a id="FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR"></a>
|
|
163
|
+
|
|
164
|
+
ajv.plugins option should be an array.
|
|
165
|
+
|
|
166
|
+
#### FST_ERR_VERSION_CONSTRAINT_NOT_STR
|
|
167
|
+
<a id="FST_ERR_VERSION_CONSTRAINT_NOT_STR"></a>
|
|
168
|
+
|
|
169
|
+
Version constraint should be a string.
|
|
170
|
+
|
|
136
171
|
<a name="FST_ERR_CTP_ALREADY_PRESENT"></a>
|
|
172
|
+
|
|
137
173
|
#### FST_ERR_CTP_ALREADY_PRESENT
|
|
138
174
|
<a id="FST_ERR_CTP_ALREADY_PRESENT"></a>
|
|
139
175
|
|
|
@@ -184,6 +220,16 @@ Request body size did not match `Content-Length`.
|
|
|
184
220
|
|
|
185
221
|
Body cannot be empty when content-type is set to `application/json`.
|
|
186
222
|
|
|
223
|
+
#### FST_ERR_CTP_INSTANCE_ALREADY_STARTED
|
|
224
|
+
<a id="FST_ERR_CTP_INSTANCE_ALREADY_STARTED"></a>
|
|
225
|
+
|
|
226
|
+
Fastify is already started.
|
|
227
|
+
|
|
228
|
+
#### FST_ERR_INSTANCE_ALREADY_LISTENING
|
|
229
|
+
<a id="FST_ERR_INSTANCE_ALREADY_LISTENING"></a>
|
|
230
|
+
|
|
231
|
+
Fastify instance is already listening.
|
|
232
|
+
|
|
187
233
|
#### FST_ERR_DEC_ALREADY_PRESENT
|
|
188
234
|
<a id="FST_ERR_DEC_ALREADY_PRESENT"></a>
|
|
189
235
|
|
|
@@ -214,10 +260,15 @@ The hook name must be a string.
|
|
|
214
260
|
|
|
215
261
|
The hook callback must be a function.
|
|
216
262
|
|
|
263
|
+
#### FST_ERR_HOOK_NOT_SUPPORTED
|
|
264
|
+
<a id="FST_ERR_HOOK_NOT_SUPPORTED"></a>
|
|
265
|
+
|
|
266
|
+
The hook is not supported.
|
|
267
|
+
|
|
217
268
|
#### FST_ERR_MISSING_MIDDLEWARE
|
|
218
269
|
<a id="FST_ERR_MISSING_MIDDLEWARE"></a>
|
|
219
270
|
|
|
220
|
-
You must register a plugin for handling middlewares,
|
|
271
|
+
You must register a plugin for handling middlewares,
|
|
221
272
|
visit [`Middleware`](./Middleware.md) for more info.
|
|
222
273
|
|
|
223
274
|
<a name="FST_ERR_HOOK_TIMEOUT"></a>
|
|
@@ -278,7 +329,7 @@ Missing serialization function.
|
|
|
278
329
|
#### FST_ERR_REQ_INVALID_VALIDATION_INVOCATION
|
|
279
330
|
<a id="FST_ERR_REQ_INVALID_VALIDATION_INVOCATION"></a>
|
|
280
331
|
|
|
281
|
-
Invalid validation invocation. Missing validation function for
|
|
332
|
+
Invalid validation invocation. Missing validation function for
|
|
282
333
|
HTTP part nor schema provided.
|
|
283
334
|
|
|
284
335
|
#### FST_ERR_SCH_MISSING_ID
|
|
@@ -306,6 +357,11 @@ The JSON schema provided for validation to a route is not valid.
|
|
|
306
357
|
|
|
307
358
|
The JSON schema provided for serialization of a route response is not valid.
|
|
308
359
|
|
|
360
|
+
#### FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX
|
|
361
|
+
<a id="FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX"></a>
|
|
362
|
+
|
|
363
|
+
Response schemas should be nested under a valid status code (2XX).
|
|
364
|
+
|
|
309
365
|
#### FST_ERR_HTTP2_INVALID_VERSION
|
|
310
366
|
<a id="FST_ERR_HTTP2_INVALID_VERSION"></a>
|
|
311
367
|
|
|
@@ -319,7 +375,7 @@ Invalid initialization options.
|
|
|
319
375
|
#### FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE
|
|
320
376
|
<a id="FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE"></a>
|
|
321
377
|
|
|
322
|
-
Cannot set forceCloseConnections to `idle` as your HTTP server
|
|
378
|
+
Cannot set forceCloseConnections to `idle` as your HTTP server
|
|
323
379
|
does not support `closeIdleConnections` method.
|
|
324
380
|
|
|
325
381
|
<a name="FST_ERR_DUPLICATED_ROUTE"></a>
|
|
@@ -347,6 +403,46 @@ The `defaultRoute` type should be a function.
|
|
|
347
403
|
|
|
348
404
|
URL must be a string.
|
|
349
405
|
|
|
406
|
+
#### FST_ERR_ROUTE_OPTIONS_NOT_OBJ
|
|
407
|
+
<a id="FST_ERR_ROUTE_OPTIONS_NOT_OBJ"></a>
|
|
408
|
+
|
|
409
|
+
Options for the route must be an object.
|
|
410
|
+
|
|
411
|
+
#### FST_ERR_ROUTE_DUPLICATED_HANDLER
|
|
412
|
+
<a id="FST_ERR_ROUTE_DUPLICATED_HANDLER"></a>
|
|
413
|
+
|
|
414
|
+
Duplicate handler for the route is not allowed.
|
|
415
|
+
|
|
416
|
+
#### FST_ERR_ROUTE_HANDLER_NOT_FN
|
|
417
|
+
<a id="FST_ERR_ROUTE_HANDLER_NOT_FN"></a>
|
|
418
|
+
|
|
419
|
+
Handler for the route must be a function.
|
|
420
|
+
|
|
421
|
+
#### FST_ERR_ROUTE_MISSING_HANDLER
|
|
422
|
+
<a id="FST_ERR_ROUTE_MISSING_HANDLER"></a>
|
|
423
|
+
|
|
424
|
+
Missing handler function for the route.
|
|
425
|
+
|
|
426
|
+
#### FST_ERR_ROUTE_METHOD_NOT_SUPPORTED
|
|
427
|
+
<a id="FST_ERR_ROUTE_METHOD_NOT_SUPPORTED"></a>
|
|
428
|
+
|
|
429
|
+
Method is not supported for the route.
|
|
430
|
+
|
|
431
|
+
#### FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED
|
|
432
|
+
<a id="FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED"></a>
|
|
433
|
+
|
|
434
|
+
Body validation schema route is not supported.
|
|
435
|
+
|
|
436
|
+
#### FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT
|
|
437
|
+
<a id="FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT"></a>
|
|
438
|
+
|
|
439
|
+
BodyLimit option must be an integer.
|
|
440
|
+
|
|
441
|
+
#### FST_ERR_ROUTE_REWRITE_NOT_STR
|
|
442
|
+
<a id="FST_ERR_ROUTE_REWRITE_NOT_STR"></a>
|
|
443
|
+
|
|
444
|
+
Rewrite url needs to be of type "string".
|
|
445
|
+
|
|
350
446
|
#### FST_ERR_REOPENED_CLOSE_SERVER
|
|
351
447
|
<a id="FST_ERR_REOPENED_CLOSE_SERVER"></a>
|
|
352
448
|
|
|
@@ -363,26 +459,37 @@ Fastify is already listening.
|
|
|
363
459
|
Installed Fastify plugin mismatched expected version.
|
|
364
460
|
|
|
365
461
|
<a name="FST_ERR_PLUGIN_CALLBACK_NOT_FN"></a>
|
|
462
|
+
|
|
366
463
|
#### FST_ERR_PLUGIN_CALLBACK_NOT_FN
|
|
367
464
|
|
|
368
465
|
Callback for a hook is not a function (mapped directly from `avvio`)
|
|
369
466
|
|
|
370
467
|
<a name="FST_ERR_PLUGIN_NOT_VALID"></a>
|
|
468
|
+
|
|
371
469
|
#### FST_ERR_PLUGIN_NOT_VALID
|
|
372
470
|
|
|
373
471
|
Plugin must be a function or a promise.
|
|
374
472
|
|
|
375
473
|
<a name="FST_ERR_ROOT_PLG_BOOTED"></a>
|
|
474
|
+
|
|
376
475
|
#### FST_ERR_ROOT_PLG_BOOTED
|
|
377
476
|
|
|
378
477
|
Root plugin has already booted (mapped directly from `avvio`)
|
|
379
478
|
|
|
380
479
|
<a name="FST_ERR_PARENT_PLUGIN_BOOTED"></a>
|
|
480
|
+
|
|
381
481
|
#### FST_ERR_PARENT_PLUGIN_BOOTED
|
|
382
482
|
|
|
383
483
|
Impossible to load plugin because the parent (mapped directly from `avvio`)
|
|
384
484
|
|
|
385
485
|
<a name="FST_ERR_PLUGIN_TIMEOUT"></a>
|
|
486
|
+
|
|
386
487
|
#### FST_ERR_PLUGIN_TIMEOUT
|
|
387
488
|
|
|
388
489
|
Plugin did not start in time. Default timeout (in millis): `10000`
|
|
490
|
+
|
|
491
|
+
<a name="FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE"></a>
|
|
492
|
+
|
|
493
|
+
#### FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE
|
|
494
|
+
|
|
495
|
+
The decorator is not present in the instance.
|
package/docs/Reference/Server.md
CHANGED
|
@@ -9,6 +9,7 @@ options object which is used to customize the resulting instance. This document
|
|
|
9
9
|
describes the properties available in that options object.
|
|
10
10
|
|
|
11
11
|
- [Factory](#factory)
|
|
12
|
+
- [`http`](#http)
|
|
12
13
|
- [`http2`](#http2)
|
|
13
14
|
- [`https`](#https)
|
|
14
15
|
- [`connectionTimeout`](#connectiontimeout)
|
|
@@ -91,6 +92,18 @@ describes the properties available in that options object.
|
|
|
91
92
|
- [errorHandler](#errorhandler)
|
|
92
93
|
- [initialConfig](#initialconfig)
|
|
93
94
|
|
|
95
|
+
### `http`
|
|
96
|
+
<a id="factory-http"></a>
|
|
97
|
+
|
|
98
|
+
An object used to configure the server's listening socket. The options
|
|
99
|
+
are the same as the Node.js core [`createServer`
|
|
100
|
+
method](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_http_createserver_options_requestlistener).
|
|
101
|
+
|
|
102
|
+
This option is ignored if options [`http2`](#factory-http2) or
|
|
103
|
+
[`https`](#factory-https) are set.
|
|
104
|
+
|
|
105
|
+
+ Default: `null`
|
|
106
|
+
|
|
94
107
|
### `http2`
|
|
95
108
|
<a id="factory-http2"></a>
|
|
96
109
|
|
|
@@ -494,7 +507,7 @@ request-id](./Logging.md#logging-request-id) section.
|
|
|
494
507
|
Setting `requestIdHeader` to `false` will always use [genReqId](#genreqid)
|
|
495
508
|
|
|
496
509
|
+ Default: `'request-id'`
|
|
497
|
-
|
|
510
|
+
|
|
498
511
|
```js
|
|
499
512
|
const fastify = require('fastify')({
|
|
500
513
|
requestIdHeader: 'x-custom-id', // -> use 'X-Custom-Id' header if available
|
|
@@ -1440,9 +1453,9 @@ plugins are registered. If you would like to augment the behavior of the default
|
|
|
1440
1453
|
arguments `fastify.setNotFoundHandler()` within the context of these registered
|
|
1441
1454
|
plugins.
|
|
1442
1455
|
|
|
1443
|
-
> Note: Some config properties from the request object will be
|
|
1456
|
+
> Note: Some config properties from the request object will be
|
|
1444
1457
|
> undefined inside the custom not found handler. E.g:
|
|
1445
|
-
> `request.routerPath`, `routerMethod` and `context.config`.
|
|
1458
|
+
> `request.routerPath`, `routerMethod` and `context.config`.
|
|
1446
1459
|
> This method design goal is to allow calling the common not found route.
|
|
1447
1460
|
> To return a per-route customized 404 response, you can do it in
|
|
1448
1461
|
> the response itself.
|
|
@@ -394,9 +394,11 @@ configuration](https://github.com/fastify/ajv-compiler#ajv-configuration) is:
|
|
|
394
394
|
|
|
395
395
|
```js
|
|
396
396
|
{
|
|
397
|
-
coerceTypes:
|
|
397
|
+
coerceTypes: 'array', // change data type of data to match type keyword
|
|
398
398
|
useDefaults: true, // replace missing properties and items with the values from corresponding default keyword
|
|
399
399
|
removeAdditional: true, // remove additional properties
|
|
400
|
+
uriResolver: require('fast-uri'),
|
|
401
|
+
addUsedSchema: false,
|
|
400
402
|
// Explicitly set allErrors to `false`.
|
|
401
403
|
// When set to `true`, a DoS attack is possible.
|
|
402
404
|
allErrors: false
|