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.
Files changed (67) hide show
  1. package/README.md +30 -29
  2. package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
  3. package/docs/Guides/Ecosystem.md +513 -0
  4. package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
  5. package/docs/{Getting-Started.md → Guides/Getting-Started.md} +180 -60
  6. package/docs/Guides/Index.md +30 -4
  7. package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
  8. package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
  9. package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
  10. package/docs/{Serverless.md → Guides/Serverless.md} +200 -42
  11. package/docs/Guides/Style-Guide.md +246 -0
  12. package/docs/{Testing.md → Guides/Testing.md} +26 -12
  13. package/docs/Guides/Write-Plugin.md +102 -0
  14. package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
  15. package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
  16. package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
  17. package/docs/{Errors.md → Reference/Errors.md} +77 -47
  18. package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
  19. package/docs/{Hooks.md → Reference/Hooks.md} +157 -70
  20. package/docs/Reference/Index.md +71 -0
  21. package/docs/{LTS.md → Reference/LTS.md} +31 -32
  22. package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
  23. package/docs/{Logging.md → Reference/Logging.md} +68 -28
  24. package/docs/Reference/Middleware.md +78 -0
  25. package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
  26. package/docs/{Reply.md → Reference/Reply.md} +205 -94
  27. package/docs/{Request.md → Reference/Request.md} +32 -16
  28. package/docs/{Routes.md → Reference/Routes.md} +243 -113
  29. package/docs/{Server.md → Reference/Server.md} +516 -267
  30. package/docs/{TypeScript.md → Reference/TypeScript.md} +451 -191
  31. package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
  32. package/docs/index.md +24 -0
  33. package/examples/typescript-server.ts +1 -1
  34. package/fastify.js +2 -3
  35. package/lib/contentTypeParser.js +11 -6
  36. package/lib/decorate.js +6 -3
  37. package/lib/logger.js +1 -1
  38. package/lib/route.js +1 -1
  39. package/lib/server.js +9 -8
  40. package/package.json +9 -4
  41. package/test/als.test.js +74 -0
  42. package/test/constrained-routes.test.js +220 -0
  43. package/test/custom-parser.test.js +11 -2
  44. package/test/decorator.test.js +38 -0
  45. package/test/handler-context.test.js +11 -4
  46. package/test/http2/closing.test.js +14 -5
  47. package/test/http2/constraint.test.js +91 -0
  48. package/test/listen.test.js +36 -22
  49. package/test/logger.test.js +16 -0
  50. package/test/maxRequestsPerSocket.test.js +10 -0
  51. package/test/request-error.test.js +2 -8
  52. package/test/requestTimeout.test.js +4 -1
  53. package/test/router-options.test.js +10 -1
  54. package/test/schema-feature.test.js +146 -0
  55. package/test/stream.test.js +14 -3
  56. package/test/trust-proxy.test.js +15 -7
  57. package/test/types/instance.test-d.ts +52 -1
  58. package/test/types/request.test-d.ts +7 -1
  59. package/test/types/route.test-d.ts +21 -0
  60. package/types/hooks.d.ts +12 -1
  61. package/types/instance.d.ts +16 -6
  62. package/types/request.d.ts +4 -1
  63. package/types/route.d.ts +1 -1
  64. package/docs/Ecosystem.md +0 -211
  65. package/docs/Middleware.md +0 -53
  66. package/docs/Style-Guide.md +0 -185
  67. package/docs/Write-Plugin.md +0 -58
@@ -2,9 +2,12 @@
2
2
 
3
3
  ## Hooks
4
4
 
5
- Hooks are registered with the `fastify.addHook` method and allow you to listen to specific events in the application or request/response lifecycle. You have to register a hook before the event is triggered, otherwise, the event is lost.
5
+ Hooks are registered with the `fastify.addHook` method and allow you to listen
6
+ to specific events in the application or request/response lifecycle. You have to
7
+ register a hook before the event is triggered, otherwise, the event is lost.
6
8
 
7
- By using hooks you can interact directly with the lifecycle of Fastify. There are Request/Reply hooks and application hooks:
9
+ By using hooks you can interact directly with the lifecycle of Fastify. There
10
+ are Request/Reply hooks and application hooks:
8
11
 
9
12
  - [Request/Reply Hooks](#requestreply-hooks)
10
13
  - [onRequest](#onrequest)
@@ -25,18 +28,26 @@ By using hooks you can interact directly with the lifecycle of Fastify. There ar
25
28
  - [onRegister](#onregister)
26
29
  - [Scope](#scope)
27
30
  - [Route level hooks](#route-level-hooks)
31
+ - [Diagnostics Channel Hooks](#diagnostics-channel-hooks)
28
32
 
29
- **Notice:** the `done` callback is not available when using `async`/`await` or returning a `Promise`. If you do invoke a `done` callback in this situation unexpected behavior may occur, e.g. duplicate invocation of handlers.
33
+ **Notice:** the `done` callback is not available when using `async`/`await` or
34
+ returning a `Promise`. If you do invoke a `done` callback in this situation
35
+ unexpected behavior may occur, e.g. duplicate invocation of handlers.
30
36
 
31
37
  ## Request/Reply Hooks
32
38
 
33
- [Request](Request.md) and [Reply](Reply.md) are the core Fastify objects.<br/>
34
- `done` is the function to continue with the [lifecycle](Lifecycle.md).
39
+ [Request](./Request.md) and [Reply](./Reply.md) are the core Fastify objects.
35
40
 
36
- It is easy to understand where each hook is executed by looking at the [lifecycle page](Lifecycle.md).<br>
37
- Hooks are affected by Fastify's encapsulation, and can thus be applied to selected routes. See the [Scopes](#scope) section for more information.
41
+ `done` is the function to continue with the [lifecycle](./Lifecycle.md).
38
42
 
39
- There are eight different hooks that you can use in Request/Reply *(in order of execution)*:
43
+ It is easy to understand where each hook is executed by looking at the
44
+ [lifecycle page](./Lifecycle.md).
45
+
46
+ Hooks are affected by Fastify's encapsulation, and can thus be applied to
47
+ selected routes. See the [Scopes](#scope) section for more information.
48
+
49
+ There are eight different hooks that you can use in Request/Reply *(in order of
50
+ execution)*:
40
51
 
41
52
  ### onRequest
42
53
  ```js
@@ -53,13 +64,18 @@ fastify.addHook('onRequest', async (request, reply) => {
53
64
  })
54
65
  ```
55
66
 
56
- **Notice:** in the [onRequest](#onrequest) hook, `request.body` will always be `null`, because the body parsing happens before the [preValidation](#prevalidation) hook.
67
+ **Notice:** in the [onRequest](#onrequest) hook, `request.body` will always be
68
+ `null`, because the body parsing happens before the
69
+ [preValidation](#prevalidation) hook.
57
70
 
58
71
  ### preParsing
59
72
 
60
- If you are using the `preParsing` hook, you can transform the request payload stream before it is parsed. It receives the request and reply objects as other hooks, and a stream with the current request payload.
73
+ If you are using the `preParsing` hook, you can transform the request payload
74
+ stream before it is parsed. It receives the request and reply objects as other
75
+ hooks, and a stream with the current request payload.
61
76
 
62
- If it returns a value (via `return` or via the callback function), it must return a stream.
77
+ If it returns a value (via `return` or via the callback function), it must
78
+ return a stream.
63
79
 
64
80
  For instance, you can uncompress the request body:
65
81
 
@@ -78,15 +94,23 @@ fastify.addHook('preParsing', async (request, reply, payload) => {
78
94
  })
79
95
  ```
80
96
 
81
- **Notice:** in the [preParsing](#preparsing) hook, `request.body` will always be `null`, because the body parsing happens before the [preValidation](#prevalidation) hook.
97
+ **Notice:** in the [preParsing](#preparsing) hook, `request.body` will always be
98
+ `null`, because the body parsing happens before the
99
+ [preValidation](#prevalidation) hook.
82
100
 
83
- **Notice:** you should also add a `receivedEncodedLength` property to the returned stream. This property is used to correctly match the request payload with the `Content-Length` header value. Ideally, this property should be updated on each received chunk.
101
+ **Notice:** you should also add a `receivedEncodedLength` property to the
102
+ returned stream. This property is used to correctly match the request payload
103
+ with the `Content-Length` header value. Ideally, this property should be updated
104
+ on each received chunk.
84
105
 
85
- **Notice**: The old syntaxes `function(request, reply, done)` and `async function(request, reply)` for the parser are still supported but they are deprecated.
106
+ **Notice**: The old syntaxes `function(request, reply, done)` and `async
107
+ function(request, reply)` for the parser are still supported but they are
108
+ deprecated.
86
109
 
87
110
  ### preValidation
88
111
 
89
- If you are using the `preValidation` hook, you can change the payload before it is validated. For example:
112
+ If you are using the `preValidation` hook, you can change the payload before it
113
+ is validated. For example:
90
114
 
91
115
  ```js
92
116
  fastify.addHook('preValidation', (request, reply, done) => {
@@ -118,7 +142,8 @@ fastify.addHook('preHandler', async (request, reply) => {
118
142
  ```
119
143
  ### preSerialization
120
144
 
121
- If you are using the `preSerialization` hook, you can change (or replace) the payload before it is serialized. For example:
145
+ If you are using the `preSerialization` hook, you can change (or replace) the
146
+ payload before it is serialized. For example:
122
147
 
123
148
  ```js
124
149
  fastify.addHook('preSerialization', (request, reply, payload, done) => {
@@ -134,7 +159,8 @@ fastify.addHook('preSerialization', async (request, reply, payload) => {
134
159
  })
135
160
  ```
136
161
 
137
- Note: the hook is NOT called if the payload is a `string`, a `Buffer`, a `stream`, or `null`.
162
+ Note: the hook is NOT called if the payload is a `string`, a `Buffer`, a
163
+ `stream`, or `null`.
138
164
 
139
165
  ### onError
140
166
  ```js
@@ -150,10 +176,19 @@ fastify.addHook('onError', async (request, reply, error) => {
150
176
  // You should not use this hook to update the error
151
177
  })
152
178
  ```
153
- This hook is useful if you need to do some custom error logging or add some specific header in case of error.<br/>
154
- It is not intended for changing the error, and calling `reply.send` will throw an exception.<br/>
155
- This hook will be executed only after the `customErrorHandler` has been executed, and only if the `customErrorHandler` sends an error back to the user *(Note that the default `customErrorHandler` always sends the error back to the user)*.<br/>
156
- **Notice:** unlike the other hooks, pass an error to the `done` function is not supported.
179
+ This hook is useful if you need to do some custom error logging or add some
180
+ specific header in case of error.
181
+
182
+ It is not intended for changing the error, and calling `reply.send` will throw
183
+ an exception.
184
+
185
+ This hook will be executed only after the `customErrorHandler` has been
186
+ executed, and only if the `customErrorHandler` sends an error back to the user
187
+ *(Note that the default `customErrorHandler` always sends the error back to the
188
+ user)*.
189
+
190
+ **Notice:** unlike the other hooks, pass an error to the `done` function is not
191
+ supported.
157
192
 
158
193
  ### onSend
159
194
  If you are using the `onSend` hook, you can change the payload. For example:
@@ -173,7 +208,8 @@ fastify.addHook('onSend', async (request, reply, payload) => {
173
208
  })
174
209
  ```
175
210
 
176
- You can also clear the payload to send a response with an empty body by replacing the payload with `null`:
211
+ You can also clear the payload to send a response with an empty body by
212
+ replacing the payload with `null`:
177
213
 
178
214
  ```js
179
215
  fastify.addHook('onSend', (request, reply, payload, done) => {
@@ -183,14 +219,17 @@ fastify.addHook('onSend', (request, reply, payload, done) => {
183
219
  })
184
220
  ```
185
221
 
186
- > You can also send an empty body by replacing the payload with the empty string `''`, but be aware that this will cause the `Content-Length` header to be set to `0`, whereas the `Content-Length` header will not be set if the payload is `null`.
222
+ > You can also send an empty body by replacing the payload with the empty string
223
+ > `''`, but be aware that this will cause the `Content-Length` header to be set
224
+ > to `0`, whereas the `Content-Length` header will not be set if the payload is
225
+ > `null`.
187
226
 
188
- Note: If you change the payload, you may only change it to a `string`, a `Buffer`, a `stream`, or `null`.
227
+ Note: If you change the payload, you may only change it to a `string`, a
228
+ `Buffer`, a `stream`, or `null`.
189
229
 
190
230
 
191
231
  ### onResponse
192
232
  ```js
193
-
194
233
  fastify.addHook('onResponse', (request, reply, done) => {
195
234
  // Some code
196
235
  done()
@@ -204,12 +243,13 @@ fastify.addHook('onResponse', async (request, reply) => {
204
243
  })
205
244
  ```
206
245
 
207
- The `onResponse` hook is executed when a response has been sent, so you will not be able to send more data to the client. It can however be useful for sending data to external services, for example, to gather statistics.
246
+ The `onResponse` hook is executed when a response has been sent, so you will not
247
+ be able to send more data to the client. It can however be useful for sending
248
+ data to external services, for example, to gather statistics.
208
249
 
209
250
  ### onTimeout
210
251
 
211
252
  ```js
212
-
213
253
  fastify.addHook('onTimeout', (request, reply, done) => {
214
254
  // Some code
215
255
  done()
@@ -222,11 +262,16 @@ fastify.addHook('onTimeout', async (request, reply) => {
222
262
  await asyncMethod()
223
263
  })
224
264
  ```
225
- `onTimeout` is useful if you need to monitor the request timed out in your service (if the `connectionTimeout` property is set on the Fastify instance). The `onTimeout` hook is executed when a request is timed out and the HTTP socket has been hanged up. Therefore, you will not be able to send data to the client.
265
+ `onTimeout` is useful if you need to monitor the request timed out in your
266
+ service (if the `connectionTimeout` property is set on the Fastify instance).
267
+ The `onTimeout` hook is executed when a request is timed out and the HTTP socket
268
+ has been hanged up. Therefore, you will not be able to send data to the client.
226
269
 
227
270
 
228
271
  ### Manage Errors from a hook
229
- If you get an error during the execution of your hook, just pass it to `done()` and Fastify will automatically close the request and send the appropriate error code to the user.
272
+ If you get an error during the execution of your hook, just pass it to `done()`
273
+ and Fastify will automatically close the request and send the appropriate error
274
+ code to the user.
230
275
 
231
276
  ```js
232
277
  fastify.addHook('onRequest', (request, reply, done) => {
@@ -241,7 +286,7 @@ fastify.addHook('preHandler', (request, reply, done) => {
241
286
  done(new Error('Some error'))
242
287
  })
243
288
  ```
244
- *The error will be handled by [`Reply`](Reply.md#errors).*
289
+ *The error will be handled by [`Reply`](./Reply.md#errors).*
245
290
 
246
291
  Or if you're using `async/await` you can just throw an error:
247
292
  ```js
@@ -252,20 +297,19 @@ fastify.addHook('onResponse', async (request, reply) => {
252
297
 
253
298
  ### Respond to a request from a hook
254
299
 
255
- If needed, you can respond to a request before you reach the route handler,
256
- for example when implementing an authentication hook.
257
- Replying from a hook implies that the hook chain is __stopped__ and
258
- the rest of the hooks and handlers are not executed. If the hook is
259
- using the callback approach, i.e. it is not an `async` function or it
260
- returns a `Promise`, it is as simple as calling `reply.send()` and avoiding
261
- calling the callback. If the hook is `async`, `reply.send()` __must__ be
262
- called _before_ the function returns or the promise resolves, otherwise, the
263
- request will proceed. When `reply.send()` is called outside of the
264
- promise chain, it is important to `return reply` otherwise the request
265
- will be executed twice.
266
-
267
- It is important to __not mix callbacks and `async`/`Promise`__, otherwise
268
- the hook chain will be executed twice.
300
+ If needed, you can respond to a request before you reach the route handler, for
301
+ example when implementing an authentication hook. Replying from a hook implies
302
+ that the hook chain is __stopped__ and the rest of the hooks and handlers are
303
+ not executed. If the hook is using the callback approach, i.e. it is not an
304
+ `async` function or it returns a `Promise`, it is as simple as calling
305
+ `reply.send()` and avoiding calling the callback. If the hook is `async`,
306
+ `reply.send()` __must__ be called _before_ the function returns or the promise
307
+ resolves, otherwise, the request will proceed. When `reply.send()` is called
308
+ outside of the promise chain, it is important to `return reply` otherwise the
309
+ request will be executed twice.
310
+
311
+ It is important to __not mix callbacks and `async`/`Promise`__, otherwise the
312
+ hook chain will be executed twice.
269
313
 
270
314
  If you are using `onRequest` or `preHandler` use `reply.send`.
271
315
 
@@ -282,7 +326,10 @@ fastify.addHook('preHandler', async (request, reply) => {
282
326
  })
283
327
  ```
284
328
 
285
- If you want to respond with a stream, you should avoid using an `async` function for the hook. If you must use an `async` function, your code will need to follow the pattern in [test/hooks-async.js](https://github.com/fastify/fastify/blob/94ea67ef2d8dce8a955d510cd9081aabd036fa85/test/hooks-async.js#L269-L275).
329
+ If you want to respond with a stream, you should avoid using an `async` function
330
+ for the hook. If you must use an `async` function, your code will need to follow
331
+ the pattern in
332
+ [test/hooks-async.js](https://github.com/fastify/fastify/blob/94ea67ef2d8dce8a955d510cd9081aabd036fa85/test/hooks-async.js#L269-L275).
286
333
 
287
334
  ```js
288
335
  fastify.addHook('onRequest', (request, reply, done) => {
@@ -291,8 +338,8 @@ fastify.addHook('onRequest', (request, reply, done) => {
291
338
  })
292
339
  ```
293
340
 
294
- If you are sending a response without `await` on it, make sure to always
295
- `return reply`:
341
+ If you are sending a response without `await` on it, make sure to always `return
342
+ reply`:
296
343
 
297
344
  ```js
298
345
  fastify.addHook('preHandler', async (request, reply) => {
@@ -321,10 +368,11 @@ You can hook into the application-lifecycle as well.
321
368
  - [onRegister](#onregister)
322
369
 
323
370
  ### onReady
324
- Triggered before the server starts listening for requests and when `.ready()` is invoked. It cannot change the routes or add new hooks.
325
- Registered hook functions are executed serially.
326
- Only after all `onReady` hook functions have completed will the server start listening for requests.
327
- Hook functions accept one argument: a callback, `done`, to be invoked after the hook function is complete.
371
+ Triggered before the server starts listening for requests and when `.ready()` is
372
+ invoked. It cannot change the routes or add new hooks. Registered hook functions
373
+ are executed serially. Only after all `onReady` hook functions have completed
374
+ will the server start listening for requests. Hook functions accept one
375
+ argument: a callback, `done`, to be invoked after the hook function is complete.
328
376
  Hook functions are invoked with `this` bound to the associated Fastify instance.
329
377
 
330
378
  ```js
@@ -342,20 +390,35 @@ fastify.addHook('onReady', async function () {
342
390
  })
343
391
  ```
344
392
 
345
- <a name="on-close"></a>
346
393
  ### onClose
347
- Triggered when `fastify.close()` is invoked to stop the server. It is useful when [plugins](Plugins.md) need a "shutdown" event, for example, to close an open connection to a database.<br>
348
- The first argument is the Fastify instance, the second one the `done` callback.
394
+ <a id="on-close"></a>
395
+
396
+ Triggered when `fastify.close()` is invoked to stop the server. It is useful
397
+ when [plugins](./Plugins.md) need a "shutdown" event, for example, to close an
398
+ open connection to a database.
399
+
400
+ The hook function takes the Fastify instance as a first argument,
401
+ and a `done` callback for synchronous hook functions.
349
402
  ```js
403
+ // callback style
350
404
  fastify.addHook('onClose', (instance, done) => {
351
405
  // Some code
352
406
  done()
353
407
  })
408
+
409
+ // or async/await style
410
+ fastify.addHook('onClose', async (instance) => {
411
+ // Some async code
412
+ await closeDatabaseConnections()
413
+ })
354
414
  ```
355
415
 
356
- <a name="on-route"></a>
357
416
  ### onRoute
358
- Triggered when a new route is registered. Listeners are passed a `routeOptions` object as the sole parameter. The interface is synchronous, and, as such, the listeners are not passed a callback. This hook is encapsulated.
417
+ <a id="on-route"></a>
418
+
419
+ Triggered when a new route is registered. Listeners are passed a `routeOptions`
420
+ object as the sole parameter. The interface is synchronous, and, as such, the
421
+ listeners are not passed a callback. This hook is encapsulated.
359
422
  ```js
360
423
  fastify.addHook('onRoute', (routeOptions) => {
361
424
  //Some code
@@ -371,7 +434,8 @@ fastify.addHook('onRoute', (routeOptions) => {
371
434
  })
372
435
  ```
373
436
 
374
- If you are authoring a plugin and you need to customize application routes, like modifying the options or adding new route hooks, this is the right place.
437
+ If you are authoring a plugin and you need to customize application routes, like
438
+ modifying the options or adding new route hooks, this is the right place.
375
439
 
376
440
  ```js
377
441
  fastify.addHook('onRoute', (routeOptions) => {
@@ -384,11 +448,18 @@ fastify.addHook('onRoute', (routeOptions) => {
384
448
  })
385
449
  ```
386
450
 
387
- <a name="on-register"></a>
388
451
  ### onRegister
389
- Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed **before** the registered code.<br/>
390
- This hook can be useful if you are developing a plugin that needs to know when a plugin context is formed, and you want to operate in that specific context, thus this hook is encapsulated.<br/>
391
- **Note:** This hook will not be called if a plugin is wrapped inside [`fastify-plugin`](https://github.com/fastify/fastify-plugin).
452
+ <a id="on-register"></a>
453
+
454
+ Triggered when a new plugin is registered and a new encapsulation context is
455
+ created. The hook will be executed **before** the registered code.
456
+
457
+ This hook can be useful if you are developing a plugin that needs to know when a
458
+ plugin context is formed, and you want to operate in that specific context, thus
459
+ this hook is encapsulated.
460
+
461
+ **Note:** This hook will not be called if a plugin is wrapped inside
462
+ [`fastify-plugin`](https://github.com/fastify/fastify-plugin).
392
463
  ```js
393
464
  fastify.decorate('data', [])
394
465
 
@@ -418,9 +489,14 @@ fastify.addHook('onRegister', (instance, opts) => {
418
489
  })
419
490
  ```
420
491
 
421
- <a name="scope"></a>
422
492
  ## Scope
423
- Except for [onClose](#onclose), all hooks are encapsulated. This means that you can decide where your hooks should run by using `register` as explained in the [plugins guide](Plugins-Guide.md). If you pass a function, that function is bound to the right Fastify context and from there you have full access to the Fastify API.
493
+ <a id="scope"></a>
494
+
495
+ Except for [onClose](#onclose), all hooks are encapsulated. This means that you
496
+ can decide where your hooks should run by using `register` as explained in the
497
+ [plugins guide](../Guides/Plugins-Guide.md). If you pass a function, that
498
+ function is bound to the right Fastify context and from there you have full
499
+ access to the Fastify API.
424
500
 
425
501
  ```js
426
502
  fastify.addHook('onRequest', function (request, reply, done) {
@@ -429,7 +505,8 @@ fastify.addHook('onRequest', function (request, reply, done) {
429
505
  })
430
506
  ```
431
507
 
432
- Note that the Fastify context in each hook is the same as the plugin where the route was registered, for example:
508
+ Note that the Fastify context in each hook is the same as the plugin where the
509
+ route was registered, for example:
433
510
 
434
511
  ```js
435
512
  fastify.addHook('onRequest', async function (req, reply) {
@@ -455,15 +532,25 @@ fastify.register(async function plugin (fastify, opts) {
455
532
  })
456
533
  ```
457
534
 
458
- Warn: if you declare the function with an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), the `this` will not be Fastify, but the one of the current scope.
535
+ Warn: if you declare the function with an [arrow
536
+ function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions),
537
+ the `this` will not be Fastify, but the one of the current scope.
459
538
 
460
- <a name="route-hooks"></a>
461
539
 
462
540
  ## Route level hooks
463
- You can declare one or more custom lifecycle hooks ([onRequest](#onrequest), [onResponse](#onresponse), [preParsing](#preparsing), [preValidation](#prevalidation), [preHandler](#prehandler), [preSerialization](#preserialization), [onSend](#onsend), [onTimeout](#ontimeout), and [onError](#onerror)) hook(s) that will be **unique** for the route.
464
- If you do so, those hooks are always executed as the last hook in their category. <br/>
465
- This can be useful if you need to implement authentication, where the [preParsing](#preparsing) or [preValidation](#prevalidation) hooks are exactly what you need.
466
- Multiple route-level hooks can also be specified as an array.
541
+ <a id="route-hooks"></a>
542
+
543
+ You can declare one or more custom lifecycle hooks ([onRequest](#onrequest),
544
+ [onResponse](#onresponse), [preParsing](#preparsing),
545
+ [preValidation](#prevalidation), [preHandler](#prehandler),
546
+ [preSerialization](#preserialization), [onSend](#onsend),
547
+ [onTimeout](#ontimeout), and [onError](#onerror)) hook(s) that will be
548
+ **unique** for the route. If you do so, those hooks are always executed as the
549
+ last hook in their category.
550
+
551
+ This can be useful if you need to implement authentication, where the
552
+ [preParsing](#preparsing) or [preValidation](#prevalidation) hooks are exactly
553
+ what you need. Multiple route-level hooks can also be specified as an array.
467
554
 
468
555
  ```js
469
556
  fastify.addHook('onRequest', (request, reply, done) => {
@@ -0,0 +1,71 @@
1
+ <h1 align="center">Fastify</h1>
2
+
3
+ ## Core Documents
4
+ <a id="reference-core-docs"></a>
5
+
6
+ For the full table of contents (TOC), see [below](#reference-toc). The following
7
+ list is a subset of the full TOC that detail core Fastify APIs and concepts in
8
+ order of most likely importance to the reader:
9
+
10
+ + [Server](./Server.md): Documents the core Fastify API. Includes documentation
11
+ for the factory function and the object returned by the factory function.
12
+ + [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
13
+ illustrates where [Hooks](./Hooks.md) are available for integrating with it.
14
+ + [Routes](./Routes.md): Details how to register routes with Fastify and how
15
+ Fastify builds and evaluates the routing trie.
16
+ + [Request](./Request.md): Details Fastify's request object that is passed into
17
+ each request handler.
18
+ + [Reply](./Reply.md): Details Fastify's response object available to each
19
+ request handler.
20
+ + [Validation and Serialization](./Validation-and-Serialization.md): Details
21
+ Fastify's support for validating incoming data and how Fastify serializes data
22
+ for responses.
23
+ + [Plugins](./Plugins.md): Explains Fastify's plugin architecture and API.
24
+ + [Encapsulation](./Encapsulation.md): Explains a core concept upon which all
25
+ Fastify plugins are built.
26
+ + [Decorators](./Decorators.md): Explains the server, request, and response
27
+ decorator APIs.
28
+ + [Hooks](./Hooks.md): Details the API by which Fastify plugins can inject
29
+ themselves into Fastify's handling of the request lifecycle.
30
+
31
+
32
+ ## Reference Documentation Table Of Contents
33
+ <a id="reference-toc"></a>
34
+
35
+ This table of contents is in alphabetical order.
36
+
37
+ + [Content Type Parser](./ContentTypeParser.md): Documents Fastify's default
38
+ content type parser and how to add support for new content types.
39
+ + [Decorators](./Decorators.md): Explains the server, request, and response
40
+ decorator APIs.
41
+ + [Encapsulation](./Encapsulation.md): Explains a core concept upon which all
42
+ Fastify plugins are built.
43
+ + [Errors](./Errors.md): Details how Fastify handles errors and lists the
44
+ standard set of errors Fastify generates.
45
+ + [Hooks](./Hooks.md): Details the API by which Fastify plugins can inject
46
+ themselves into Fastify's handling of the request lifecycle.
47
+ + [HTTP2](./HTTP2.md): Details Fastify's HTTP2 support.
48
+ + [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
49
+ illustrates where [Hooks](./Hooks.md) are available for integrating with it.
50
+ + [Logging](./Logging.md): Details Fastify's included logging and how to
51
+ customize it.
52
+ + [Long Term Support](./LTS.md): Explains Fastify's long term support (LTS)
53
+ guarantee and the exceptions possible to the [semver](https://semver.org)
54
+ contract.
55
+ + [Middleware](./Middleware.md): Details Fastify's support for Express.js style
56
+ middleware.
57
+ + [Plugins](./Plugins.md): Explains Fastify's plugin architecture and API.
58
+ + [Reply](./Reply.md): Details Fastify's response object available to each
59
+ request handler.
60
+ + [Request](./Request.md): Details Fastify's request object that is passed into
61
+ each request handler.
62
+ + [Routes](./Routes.md): Details how to register routes with Fastify and how
63
+ Fastify builds and evaluates the routing trie.
64
+ + [Server](./Server.md): Documents the core Fastify API. Includes documentation
65
+ for the factory function and the object returned by the factory function.
66
+ + [TypeScript](./TypeScript.md): Documents Fastify's TypeScript support and
67
+ provides recommendations for writing applications in TypeScript that utilize
68
+ Fastify.
69
+ + [Validation and Serialization](./Validation-and-Serialization.md): Details
70
+ Fastify's support for validating incoming data and how Fastify serializes data
71
+ for responses.
@@ -1,49 +1,47 @@
1
1
  <h1 align="center">Fastify</h1>
2
2
 
3
- <a name="lts"></a>
4
-
5
3
  ## Long Term Support
4
+ <a id="lts"></a>
6
5
 
7
- Fastify's Long Term Support (LTS) is provided according to the schedule laid
8
- out in this document:
6
+ Fastify's Long Term Support (LTS) is provided according to the schedule laid out
7
+ in this document:
9
8
 
10
9
  1. Major releases, "X" release of [semantic versioning][semver] X.Y.Z release
11
10
  versions, are supported for a minimum period of six months from their release
12
11
  date. The release date of any specific version can be found at
13
12
  [https://github.com/fastify/fastify/releases](https://github.com/fastify/fastify/releases).
14
13
 
15
- 1. Major releases will receive security updates for an additional six months
16
- from the release of the next major release. After this period
17
- we will still review and release security fixes as long as they are
18
- provided by the community and they do not violate other constraints,
19
- e.g. minimum supported Node.js version.
14
+ 2. Major releases will receive security updates for an additional six months
15
+ from the release of the next major release. After this period we will still
16
+ review and release security fixes as long as they are provided by the
17
+ community and they do not violate other constraints, e.g. minimum supported
18
+ Node.js version.
20
19
 
21
- 1. Major releases will be tested and verified against all Node.js
22
- release lines that are supported by the
23
- [Node.js LTS policy](https://github.com/nodejs/Release) within the
24
- LTS period of that given Fastify release line. This implies that only
25
- the latest Node.js release of a given line is supported.
20
+ 3. Major releases will be tested and verified against all Node.js release lines
21
+ that are supported by the [Node.js LTS
22
+ policy](https://github.com/nodejs/Release) within the LTS period of that
23
+ given Fastify release line. This implies that only the latest Node.js release
24
+ of a given line is supported.
26
25
 
27
26
  A "month" is defined as 30 consecutive days.
28
27
 
29
28
  > ## Security Releases and Semver
30
29
  >
31
- > As a consequence of providing long-term support for major releases, there
32
- > are occasions where we need to release breaking changes as a _minor_
33
- > version release. Such changes will _always_ be noted in the
34
- > [release notes](https://github.com/fastify/fastify/releases).
30
+ > As a consequence of providing long-term support for major releases, there are
31
+ > occasions where we need to release breaking changes as a _minor_ version
32
+ > release. Such changes will _always_ be noted in the [release
33
+ > notes](https://github.com/fastify/fastify/releases).
35
34
  >
36
- > To avoid automatically receiving breaking security updates it is possible to use
37
- > the tilde (`~`) range qualifier. For example, to get patches for the 3.15
38
- > release, and avoid automatically updating to the 3.16 release, specify
39
- > the dependency as `"fastify": "~3.15.x"`. This will leave your application vulnerable,
40
- > so please use with caution.
35
+ > To avoid automatically receiving breaking security updates it is possible to
36
+ > use the tilde (`~`) range qualifier. For example, to get patches for the 3.15
37
+ > release, and avoid automatically updating to the 3.16 release, specify the
38
+ > dependency as `"fastify": "~3.15.x"`. This will leave your application
39
+ > vulnerable, so please use with caution.
41
40
 
42
41
  [semver]: https://semver.org/
43
42
 
44
- <a name="lts-schedule"></a>
45
-
46
43
  ### Schedule
44
+ <a id="lts-schedule"></a>
47
45
 
48
46
  | Version | Release Date | End Of LTS Date | Node.js |
49
47
  | :------ | :----------- | :-------------- | :------------------- |
@@ -51,14 +49,14 @@ A "month" is defined as 30 consecutive days.
51
49
  | 2.0.0 | 2019-02-25 | 2021-01-31 | 6, 8, 10, 12, 14 |
52
50
  | 3.0.0 | 2020-07-07 | TBD | 10, 12, 14, 16 |
53
51
 
54
- <a name="supported-os"></a>
55
-
56
52
  ### CI tested operating systems
53
+ <a id="supported-os"></a>
57
54
 
58
- Fastify uses GitHub Actions for CI testing, please refer to
59
- [GitHub's documentation regarding workflow runners](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources)
60
- for further details on what the latest virtual environment is in relation to
61
- the YAML workflow labels below:
55
+ Fastify uses GitHub Actions for CI testing, please refer to [GitHub's
56
+ documentation regarding workflow
57
+ runners](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources)
58
+ for further details on what the latest virtual environment is in relation to the
59
+ YAML workflow labels below:
62
60
 
63
61
  | OS | YAML Workflow Label | Package Manager | Node.js |
64
62
  |---------|------------------------|---------------------------|--------------|
@@ -67,4 +65,5 @@ the YAML workflow labels below:
67
65
  | Windows | `windows-latest` | npm | 10,12,14,16 |
68
66
  | MacOS | `macos-latest` | npm | 10,12,14,16 |
69
67
 
70
- Using [yarn](https://yarnpkg.com/) might require passing the `--ignore-engines` flag.
68
+ Using [yarn](https://yarnpkg.com/) might require passing the `--ignore-engines`
69
+ flag.
@@ -1,8 +1,12 @@
1
1
  <h1 align="center">Fastify</h1>
2
2
 
3
3
  ## Lifecycle
4
- Following the schema of the internal lifecycle of Fastify.<br>
5
- On the right branch of every section there is the next phase of the lifecycle, on the left branch there is the corresponding error code that will be generated if the parent throws an error *(note that all the errors are automatically handled by Fastify)*.
4
+ Following the schema of the internal lifecycle of Fastify.
5
+
6
+ On the right branch of every section there is the next phase of the lifecycle,
7
+ on the left branch there is the corresponding error code that will be generated
8
+ if the parent throws an error *(note that all the errors are automatically
9
+ handled by Fastify)*.
6
10
 
7
11
  ```
8
12
  Incoming Request
@@ -36,11 +40,13 @@ Incoming Request
36
40
  └─▶ onResponse Hook
37
41
  ```
38
42
 
39
- At any point before or during the `User Handler`, `reply.hijack()` can be called to prevent Fastify from:
43
+ At any point before or during the `User Handler`, `reply.hijack()` can be called
44
+ to prevent Fastify from:
40
45
  - Running all the following hooks and user handler
41
46
  - Sending the response automatically
42
47
 
43
- NB (*): If `reply.raw` is used to send a response back to the user, `onResponse` hooks will still be executed
48
+ NB (*): If `reply.raw` is used to send a response back to the user, `onResponse`
49
+ hooks will still be executed
44
50
 
45
51
  ## Reply Lifecycle
46
52
 
@@ -51,7 +57,8 @@ Whenever the user handles the request, the result may be:
51
57
  - in sync handler: it sends a payload
52
58
  - in sync handler: it sends an `Error` instance
53
59
 
54
- If the reply was hijacked, we skip all the below steps. Otherwise, when it is being submitted, the data flow performed is the following:
60
+ If the reply was hijacked, we skip all the below steps. Otherwise, when it is
61
+ being submitted, the data flow performed is the following:
55
62
 
56
63
  ```
57
64
  ★ schema validation Error
@@ -73,6 +80,7 @@ If the reply was hijacked, we skip all the below steps. Otherwise, when it is be
73
80
 
74
81
  Note: `reply sent` means that the JSON payload will be serialized by:
75
82
 
76
- - the [reply serialized](Server.md#setreplyserializer) if set
77
- - or by the [serializer compiler](Server.md#setserializercompiler) when a JSON schema has been set for the returning HTTP status code
83
+ - the [reply serialized](./Server.md#setreplyserializer) if set
84
+ - or by the [serializer compiler](./Server.md#setserializercompiler) when a JSON
85
+ schema has been set for the returning HTTP status code
78
86
  - or by the default `JSON.stringify` function