fastify 4.19.2 → 4.21.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.
Files changed (58) hide show
  1. package/.c8rc.json +8 -0
  2. package/.taprc +3 -2
  3. package/README.md +2 -1
  4. package/SECURITY.md +9 -0
  5. package/docs/Guides/Prototype-Poisoning.md +2 -2
  6. package/docs/Reference/Errors.md +39 -17
  7. package/docs/Reference/Logging.md +1 -1
  8. package/docs/Reference/Plugins.md +4 -0
  9. package/docs/Reference/Routes.md +8 -0
  10. package/docs/Reference/Server.md +230 -178
  11. package/docs/Reference/TypeScript.md +1 -1
  12. package/fastify.d.ts +3 -2
  13. package/fastify.js +36 -17
  14. package/lib/context.js +6 -0
  15. package/lib/errors.js +51 -20
  16. package/lib/fourOhFour.js +5 -9
  17. package/lib/handleRequest.js +3 -5
  18. package/lib/hooks.js +91 -25
  19. package/lib/logger.js +40 -3
  20. package/lib/reply.js +19 -13
  21. package/lib/reqIdGenFactory.js +18 -3
  22. package/lib/route.js +14 -61
  23. package/lib/schema-controller.js +2 -0
  24. package/lib/server.js +23 -8
  25. package/lib/symbols.js +1 -0
  26. package/package.json +8 -10
  27. package/test/500s.test.js +22 -0
  28. package/test/async-await.test.js +1 -1
  29. package/test/childLoggerFactory.test.js +91 -0
  30. package/test/encapsulated-child-logger-factory.test.js +69 -0
  31. package/test/fastify-instance.test.js +43 -10
  32. package/test/inject.test.js +1 -2
  33. package/test/internals/errors.test.js +843 -0
  34. package/test/internals/hookRunner.test.js +22 -8
  35. package/test/internals/initialConfig.test.js +9 -2
  36. package/test/internals/reply.test.js +82 -45
  37. package/test/internals/reqIdGenFactory.test.js +129 -0
  38. package/test/internals/request-validate.test.js +40 -1
  39. package/test/internals/request.test.js +14 -4
  40. package/test/reply-error.test.js +25 -0
  41. package/test/request-id.test.js +131 -0
  42. package/test/route.test.js +135 -0
  43. package/test/serial/logger.0.test.js +6 -1
  44. package/test/server.test.js +64 -2
  45. package/test/stream.test.js +4 -4
  46. package/test/types/errors.test-d.ts +82 -0
  47. package/test/types/fastify.test-d.ts +4 -0
  48. package/test/types/instance.test-d.ts +37 -0
  49. package/test/types/reply.test-d.ts +26 -0
  50. package/test/types/route.test-d.ts +3 -0
  51. package/test/types/type-provider.test-d.ts +56 -0
  52. package/types/errors.d.ts +29 -23
  53. package/types/instance.d.ts +33 -7
  54. package/types/logger.d.ts +25 -0
  55. package/types/reply.d.ts +8 -6
  56. package/types/route.d.ts +2 -1
  57. package/types/type-provider.d.ts +2 -1
  58. package/types/utils.d.ts +9 -0
package/.c8rc.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "exclude": [
3
+ "lib/configValidator.js",
4
+ "lib/error-serializer.js",
5
+ "build/build-error-serializer.js",
6
+ "test/*"
7
+ ]
8
+ }
package/.taprc CHANGED
@@ -1,8 +1,9 @@
1
1
  ts: false
2
2
  jsx: false
3
3
  flow: false
4
- check-coverage: true
5
- coverage: true
4
+ # the coverage is performed by c8
5
+ check-coverage: false
6
+ coverage: false
6
7
  node-arg: --allow-natives-syntax
7
8
 
8
9
  files:
package/README.md CHANGED
@@ -15,6 +15,7 @@ CI](https://github.com/fastify/fastify/workflows/package-manager-ci/badge.svg?br
15
15
  [![Web
16
16
  SIte](https://github.com/fastify/fastify/workflows/website/badge.svg?branch=main)](https://github.com/fastify/fastify/actions/workflows/website.yml)
17
17
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
18
+ [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/7585/badge)](https://bestpractices.coreinfrastructure.org/projects/7585)
18
19
 
19
20
  </div>
20
21
 
@@ -48,7 +49,7 @@ The `main` branch refers to the Fastify `v4` release. Check out the
48
49
 
49
50
 
50
51
 
51
- ### Table of Contents
52
+ ### Table of Contents
52
53
 
53
54
  - [Quick start](#quick-start)
54
55
  - [Install](#install)
package/SECURITY.md CHANGED
@@ -27,6 +27,15 @@ reported vulnerabilities:
27
27
  validity of the report. In any case, the report should follow the same process
28
28
  as outlined below of inviting the maintainers to review and accept the
29
29
  vulnerability.
30
+ * ***Do not*** attempt to show CI/CD vulnerabilities by creating new pull
31
+ requests to any of the Fastify organization's repositories. Doing so will
32
+ result in a [content report][cr] to GitHub as an unsolicited exploit.
33
+ The proper way to provide such reports is by creating a new repository,
34
+ configured in the same manner as the repository you would like to submit
35
+ a report about, and with a pull request to your own repository showing
36
+ the proof of concept.
37
+
38
+ [cr]: https://docs.github.com/en/communities/maintaining-your-safety-on-github/reporting-abuse-or-spam#reporting-an-issue-or-pull-request
30
39
 
31
40
  ### Vulnerabilities found outside this process
32
41
 
@@ -13,8 +13,8 @@ open-source software and the limitations of existing communication channels.
13
13
 
14
14
  But first, if we use a JavaScript framework to process incoming JSON data, take
15
15
  a moment to read up on [Prototype Poisoning](https://medium.com/intrinsic/javascript-prototype-poisoning-vulnerabilities-in-the-wild-7bc15347c96)
16
- in general, and the specific [technical details]
17
- (https://github.com/hapijs/hapi/issues/3916) of this issue.
16
+ in general, and the specific
17
+ [technical details](https://github.com/hapijs/hapi/issues/3916) of this issue.
18
18
  This could be a critical issue so, we might need to verify your own code first.
19
19
  It focuses on specific framework however, any solution that uses `JSON.parse()`
20
20
  to process external data is potentially at risk.
@@ -168,8 +168,6 @@ ajv.plugins option should be an array.
168
168
 
169
169
  Version constraint should be a string.
170
170
 
171
- <a name="FST_ERR_CTP_ALREADY_PRESENT"></a>
172
-
173
171
  #### FST_ERR_CTP_ALREADY_PRESENT
174
172
  <a id="FST_ERR_CTP_ALREADY_PRESENT"></a>
175
173
 
@@ -260,6 +258,11 @@ The hook name must be a string.
260
258
 
261
259
  The hook callback must be a function.
262
260
 
261
+ #### FST_ERR_HOOK_INVALID_ASYNC_HANDLER
262
+ <a id="FST_ERR_HOOK_INVALID_ASYNC_HANDLER"></a>
263
+
264
+ Async function has too many arguments. Async hooks should not use the `done` argument.
265
+
263
266
  #### FST_ERR_HOOK_NOT_SUPPORTED
264
267
  <a id="FST_ERR_HOOK_NOT_SUPPORTED"></a>
265
268
 
@@ -271,8 +274,8 @@ The hook is not supported.
271
274
  You must register a plugin for handling middlewares,
272
275
  visit [`Middleware`](./Middleware.md) for more info.
273
276
 
274
- <a name="FST_ERR_HOOK_TIMEOUT"></a>
275
277
  #### FST_ERR_HOOK_TIMEOUT
278
+ <a id="FST_ERR_HOOK_TIMEOUT"></a>
276
279
 
277
280
  A callback for a hook timed out
278
281
 
@@ -327,11 +330,21 @@ Called `reply.trailer` with an invalid header name.
327
330
 
328
331
  Called `reply.trailer` with an invalid type. Expected a function.
329
332
 
333
+ #### FST_ERR_FAILED_ERROR_SERIALIZATION
334
+ <a id="FST_ERR_FAILED_ERROR_SERIALIZATION"></a>
335
+
336
+ Failed to serialize an error.
337
+
330
338
  #### FST_ERR_MISSING_SERIALIZATION_FN
331
339
  <a id="FST_ERR_MISSING_SERIALIZATION_FN"></a>
332
340
 
333
341
  Missing serialization function.
334
342
 
343
+ #### FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN
344
+ <a id="FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN"></a>
345
+
346
+ Missing serialization function.
347
+
335
348
  #### FST_ERR_REQ_INVALID_VALIDATION_INVOCATION
336
349
  <a id="FST_ERR_REQ_INVALID_VALIDATION_INVOCATION"></a>
337
350
 
@@ -348,6 +361,11 @@ The schema provided does not have `$id` property.
348
361
 
349
362
  A schema with the same `$id` already exists.
350
363
 
364
+ #### FST_ERR_SCH_CONTENT_MISSING_SCHEMA
365
+ <a id="FST_ERR_SCH_CONTENT_MISSING_SCHEMA"></a>
366
+
367
+ A schema is missing for the corresponding content type.
368
+
351
369
  #### FST_ERR_SCH_DUPLICATE
352
370
  <a id="FST_ERR_SCH_DUPLICATE"></a>
353
371
 
@@ -384,8 +402,8 @@ Invalid initialization options.
384
402
  Cannot set forceCloseConnections to `idle` as your HTTP server
385
403
  does not support `closeIdleConnections` method.
386
404
 
387
- <a name="FST_ERR_DUPLICATED_ROUTE"></a>
388
405
  #### FST_ERR_DUPLICATED_ROUTE
406
+ <a id="FST_ERR_DUPLICATED_ROUTE"></a>
389
407
 
390
408
  The HTTP method already has a registered controller for that URL
391
409
 
@@ -394,7 +412,7 @@ The HTTP method already has a registered controller for that URL
394
412
 
395
413
  The router received an invalid url.
396
414
 
397
- ### FST_ERR_ASYNC_CONSTRAINT
415
+ #### FST_ERR_ASYNC_CONSTRAINT
398
416
  <a id="FST_ERR_ASYNC_CONSTRAINT"></a>
399
417
 
400
418
  The router received an error when using asynchronous constraints.
@@ -469,38 +487,42 @@ Fastify is already listening.
469
487
 
470
488
  Installed Fastify plugin mismatched expected version.
471
489
 
472
- <a name="FST_ERR_PLUGIN_CALLBACK_NOT_FN"></a>
473
-
474
490
  #### FST_ERR_PLUGIN_CALLBACK_NOT_FN
491
+ <a id="FST_ERR_PLUGIN_CALLBACK_NOT_FN"></a>
475
492
 
476
493
  Callback for a hook is not a function (mapped directly from `avvio`)
477
494
 
478
- <a name="FST_ERR_PLUGIN_NOT_VALID"></a>
479
-
480
495
  #### FST_ERR_PLUGIN_NOT_VALID
496
+ <a id="FST_ERR_PLUGIN_NOT_VALID"></a>
481
497
 
482
498
  Plugin must be a function or a promise.
483
499
 
484
- <a name="FST_ERR_ROOT_PLG_BOOTED"></a>
485
-
486
500
  #### FST_ERR_ROOT_PLG_BOOTED
501
+ <a id="FST_ERR_ROOT_PLG_BOOTED"></a>
487
502
 
488
503
  Root plugin has already booted (mapped directly from `avvio`)
489
504
 
490
- <a name="FST_ERR_PARENT_PLUGIN_BOOTED"></a>
491
-
492
505
  #### FST_ERR_PARENT_PLUGIN_BOOTED
506
+ <a id="FST_ERR_PARENT_PLUGIN_BOOTED"></a>
493
507
 
494
508
  Impossible to load plugin because the parent (mapped directly from `avvio`)
495
509
 
496
- <a name="FST_ERR_PLUGIN_TIMEOUT"></a>
497
-
498
510
  #### FST_ERR_PLUGIN_TIMEOUT
511
+ <a id="FST_ERR_PLUGIN_TIMEOUT"></a>
499
512
 
500
513
  Plugin did not start in time. Default timeout (in millis): `10000`
501
514
 
502
- <a name="FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE"></a>
503
-
504
515
  #### FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE
516
+ <a id="FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE"></a>
505
517
 
506
518
  The decorator is not present in the instance.
519
+
520
+ #### FST_ERR_VALIDATION
521
+ <a id="FST_ERR_VALIDATION"></a>
522
+
523
+ The Request failed the payload validation.
524
+
525
+ #### FST_ERR_LISTEN_OPTIONS_INVALID
526
+ <a id="FST_ERR_LISTEN_OPTIONS_INVALID"></a>
527
+
528
+ Invalid listen options.
@@ -196,7 +196,7 @@ app.addHook('preHandler', function (req, reply, done) {
196
196
  })
197
197
  ```
198
198
 
199
- **Note**: Care should be take to ensure serializers never throw, as an error
199
+ **Note**: Care should be taken to ensure serializers never throw, as an error
200
200
  thrown from a serializer has the potential to cause the Node process to exit.
201
201
  See the [Pino documentation](https://getpino.io/#/docs/api?id=opt-serializers)
202
202
  on serializers for more information.
@@ -145,6 +145,10 @@ await fastify.ready()
145
145
 
146
146
  await fastify.listen({ port: 3000 })
147
147
  ```
148
+ *Note: Using `await` when registering a plugin loads the plugin
149
+ and the underlying dependency tree, "finalizing" the encapsulation process.
150
+ Any mutations to the plugin after it and its dependencies have been
151
+ loaded will not be reflected in the parent instance.*
148
152
 
149
153
  #### ESM support
150
154
  <a id="esm-support"></a>
@@ -90,6 +90,14 @@ fastify.route(options)
90
90
  To access the default handler, you can access `instance.errorHandler`. Note
91
91
  that this will point to fastify's default `errorHandler` only if a plugin
92
92
  hasn't overridden it already.
93
+ * `childLoggerFactory(logger, binding, opts, rawReq)`: a custom factory function
94
+ that will be called to produce a child logger instance for every request.
95
+ See [`childLoggerFactory`](./Server.md#childloggerfactory) for more info.
96
+ Overrides the default logger factory, and anything set by
97
+ [`setChildLoggerFactory`](./Server.md#setchildloggerfactory), for requests to
98
+ the route. To access the default factory, you can access
99
+ `instance.childLoggerFactory`. Note that this will point to Fastify's default
100
+ `childLoggerFactory` only if a plugin hasn't overridden it already.
93
101
  * `validatorCompiler({ schema, method, url, httpPart })`: function that builds
94
102
  schemas for request validations. See the [Validation and
95
103
  Serialization](./Validation-and-Serialization.md#schema-validator)