fastify 3.24.0 → 3.25.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -29
- package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
- package/docs/Guides/Ecosystem.md +513 -0
- package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
- package/docs/{Getting-Started.md → Guides/Getting-Started.md} +180 -60
- package/docs/Guides/Index.md +30 -4
- package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
- package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
- package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
- package/docs/{Serverless.md → Guides/Serverless.md} +200 -42
- package/docs/Guides/Style-Guide.md +246 -0
- package/docs/{Testing.md → Guides/Testing.md} +26 -12
- package/docs/Guides/Write-Plugin.md +102 -0
- package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
- package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
- package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
- package/docs/{Errors.md → Reference/Errors.md} +77 -47
- package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
- package/docs/{Hooks.md → Reference/Hooks.md} +157 -70
- package/docs/Reference/Index.md +71 -0
- package/docs/{LTS.md → Reference/LTS.md} +31 -32
- package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
- package/docs/{Logging.md → Reference/Logging.md} +68 -28
- package/docs/Reference/Middleware.md +78 -0
- package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
- package/docs/{Reply.md → Reference/Reply.md} +205 -94
- package/docs/{Request.md → Reference/Request.md} +32 -16
- package/docs/{Routes.md → Reference/Routes.md} +243 -113
- package/docs/{Server.md → Reference/Server.md} +516 -267
- package/docs/{TypeScript.md → Reference/TypeScript.md} +451 -191
- package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
- package/docs/index.md +24 -0
- package/examples/typescript-server.ts +1 -1
- package/fastify.js +2 -3
- package/lib/contentTypeParser.js +11 -6
- package/lib/decorate.js +6 -3
- package/lib/logger.js +1 -1
- package/lib/route.js +1 -1
- package/lib/server.js +9 -8
- package/package.json +9 -4
- package/test/als.test.js +74 -0
- package/test/constrained-routes.test.js +220 -0
- package/test/custom-parser.test.js +11 -2
- package/test/decorator.test.js +38 -0
- package/test/handler-context.test.js +11 -4
- package/test/http2/closing.test.js +14 -5
- package/test/http2/constraint.test.js +91 -0
- package/test/listen.test.js +36 -22
- package/test/logger.test.js +16 -0
- package/test/maxRequestsPerSocket.test.js +10 -0
- package/test/request-error.test.js +2 -8
- package/test/requestTimeout.test.js +4 -1
- package/test/router-options.test.js +10 -1
- package/test/schema-feature.test.js +146 -0
- package/test/stream.test.js +14 -3
- package/test/trust-proxy.test.js +15 -7
- package/test/types/instance.test-d.ts +52 -1
- package/test/types/request.test-d.ts +7 -1
- package/test/types/route.test-d.ts +21 -0
- package/types/hooks.d.ts +12 -1
- package/types/instance.d.ts +16 -6
- package/types/request.d.ts +4 -1
- package/types/route.d.ts +1 -1
- package/docs/Ecosystem.md +0 -211
- package/docs/Middleware.md +0 -53
- package/docs/Style-Guide.md +0 -185
- package/docs/Write-Plugin.md +0 -58
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
<h1 align="center">Fastify</h1>
|
|
2
2
|
|
|
3
3
|
## Getting Started
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
Hello! Thank you for checking out Fastify!
|
|
6
|
+
|
|
7
|
+
This document aims to be a gentle introduction to the framework and its
|
|
8
|
+
features. It is an elementary preface with examples and links to other parts of
|
|
9
|
+
the documentation.
|
|
10
|
+
|
|
6
11
|
Let's start!
|
|
7
12
|
|
|
8
|
-
<a name="install"></a>
|
|
9
13
|
### Install
|
|
14
|
+
<a id="install"></a>
|
|
15
|
+
|
|
10
16
|
Install with npm:
|
|
11
17
|
```
|
|
12
18
|
npm i fastify --save
|
|
@@ -16,8 +22,9 @@ Install with yarn:
|
|
|
16
22
|
yarn add fastify
|
|
17
23
|
```
|
|
18
24
|
|
|
19
|
-
<a name="first-server"></a>
|
|
20
25
|
### Your first server
|
|
26
|
+
<a id="first-server"></a>
|
|
27
|
+
|
|
21
28
|
Let's write our first server:
|
|
22
29
|
```js
|
|
23
30
|
// Require the framework and instantiate it
|
|
@@ -47,8 +54,11 @@ fastify.listen(3000, function (err, address) {
|
|
|
47
54
|
})
|
|
48
55
|
```
|
|
49
56
|
|
|
50
|
-
Do you prefer to use `async/await`? Fastify supports it out-of-the-box
|
|
51
|
-
|
|
57
|
+
Do you prefer to use `async/await`? Fastify supports it out-of-the-box.
|
|
58
|
+
|
|
59
|
+
*(We also suggest using
|
|
60
|
+
[make-promises-safe](https://github.com/mcollina/make-promises-safe) to avoid
|
|
61
|
+
file descriptor and memory leaks.)*
|
|
52
62
|
```js
|
|
53
63
|
// ESM
|
|
54
64
|
import Fastify from 'fastify'
|
|
@@ -75,12 +85,21 @@ const start = async () => {
|
|
|
75
85
|
start()
|
|
76
86
|
```
|
|
77
87
|
|
|
78
|
-
Awesome, that was easy
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
Awesome, that was easy.
|
|
89
|
+
|
|
90
|
+
Unfortunately, writing a complex application requires significantly more code
|
|
91
|
+
than this example. A classic problem when you are building a new application is
|
|
92
|
+
how to handle multiple files, asynchronous bootstrapping, and the architecture
|
|
93
|
+
of your code.
|
|
94
|
+
|
|
95
|
+
Fastify offers an easy platform that helps to solve all of the problems outlined
|
|
96
|
+
above, and more!
|
|
81
97
|
|
|
82
98
|
> ## Note
|
|
83
|
-
> The above examples, and subsequent examples in this document, default to
|
|
99
|
+
> The above examples, and subsequent examples in this document, default to
|
|
100
|
+
> listening *only* on the localhost `127.0.0.1` interface. To listen on all
|
|
101
|
+
> available IPv4 interfaces the example should be modified to listen on
|
|
102
|
+
> `0.0.0.0` like so:
|
|
84
103
|
>
|
|
85
104
|
> ```js
|
|
86
105
|
> fastify.listen(3000, '0.0.0.0', function (err, address) {
|
|
@@ -92,15 +111,24 @@ Fastify offers an easy platform that helps to solve all of the problems outlined
|
|
|
92
111
|
> })
|
|
93
112
|
> ```
|
|
94
113
|
>
|
|
95
|
-
> Similarly, specify `::1` to accept only local connections via IPv6. Or specify
|
|
114
|
+
> Similarly, specify `::1` to accept only local connections via IPv6. Or specify
|
|
115
|
+
> `::` to accept connections on all IPv6 addresses, and, if the operating system
|
|
116
|
+
> supports it, also on all IPv4 addresses.
|
|
96
117
|
>
|
|
97
|
-
> When deploying to a Docker (or another type of) container using `0.0.0.0` or
|
|
118
|
+
> When deploying to a Docker (or another type of) container using `0.0.0.0` or
|
|
119
|
+
> `::` would be the easiest method for exposing the application.
|
|
98
120
|
|
|
99
|
-
<a name="first-plugin"></a>
|
|
100
121
|
### Your first plugin
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
122
|
+
<a id="first-plugin"></a>
|
|
123
|
+
|
|
124
|
+
As with JavaScript, where everything is an object, with Fastify everything is a
|
|
125
|
+
plugin.
|
|
126
|
+
|
|
127
|
+
Before digging into it, let's see how it works!
|
|
128
|
+
|
|
129
|
+
Let's declare our basic server, but instead of declaring the route inside the
|
|
130
|
+
entry point, we'll declare it in an external file (check out the [route
|
|
131
|
+
declaration](../Reference/Routes.md) docs).
|
|
104
132
|
```js
|
|
105
133
|
// ESM
|
|
106
134
|
import Fastify from 'fastify'
|
|
@@ -148,14 +176,24 @@ async function routes (fastify, options) {
|
|
|
148
176
|
|
|
149
177
|
module.exports = routes
|
|
150
178
|
```
|
|
151
|
-
In this example, we used the `register` API, which is the core of the Fastify
|
|
179
|
+
In this example, we used the `register` API, which is the core of the Fastify
|
|
180
|
+
framework. It is the only way to add routes, plugins, et cetera.
|
|
181
|
+
|
|
182
|
+
At the beginning of this guide, we noted that Fastify provides a foundation that
|
|
183
|
+
assists with asynchronous bootstrapping of your application. Why is this
|
|
184
|
+
important?
|
|
185
|
+
|
|
186
|
+
Consider the scenario where a database connection is needed to handle data
|
|
187
|
+
storage. The database connection needs to be available before the server is
|
|
188
|
+
accepting connections. How do we address this problem?
|
|
189
|
+
|
|
190
|
+
A typical solution is to use a complex callback, or promises - a system that
|
|
191
|
+
will mix the framework API with other libraries and the application code.
|
|
152
192
|
|
|
153
|
-
At the beginning of this guide, we noted that Fastify provides a foundation that assists with asynchronous bootstrapping of your application. Why is this important?
|
|
154
|
-
Consider the scenario where a database connection is needed to handle data storage. The database connection needs to be available before the server is accepting connections. How do we address this problem?<br>
|
|
155
|
-
A typical solution is to use a complex callback, or promises - a system that will mix the framework API with other libraries and the application code.<br>
|
|
156
193
|
Fastify handles this internally, with minimum effort!
|
|
157
194
|
|
|
158
|
-
Let's rewrite the above example with a database connection
|
|
195
|
+
Let's rewrite the above example with a database connection.
|
|
196
|
+
|
|
159
197
|
|
|
160
198
|
First, install `fastify-plugin` and `fastify-mongodb`:
|
|
161
199
|
|
|
@@ -216,7 +254,7 @@ async function dbConnector (fastify, options) {
|
|
|
216
254
|
})
|
|
217
255
|
}
|
|
218
256
|
|
|
219
|
-
// Wrapping a plugin function with fastify-plugin exposes the decorators
|
|
257
|
+
// Wrapping a plugin function with fastify-plugin exposes the decorators
|
|
220
258
|
// and hooks, declared inside the plugin to the parent scope.
|
|
221
259
|
module.exports = fastifyPlugin(dbConnector)
|
|
222
260
|
|
|
@@ -232,7 +270,7 @@ async function dbConnector (fastify, options) {
|
|
|
232
270
|
})
|
|
233
271
|
}
|
|
234
272
|
|
|
235
|
-
// Wrapping a plugin function with fastify-plugin exposes the decorators
|
|
273
|
+
// Wrapping a plugin function with fastify-plugin exposes the decorators
|
|
236
274
|
// and hooks, declared inside the plugin to the parent scope.
|
|
237
275
|
module.exports = fastifyPlugin(dbConnector)
|
|
238
276
|
|
|
@@ -262,24 +300,61 @@ async function routes (fastify, options) {
|
|
|
262
300
|
}
|
|
263
301
|
return result
|
|
264
302
|
})
|
|
303
|
+
|
|
304
|
+
const animalBodyJsonSchema = {
|
|
305
|
+
type: 'object',
|
|
306
|
+
required: ['animal'],
|
|
307
|
+
properties: {
|
|
308
|
+
animal: { type: 'string' },
|
|
309
|
+
},
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const schema = {
|
|
313
|
+
body: animalBodyJsonSchema,
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
fastify.post('/animals', { schema }, async (request, reply) => {
|
|
317
|
+
// we can use the `request.body` object to get the data sent by the client
|
|
318
|
+
const result = await collection.insertOne({ animal: request.body.animal })
|
|
319
|
+
return result
|
|
320
|
+
})
|
|
265
321
|
}
|
|
266
322
|
|
|
267
323
|
module.exports = routes
|
|
268
324
|
```
|
|
269
325
|
|
|
270
|
-
Wow, that was fast
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
326
|
+
Wow, that was fast!
|
|
327
|
+
|
|
328
|
+
Let's recap what we have done here since we've introduced some new concepts.
|
|
329
|
+
|
|
330
|
+
As you can see, we used `register` for both the database connector and the
|
|
331
|
+
registration of the routes.
|
|
275
332
|
|
|
276
|
-
|
|
333
|
+
This is one of the best features of Fastify, it will load your plugins in the
|
|
334
|
+
same order you declare them, and it will load the next plugin only once the
|
|
335
|
+
current one has been loaded. In this way, we can register the database connector
|
|
336
|
+
in the first plugin and use it in the second *(read
|
|
337
|
+
[here](../Reference/Plugins.md#handle-the-scope) to understand how to handle the
|
|
338
|
+
scope of a plugin)*.
|
|
277
339
|
|
|
278
|
-
|
|
340
|
+
Plugin loading starts when you call `fastify.listen()`, `fastify.inject()` or
|
|
341
|
+
`fastify.ready()`
|
|
342
|
+
|
|
343
|
+
The MongoDB plugin uses the `decorate` API to add custom objects to the Fastify
|
|
344
|
+
instance, making them available for use everywhere. Use of this API is
|
|
345
|
+
encouraged to facilitate easy code reuse and to decrease code or logic
|
|
346
|
+
duplication.
|
|
347
|
+
|
|
348
|
+
To dig deeper into how Fastify plugins work, how to develop new plugins, and for
|
|
349
|
+
details on how to use the whole Fastify API to deal with the complexity of
|
|
350
|
+
asynchronously bootstrapping an application, read [the hitchhiker's guide to
|
|
351
|
+
plugins](./Plugins-Guide.md).
|
|
279
352
|
|
|
280
|
-
<a name="plugin-loading-order"></a>
|
|
281
353
|
### Loading order of your plugins
|
|
282
|
-
|
|
354
|
+
<a id="plugin-loading-order"></a>
|
|
355
|
+
|
|
356
|
+
To guarantee consistent and predictable behavior of your application, we highly
|
|
357
|
+
recommend to always load your code as shown below:
|
|
283
358
|
```
|
|
284
359
|
└── plugins (from the Fastify ecosystem)
|
|
285
360
|
└── your plugins (your custom plugins)
|
|
@@ -287,8 +362,13 @@ To guarantee consistent and predictable behavior of your application, we highly
|
|
|
287
362
|
└── hooks
|
|
288
363
|
└── your services
|
|
289
364
|
```
|
|
290
|
-
In this way, you will always have access to all of the properties declared in
|
|
291
|
-
|
|
365
|
+
In this way, you will always have access to all of the properties declared in
|
|
366
|
+
the current scope.
|
|
367
|
+
|
|
368
|
+
As discussed previously, Fastify offers a solid encapsulation model, to help you
|
|
369
|
+
build your application as single and independent services. If you want to
|
|
370
|
+
register a plugin only for a subset of routes, you just have to replicate the
|
|
371
|
+
above structure.
|
|
292
372
|
```
|
|
293
373
|
└── plugins (from the Fastify ecosystem)
|
|
294
374
|
└── your plugins (your custom plugins)
|
|
@@ -311,10 +391,14 @@ As discussed previously, Fastify offers a solid encapsulation model, to help you
|
|
|
311
391
|
└── your services
|
|
312
392
|
```
|
|
313
393
|
|
|
314
|
-
<a name="validate-data"></a>
|
|
315
394
|
### Validate your data
|
|
316
|
-
|
|
317
|
-
|
|
395
|
+
<a id="validate-data"></a>
|
|
396
|
+
|
|
397
|
+
Data validation is extremely important and a core concept of the framework.
|
|
398
|
+
|
|
399
|
+
To validate incoming requests, Fastify uses [JSON
|
|
400
|
+
Schema](https://json-schema.org/).
|
|
401
|
+
|
|
318
402
|
(JTD schemas are loosely supported, but `jsonShorthand` must be disabled first)
|
|
319
403
|
|
|
320
404
|
Let's look at an example demonstrating validation for routes:
|
|
@@ -335,13 +419,21 @@ fastify.post('/', opts, async (request, reply) => {
|
|
|
335
419
|
return { hello: 'world' }
|
|
336
420
|
})
|
|
337
421
|
```
|
|
338
|
-
This example shows how to pass an options object to the route, which accepts a
|
|
339
|
-
|
|
422
|
+
This example shows how to pass an options object to the route, which accepts a
|
|
423
|
+
`schema` key that contains all of the schemas for route, `body`, `querystring`,
|
|
424
|
+
`params`, and `headers`.
|
|
425
|
+
|
|
426
|
+
Read [Validation and
|
|
427
|
+
Serialization](../Reference/Validation-and-Serialization.md) to learn more.
|
|
340
428
|
|
|
341
|
-
<a name="serialize-data"></a>
|
|
342
429
|
### Serialize your data
|
|
343
|
-
|
|
344
|
-
|
|
430
|
+
<a id="serialize-data"></a>
|
|
431
|
+
|
|
432
|
+
Fastify has first class support for JSON. It is extremely optimized to parse
|
|
433
|
+
JSON bodies and to serialize JSON output.
|
|
434
|
+
|
|
435
|
+
To speed up JSON serialization (yes, it is slow!) use the `response` key of the
|
|
436
|
+
schema option as shown in the following example:
|
|
345
437
|
```js
|
|
346
438
|
const opts = {
|
|
347
439
|
schema: {
|
|
@@ -360,12 +452,19 @@ fastify.get('/', opts, async (request, reply) => {
|
|
|
360
452
|
return { hello: 'world' }
|
|
361
453
|
})
|
|
362
454
|
```
|
|
363
|
-
By specifying a schema as shown, you can speed up serialization by a factor of
|
|
364
|
-
|
|
455
|
+
By specifying a schema as shown, you can speed up serialization by a factor of
|
|
456
|
+
2-3. This also helps to protect against leakage of potentially sensitive data,
|
|
457
|
+
since Fastify will serialize only the data present in the response schema. Read
|
|
458
|
+
[Validation and Serialization](../Reference/Validation-and-Serialization.md) to
|
|
459
|
+
learn more.
|
|
365
460
|
|
|
366
|
-
<a name="request-payload"></a>
|
|
367
461
|
### Parsing request payloads
|
|
368
|
-
|
|
462
|
+
<a id="request-payload"></a>
|
|
463
|
+
|
|
464
|
+
Fastify parses `'application/json'` and `'text/plain'` request payloads
|
|
465
|
+
natively, with the result accessible from the [Fastify
|
|
466
|
+
request](../Reference/Request.md) object at `request.body`.
|
|
467
|
+
|
|
369
468
|
The following example returns the parsed body of a request back to the client:
|
|
370
469
|
|
|
371
470
|
```js
|
|
@@ -375,21 +474,33 @@ fastify.post('/', opts, async (request, reply) => {
|
|
|
375
474
|
})
|
|
376
475
|
```
|
|
377
476
|
|
|
378
|
-
Read [Content
|
|
477
|
+
Read [Content-Type Parser](../Reference/ContentTypeParser.md) to learn more
|
|
478
|
+
about Fastify's default parsing functionality and how to support other content
|
|
479
|
+
types.
|
|
379
480
|
|
|
380
|
-
<a name="extend-server"></a>
|
|
381
481
|
### Extend your server
|
|
382
|
-
|
|
383
|
-
|
|
482
|
+
<a id="extend-server"></a>
|
|
483
|
+
|
|
484
|
+
Fastify is built to be extremely extensible and minimal, we believe that a
|
|
485
|
+
bare-bones framework is all that is necessary to make great applications
|
|
486
|
+
possible.
|
|
487
|
+
|
|
488
|
+
In other words, Fastify is not a "batteries included" framework, and relies on
|
|
489
|
+
an amazing [ecosystem](./Ecosystem.md)!
|
|
384
490
|
|
|
385
|
-
<a name="test-server"></a>
|
|
386
491
|
### Test your server
|
|
387
|
-
|
|
388
|
-
|
|
492
|
+
<a id="test-server"></a>
|
|
493
|
+
|
|
494
|
+
Fastify does not offer a testing framework, but we do recommend a way to write
|
|
495
|
+
your tests that uses the features and architecture of Fastify.
|
|
496
|
+
|
|
497
|
+
Read the [testing](./Testing.md) documentation to learn more!
|
|
389
498
|
|
|
390
|
-
<a name="cli"></a>
|
|
391
499
|
### Run your server from CLI
|
|
392
|
-
|
|
500
|
+
<a id="cli"></a>
|
|
501
|
+
|
|
502
|
+
Fastify also has CLI integration thanks to
|
|
503
|
+
[fastify-cli](https://github.com/fastify/fastify-cli).
|
|
393
504
|
|
|
394
505
|
First, install `fastify-cli`:
|
|
395
506
|
|
|
@@ -425,12 +536,21 @@ Then run your server with:
|
|
|
425
536
|
npm start
|
|
426
537
|
```
|
|
427
538
|
|
|
428
|
-
<a name="slides"></a>
|
|
429
539
|
### Slides and Videos
|
|
540
|
+
<a id="slides"></a>
|
|
541
|
+
|
|
430
542
|
- Slides
|
|
431
|
-
- [Take your HTTP server to ludicrous
|
|
432
|
-
|
|
543
|
+
- [Take your HTTP server to ludicrous
|
|
544
|
+
speed](https://mcollina.github.io/take-your-http-server-to-ludicrous-speed)
|
|
545
|
+
by [@mcollina](https://github.com/mcollina)
|
|
546
|
+
- [What if I told you that HTTP can be
|
|
547
|
+
fast](https://delvedor.github.io/What-if-I-told-you-that-HTTP-can-be-fast)
|
|
548
|
+
by [@delvedor](https://github.com/delvedor)
|
|
433
549
|
|
|
434
550
|
- Videos
|
|
435
|
-
- [Take your HTTP server to ludicrous
|
|
436
|
-
|
|
551
|
+
- [Take your HTTP server to ludicrous
|
|
552
|
+
speed](https://www.youtube.com/watch?v=5z46jJZNe8k) by
|
|
553
|
+
[@mcollina](https://github.com/mcollina)
|
|
554
|
+
- [What if I told you that HTTP can be
|
|
555
|
+
fast](https://www.webexpo.net/prague2017/talk/what-if-i-told-you-that-http-can-be-fast/)
|
|
556
|
+
by [@delvedor](https://github.com/delvedor)
|
package/docs/Guides/Index.md
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
<a id="guides-
|
|
3
|
+
## Guides Table Of Contents
|
|
4
|
+
<a id="guides-toc"></a>
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
This table of contents is in alphabetical order.
|
|
7
|
+
|
|
8
|
+
+ [Benchmarking](./Benchmarking.md): This guide introduces how to benchmark
|
|
9
|
+
applications based upon Fastify.
|
|
10
|
+
+ [Contributing](./Contributing.md): Details how to participate in the
|
|
11
|
+
development of Fastify, and shows how to setup an environment compatible with
|
|
12
|
+
the project's code style.
|
|
13
|
+
+ [Ecosystem](./Ecosystem.md): Lists all core plugins and many known community
|
|
14
|
+
plugins.
|
|
15
|
+
+ [Fluent Schema](./Fluent-Schema.md): Shows how writing JSON Schema can be
|
|
16
|
+
written with a fluent API and used in Fastify.
|
|
17
|
+
+ [Getting Started](./Getting-Started.md): Introduction tutorial for Fastify.
|
|
18
|
+
This is where beginners should start.
|
|
19
|
+
+ [Migration Guide (v3)](./Migration-Guide-V3.md): Details how to migrate to
|
|
20
|
+
Fastify v3 from earlier versions.
|
|
21
|
+
+ [Plugins Guide](./Plugins-Guide.md): An informal introduction to writing
|
|
22
|
+
Fastify plugins.
|
|
23
|
+
+ [Recommendations](./Recommendations.md): Recommendations for how to deploy
|
|
24
|
+
Fastify into production environments.
|
|
25
|
+
+ [Serverless](./Serverless.md): Details on how to deploy Fastify applications
|
|
26
|
+
in various Function as a Service (FaaS) environments.
|
|
27
|
+
+ [Style Guide](./Style-Guide.md): Explains the writing style we use for the
|
|
28
|
+
Fastify documentation for those who want to contribute documentation.
|
|
29
|
+
+ [Testing](./Testing.md): Explains how to write unit tests for Fastify
|
|
30
|
+
applications.
|
|
31
|
+
+ [Write Plugin](./Write-Plugin.md): A set of guidelines for what the Fastify
|
|
32
|
+
team considers good practices for writing a Fastify plugin.
|
|
@@ -34,9 +34,9 @@ fastify.use(require('cors')());
|
|
|
34
34
|
|
|
35
35
|
### Changed logging serialization ([#2017](https://github.com/fastify/fastify/pull/2017))
|
|
36
36
|
|
|
37
|
-
The logging [Serializers](Logging.md) have been updated to now
|
|
38
|
-
[`Request`](Request.md) and
|
|
39
|
-
native ones.
|
|
37
|
+
The logging [Serializers](../Reference/Logging.md) have been updated to now
|
|
38
|
+
Fastify [`Request`](../Reference/Request.md) and
|
|
39
|
+
[`Reply`](../Reference/Reply.md) objects instead of native ones.
|
|
40
40
|
|
|
41
41
|
Any custom serializers must be updated if they rely upon `request` or `reply`
|
|
42
42
|
properties that are present on the native objects but not the Fastify objects.
|
|
@@ -79,8 +79,9 @@ const fastify = require('fastify')({
|
|
|
79
79
|
|
|
80
80
|
The non-standard `replace-way` shared schema support has been removed. This
|
|
81
81
|
feature has been replaced with JSON Schema specification compliant `$ref` based
|
|
82
|
-
substitution. To help understand this change read
|
|
83
|
-
|
|
82
|
+
substitution. To help understand this change read [Validation and Serialization
|
|
83
|
+
in Fastify
|
|
84
|
+
v3](https://dev.to/eomm/validation-and-serialization-in-fastify-v3-2e8l).
|
|
84
85
|
|
|
85
86
|
**v2:**
|
|
86
87
|
|
|
@@ -104,10 +105,10 @@ fastify.route({ method, url, schema, handler });
|
|
|
104
105
|
|
|
105
106
|
### Changed schema validation options ([#2023](https://github.com/fastify/fastify/pull/2023))
|
|
106
107
|
|
|
107
|
-
The `setSchemaCompiler` and `setSchemaResolver` options have been replaced
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
The `setSchemaCompiler` and `setSchemaResolver` options have been replaced with
|
|
109
|
+
the `setValidatorCompiler` to enable future tooling improvements. To help
|
|
110
|
+
understand this change read [Validation and Serialization in Fastify
|
|
111
|
+
v3](https://dev.to/eomm/validation-and-serialization-in-fastify-v3-2e8l).
|
|
111
112
|
|
|
112
113
|
**v2:**
|
|
113
114
|
|
|
@@ -136,22 +137,23 @@ fastify.setValidatorCompiler(({ schema, method, url, httpPart }) =>
|
|
|
136
137
|
|
|
137
138
|
### Changed preParsing hook behavior ([#2286](https://github.com/fastify/fastify/pull/2286))
|
|
138
139
|
|
|
139
|
-
From Fastify v3, the behavior of the `preParsing` hook will change slightly
|
|
140
|
-
|
|
140
|
+
From Fastify v3, the behavior of the `preParsing` hook will change slightly in
|
|
141
|
+
order to support request payload manipulation.
|
|
141
142
|
|
|
142
143
|
The hook now takes an additional argument, `payload`, and therefore the new hook
|
|
143
|
-
signature is `fn(request, reply, payload, done)` or
|
|
144
|
-
|
|
144
|
+
signature is `fn(request, reply, payload, done)` or `async fn(request, reply,
|
|
145
|
+
payload)`.
|
|
145
146
|
|
|
146
147
|
The hook can optionally return a new stream via `done(null, stream)` or
|
|
147
148
|
returning the stream in case of async functions.
|
|
148
149
|
|
|
149
|
-
If the hook returns a new stream, it will be used instead of the original one in
|
|
150
|
+
If the hook returns a new stream, it will be used instead of the original one in
|
|
151
|
+
subsequent hooks. A sample use case for this is handling compressed requests.
|
|
150
152
|
|
|
151
153
|
The new stream should add the `receivedEncodedLength` property to the stream
|
|
152
154
|
that should reflect the actual data size received from the client. For instance,
|
|
153
|
-
in a compressed request it should be the size of the compressed payload.
|
|
154
|
-
|
|
155
|
+
in a compressed request it should be the size of the compressed payload. This
|
|
156
|
+
property can (and should) be dynamically updated during `data` events.
|
|
155
157
|
|
|
156
158
|
The old syntax of Fastify v2 without payload is supported but it is deprecated.
|
|
157
159
|
|
|
@@ -161,23 +163,24 @@ From Fastify v3, the behavior of `onRoute` and `onRegister` hooks will change
|
|
|
161
163
|
slightly in order to support hook encapsulation.
|
|
162
164
|
|
|
163
165
|
- `onRoute` - The hook will be called asynchronously. The hook is now inherited
|
|
164
|
-
when registering a new plugin within the same encapsulation scope. Thus, this
|
|
165
|
-
hook should be registered _before_ registering any plugins.
|
|
166
|
+
when registering a new plugin within the same encapsulation scope. Thus, this
|
|
167
|
+
hook should be registered _before_ registering any plugins.
|
|
166
168
|
- `onRegister` - Same as the onRoute hook. The only difference is that now the
|
|
167
|
-
very first call will no longer be the framework itself, but the first
|
|
168
|
-
plugin.
|
|
169
|
+
very first call will no longer be the framework itself, but the first
|
|
170
|
+
registered plugin.
|
|
169
171
|
|
|
170
172
|
### Changed Content Type Parser syntax ([#2286](https://github.com/fastify/fastify/pull/2286))
|
|
171
173
|
|
|
172
174
|
In Fastify v3 the content type parsers now have a single signature for parsers.
|
|
173
175
|
|
|
174
|
-
The new signatures are `fn(request, payload, done)` or `async fn(request,
|
|
175
|
-
Note that `request` is now a Fastify request, not an
|
|
176
|
-
The payload is by default a stream. If the `parseAs` option
|
|
177
|
-
`addContentTypeParser`, then `payload` reflects the option value
|
|
176
|
+
The new signatures are `fn(request, payload, done)` or `async fn(request,
|
|
177
|
+
payload)`. Note that `request` is now a Fastify request, not an
|
|
178
|
+
`IncomingMessage`. The payload is by default a stream. If the `parseAs` option
|
|
179
|
+
is used in `addContentTypeParser`, then `payload` reflects the option value
|
|
180
|
+
(string or buffer).
|
|
178
181
|
|
|
179
|
-
The old signatures `fn(req, [done])` or `fn(req, payload, [done])`
|
|
180
|
-
|
|
182
|
+
The old signatures `fn(req, [done])` or `fn(req, payload, [done])` (where `req`
|
|
183
|
+
is `IncomingMessage`) are still supported but are deprecated.
|
|
181
184
|
|
|
182
185
|
### Changed TypeScript support
|
|
183
186
|
|
|
@@ -267,15 +270,18 @@ fastify.get('/', (request, reply) => {
|
|
|
267
270
|
## Further additions and improvements
|
|
268
271
|
|
|
269
272
|
- Hooks now have consistent context regardless of how they are registered
|
|
270
|
-
([#2005](https://github.com/fastify/fastify/pull/2005))
|
|
271
|
-
- Deprecated `request.req` and `reply.res` for
|
|
272
|
-
[`
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
option
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
273
|
+
([#2005](https://github.com/fastify/fastify/pull/2005))
|
|
274
|
+
- Deprecated `request.req` and `reply.res` for
|
|
275
|
+
[`request.raw`](../Reference/Request.md) and
|
|
276
|
+
[`reply.raw`](../Reference/Reply.md)
|
|
277
|
+
([#2008](https://github.com/fastify/fastify/pull/2008))
|
|
278
|
+
- Removed `modifyCoreObjects` option
|
|
279
|
+
([#2015](https://github.com/fastify/fastify/pull/2015))
|
|
280
|
+
- Added [`connectionTimeout`](../Reference/Server.md#factory-connection-timeout)
|
|
281
|
+
option ([#2086](https://github.com/fastify/fastify/pull/2086))
|
|
282
|
+
- Added [`keepAliveTimeout`](../Reference/Server.md#factory-keep-alive-timeout)
|
|
283
|
+
option ([#2086](https://github.com/fastify/fastify/pull/2086))
|
|
284
|
+
- Added async-await support for [plugins](../Reference/Plugins.md#async-await)
|
|
285
|
+
([#2093](https://github.com/fastify/fastify/pull/2093))
|
|
280
286
|
- Added the feature to throw object as error
|
|
281
|
-
([#2134](https://github.com/fastify/fastify/pull/2134))
|
|
287
|
+
([#2134](https://github.com/fastify/fastify/pull/2134))
|