fastify 3.25.0 → 3.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -156,7 +156,7 @@ In a similar way, all Fastify **v2.x** related changes should be based on [**`br
156
156
 
157
157
  > ## Note
158
158
  > `.listen` binds to the local host, `localhost`, interface by default (`127.0.0.1` or `::1`, depending on the operating system configuration). If you are running Fastify in a container (Docker, [GCP](https://cloud.google.com/), etc.), you may need to bind to `0.0.0.0`. Be careful when deciding to listen on all interfaces; it comes with inherent [security risks](https://web.archive.org/web/20170711105010/https://snyk.io/blog/mongodb-hack-and-secure-defaults/).
159
- > See [the documentation](./docs/Server.md#listen) for more information.
159
+ > See [the documentation](./docs/Reference/Server.md#listen) for more information.
160
160
 
161
161
  ### Core features
162
162
 
@@ -192,36 +192,36 @@ matters to you.
192
192
  * <a href="./docs/Guides/Getting-Started.md"><code><b>Getting Started</b></code></a>
193
193
  * <a href="./docs/Guides/Index.md"><code><b>Guides</b></code></a>
194
194
  * <a href="./docs/Reference/Server.md"><code><b>Server</b></code></a>
195
- * <a href="./docs/Routes.md"><code><b>Routes</b></code></a>
195
+ * <a href="./docs/Reference/Routes.md"><code><b>Routes</b></code></a>
196
196
  * <a href="./docs/Reference/Encapsulation.md"><code><b>Encapsulation</b></code></a>
197
- * <a href="./docs/Logging.md"><code><b>Logging</b></code></a>
198
- * <a href="./docs/Middleware.md"><code><b>Middleware</b></code></a>
199
- * <a href="./docs/Hooks.md"><code><b>Hooks</b></code></a>
200
- * <a href="./docs/Decorators.md"><code><b>Decorators</b></code></a>
201
- * <a href="./docs/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
202
- * <a href="./docs/Fluent-Schema.md"><code><b>Fluent Schema</b></code></a>
203
- * <a href="./docs/Lifecycle.md"><code><b>Lifecycle</b></code></a>
204
- * <a href="./docs/Reply.md"><code><b>Reply</b></code></a>
205
- * <a href="./docs/Request.md"><code><b>Request</b></code></a>
206
- * <a href="./docs/Errors.md"><code><b>Errors</b></code></a>
207
- * <a href="./docs/ContentTypeParser.md"><code><b>Content Type Parser</b></code></a>
208
- * <a href="./docs/Plugins.md"><code><b>Plugins</b></code></a>
209
- * <a href="./docs/Testing.md"><code><b>Testing</b></code></a>
210
- * <a href="./docs/Benchmarking.md"><code><b>Benchmarking</b></code></a>
211
- * <a href="./docs/Write-Plugin.md"><code><b>How to write a good plugin</b></code></a>
212
- * <a href="./docs/Plugins-Guide.md"><code><b>Plugins Guide</b></code></a>
213
- * <a href="./docs/HTTP2.md"><code><b>HTTP2</b></code></a>
214
- * <a href="./docs/LTS.md"><code><b>Long Term Support</b></code></a>
215
- * <a href="./docs/TypeScript.md"><code><b>TypeScript and types support</b></code></a>
216
- * <a href="./docs/Serverless.md"><code><b>Serverless</b></code></a>
217
- * <a href="./docs/Recommendations.md"><code><b>Recommendations</b></code></a>
197
+ * <a href="./docs/Reference/Logging.md"><code><b>Logging</b></code></a>
198
+ * <a href="./docs/Reference/Middleware.md"><code><b>Middleware</b></code></a>
199
+ * <a href="./docs/Reference/Hooks.md"><code><b>Hooks</b></code></a>
200
+ * <a href="./docs/Reference/Decorators.md"><code><b>Decorators</b></code></a>
201
+ * <a href="./docs/Reference/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
202
+ * <a href="./docs/Guides/Fluent-Schema.md"><code><b>Fluent Schema</b></code></a>
203
+ * <a href="./docs/Reference/Lifecycle.md"><code><b>Lifecycle</b></code></a>
204
+ * <a href="./docs/Reference/Reply.md"><code><b>Reply</b></code></a>
205
+ * <a href="./docs/Reference/Request.md"><code><b>Request</b></code></a>
206
+ * <a href="./docs/Reference/Errors.md"><code><b>Errors</b></code></a>
207
+ * <a href="./docs/Reference/ContentTypeParser.md"><code><b>Content Type Parser</b></code></a>
208
+ * <a href="./docs/Reference/Plugins.md"><code><b>Plugins</b></code></a>
209
+ * <a href="./docs/Guides/Testing.md"><code><b>Testing</b></code></a>
210
+ * <a href="./docs/Guides/Benchmarking.md"><code><b>Benchmarking</b></code></a>
211
+ * <a href="./docs/Guides/Write-Plugin.md"><code><b>How to write a good plugin</b></code></a>
212
+ * <a href="./docs/Guides/Plugins-Guide.md"><code><b>Plugins Guide</b></code></a>
213
+ * <a href="./docs/Reference/HTTP2.md"><code><b>HTTP2</b></code></a>
214
+ * <a href="./docs/Reference/LTS.md"><code><b>Long Term Support</b></code></a>
215
+ * <a href="./docs/Reference/TypeScript.md"><code><b>TypeScript and types support</b></code></a>
216
+ * <a href="./docs/Guides/Serverless.md"><code><b>Serverless</b></code></a>
217
+ * <a href="./docs/Guides/Recommendations.md"><code><b>Recommendations</b></code></a>
218
218
 
219
219
  中文文档[地址](https://github.com/fastify/docs-chinese/blob/HEAD/README.md)
220
220
 
221
221
  ## Ecosystem
222
222
 
223
- - [Core](./docs/Ecosystem.md#core) - Core plugins maintained by the _Fastify_ [team](#team).
224
- - [Community](./docs/Ecosystem.md#community) - Community supported plugins.
223
+ - [Core](./docs/Guides/Ecosystem.md#core) - Core plugins maintained by the _Fastify_ [team](#team).
224
+ - [Community](./docs/Guides/Ecosystem.md#community) - Community supported plugins.
225
225
  - [Live Examples](https://github.com/fastify/example) - Multirepo with a broad set of real working examples.
226
226
  - [Discord](https://discord.gg/D3FZYPy) - Join our discord server and chat with the maintainers.
227
227
 
@@ -242,6 +242,8 @@ section.
242
242
  Fastify feature flags plugin with multiple providers support (e.g. env,
243
243
  [config](https://lorenwest.github.io/node-config/),
244
244
  [unleash](https://unleash.github.io/)).
245
+ - [`fastify-file-routes`](https://github.com/spa5k/fastify-file-routes)
246
+ Get Next.js based file system routing into fastify.
245
247
  - [`fastify-file-upload`](https://github.com/huangang/fastify-file-upload)
246
248
  Fastify plugin for uploading files.
247
249
  - [`fastify-firebase`](https://github.com/now-ims/fastify-firebase) Fastify
@@ -259,6 +261,8 @@ section.
259
261
  - [`fastify-get-head`](https://github.com/MetCoder95/fastify-get-head) Small
260
262
  plugin to set a new HEAD route handler for each GET route previously
261
263
  registered in Fastify.
264
+ - [`fastify-get-only`](https://github.com/DanieleFedeli/fastify-get-only) Small
265
+ plugin used to make fastify accept only GET requests
262
266
  - [`fastify-good-sessions`](https://github.com/Phara0h/fastify-good-sessions) A
263
267
  good Fastify sessions plugin focused on speed.
264
268
  - [`fastify-google-cloud-storage`](https://github.com/carlozamagni/fastify-google-cloud-storage)
@@ -440,6 +444,7 @@ section.
440
444
  client plugin for Fastify.
441
445
  - [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) a
442
446
  Socket.io plugin for Fastify.
447
+ - [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator) Small plugin to allow you use multiple validators in one route based on each HTTP part of the request.
443
448
  - [`fastify-sse`](https://github.com/lolo32/fastify-sse) to provide Server-Sent
444
449
  Events with `reply.sse( … )` to Fastify.
445
450
  - [`fastify-sse-v2`](https://github.com/nodefactoryio/fastify-sse-v2) to provide
@@ -3,7 +3,7 @@
3
3
  ## Fluent Schema
4
4
 
5
5
  The [Validation and
6
- Serialization](/docs/Reference/Validation-and-Serialization.md) documentation
6
+ Serialization](../Reference/Validation-and-Serialization.md) documentation
7
7
  outlines all parameters accepted by Fastify to set up JSON Schema Validation to
8
8
  validate the input, and JSON Schema Serialization to optimize the output.
9
9
 
@@ -59,7 +59,7 @@ With `fluent-json-schema` you can manipulate your schemas more easily and
59
59
  programmatically and then reuse them thanks to the `addSchema()` method. You can
60
60
  refer to the schema in two different manners that are detailed in the
61
61
  [Validation and
62
- Serialization](./Validation-and-Serialization.md#adding-a-shared-schema)
62
+ Serialization](../Reference/Validation-and-Serialization.md#adding-a-shared-schema)
63
63
  documentation.
64
64
 
65
65
  Here are some usage examples:
@@ -64,7 +64,7 @@ if (require.main === module) {
64
64
 
65
65
  When executed in your lambda function we do not need to listen to a specific
66
66
  port, so we just export the wrapper function `init` in this case. The
67
- [`lambda.js`](https://www.fastify.io/docs/latest/Serverless/#lambda-js) file
67
+ [`lambda.js`](#lambdajs) file
68
68
  will use this export.
69
69
 
70
70
  When you execute your Fastify application like always, i.e. `node app.js` *(the
@@ -93,7 +93,7 @@ exports.handler = proxy;
93
93
  We just require
94
94
  [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify) (make sure
95
95
  you install the dependency `npm i --save aws-lambda-fastify`) and our
96
- [`app.js`](https://www.fastify.io/docs/latest/Serverless/#app-js) file and call
96
+ [`app.js`](#appjs) file and call
97
97
  the exported `awsLambdaFastify` function with the `app` as the only parameter.
98
98
  The resulting `proxy` function has the correct signature to be used as a lambda
99
99
  `handler` function. This way all the incoming events (API Gateway requests) are
@@ -111,7 +111,7 @@ found
111
111
  ### Considerations
112
112
 
113
113
  - API Gateway does not support streams yet, so you are not able to handle
114
- [streams](https://www.fastify.io/docs/latest/Reply/#streams).
114
+ [streams](../Reference/Reply.md#streams).
115
115
  - API Gateway has a timeout of 29 seconds, so it is important to provide a reply
116
116
  during this time.
117
117
 
@@ -70,12 +70,12 @@ markdown.
70
70
  **Example**
71
71
 
72
72
  ```
73
- To learn more about hooks, see [Fastify hooks](https://www.fastify.io/docs/latest/Hooks).
73
+ To learn more about hooks, see [Fastify hooks](https://www.fastify.io/docs/latest/Reference/Hooks/).
74
74
  ```
75
75
 
76
76
  Result:
77
77
  >To learn more about hooks, see [Fastify
78
- >hooks](https://www.fastify.io/docs/latest/Hooks/).
78
+ >hooks](https://www.fastify.io/docs/latest/Reference/Hooks/).
79
79
 
80
80
 
81
81
 
@@ -112,7 +112,7 @@ existing content type parsers. In the example below we achieve exactly the same
112
112
  as in the example above except that we do not need to specify each content type
113
113
  to delete. Just like `removeContentTypeParser`, this API supports encapsulation.
114
114
  The API is especially useful if you want to register a [catch-all content type
115
- parser](#Catch-All) that should be executed for every content type and the
115
+ parser](#catch-all) that should be executed for every content type and the
116
116
  built-in parsers should be ignored as well.
117
117
 
118
118
  ```js
@@ -72,7 +72,7 @@ topic.
72
72
  #### `decorate(name, value, [dependencies])`
73
73
  <a id="decorate"></a>
74
74
 
75
- This method is used to customize the Fastify [server](./Reference/Server.md)
75
+ This method is used to customize the Fastify [server](./Server.md)
76
76
  instance.
77
77
 
78
78
  For example, to attach a new method to the server instance:
@@ -100,8 +100,8 @@ fastify.utility()
100
100
  console.log(fastify.conf.db)
101
101
  ```
102
102
 
103
- The decorated [Fastify server](./Reference/Server.md) is bound to `this` in
104
- route [route](Routes.md) handlers:
103
+ The decorated [Fastify server](./Server.md) is bound to `this` in
104
+ route [route](./Routes.md) handlers:
105
105
 
106
106
  ```js
107
107
  fastify.decorate('db', new DbConnection())
@@ -96,7 +96,7 @@ The parser for this content type was already registered.
96
96
  The request body is larger than the provided limit.
97
97
 
98
98
  This setting can be defined in the Fastify server instance:
99
- [`bodyLimit`](./Server.md#bodyLimit)
99
+ [`bodyLimit`](./Server.md#bodylimit)
100
100
 
101
101
  #### FST_ERR_CTP_EMPTY_TYPE
102
102
  <a id="FST_ERR_CTP_EMPTY_TYPE"></a>
@@ -397,12 +397,20 @@ Triggered when `fastify.close()` is invoked to stop the server. It is useful
397
397
  when [plugins](./Plugins.md) need a "shutdown" event, for example, to close an
398
398
  open connection to a database.
399
399
 
400
- The first argument is the Fastify instance, the second one the `done` callback.
400
+ The hook function takes the Fastify instance as a first argument,
401
+ and a `done` callback for synchronous hook functions.
401
402
  ```js
403
+ // callback style
402
404
  fastify.addHook('onClose', (instance, done) => {
403
405
  // Some code
404
406
  done()
405
407
  })
408
+
409
+ // or async/await style
410
+ fastify.addHook('onClose', async (instance) => {
411
+ // Some async code
412
+ await closeDatabaseConnections()
413
+ })
406
414
  ```
407
415
 
408
416
  ### onRoute
@@ -44,7 +44,7 @@ This table of contents is in alphabetical order.
44
44
  standard set of errors Fastify generates.
45
45
  + [Hooks](./Hooks.md): Details the API by which Fastify plugins can inject
46
46
  themselves into Fastify's handling of the request lifecycle.
47
- + [HTTP2](/./HTTP2.md): Details Fastify's HTTP2 support.
47
+ + [HTTP2](./HTTP2.md): Details Fastify's HTTP2 support.
48
48
  + [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
49
49
  illustrates where [Hooks](./Hooks.md) are available for integrating with it.
50
50
  + [Logging](./Logging.md): Details Fastify's included logging and how to
@@ -12,7 +12,7 @@ feature allows us to achieve plugin *encapsulation* and *inheritance*, in this
12
12
  way we create a *direct acyclic graph* (DAG) and we will not have issues caused
13
13
  by cross dependencies.
14
14
 
15
- You already see in the [getting started](../Guides/Getting-Started.md#register)
15
+ You already see in the [getting started](../Guides/Getting-Started.md#your-first-plugin)
16
16
  section how using this API is pretty straightforward.
17
17
  ```
18
18
  fastify.register(plugin, [options])
@@ -30,7 +30,7 @@ Fastify specific options is:
30
30
 
31
31
  + [`logLevel`](./Routes.md#custom-log-level)
32
32
  + [`logSerializers`](./Routes.md#custom-log-serializer)
33
- + [`prefix`](#route-prefixing-options)
33
+ + [`prefix`](#route-prefixing-option)
34
34
 
35
35
  **Note: Those options will be ignored when used with fastify-plugin**
36
36
 
@@ -67,7 +67,7 @@ object that exposes the following functions and properties:
67
67
  from Node core.
68
68
  - `.log` - The logger instance of the incoming request.
69
69
  - `.request` - The incoming request.
70
- - `.context` - Access the [Request's context](./Request.md#Request) property.
70
+ - `.context` - Access the [Request's context](./Request.md) property.
71
71
 
72
72
  ```js
73
73
  fastify.get('/', options, function (request, reply) {
@@ -510,7 +510,7 @@ fastify.setNotFoundHandler(function (request, reply) {
510
510
  <a id="payload-type"></a>
511
511
 
512
512
  The type of the sent payload (after serialization and going through any
513
- [`onSend` hooks](./Hooks.md#the-onsend-hook)) must be one of the following
513
+ [`onSend` hooks](./Hooks.md#onsend)) must be one of the following
514
514
  types, otherwise, an error will be thrown:
515
515
 
516
516
  - `string`
@@ -82,7 +82,7 @@ fastify.route(options)
82
82
  called. Note: using an arrow function will break the binding of `this`.
83
83
  * `errorHandler(error, request, reply)`: a custom error handler for the scope of
84
84
  the request. Overrides the default error global handler, and anything set by
85
- [`setErrorHandler`](./Server.md#setErrorHandler), for requests to the route.
85
+ [`setErrorHandler`](./Server.md#seterrorhandler), for requests to the route.
86
86
  To access the default handler, you can access `instance.errorHandler`. Note
87
87
  that this will point to fastify's default `errorHandler` only if a plugin
88
88
  hasn't overridden it already.
@@ -107,7 +107,7 @@ fastify.route(options)
107
107
  * `logSerializers`: set serializers to log for this route.
108
108
  * `config`: object used to store custom configuration.
109
109
  * `version`: a [semver](https://semver.org/) compatible string that defined the
110
- version of the endpoint. [Example](#version).
110
+ version of the endpoint. [Example](#version-constraints).
111
111
  * `prefixTrailingSlash`: string used to determine how to handle passing `/` as a
112
112
  route with a prefix.
113
113
  * `both` (default): Will register both `/prefix` and `/prefix/`.
@@ -437,7 +437,7 @@ the global Fastify Logger, accessible with `fastify.log`*
437
437
 
438
438
  In some context, you may need to log a large object but it could be a waste of
439
439
  resources for some routes. In this case, you can define some
440
- [`serializer`](https://github.com/pinojs/pino/blob/master/docs/api.md#bindingsserializers-object)
440
+ [`serializer`](https://github.com/pinojs/pino/blob/master/docs/api.md#serializers-object)
441
441
  and attach them in the right context!
442
442
 
443
443
  ```js
@@ -1075,7 +1075,7 @@ used by plugins.
1075
1075
  #### inject
1076
1076
  <a id="inject"></a>
1077
1077
 
1078
- Fake HTTP injection (for testing purposes) [here](../Guides/Testing.md#inject).
1078
+ Fake HTTP injection (for testing purposes) [here](../Guides/Testing.md#benefits-of-using-fastifyinject).
1079
1079
 
1080
1080
  #### addSchema
1081
1081
  <a id="add-schema"></a>
@@ -1291,8 +1291,8 @@ handler so requests will go through the full [Fastify
1291
1291
  lifecycle](./Lifecycle.md#lifecycle).
1292
1292
 
1293
1293
  You can also register
1294
- [`preValidation`](https://www.fastify.io/docs/latest/Hooks/#route-hooks) and
1295
- [`preHandler`](https://www.fastify.io/docs/latest/Hooks/#route-hooks) hooks for
1294
+ [`preValidation`](./Hooks.md#route-hooks) and
1295
+ [`preHandler`](./Hooks.md#route-hooks) hooks for
1296
1296
  the 404 handler.
1297
1297
 
1298
1298
  _Note: The `preValidation` hook registered using this method will run for a
@@ -1402,7 +1402,7 @@ fastify.ready(() => {
1402
1402
  the `route.store` object for each displayed route. This can be an `array` of
1403
1403
  keys (e.g. `['onRequest', Symbol('key')]`), or `true` to display all properties.
1404
1404
  A shorthand option, `fastify.printRoutes({ includeHooks: true })` will include
1405
- all [hooks](https://www.fastify.io/docs/latest/Hooks/).
1405
+ all [hooks](./Hooks.md).
1406
1406
 
1407
1407
  ```js
1408
1408
  console.log(fastify.printRoutes({ includeHooks: true, includeMeta: ['metaProperty'] }))
@@ -713,7 +713,7 @@ fastify.setSchemaErrorFormatter(function (errors, dataVar) {
713
713
  ```
714
714
 
715
715
  You can also use
716
- [setErrorHandler](https://www.fastify.io/docs/latest/Server/#seterrorhandler) to
716
+ [setErrorHandler](./Server.md#seterrorhandler) to
717
717
  define a custom response for validation errors such as
718
718
 
719
719
  ```js
package/docs/index.md CHANGED
@@ -5,7 +5,7 @@ The documentation for Fastify is split into two categories:
5
5
  - [Reference documentation](./Reference/Index.md)
6
6
  - [Guides](./Guides/Index.md)
7
7
 
8
- The reference documenation utilizes a very formal style in an effort to document
8
+ The reference documentation utilizes a very formal style in an effort to document
9
9
  Fastify's API and implementation details thoroughly for the developer who needs
10
10
  such. The guides category utilizes an informal, educational, style as a means to
11
11
  introduce newcomers to core, and advanced, Fastify concepts.
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '3.25.0'
3
+ const VERSION = '3.25.1'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
package/lib/decorate.js CHANGED
@@ -68,7 +68,7 @@ function decorateFastify (name, fn, dependencies) {
68
68
 
69
69
  function checkExistence (instance, name) {
70
70
  if (name) {
71
- return name in instance || hasKey(instance, name)
71
+ return name in instance || (instance.prototype && name in instance.prototype) || hasKey(instance, name)
72
72
  }
73
73
 
74
74
  return instance in this
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "3.25.0",
3
+ "version": "3.25.1",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -184,7 +184,7 @@
184
184
  "fast-json-stringify": "^2.5.2",
185
185
  "fastify-error": "^0.3.0",
186
186
  "fastify-warning": "^0.2.0",
187
- "find-my-way": "^4.1.0",
187
+ "find-my-way": "^4.5.0",
188
188
  "flatstr": "^1.0.12",
189
189
  "light-my-request": "^4.2.0",
190
190
  "pino": "^6.13.0",
@@ -1029,13 +1029,27 @@ test('decorateRequest with dependencies', (t) => {
1029
1029
  t.plan(2)
1030
1030
  const app = Fastify()
1031
1031
 
1032
- const decorator1 = {
1033
- config: {},
1034
- app: {}
1035
- }
1036
- const decorator2 = {
1037
- stuff: {}
1032
+ const decorator1 = 'bar'
1033
+ const decorator2 = 'foo'
1034
+
1035
+ app.decorate('decorator1', decorator1)
1036
+ app.decorateRequest('decorator1', decorator1)
1037
+
1038
+ if (
1039
+ app.hasDecorator('decorator1') &&
1040
+ app.hasRequestDecorator('decorator1')
1041
+ ) {
1042
+ t.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
1043
+ t.ok(app.hasRequestDecorator('decorator2'))
1038
1044
  }
1045
+ })
1046
+
1047
+ test('decorateRequest with dependencies (functions)', (t) => {
1048
+ t.plan(2)
1049
+ const app = Fastify()
1050
+
1051
+ const decorator1 = () => 'bar'
1052
+ const decorator2 = () => 'foo'
1039
1053
 
1040
1054
  app.decorate('decorator1', decorator1)
1041
1055
  app.decorateRequest('decorator1', decorator1)
@@ -0,0 +1,91 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const test = t.test
5
+ const Fastify = require('../..')
6
+ const h2url = require('h2url')
7
+
8
+ const alpha = { res: 'alpha' }
9
+ const beta = { res: 'beta' }
10
+
11
+ const { buildCertificate } = require('../build-certificate')
12
+ t.before(buildCertificate)
13
+
14
+ test('A route supports host constraints under http2 protocol and secure connection', (t) => {
15
+ t.plan(5)
16
+
17
+ let fastify
18
+ try {
19
+ fastify = Fastify({
20
+ http2: true,
21
+ https: {
22
+ key: global.context.key,
23
+ cert: global.context.cert
24
+ }
25
+ })
26
+ t.pass('Key/cert successfully loaded')
27
+ } catch (e) {
28
+ t.fail('Key/cert loading failed', e)
29
+ }
30
+
31
+ const constrain = 'fastify.io'
32
+
33
+ fastify.route({
34
+ method: 'GET',
35
+ url: '/',
36
+ handler: function (_, reply) {
37
+ reply.code(200).send(alpha)
38
+ }
39
+ })
40
+ fastify.route({
41
+ method: 'GET',
42
+ url: '/beta',
43
+ constraints: { host: constrain },
44
+ handler: function (_, reply) {
45
+ reply.code(200).send(beta)
46
+ }
47
+ })
48
+
49
+ fastify.listen(0, err => {
50
+ t.error(err)
51
+ fastify.server.unref()
52
+
53
+ t.test('https get request - no constrain', async (t) => {
54
+ t.plan(3)
55
+
56
+ const url = `https://localhost:${fastify.server.address().port}`
57
+ const res = await h2url.concat({ url })
58
+
59
+ t.equal(res.headers[':status'], 200)
60
+ t.equal(res.headers['content-length'], '' + JSON.stringify(alpha).length)
61
+ t.same(JSON.parse(res.body), alpha)
62
+ })
63
+
64
+ t.test('https get request - constrain', async (t) => {
65
+ t.plan(3)
66
+
67
+ const url = `https://localhost:${fastify.server.address().port}/beta`
68
+ const res = await h2url.concat({
69
+ url,
70
+ headers: {
71
+ ':authority': constrain
72
+ }
73
+ })
74
+
75
+ t.equal(res.headers[':status'], 200)
76
+ t.equal(res.headers['content-length'], '' + JSON.stringify(beta).length)
77
+ t.same(JSON.parse(res.body), beta)
78
+ })
79
+
80
+ t.test('https get request - constrain - not found', async (t) => {
81
+ t.plan(1)
82
+
83
+ const url = `https://localhost:${fastify.server.address().port}/beta`
84
+ const res = await h2url.concat({
85
+ url
86
+ })
87
+
88
+ t.equal(res.headers[':status'], 404)
89
+ })
90
+ })
91
+ })
package/types/hooks.d.ts CHANGED
@@ -395,5 +395,16 @@ export interface onCloseHookHandler<
395
395
  (
396
396
  instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
397
397
  done: HookHandlerDoneFunction
398
- ): Promise<unknown> | void;
398
+ ): void;
399
+ }
400
+
401
+ export interface onCloseAsyncHookHandler<
402
+ RawServer extends RawServerBase = RawServerDefault,
403
+ RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
404
+ RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
405
+ Logger = FastifyLoggerInstance
406
+ > {
407
+ (
408
+ instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>
409
+ ): Promise<unknown>;
399
410
  }
@@ -4,7 +4,7 @@ import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, Fas
4
4
  import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
5
5
  import { FastifyLoggerInstance } from './logger'
6
6
  import { FastifyRegister } from './register'
7
- import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler, preParsingAsyncHookHandler, preValidationAsyncHookHandler, preHandlerAsyncHookHandler, preSerializationAsyncHookHandler, onSendAsyncHookHandler, onResponseAsyncHookHandler, onTimeoutAsyncHookHandler, onErrorAsyncHookHandler, onReadyAsyncHookHandler, onRequestAsyncHookHandler } from './hooks'
7
+ import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onCloseAsyncHookHandler, onReadyHookHandler, onTimeoutHookHandler, preParsingAsyncHookHandler, preValidationAsyncHookHandler, preHandlerAsyncHookHandler, preSerializationAsyncHookHandler, onSendAsyncHookHandler, onResponseAsyncHookHandler, onTimeoutAsyncHookHandler, onErrorAsyncHookHandler, onReadyAsyncHookHandler, onRequestAsyncHookHandler } from './hooks'
8
8
  import { FastifyRequest } from './request'
9
9
  import { FastifyReply } from './reply'
10
10
  import { FastifyError } from 'fastify-error'
@@ -333,6 +333,11 @@ export interface FastifyInstance<
333
333
  hook: onCloseHookHandler<RawServer, RawRequest, RawReply, Logger>
334
334
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
335
335
 
336
+ addHook(
337
+ name: 'onClose',
338
+ hook: onCloseAsyncHookHandler<RawServer, RawRequest, RawReply, Logger>
339
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
340
+
336
341
  /**
337
342
  * Set the 404 handler
338
343
  */