fastify 3.9.2 → 3.12.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 (92) hide show
  1. package/GOVERNANCE.md +1 -1
  2. package/README.md +12 -8
  3. package/SECURITY.md +3 -3
  4. package/docs/ContentTypeParser.md +1 -1
  5. package/docs/Ecosystem.md +16 -6
  6. package/docs/Encapsulation.md +5 -2
  7. package/docs/Fluent-Schema.md +4 -4
  8. package/docs/Getting-Started.md +1 -1
  9. package/docs/Hooks.md +28 -1
  10. package/docs/Lifecycle.md +8 -1
  11. package/docs/Middleware.md +5 -4
  12. package/docs/Reply.md +13 -4
  13. package/docs/Routes.md +4 -3
  14. package/docs/Server.md +78 -4
  15. package/docs/Serverless.md +23 -51
  16. package/docs/TypeScript.md +35 -18
  17. package/docs/Validation-and-Serialization.md +4 -4
  18. package/docs/Write-Plugin.md +4 -4
  19. package/examples/hooks-benchmark.js +12 -12
  20. package/examples/hooks.js +16 -16
  21. package/examples/plugin.js +2 -2
  22. package/examples/route-prefix.js +4 -4
  23. package/fastify.d.ts +16 -1
  24. package/fastify.js +33 -16
  25. package/isolate-0x426d1e0-1227-v8.log +4019 -0
  26. package/isolate-0x4d4c7e0-1988-v8.log +4081 -0
  27. package/lib/errors.js +6 -0
  28. package/lib/headRoute.js +31 -0
  29. package/lib/pluginOverride.js +5 -5
  30. package/lib/pluginUtils.js +7 -6
  31. package/lib/reply.js +14 -2
  32. package/lib/reqIdGenFactory.js +5 -0
  33. package/lib/request.js +1 -1
  34. package/lib/route.js +66 -41
  35. package/lib/schema-compilers.js +5 -3
  36. package/lib/schema-controller.js +106 -0
  37. package/lib/schemas.js +14 -24
  38. package/lib/server.js +1 -0
  39. package/lib/symbols.js +1 -3
  40. package/lib/warnings.js +2 -0
  41. package/lib/wrapThenable.js +2 -1
  42. package/package.json +25 -21
  43. package/test/404s.test.js +120 -120
  44. package/test/500s.test.js +8 -8
  45. package/test/async-await.test.js +29 -1
  46. package/test/close.test.js +8 -8
  47. package/test/context-config.test.js +52 -0
  48. package/test/custom-parser.test.js +8 -8
  49. package/test/decorator.test.js +49 -49
  50. package/test/default-route.test.js +43 -0
  51. package/test/fastify-instance.test.js +2 -2
  52. package/test/fluent-schema.test.js +3 -3
  53. package/test/handler-context.test.js +2 -2
  54. package/test/hooks-async.test.js +3 -3
  55. package/test/hooks.on-ready.test.js +12 -12
  56. package/test/hooks.test.js +75 -32
  57. package/test/http2/closing.test.js +23 -1
  58. package/test/inject.test.js +6 -6
  59. package/test/input-validation.js +2 -2
  60. package/test/internals/hookRunner.test.js +50 -50
  61. package/test/internals/reply.test.js +47 -22
  62. package/test/internals/request.test.js +3 -9
  63. package/test/internals/version.test.js +2 -2
  64. package/test/logger.test.js +30 -30
  65. package/test/middleware.test.js +4 -4
  66. package/test/plugin.helper.js +2 -2
  67. package/test/plugin.test.js +154 -99
  68. package/test/register.test.js +11 -11
  69. package/test/request-error.test.js +2 -2
  70. package/test/route-hooks.test.js +24 -24
  71. package/test/route-prefix.test.js +81 -52
  72. package/test/route.test.js +568 -0
  73. package/test/schema-feature.test.js +168 -38
  74. package/test/schema-serialization.test.js +4 -4
  75. package/test/schema-special-usage.test.js +136 -0
  76. package/test/schema-validation.test.js +7 -7
  77. package/test/skip-reply-send.test.js +315 -0
  78. package/test/stream.test.js +6 -6
  79. package/test/throw.test.js +4 -4
  80. package/test/types/instance.test-d.ts +5 -3
  81. package/test/types/plugin.test-d.ts +7 -7
  82. package/test/types/reply.test-d.ts +1 -0
  83. package/test/types/schema.test-d.ts +15 -0
  84. package/test/validation-error-handling.test.js +5 -5
  85. package/test/versioned-routes.test.js +1 -1
  86. package/types/content-type-parser.d.ts +1 -1
  87. package/types/instance.d.ts +6 -3
  88. package/types/plugin.d.ts +1 -1
  89. package/types/reply.d.ts +1 -0
  90. package/types/route.d.ts +8 -2
  91. package/types/schema.d.ts +3 -0
  92. package/test/skip-reply-send.js +0 -98
package/GOVERNANCE.md CHANGED
@@ -32,7 +32,7 @@ They are split into the following teams:
32
32
  | `@fastify/benchmarks` | Build and maintain our benchmarks suite | `benchmarks` |
33
33
  | `@fastify/docs-chinese` | Translate the Fastify documentation in Chinese | `docs-chinese` |
34
34
 
35
- Every memeber of the org is also part of `@fastify/fastify`.
35
+ Every member of the org is also part of `@fastify/fastify`.
36
36
 
37
37
  Collaborators have:
38
38
 
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  ![](https://github.com/fastify/fastify/workflows/website/badge.svg)
10
10
  [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify/badge.svg)](https://snyk.io/test/github/fastify/fastify)
11
11
  [![Coverage Status](https://coveralls.io/repos/github/fastify/fastify/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify?branch=master)
12
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
12
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
13
13
 
14
14
  </div>
15
15
 
@@ -19,7 +19,7 @@
19
19
  [![NPM downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
20
20
  [![Security Responsible
21
21
  Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md)
22
- [![Discord](https://img.shields.io/discord/725613461949906985)](https://discord.gg/D3FZYPy)
22
+ [![Discord](https://img.shields.io/discord/725613461949906985)](https://discord.gg/fastify)
23
23
 
24
24
  </div>
25
25
 
@@ -38,7 +38,7 @@ Node.js v10 LTS (10.16.0) or later.
38
38
 
39
39
  Create a folder and make it your current working directory:
40
40
 
41
- ```
41
+ ```sh
42
42
  mkdir my-app
43
43
  cd my-app
44
44
  ```
@@ -51,7 +51,7 @@ npm init fastify
51
51
 
52
52
  Install dependencies:
53
53
 
54
- ```js
54
+ ```sh
55
55
  npm install
56
56
  ```
57
57
 
@@ -76,11 +76,11 @@ which in turn uses the generate functionality of [Fastify CLI](https://github.co
76
76
  If installing in an existing project, then Fastify can be installed into the project as a dependency:
77
77
 
78
78
  Install with npm:
79
- ```
79
+ ```sh
80
80
  npm i fastify --save
81
81
  ```
82
82
  Install with yarn:
83
- ```
83
+ ```sh
84
84
  yarn add fastify
85
85
  ```
86
86
 
@@ -138,7 +138,7 @@ In a similar way, all Fastify **v2.x** related changes should be based on [**`br
138
138
 
139
139
  - **Highly performant:** as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second.
140
140
  - **Extendible:** Fastify is fully extensible via its hooks, plugins and decorators.
141
- - **Schema based:** even if it is not mandatory we recommend to use [JSON Schema](http://json-schema.org/) to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
141
+ - **Schema based:** even if it is not mandatory we recommend to use [JSON Schema](https://json-schema.org/) to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
142
142
  - **Logging:** logs are extremely important but are costly; we chose the best logger to almost remove this cost, [Pino](https://github.com/pinojs/pino)!
143
143
  - **Developer friendly:** the framework is built to be very expressive and help the developer in their daily use, without sacrificing performance and security.
144
144
 
@@ -216,10 +216,12 @@ Team members are listed in alphabetical order.
216
216
  ### Fastify Core team
217
217
  * [__Tommaso Allevi__](https://github.com/allevo), <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
218
218
  * [__Ethan Arrowood__](https://github.com/Ethan-Arrowood/), <https://twitter.com/arrowoodtech>, <https://www.npmjs.com/~ethan_arrowood>
219
+ * [__Harry Brundage__](https://github.com/airhorns/), <https://twitter.com/harrybrundage>, <https://www.npmjs.com/~airhorns>
219
220
  * [__David Mark Clements__](https://github.com/davidmarkclements), <https://twitter.com/davidmarkclem>, <https://www.npmjs.com/~davidmarkclements>
220
221
  * [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
221
222
  * [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
222
223
  * [__Dustin Deus__](https://github.com/StarpTech), <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
224
+ * [__Ayoub El Khattabi__](https://github.com/AyoubElk), <https://twitter.com/ayoubelkh>, <https://www.npmjs.com/~ayoubelk>
223
225
  * [__Denis Fäcke__](https://github.com/SerayaEryn), <https://twitter.com/serayaeryn>, <https://www.npmjs.com/~serayaeryn>
224
226
  * [__Rafael Gonzaga__](https://github.com/rafaelgss), <https://twitter.com/_rafaelgss>, <https://www.npmjs.com/~rafaelgss>
225
227
  * [__Vincent Le Goff__](https://github.com/zekth)
@@ -230,7 +232,9 @@ Team members are listed in alphabetical order.
230
232
 
231
233
  ### Fastify Plugins team
232
234
  * [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
235
+ * [__Harry Brundage__](https://github.com/airhorns/), <https://twitter.com/harrybrundage>, <https://www.npmjs.com/~airhorns>
233
236
  * [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
237
+ * [__Ayoub El Khattabi__](https://github.com/AyoubElk), <https://twitter.com/ayoubelkh>, <https://www.npmjs.com/~ayoubelk>
234
238
  * [__Vincent Le Goff__](https://github.com/zekth)
235
239
  * [__Salman Mitha__](https://github.com/salmanm), <https://www.npmjs.com/~salmanm>
236
240
  * [__Maksim Sinik__](https://github.com/fox1t), <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
@@ -258,7 +262,7 @@ We are a [Growth Project](https://github.com/openjs-foundation/cross-project-cou
258
262
  ## Acknowledgements
259
263
 
260
264
  This project is kindly sponsored by:
261
- - [nearForm](http://nearform.com)
265
+ - [nearForm](https://nearform.com)
262
266
 
263
267
  Past Sponsors:
264
268
  - [LetzDoIt](http://www.letzdoitapp.com/)
package/SECURITY.md CHANGED
@@ -1,15 +1,15 @@
1
1
  # Security Policy
2
2
 
3
- This document describes the management of vulnerabilities for the Fastify project and it's officials' plugins.
3
+ This document describes the management of vulnerabilities for the Fastify project and its official plugins.
4
4
 
5
5
 
6
6
  ## Reporting vulnerabilities
7
7
 
8
8
  Individuals who find potential vulnerabilities in Fastify are invited to complete a vulnerability report via the dedicated HackerOne tool for Node.js modules: [https://hackerone.com/nodejs-ecosystem](https://hackerone.com/nodejs-ecosystem).
9
9
 
10
- ### How to report a vulnerabiliy
10
+ ### How to report a vulnerability
11
11
 
12
- It is of the utmost importance that you read carefully [**HOW TO REPORT A VULNERABILIY**](https://github.com/nodejs/security-wg/blob/master/processes/third_party_vuln_process.md) written by the Security Working Group of Node.js.
12
+ It is of the utmost importance that you read carefully [**HOW TO REPORT A VULNERABILITY**](https://github.com/nodejs/security-wg/blob/master/processes/third_party_vuln_process.md) written by the Security Working Group of Node.js.
13
13
 
14
14
 
15
15
  ## Handling vulnerability reports
@@ -97,7 +97,7 @@ app.post('/hello', (request, reply) => {
97
97
  })
98
98
  ```
99
99
 
100
- Here is a complete example that logs incoming [json line](http://jsonlines.org/) objects:
100
+ Here is a complete example that logs incoming [json line](https://jsonlines.org/) objects:
101
101
 
102
102
  ```js
103
103
  const split2 = require('split2')
package/docs/Ecosystem.md CHANGED
@@ -9,6 +9,8 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
9
9
  - [`fastify-accepts`](https://github.com/fastify/fastify-accepts) to have [accepts](https://www.npmjs.com/package/accepts) in your request object.
10
10
  - [`fastify-accepts-serializer`](https://github.com/fastify/fastify-accepts-serializer) to serialize to output according to `Accept` header.
11
11
  - [`fastify-auth`](https://github.com/fastify/fastify-auth) Run multiple auth functions in Fastify.
12
+ - [`fastify-autoload`](https://github.com/fastify/fastify-autoload) Require all plugins in a directory.
13
+ - [`fastify-awilix`](https://github.com/fastify/fastify-awilix) Dependency injection support for fastify, based on [awilix](https://github.com/jeffijoe/awilix).
12
14
  - [`fastify-bankai`](https://github.com/fastify/fastify-bankai) [Bankai](https://github.com/yoshuawuyts/bankai) assets compiler for Fastify.
13
15
  - [`fastify-basic-auth`](https://github.com/fastify/fastify-basic-auth) Basic auth plugin for Fastify.
14
16
  - [`fastify-bearer-auth`](https://github.com/fastify/fastify-bearer-auth) Bearer auth plugin for Fastify.
@@ -39,9 +41,10 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
39
41
  - [`fastify-redis`](https://github.com/fastify/fastify-redis) Fastify Redis connection plugin, with which you can share the same Redis connection across every part of your server.
40
42
  - [`fastify-reply-from`](https://github.com/fastify/fastify-reply-from) Plugin to forward the current http request to another server.
41
43
  - [`fastify-routes`](https://github.com/fastify/fastify-routes) Plugin that provides a `Map` of routes.
44
+ - [`fastify-schedule`](https://github.com/fastify/fastify-schedule) Plugin for scheduling periodic jobs, based on [toad-scheduler](https://github.com/kibertoad/toad-scheduler).
42
45
  - [`fastify-sensible`](https://github.com/fastify/fastify-sensible) Defaults for Fastify that everyone can agree on. It adds some useful decorators such as http errors and assertions, but also more request and reply methods.
43
46
  - [`fastify-static`](https://github.com/fastify/fastify-static) Plugin for serving static files as fast as possible.
44
- - [`fastify-swagger`](https://github.com/fastify/fastify-swagger) Swagger documentation generator for Fastify.
47
+ - [`fastify-swagger`](https://github.com/fastify/fastify-swagger) Plugin for serving Swagger/OpenAPI documentation for Fastify, supporting dynamic generation.
45
48
  - [`fastify-websocket`](https://github.com/fastify/fastify-websocket) WebSocket support for Fastify. Built upon [websocket-stream](https://github.com/maxogden/websocket-stream).
46
49
  - [`fastify-url-data`](https://github.com/fastify/fastify-url-data) Decorate the `Request` object with a method to access raw URL components.
47
50
  - [`point-of-view`](https://github.com/fastify/point-of-view) Templates rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
@@ -49,6 +52,8 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
49
52
 
50
53
  #### [Community](#community)
51
54
 
55
+ - [`@dnlup/fastify-doc`](https://github.com/dnlup/fastify-doc) A plugin for sampling process metrics.
56
+ - [`@dnlup/fastify-traps`](https://github.com/dnlup/fastify-traps) A plugin to close the server gracefully on `SIGINT` and `SIGTERM` signals.
52
57
  - [`apollo-server-fastify`](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-fastify) Run an [Apollo Server](https://github.com/apollographql/apollo-server) to serve GraphQL with Fastify.
53
58
  - [`arecibo`](https://github.com/nucleode/arecibo) Fastify ping responder for Kubernetes Liveness and Readiness Probes.
54
59
  - [`cls-rtracer`](https://github.com/puzpuzpuz/cls-rtracer) Fastify middleware for CLS-based request id generation. An out-of-the-box solution for adding request ids into your logs.
@@ -61,7 +66,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
61
66
  - [`fastify-axios`](https://github.com/davidedantonio/fastify-axios) Plugin to send HTTP requests via [axios](https://github.com/axios/axios).
62
67
  - [`fastify-babel`](https://github.com/cfware/fastify-babel) Fastify plugin for development servers which require babel transformations of JavaScript sources.
63
68
  - [`fastify-blipp`](https://github.com/PavelPolyakov/fastify-blipp) Prints your routes to the console, so you definitely know which endpoints are available.
64
- - [`fastify-bookshelf`](https://github.com/butlerx/fastify-bookshelfjs) Fastify plugin to add [bookshelf.js](http://bookshelfjs.org/) orm support.
69
+ - [`fastify-bookshelf`](https://github.com/butlerx/fastify-bookshelfjs) Fastify plugin to add [bookshelf.js](https://bookshelfjs.org/) orm support.
65
70
  - [`fastify-boom`](https://github.com/jeromemacias/fastify-boom) Fastify plugin to add [boom](https://github.com/hapijs/boom) support.
66
71
  - [`fastify-casbin`](https://github.com/nearform/fastify-casbin) Casbin support for Fastify.
67
72
  - [`fastify-casbin-rest`](https://github.com/nearform/fastify-casbin-rest) Casbin support for Fastify based on a RESTful model.
@@ -71,6 +76,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
71
76
  - [`fastify-couchdb`](https://github.com/nigelhanlon/fastify-couchdb) Fastify plugin to add CouchDB support via [nano](https://github.com/apache/nano).
72
77
  - [`fastify-custom-healthcheck`](https://github.com/gkampitakis/fastify-custom-healthcheck) Fastify plugin to add health route in your server that asserts custom functions.
73
78
  - [`fastify-decorators`](https://github.com/L2jLiga/fastify-decorators) Fastify plugin that provides the set of TypeScript decorators.
79
+ - [`fastify-disablecache`](https://github.com/Fdawgs/fastify-disablecache) Fastify plugin to disable client-side caching, inspired by [nocache](https://github.com/helmetjs/nocache).
74
80
  - [`fastify-dynamodb`](https://github.com/matrus2/fastify-dynamodb) AWS DynamoDB plugin for Fastify. It exposes [AWS.DynamoDB.DocumentClient()](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) object.
75
81
  - [`fastify-dynareg`](https://github.com/greguz/fastify-dynareg) Dynamic plugin register for Fastify.
76
82
  - [`fastify-envalid`](https://github.com/alemagio/fastify-envalid) Fastify plugin to integrate [envalid](https://github.com/af/envalid) in your Fastify project.
@@ -78,7 +84,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
78
84
  - [`fastify-explorer`](https://github.com/Eomm/fastify-explorer) Get control of your decorators across all the encapsulated contexts
79
85
  - [`fastify-esso`](https://github.com/patrickpissurno/fastify-esso) The easiest authentication plugin for Fastify, with built-in support for Single sign-on (and great documentation)
80
86
  - [`fastify-favicon`](https://github.com/smartiniOnGitHub/fastify-favicon) Fastify plugin to serve default favicon.
81
- - [`fastify-feature-flags`](https://gitlab.com/m03geek/fastify-feature-flags) Fastify feature flags plugin with multiple providers support (e.g. env, [config](http://lorenwest.github.io/node-config/), [unleash](https://unleash.github.io/)).
87
+ - [`fastify-feature-flags`](https://gitlab.com/m03geek/fastify-feature-flags) Fastify feature flags plugin with multiple providers support (e.g. env, [config](https://lorenwest.github.io/node-config/), [unleash](https://unleash.github.io/)).
82
88
  - [`fastify-file-upload`](https://github.com/huangang/fastify-file-upload) Fastify plugin for uploading files.
83
89
  - [`fastify-firebase`](https://github.com/now-ims/fastify-firebase) Fastify plugin for [Firebase Admin SDK](https://firebase.google.com/docs/admin/setup) to Fastify so you can easy use Firebase Auth, Firestore, Cloud Storage, Cloud Messaging, and more.
84
90
  - [`fastify-firebase-auth`](https://github.com/oxsav/fastify-firebase-auth) Firebase Authentication for Fastify supporting all of the methods relating to the authentication API.
@@ -94,7 +100,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
94
100
  - [`fastify-http-context`](https://github.com/thorough-developer/fastify-http-context) Fastify plugin for "simulating" a thread of execution to allow for true http context to take place per api call within the fastify lifecycle of calls.
95
101
  - [`fastify-https-redirect`](https://github.com/tomsvogel/fastify-https-redirect) Fastify plugin for auto redirect from http to https
96
102
  - [`fastify-http-client`](https://github.com/kenuyx/fastify-http-client) Plugin to send HTTP(s) requests. Built upon [urllib](https://github.com/node-modules/urllib).
97
- - [`fastify-http-errors-enhanced`](https://github.com/ShogunPanda/fastify-http-errors-enhanced) A error handling plugin for Fastify that uses enhanced HTTP errors.
103
+ - [`fastify-http-errors-enhanced`](https://github.com/ShogunPanda/fastify-http-errors-enhanced) An error handling plugin for Fastify that uses enhanced HTTP errors.
98
104
  - [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify InfluxDB plugin connecting to an InfluxDB instance via the Influx default package.
99
105
  - [`fastify-jwt-authz`](https://github.com/Ethan-Arrowood/fastify-jwt-authz) JWT user scope verifier.
100
106
  - [`fastify-jwt-webapp`](https://github.com/charlesread/fastify-jwt-webapp) JWT authentication for fastify-based web apps.
@@ -107,19 +113,22 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
107
113
  - [`fastify-markdown`](https://github.com/freezestudio/fastify-markdown) Plugin to markdown support.
108
114
  - [`fastify-method-override`](https://github.com/corsicanec82/fastify-method-override) Plugin for Fastify, which allows use HTTP verbs, such as DELETE, PATCH, HEAD, PUT, OPTIONS in case the client doesn't support them.
109
115
  - [`fastify-metrics`](https://gitlab.com/m03geek/fastify-metrics) Plugin for exporting [Prometheus](https://prometheus.io) metrics.
116
+ - [`fastify-minify`](https://github.com/Jelenkee/fastify-minify) Plugin for minification and transformation of responses.
110
117
  - [`fastify-mongo-memory`](https://github.com/chapuletta/fastify-mongo-memory) Fastify MongoDB in Memory Plugin for testing support.
111
118
  - [`fastify-mongoose-api`](https://github.com/jeka-kiselyov/fastify-mongoose-api) Fastify plugin to create REST API methods based on Mongoose MongoDB models.
112
119
  - [`fastify-mongoose-driver`](https://github.com/alex-ppg/fastify-mongoose) Fastify Mongoose plugin that connects to a MongoDB via the Mongoose plugin with support for Models.
113
120
  - [`fastify-multer`](https://github.com/fox1t/fastify-multer) Multer is a plugin for handling multipart/form-data, which is primarily used for uploading files.
114
- - [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share [NATS](http://nats.io) client across Fastify.
121
+ - [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share [NATS](https://nats.io) client across Fastify.
115
122
  - [`fastify-no-additional-properties`](https://github.com/greguz/fastify-no-additional-properties) Add `additionalProperties: false` by default to your JSON Schemas.
116
123
  - [`fastify-no-icon`](https://github.com/jsumners/fastify-no-icon) Plugin to eliminate thrown errors for `/favicon.ico` requests.
117
- - [`fastify-nodemailer`](https://github.com/lependu/fastify-nodemailer) Plugin to share [nodemailer](http://nodemailer.com) transporter across Fastify.
124
+ - [`fastify-nodemailer`](https://github.com/lependu/fastify-nodemailer) Plugin to share [nodemailer](https://nodemailer.com) transporter across Fastify.
118
125
  - [`fastify-normalize-request-reply`](https://github.com/ericrglass/fastify-normalize-request-reply) Plugin to normalize the request and reply to the Express version 4.x request and response, which allows use of middleware, like swagger-stats, that was originally written for Express.
119
126
  - [`fastify-now`](https://github.com/yonathan06/fastify-now) Structure your endpoints in a folder and load them dynamically with fastify
127
+ - [`fastify-nuxtjs`](https://github.com/gomah/fastify-nuxtjs) Vue server side rendering support for Fastify with Nuxt.js Framework.
120
128
  - [`fastify-oas`](https://gitlab.com/m03geek/fastify-oas) Generates OpenAPI 3.0+ documentation from routes schemas for Fastify.
121
129
  - [`fastify-objectionjs`](https://github.com/jarcodallo/fastify-objectionjs) Plugin for the `fastify` framework that provides integration with objectionjs ORM.
122
130
  - [`fastify-objectionjs-classes`](https://github.com/kamikazechaser/fastify-objectionjs-classes) Plugin to cherry pick classes from objectionjs ORM.
131
+ - [`fastify-openapi-docs`](https://github.com/ShogunPanda/fastify-openapi-docs) A fastify plugin that generates OpenAPI spec automatically.
123
132
  - [`fastify-openapi-glue`](https://github.com/seriousme/fastify-openapi-glue) Glue for Open Api specifications in Fastify, autogenerates routes based on an Open Api Specification
124
133
  - [`fastify-opentelemetry`](https://github.com/autotelic/fastify-opentelemetry) A Fastify plugin that uses the [OpenTelemetry API](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-api) to provide request tracing.
125
134
  - [`fastify-oracle`](https://github.com/cemremengu/fastify-oracle) Attaches an [`oracledb`](https://github.com/oracle/node-oracledb) connection pool to a Fastify server instance.
@@ -128,6 +137,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
128
137
  - [`fastify-peekaboo`](https://github.com/simone-sanfratello/fastify-peekaboo) Fastify plugin for memoize responses by expressive settings.
129
138
  - [`fastify-postgraphile`](https://github.com/alemagio/fastify-postgraphile) Plugin to integrate [PostGraphile](https://www.graphile.org/postgraphile/) in a Fastify project.
130
139
  - [`fastify-prettier`](https://github.com/hsynlms/fastify-prettier) A fastify plugin that uses [prettier](https://github.com/prettier/prettier) under the hood to beautify outgoing responses and/or other things in the fastify server.
140
+ - [`fastify-print-routes`](https://github.com/ShogunPanda/fastify-print-routes) A fastify plugin that prints all available routes.
131
141
  - [`fastify-qrcode`](https://github.com/chonla/fastify-qrcode) This plugin utilizes [qrcode](https://github.com/soldair/node-qrcode) to generate QR Code.
132
142
  - [`fastify-qs`](https://github.com/webdevium/fastify-qs) A plugin for Fastify that adds support for parsing URL query parameters with [qs](https://github.com/ljharb/qs).
133
143
  - [`fastify-raw-body`](https://github.com/Eomm/fastify-raw-body) Add the `request.rawBody` field.
@@ -21,7 +21,7 @@ In the above figure, there are several entities:
21
21
  - Three _child plugins_
22
22
 
23
23
  Every _child context_ and _grandchild context_ has access to the _root plugins_.
24
- Within each _child context_ the _granchild contexts_ have access to the
24
+ Within each _child context_ the _grandchild contexts_ have access to the
25
25
  _child plugins_ registered within the containing _child context_, but the
26
26
  containing _child context_ **does not** have access to the _child plugins_
27
27
  registered within its _grandchild context_.
@@ -52,7 +52,9 @@ fastify.register(async function authenticatedContext (childServer) {
52
52
  handler (request, response) {
53
53
  response.send({
54
54
  answer: request.answer,
55
+ // request.foo will be undefined as it's only defined in publicContext
55
56
  foo: request.foo,
57
+ // request.bar will be undefined as it's only defined in grandchildContext
56
58
  bar: request.bar
57
59
  })
58
60
  }
@@ -69,6 +71,7 @@ fastify.register(async function publicContext (childServer) {
69
71
  response.send({
70
72
  answer: request.answer,
71
73
  foo: request.foo,
74
+ // request.bar will be undefined as it's only defined in grandchildContext
72
75
  bar: request.bar
73
76
  })
74
77
  }
@@ -124,7 +127,7 @@ To see this, start the server and issue requests:
124
127
 
125
128
  Notice that each context in the prior example inherits _only_ from the parent
126
129
  contexts. Parent contexts cannot access any entities within its descendent
127
- contexts. This default is occassionally not desired. In such cases, the
130
+ contexts. This default is occasionally not desired. In such cases, the
128
131
  encapsulation context can be broken through the usage of
129
132
  [fastify-plugin][fastify-plugin] such that anything registered in a descendent
130
133
  context is available to the containing parent context.
@@ -4,15 +4,15 @@
4
4
 
5
5
  The [Validation and Serialization](Validation-and-Serialization.md) documentation outlines all parameters accepted by Fastify to set up JSON Schema Validation in order to validate the input, and JSON Schema Serialization in order to optimize the output.
6
6
 
7
- [`fluent-schema`](https://github.com/fastify/fluent-schema) can be used to simplify this task while allowing the reuse of constants.
7
+ [`fluent-json-schema`](https://github.com/fastify/fluent-json-schema) can be used to simplify this task while allowing the reuse of constants.
8
8
 
9
9
  ### Basic settings
10
10
 
11
11
  ```js
12
- const S = require('fluent-schema')
12
+ const S = require('fluent-json-schema')
13
13
 
14
14
  // You can have an object like this, or query a DB to get the values
15
- const MY_KEY = {
15
+ const MY_KEYS = {
16
16
  KEY1: 'ONE',
17
17
  KEY2: 'TWO'
18
18
  }
@@ -51,7 +51,7 @@ fastify.post('/the/url', { schema }, handler)
51
51
 
52
52
  ### Reuse
53
53
 
54
- With `fluent-schema` you can manipulate your schemas in an easier and programmatic way and then reuse them
54
+ With `fluent-json-schema` you can manipulate your schemas in an easier and programmatic way and then reuse them
55
55
  thanks to the `addSchema()` method. You can refer to the schema in two different manners that are detailed
56
56
  in the [Validation-and-Serialization.md](Validation-and-Serialization.md#adding-a-shared-schema) documentation.
57
57
 
@@ -241,7 +241,7 @@ As discussed previously, Fastify offers a solid encapsulation model, to help you
241
241
  <a name="validate-data"></a>
242
242
  ### Validate your data
243
243
  Data validation is extremely important and a core concept of the framework.<br>
244
- To validate incoming requests, Fastify uses [JSON Schema](http://json-schema.org/).
244
+ To validate incoming requests, Fastify uses [JSON Schema](https://json-schema.org/).
245
245
  Let's look at an example demonstrating validation for routes:
246
246
  ```js
247
247
  const opts = {
package/docs/Hooks.md CHANGED
@@ -456,7 +456,7 @@ Warn: if you declare the function with an [arrow function](https://developer.moz
456
456
  <a name="route-hooks"></a>
457
457
 
458
458
  ## Route level hooks
459
- You can declare one or more custom [onRequest](#onrequest), [onReponse](#onresponse), [preParsing](#preparsing), [preValidation](#prevalidation), [preHandler](#prehandler) and [preSerialization](#preserialization) hook(s) that will be **unique** for the route.
459
+ 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.
460
460
  If you do so, those hooks are always executed as the last hook in their category. <br/>
461
461
  This can be useful if you need to implement authentication, where the [preParsing](#preparsing) or [preValidation](#prevalidation) hooks are exactly what you need.
462
462
  Multiple route-level hooks can also be specified as an array.
@@ -492,6 +492,21 @@ fastify.addHook('preSerialization', (request, reply, payload, done) => {
492
492
  done(null, payload)
493
493
  })
494
494
 
495
+ fastify.addHook('onSend', (request, reply, payload, done) => {
496
+ // Your code
497
+ done(null, payload)
498
+ })
499
+
500
+ fastify.addHook('onTimeout', (request, reply, done) => {
501
+ // Your code
502
+ done()
503
+ })
504
+
505
+ fastify.addHook('onError', (request, reply, error, done) => {
506
+ // Your code
507
+ done()
508
+ })
509
+
495
510
  fastify.route({
496
511
  method: 'GET',
497
512
  url: '/',
@@ -526,6 +541,18 @@ fastify.route({
526
541
  // This hook will always be executed after the shared `preSerialization` hooks
527
542
  done(null, payload)
528
543
  },
544
+ onSend: (request, reply, payload, done) => {
545
+ // This hook will always be executed after the shared `onSend` hooks
546
+ done(null, payload)
547
+ },
548
+ onTimeout: (request, reply, done) => {
549
+ // This hook will always be executed after the shared `onTimeout` hooks
550
+ done()
551
+ },
552
+ onError: (request, reply, error, done) => {
553
+ // This hook will always be executed after the shared `onError` hooks
554
+ done()
555
+ },
529
556
  handler: function (request, reply) {
530
557
  reply.send({ hello: 'world' })
531
558
  }
package/docs/Lifecycle.md CHANGED
@@ -36,6 +36,12 @@ Incoming Request
36
36
  └─▶ onResponse Hook
37
37
  ```
38
38
 
39
+ At any point before or during the `User Handler`, `reply.hijack()` can be called to prevent fastify from:
40
+ - Running all the following hooks and user handler
41
+ - Sending the response automatically
42
+
43
+ NB (*): If `reply.raw` is used to send a response back to the user, `onResponse` hooks will still be executed
44
+
39
45
  ## Reply Lifecycle
40
46
 
41
47
  Whenever the user handles the request the result may be:
@@ -45,7 +51,7 @@ Whenever the user handles the request the result may be:
45
51
  - in sync handler: it sends a payload
46
52
  - in sync handler: it sends an `Error` instance
47
53
 
48
- So, when the reply is being submitted, the data flow performed is the following:
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:
49
55
 
50
56
  ```
51
57
  ★ schema validation Error
@@ -56,6 +62,7 @@ So, when the reply is being submitted, the data flow performed is the following:
56
62
 
57
63
  │ ★ throw an Error
58
64
  ★ send or return │ │
65
+ │ │ │
59
66
  │ ▼ │
60
67
  reply sent ◀── JSON ─┴─ Error instance ──▶ setErrorHandler ◀─────┘
61
68
 
@@ -2,9 +2,10 @@
2
2
 
3
3
  ## Middleware
4
4
 
5
- From Fastify v3, middleware support does not come out of the box with the framework itself, but it's offered as an external plugin via [`fastify-express`](https://github.com/fastify/fastify-express) and [`middie`](https://github.com/fastify/middie).
5
+ Starting with Fastify v3.0.0, middleware is not supported out of the box and requires an external plugin such as [`fastify-express`](https://github.com/fastify/fastify-express) or [`middie`](https://github.com/fastify/middie).
6
6
 
7
- If you need to use an express style middleware you can use one of the two plugins above:
7
+
8
+ An example of registering the [`fastify-express`](https://github.com/fastify/fastify-express) plugin to then `use` Express middleware:
8
9
 
9
10
  ```js
10
11
  await fastify.register(require('fastify-express'))
@@ -16,7 +17,7 @@ fastify.use(require('ienoopen')())
16
17
  fastify.use(require('x-xss-protection')())
17
18
  ```
18
19
 
19
- You can also use [`middie`](https://github.com/fastify/middie), which provides support for simple Express-style middlewares but with improved performances:
20
+ You can also use [`middie`](https://github.com/fastify/middie), which provides support for simple Express-style middleware but with improved performance:
20
21
 
21
22
  ```js
22
23
  await fastify.register(require('middie'))
@@ -49,4 +50,4 @@ fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
49
50
 
50
51
  ### Alternatives
51
52
 
52
- Fastify offers some alternatives to the most commonly used middlewares, such as [`fastify-helmet`](https://github.com/fastify/fastify-helmet) in case of [`helmet`](https://github.com/helmetjs/helmet), [`fastify-cors`](https://github.com/fastify/fastify-cors) for [`cors`](https://github.com/expressjs/cors) and [`fastify-static`](https://github.com/fastify/fastify-static) for [`serve-static`](https://github.com/expressjs/serve-static).
53
+ Fastify offers some alternatives to the most commonly used middleware, such as [`fastify-helmet`](https://github.com/fastify/fastify-helmet) in case of [`helmet`](https://github.com/helmetjs/helmet), [`fastify-cors`](https://github.com/fastify/fastify-cors) for [`cors`](https://github.com/expressjs/cors) and [`fastify-static`](https://github.com/fastify/fastify-static) for [`serve-static`](https://github.com/expressjs/serve-static).
package/docs/Reply.md CHANGED
@@ -18,6 +18,7 @@
18
18
  - [.raw](#raw)
19
19
  - [.serializer(func)](#serializerfunc)
20
20
  - [.sent](#sent)
21
+ - [.hijack](#hijack)
21
22
  - [.send(data)](#senddata)
22
23
  - [Objects](#objects)
23
24
  - [Strings](#strings)
@@ -223,7 +224,7 @@ app.get('/cookie-2', (req, reply) => {
223
224
  reply.raw.end()
224
225
  })
225
226
  ```
226
- Another example of the misuse of `Reply.raw` is explained in [Reply.md#getheaders](Reply.md#getheaders).
227
+ Another example of the misuse of `Reply.raw` is explained in [Reply](Reply.md#getheaders).
227
228
 
228
229
  <a name="sent"></a>
229
230
  ### .sent
@@ -251,6 +252,14 @@ app.get('/', (req, reply) => {
251
252
 
252
253
  If the handler rejects, the error will be logged.
253
254
 
255
+ <a name="hijack"></a>
256
+ ### .hijack()
257
+ Sometimes you might need to halt the execution of the normal request lifecycle and handle sending the response manually.
258
+
259
+ To achieve this, fastify provides the method `reply.hijack()` that can be called during the request lifecycle (At any point before `reply.send()` is called), and allows you to prevent fastify from sending the response, and from running the remaining hooks (and user handler if the reply was hijacked before).
260
+
261
+ NB (*): If `reply.raw` is used to send a response back to the user, `onResponse` hooks will still be executed
262
+
254
263
  <a name="send"></a>
255
264
  ### .send(data)
256
265
  As the name suggests, `.send()` is the function that sends the payload to the end user.
@@ -351,7 +360,7 @@ fastify.get('/', {
351
360
  ```
352
361
 
353
362
  If you want to completely customize the error handling, checkout [`setErrorHandler`](Server.md#seterrorhandler) API.<br>
354
- *Note: you are responsibile for logging when customizing the error handler*
363
+ *Note: you are responsible for logging when customizing the error handler*
355
364
 
356
365
  API:
357
366
 
@@ -409,14 +418,14 @@ fastify.get('/async-await', options, async function (request, reply) {
409
418
  Rejected promises default to a `500` HTTP status code. Reject the promise, or `throw` in an `async function`, with an object that has `statusCode` (or `status`) and `message` properties to modify the reply.
410
419
 
411
420
  ```js
412
- fastify.get('/teapot', async function (request, reply) => {
421
+ fastify.get('/teapot', async function (request, reply) {
413
422
  const err = new Error()
414
423
  err.statusCode = 418
415
424
  err.message = 'short and stout'
416
425
  throw err
417
426
  })
418
427
 
419
- fastify.get('/botnet', async function (request, reply) => {
428
+ fastify.get('/botnet', async function (request, reply) {
420
429
  throw { statusCode: 418, message: 'short and stout' }
421
430
  // will return to the client the same json
422
431
  })
package/docs/Routes.md CHANGED
@@ -32,7 +32,7 @@ fastify.route(options)
32
32
  * `url`: the path of the url to match this route (alias: `path`).
33
33
  * `schema`: an object containing the schemas for the request and response.
34
34
  They need to be in
35
- [JSON Schema](http://json-schema.org/) format, check [here](Validation-and-Serialization.md) for more info.
35
+ [JSON Schema](https://json-schema.org/) format, check [here](Validation-and-Serialization.md) for more info.
36
36
 
37
37
  * `body`: validates the body of the request if it is a POST or a
38
38
  PUT.
@@ -42,6 +42,7 @@ They need to be in
42
42
  * `params`: validates the params.
43
43
  * `response`: filter and generate a schema for the response, setting a
44
44
  schema allows us to have 10-20% more throughput.
45
+ * `exposeHeadRoute`: creates a sibling `HEAD` route for any `GET` routes. Defaults to the value of [`exposeHeadRoutes`](Server.md#exposeHeadRoutes) instance option. If you want a custom `HEAD` handler without disabling this option, make sure to define it before the `GET` route.
45
46
  * `attachValidation`: attach `validationError` to request, if there is a schema validation error, instead of sending the error to the error handler.
46
47
  * `onRequest(request, reply, done)`: a [function](Hooks.md#onrequest) as soon that a request is received, it could also be an array of functions.
47
48
  * `preParsing(request, reply, done)`: a [function](Hooks.md#preparsing) called before parsing the request, it could also be an array of functions.
@@ -59,7 +60,7 @@ They need to be in
59
60
  * `logLevel`: set log level for this route. See below.
60
61
  * `logSerializers`: set serializers to log for this route.
61
62
  * `config`: object used to store custom configuration.
62
- * `version`: a [semver](http://semver.org/) compatible string that defined the version of the endpoint. [Example](Routes.md#version).
63
+ * `version`: a [semver](https://semver.org/) compatible string that defined the version of the endpoint. [Example](Routes.md#version).
63
64
  * `prefixTrailingSlash`: string used to determine how to handle passing `/` as a route with a prefix.
64
65
  * `both` (default): Will register both `/prefix` and `/prefix/`.
65
66
  * `slash`: Will register only `/prefix/`.
@@ -406,7 +407,7 @@ fastify.listen(3000)
406
407
  ### Version
407
408
 
408
409
  #### Default
409
- If needed you can provide a version option, which will allow you to declare multiple versions of the same route. The versioning should follow the [semver](http://semver.org/) specification.<br/>
410
+ If needed you can provide a version option, which will allow you to declare multiple versions of the same route. The versioning should follow the [semver](https://semver.org/) specification.<br/>
410
411
  Fastify will automatically detect the `Accept-Version` header and route the request accordingly (advanced ranges and pre-releases currently are not supported).<br/>
411
412
  *Be aware that using this feature will cause a degradation of the overall performances of the router.*
412
413