fastify 4.0.0-rc.3 → 4.0.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 (48) hide show
  1. package/README.md +2 -1
  2. package/build/build-validation.js +14 -2
  3. package/docs/Guides/Ecosystem.md +23 -11
  4. package/docs/Guides/Index.md +1 -1
  5. package/docs/Guides/Migration-Guide-V3.md +1 -1
  6. package/docs/Guides/Plugins-Guide.md +3 -3
  7. package/docs/Guides/Prototype-Poisoning.md +1 -1
  8. package/docs/Guides/Recommendations.md +2 -2
  9. package/docs/Guides/Serverless.md +5 -5
  10. package/docs/Guides/Testing.md +3 -1
  11. package/docs/Guides/Write-Plugin.md +3 -3
  12. package/docs/Migration-Guide-V4.md +1 -1
  13. package/docs/Reference/Logging.md +10 -5
  14. package/docs/Reference/Middleware.md +3 -3
  15. package/docs/Reference/Routes.md +2 -2
  16. package/docs/Reference/Server.md +59 -10
  17. package/docs/Reference/TypeScript.md +2 -2
  18. package/docs/Reference/Validation-and-Serialization.md +11 -1
  19. package/fastify.d.ts +2 -1
  20. package/fastify.js +38 -14
  21. package/lib/configValidator.js +456 -332
  22. package/lib/errors.js +4 -0
  23. package/lib/request.js +2 -2
  24. package/lib/route.js +5 -2
  25. package/lib/schemas.js +3 -0
  26. package/lib/validation.js +8 -2
  27. package/package.json +34 -34
  28. package/test/close-pipelining.test.js +4 -2
  29. package/test/close.test.js +93 -3
  30. package/test/custom-http-server.test.js +22 -0
  31. package/test/decorator.test.js +146 -0
  32. package/test/internals/initialConfig.test.js +5 -3
  33. package/test/output-validation.test.js +6 -2
  34. package/test/plugin.test.js +177 -14
  35. package/test/route-prefix.test.js +250 -0
  36. package/test/router-options.test.js +61 -0
  37. package/test/schema-feature.test.js +24 -0
  38. package/test/schema-serialization.test.js +57 -0
  39. package/test/types/fastify.test-d.ts +1 -0
  40. package/test/types/instance.test-d.ts +1 -0
  41. package/test/types/logger.test-d.ts +13 -1
  42. package/test/types/route.test-d.ts +14 -0
  43. package/test/types/type-provider.test-d.ts +6 -0
  44. package/types/instance.d.ts +1 -0
  45. package/types/logger.d.ts +3 -1
  46. package/types/route.d.ts +1 -1
  47. package/types/schema.d.ts +1 -1
  48. package/types/type-provider.d.ts +3 -4
package/README.md CHANGED
@@ -265,6 +265,7 @@ Team members are listed in alphabetical order.
265
265
  * [__Maksim Sinik__](https://github.com/fox1t), <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
266
266
  * [__Frazer Smith__](https://github.com/Fdawgs), <https://www.npmjs.com/~fdawgs>
267
267
  * [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
268
+ * [__Rafael Gonzaga__](https://github.com/rafaelgss), <https://twitter.com/_rafaelgss>, <https://www.npmjs.com/~rafaelgss>
268
269
 
269
270
  ### Great Contributors
270
271
  Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.
@@ -291,7 +292,7 @@ This project is kindly sponsored by:
291
292
  - [nearForm](https://nearform.com)
292
293
 
293
294
  Past Sponsors:
294
- - [LetzDoIt](http://www.letzdoitapp.com/)
295
+ - [LetzDoIt](https://www.letzdoitapp.com/)
295
296
 
296
297
  ## License
297
298
 
@@ -24,7 +24,7 @@ module.exports.defaultInitOptions = ${JSON.stringify(defaultInitOptions)}
24
24
  const defaultInitOptions = {
25
25
  connectionTimeout: 0, // 0 sec
26
26
  keepAliveTimeout: 72000, // 72 seconds
27
- forceCloseConnections: false, // keep-alive connections
27
+ forceCloseConnections: undefined, // keep-alive connections
28
28
  maxRequestsPerSocket: 0, // no limit
29
29
  requestTimeout: 0, // no limit
30
30
  bodyLimit: 1024 * 1024, // 1 MiB
@@ -33,6 +33,7 @@ const defaultInitOptions = {
33
33
  disableRequestLogging: false,
34
34
  jsonShorthand: true,
35
35
  ignoreTrailingSlash: false,
36
+ ignoreDuplicateSlashes: false,
36
37
  maxParamLength: 100,
37
38
  onProtoPoisoning: 'error',
38
39
  onConstructorPoisoning: 'error',
@@ -49,7 +50,17 @@ const schema = {
49
50
  properties: {
50
51
  connectionTimeout: { type: 'integer', default: defaultInitOptions.connectionTimeout },
51
52
  keepAliveTimeout: { type: 'integer', default: defaultInitOptions.keepAliveTimeout },
52
- forceCloseConnections: { type: 'boolean', default: defaultInitOptions.forceCloseConnections },
53
+ forceCloseConnections: {
54
+ oneOf: [
55
+ {
56
+ type: 'string',
57
+ pattern: 'idle'
58
+ },
59
+ {
60
+ type: 'boolean'
61
+ }
62
+ ]
63
+ },
53
64
  maxRequestsPerSocket: { type: 'integer', default: defaultInitOptions.maxRequestsPerSocket, nullable: true },
54
65
  requestTimeout: { type: 'integer', default: defaultInitOptions.requestTimeout },
55
66
  bodyLimit: { type: 'integer', default: defaultInitOptions.bodyLimit },
@@ -76,6 +87,7 @@ const schema = {
76
87
  then: { setDefaultValue: true }
77
88
  },
78
89
  ignoreTrailingSlash: { type: 'boolean', default: defaultInitOptions.ignoreTrailingSlash },
90
+ ignoreDuplicateSlashes: { type: 'boolean', default: defaultInitOptions.ignoreDuplicateSlashes },
79
91
  disableRequestLogging: {
80
92
  type: 'boolean',
81
93
  default: false
@@ -8,20 +8,22 @@ section.
8
8
 
9
9
  #### [Core](#core)
10
10
 
11
- - [`aws-lambda-fastify`](https://github.com/fastify/aws-lambda-fastify) allows you to
12
- easily build serverless web applications/services and RESTful APIs using Fastify
13
- on top of AWS Lambda and Amazon API Gateway.
14
11
  - [`@fastify/accepts`](https://github.com/fastify/fastify-accepts) to have
15
12
  [accepts](https://www.npmjs.com/package/accepts) in your request object.
16
13
  - [`@fastify/accepts-serializer`](https://github.com/fastify/fastify-accepts-serializer)
17
14
  to serialize to output according to `Accept` header.
15
+ - [`@fastify/any-schema`](https://github.com/fastify/any-schema-you-like) Save multiple
16
+ schemas and decide which one to use to serialize the payload
18
17
  - [`@fastify/auth`](https://github.com/fastify/fastify-auth) Run multiple auth
19
18
  functions in Fastify.
20
19
  - [`@fastify/autoload`](https://github.com/fastify/fastify-autoload) Require all
21
20
  plugins in a directory.
22
- - [`fastify-awilix`](https://github.com/fastify/fastify-awilix) Dependency
21
+ - [`@fastify/awilix`](https://github.com/fastify/fastify-awilix) Dependency
23
22
  injection support for Fastify, based on
24
23
  [awilix](https://github.com/jeffijoe/awilix).
24
+ - [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) allows you to
25
+ easily build serverless web applications/services and RESTful APIs using Fastify
26
+ on top of AWS Lambda and Amazon API Gateway.
25
27
  - [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) Basic
26
28
  auth plugin for Fastify.
27
29
  - [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth) Bearer
@@ -64,6 +66,7 @@ section.
64
66
  internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
65
67
  - [`@fastify/leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to
66
68
  share a common LevelDB connection across Fastify.
69
+ - [`@fastify/middie`](https://github.com/fastify/middie) Middleware engine for Fastify.
67
70
  - [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
68
71
  MongoDB connection plugin, with which you can share the same MongoDB
69
72
  connection pool across every part of your server.
@@ -93,7 +96,7 @@ section.
93
96
  to forward the current HTTP request to another server.
94
97
  - [`@fastify/routes`](https://github.com/fastify/fastify-routes) Plugin that
95
98
  provides a `Map` of routes.
96
- - [`fastify-schedule`](https://github.com/fastify/fastify-schedule) Plugin for
99
+ - [`@fastify/schedule`](https://github.com/fastify/fastify-schedule) Plugin for
97
100
  scheduling periodic jobs, based on
98
101
  [toad-scheduler](https://github.com/kibertoad/toad-scheduler).
99
102
  - [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults for
@@ -106,15 +109,14 @@ section.
106
109
  - [`@fastify/swagger`](https://github.com/fastify/fastify-swagger) Plugin for
107
110
  serving Swagger/OpenAPI documentation for Fastify, supporting dynamic
108
111
  generation.
109
- - [`@fastify/websocket`](https://github.com/fastify/fastify-websocket) WebSocket
110
- support for Fastify. Built upon [ws](https://github.com/websockets/ws).
112
+ - [`@fastify/under-pressure`](https://github.com/fastify/under-pressure) Measure process
113
+ load with automatic handling of _"Service Unavailable"_ plugin for Fastify.
111
114
  - [`@fastify/url-data`](https://github.com/fastify/fastify-url-data) Decorate the
112
115
  `Request` object with a method to access raw URL components.
113
- - [`middie`](https://github.com/fastify/middie) Middleware engine for Fastify.
114
- - [`point-of-view`](https://github.com/fastify/point-of-view) Templates
116
+ - [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
115
117
  rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
116
- - [`under-pressure`](https://github.com/fastify/under-pressure) Measure process
117
- load with automatic handling of _"Service Unavailable"_ plugin for Fastify.
118
+ - [`@fastify/websocket`](https://github.com/fastify/fastify-websocket) WebSocket
119
+ support for Fastify. Built upon [ws](https://github.com/websockets/ws).
118
120
 
119
121
  #### [Community](#community)
120
122
 
@@ -163,6 +165,9 @@ section.
163
165
  - [`fastify-amqp`](https://github.com/RafaelGSS/fastify-amqp) Fastify AMQP
164
166
  connection plugin, to use with RabbitMQ or another connector. Just a wrapper
165
167
  to [`amqplib`](https://github.com/squaremo/amqp.node).
168
+ - [`fastify-amqp-async`](https://github.com/kffl/fastify-amqp-async) Fastify
169
+ AMQP plugin with a Promise-based API provided by
170
+ [`amqplib-as-promised`](https://github.com/twawszczak/amqplib-as-promised).
166
171
  - [`fastify-angular-universal`](https://github.com/exequiel09/fastify-angular-universal)
167
172
  Angular server-side rendering support using
168
173
  [`@angular/platform-server`](https://github.com/angular/angular/tree/master/packages/platform-server)
@@ -306,6 +311,8 @@ section.
306
311
  user scope verifier.
307
312
  - [`fastify-jwt-webapp`](https://github.com/charlesread/fastify-jwt-webapp) JWT
308
313
  authentication for Fastify-based web apps.
314
+ - [`fastify-kafkajs`](https://github.com/kffl/fastify-kafkajs) Fastify plugin
315
+ that adds support for KafkaJS - a modern Apache Kafka client library.
309
316
  - [`fastify-knexjs`](https://github.com/chapuletta/fastify-knexjs) Fastify
310
317
  plugin for support KnexJS Query Builder.
311
318
  - [`fastify-knexjs-mock`](https://github.com/chapuletta/fastify-knexjs-mock)
@@ -346,6 +353,7 @@ section.
346
353
  for handling multipart/form-data, which is primarily used for uploading files.
347
354
  - [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share
348
355
  [NATS](https://nats.io) client across Fastify.
356
+ - [`fastify-next-auth`](https://github.com/wobsoriano/fastify-next-auth) NextAuth.js plugin for Fastify.
349
357
  - [`fastify-no-additional-properties`](https://github.com/greguz/fastify-no-additional-properties)
350
358
  Add `additionalProperties: false` by default to your JSON Schemas.
351
359
  - [`fastify-no-icon`](https://github.com/jsumners/fastify-no-icon) Plugin to
@@ -521,3 +529,7 @@ section.
521
529
  Fastify.
522
530
  - [`sequelize-fastify`](https://github.com/hsynlms/sequelize-fastify) A simple
523
531
  and lightweight Sequelize plugin for Fastify.
532
+
533
+ #### [Community Tools](#community-tools)
534
+ - [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration generator by
535
+ directory structure.
@@ -10,7 +10,7 @@ This table of contents is in alphabetical order.
10
10
  + [Contributing](./Contributing.md): Details how to participate in the
11
11
  development of Fastify, and shows how to setup an environment compatible with
12
12
  the project's code style.
13
- + [`Delay Accepting Requests`](./Delay-Accepting-Requests.md): A practical guide
13
+ + [Delay Accepting Requests](./Delay-Accepting-Requests.md): A practical guide
14
14
  on how to delay serving requests to specific routes until some condition is
15
15
  met in your application. This guide focuses on solving the problem using
16
16
  [`Hooks`](../Reference/Hooks.md), [`Decorators`](../Reference/Decorators.md),
@@ -15,7 +15,7 @@ framework itself.
15
15
 
16
16
  If you use Express middleware in your application, please install and register
17
17
  the [`@fastify/express`](https://github.com/fastify/fastify-express) or
18
- [`middie`](https://github.com/fastify/middie) plugin before doing so.
18
+ [`@fastify/middie`](https://github.com/fastify/middie) plugin before doing so.
19
19
 
20
20
  **v2:**
21
21
 
@@ -458,10 +458,10 @@ console.log(new CustomError())
458
458
 
459
459
  If you want to deprecate an API, or you want to warn the user about a specific
460
460
  use case, you can use the
461
- [`fastify-warning`](https://github.com/fastify/fastify-warning) module.
461
+ [`process-warning`](https://github.com/fastify/process-warning) module.
462
462
 
463
463
  ```js
464
- const warning = require('fastify-warning')()
464
+ const warning = require('process-warning')()
465
465
  warning.create('FastifyDeprecation', 'FST_ERROR_CODE', 'message')
466
466
  warning.emit('FST_ERROR_CODE')
467
467
  ```
@@ -475,7 +475,7 @@ will add it to the [*ecosystem*](https://github.com/fastify/fastify#ecosystem)
475
475
  section of our documentation!
476
476
 
477
477
  If you want to see some real-world examples, check out:
478
- - [`point-of-view`](https://github.com/fastify/point-of-view) Templates
478
+ - [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
479
479
  rendering (*ejs, pug, handlebars, marko*) plugin support for Fastify.
480
480
  - [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
481
481
  MongoDB connection plugin, with this you can share the same MongoDB connection
@@ -359,7 +359,7 @@ address.
359
359
  We also got lucky by having full access to mitigate it at the source — didn't
360
360
  need to send emails to some unknown framework maintainer and hope for a quick
361
361
  answer. hapi's total control over all of its dependencies proved its usefulness
362
- and security again. Not using [hapi](http://hapijs.com)? [Maybe you
362
+ and security again. Not using [hapi](https://hapi.dev)? [Maybe you
363
363
  should](https://hueniverse.com/why-you-should-consider-hapi-6163689bd7c2).
364
364
 
365
365
  ### The after in happy ever-after
@@ -172,7 +172,7 @@ backend static-backend
172
172
  # with 2 primary servers distributed via round-robin
173
173
  # and one backup which is used when the first 2 are not reachable
174
174
  # This also assumes your fastify servers are listening on port 80.
175
- # more info: http://nginx.org/en/docs/http/ngx_http_upstream_module.html
175
+ # more info: https://nginx.org/en/docs/http/ngx_http_upstream_module.html
176
176
  upstream fastify_app {
177
177
  server 10.10.11.1:80;
178
178
  server 10.10.11.2:80;
@@ -257,7 +257,7 @@ server {
257
257
  # trustProxy to the address of your NGINX server so the X-Forwarded
258
258
  # fields are used by fastify.
259
259
  location / {
260
- # more info: http://nginx.org/en/docs/http/ngx_http_proxy_module.html
260
+ # more info: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
261
261
  proxy_http_version 1.1;
262
262
  proxy_cache_bypass $http_upgrade;
263
263
  proxy_set_header Upgrade $http_upgrade;
@@ -36,7 +36,7 @@ The sample provided allows you to easily build serverless web
36
36
  applications/services and RESTful APIs using Fastify on top of AWS Lambda and
37
37
  Amazon API Gateway.
38
38
 
39
- *Note: Using [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify)
39
+ *Note: Using [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify)
40
40
  is just one possible way.*
41
41
 
42
42
  ### app.js
@@ -74,7 +74,7 @@ to your port, so you can still run your Fastify function locally.
74
74
  ### lambda.js
75
75
 
76
76
  ```js
77
- const awsLambdaFastify = require('aws-lambda-fastify')
77
+ const awsLambdaFastify = require('@fastify/aws-lambda')
78
78
  const init = require('./app');
79
79
 
80
80
  const proxy = awsLambdaFastify(init())
@@ -91,14 +91,14 @@ exports.handler = proxy;
91
91
  ```
92
92
 
93
93
  We just require
94
- [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify) (make sure
95
- you install the dependency `npm i aws-lambda-fastify`) and our
94
+ [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) (make sure
95
+ you install the dependency `npm i @fastify/aws-lambda`) and our
96
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
100
100
  passed to the `proxy` function of
101
- [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify).
101
+ [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify).
102
102
 
103
103
  ### Example
104
104
 
@@ -43,7 +43,9 @@ module.exports = build
43
43
  const server = require('./app')({
44
44
  logger: {
45
45
  level: 'info',
46
- prettyPrint: true
46
+ transport: {
47
+ target: 'pino-pretty'
48
+ }
47
49
  }
48
50
  })
49
51
 
@@ -36,8 +36,8 @@ at:
36
36
  - [`@fastify/caching`](https://github.com/fastify/fastify-caching)
37
37
  - [`@fastify/compress`](https://github.com/fastify/fastify-compress)
38
38
  - [`@fastify/cookie`](https://github.com/fastify/fastify-cookie)
39
- - [`point-of-view`](https://github.com/fastify/point-of-view)
40
- - [`under-pressure`](https://github.com/fastify/under-pressure)
39
+ - [`@fastify/under-pressure`](https://github.com/fastify/under-pressure)
40
+ - [`@fastify/view`](https://github.com/fastify/point-of-view)
41
41
 
42
42
  ## License
43
43
  You can license your plugin as you prefer, we do not enforce any kind of
@@ -91,7 +91,7 @@ it to the [ecosystem](https://github.com/fastify/fastify#ecosystem) section of
91
91
  our documentation!
92
92
 
93
93
  If you want to see some real world examples, check out:
94
- - [`point-of-view`](https://github.com/fastify/point-of-view) Templates
94
+ - [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
95
95
  rendering (*ejs, pug, handlebars, marko*) plugin support for Fastify.
96
96
  - [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
97
97
  MongoDB connection plugin, with this you can share the same MongoDB connection
@@ -9,4 +9,4 @@ All v3 deprecations have been removed and they will no longer work after upgradi
9
9
 
10
10
  ### Deprecation of `app.use()`
11
11
 
12
- Starting this version of Fastify, we have deprecated the use of `app.use()`. We have decided not to support the use of middlewares. Both [`middie`](https://github.com/fastify/middie) and [`@fastify/express`](https://github.com/fastify/fastify-express) will still be there and maintained. Use Fastify's [hooks](./Reference/Hooks.md) instead.
12
+ Starting this version of Fastify, we have deprecated the use of `app.use()`. We have decided not to support the use of middlewares. Both [`@fastify/middie`](https://github.com/fastify/middie) and [`@fastify/express`](https://github.com/fastify/fastify-express) will still be there and maintained. Use Fastify's [hooks](./Reference/Hooks.md) instead.
@@ -26,13 +26,16 @@ and production environment requires bit more configuration:
26
26
  ```js
27
27
  const fastify = require('fastify')({
28
28
  logger: {
29
- prettyPrint:
29
+ transport:
30
30
  environment === 'development'
31
31
  ? {
32
- translateTime: 'HH:MM:ss Z',
33
- ignore: 'pid,hostname'
32
+ target: 'pino-pretty',
33
+ options: {
34
+ translateTime: 'HH:MM:ss Z',
35
+ ignore: 'pid,hostname'
36
+ }
34
37
  }
35
- : false
38
+ : undefined
36
39
  }
37
40
  })
38
41
  ```
@@ -119,7 +122,9 @@ below (even if it is *not recommended*):
119
122
  ```js
120
123
  const fastify = require('fastify')({
121
124
  logger: {
122
- prettyPrint: true,
125
+ transport: {
126
+ target: 'pino-pretty'
127
+ },
123
128
  serializers: {
124
129
  res (reply) {
125
130
  // The default
@@ -5,7 +5,7 @@
5
5
  Starting with Fastify v3.0.0, middleware is not supported out of the box and
6
6
  requires an external plugin such as
7
7
  [`@fastify/express`](https://github.com/fastify/fastify-express) or
8
- [`middie`](https://github.com/fastify/middie).
8
+ [`@fastify/middie`](https://github.com/fastify/middie).
9
9
 
10
10
 
11
11
  An example of registering the
@@ -22,11 +22,11 @@ fastify.use(require('ienoopen')())
22
22
  fastify.use(require('x-xss-protection')())
23
23
  ```
24
24
 
25
- You can also use [`middie`](https://github.com/fastify/middie), which provides
25
+ You can also use [`@fastify/middie`](https://github.com/fastify/middie), which provides
26
26
  support for simple Express-style middleware but with improved performance:
27
27
 
28
28
  ```js
29
- await fastify.register(require('middie'))
29
+ await fastify.register(require('@fastify/middie'))
30
30
  fastify.use(require('cors')())
31
31
  ```
32
32
 
@@ -114,7 +114,7 @@ fastify.route(options)
114
114
  * `slash`: Will register only `/prefix/`.
115
115
  * `no-slash`: Will register only `/prefix`.
116
116
 
117
- Note: this option does not override `ignoreTrailingSlashes` in [Server](./Server.md) configuration.
117
+ Note: this option does not override `ignoreTrailingSlash` in [Server](./Server.md) configuration.
118
118
 
119
119
  * `request` is defined in [Request](./Request.md).
120
120
 
@@ -540,7 +540,7 @@ You can provide a `version` key in the `constraints` option to a route.
540
540
  Versioned routes allow you to declare multiple handlers for the same HTTP route
541
541
  path, which will then be matched according to each request's `Accept-Version`
542
542
  header. The `Accept-Version` header value should follow the
543
- [semver](http://semver.org/) specification, and routes should be declared with
543
+ [semver](https://semver.org/) specification, and routes should be declared with
544
544
  exact semver versions for matching.
545
545
 
546
546
  Fastify will require a request `Accept-Version` header to be set if the route
@@ -17,6 +17,7 @@ describes the properties available in that options object.
17
17
  - [`maxRequestsPerSocket`](#maxrequestspersocket)
18
18
  - [`requestTimeout`](#requesttimeout)
19
19
  - [`ignoreTrailingSlash`](#ignoretrailingslash)
20
+ - [`ignoreDuplicateSlashes`](#ignoreduplicateslashes)
20
21
  - [`maxParamLength`](#maxparamlength)
21
22
  - [`bodyLimit`](#bodylimit)
22
23
  - [`onProtoPoisoning`](#onprotopoisoning)
@@ -59,6 +60,7 @@ describes the properties available in that options object.
59
60
  - [addHook](#addhook)
60
61
  - [prefix](#prefix)
61
62
  - [pluginName](#pluginname)
63
+ - [hasPlugin](#hasplugin)
62
64
  - [log](#log)
63
65
  - [version](#version)
64
66
  - [inject](#inject)
@@ -134,15 +136,18 @@ use. Also, when `serverFactory` option is specified, this option is ignored.
134
136
  ### `forceCloseConnections`
135
137
  <a id="forcecloseconnections"></a>
136
138
 
137
- When set to `true` requests with the header `connection: keep-alive` will be
138
- tracked by the server. Upon [`close`](#close), the server will iterate the
139
- current persistent connections and [destroy their
140
- sockets](https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketdestroyerror).
141
- This means the server will shutdown immediately instead of waiting for existing
142
- persistent connections to timeout first. Important: connections are not
143
- inspected to determine if requests have been completed.
139
+ When set to `true`, upon [`close`](#close) the server will iterate the
140
+ current persistent connections and [destroy their sockets](https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketdestroyerror).
144
141
 
145
- + Default: `false`
142
+ > Important: connections are not inspected to determine if requests have been completed.
143
+
144
+ Fastify will prefer the HTTP server's [`closeAllConnections`](https://nodejs.org/dist/latest-v18.x/docs/api/http.html#servercloseallconnections) method if supported, otherwise it will use internal connection tracking.
145
+
146
+ When set to `"idle"`, upon [`close`](#close) the server will iterate the
147
+ current persistent connections which are not sending a request or waiting for a response and destroy their sockets.
148
+ The value is supported only if the HTTP server supports the [`closeIdleConnections`](https://nodejs.org/dist/latest-v18.x/docs/api/http.html#servercloseidleconnections) method, otherwise attempting to set it will throw an exception.
149
+
150
+ + Default: `"idle"` if the HTTP server allows it, `false` otherwise
146
151
 
147
152
  ### `maxRequestsPerSocket`
148
153
  <a id="factory-max-requests-per-socket"></a>
@@ -203,6 +208,29 @@ fastify.get('/bar', function (req, reply) {
203
208
  })
204
209
  ```
205
210
 
211
+ ### `ignoreDuplicateSlashes`
212
+ <a id="factory-ignore-duplicate-slashes"></a>
213
+
214
+ Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) to handle
215
+ routing. You can use `ignoreDuplicateSlashes` option to remove duplicate slashes
216
+ from the path. It removes duplicate slashes in the route path and in the request
217
+ URL. This option applies to *all* route registrations for the resulting server instance.
218
+
219
+ Note that when `ignoreTrailingSlash` and `ignoreDuplicateSlashes` are both set to true, Fastify will remove duplicate slashes, and then trailing slashes, meaning //a//b//c// will be converted to /a/b/c.
220
+
221
+ + Default: `false`
222
+
223
+ ```js
224
+ const fastify = require('fastify')({
225
+ ignoreDuplicateSlashes: true
226
+ })
227
+
228
+ // registers "/foo/bar/"
229
+ fastify.get('///foo//bar//', function (req, reply) {
230
+ reply.send('foo')
231
+ })
232
+ ```
233
+
206
234
  ### `maxParamLength`
207
235
  <a id="factory-max-param-length"></a>
208
236
 
@@ -1061,7 +1089,8 @@ fastify.register(function (instance, opts, done) {
1061
1089
  #### pluginName
1062
1090
  <a id="pluginName"></a>
1063
1091
 
1064
- Name of the current plugin. There are three ways to define a name (in order).
1092
+ Name of the current plugin. The root plugin is called `'fastify'`.
1093
+ There are three ways to define a name (in order).
1065
1094
 
1066
1095
  1. If you use [fastify-plugin](https://github.com/fastify/fastify-plugin) the
1067
1096
  metadata `name` is used.
@@ -1078,7 +1107,27 @@ Important: If you have to deal with nested plugins, the name differs with the
1078
1107
  usage of the [fastify-plugin](https://github.com/fastify/fastify-plugin) because
1079
1108
  no new scope is created and therefore we have no place to attach contextual
1080
1109
  data. In that case, the plugin name will represent the boot order of all
1081
- involved plugins in the format of `plugin-A -> plugin-B`.
1110
+ involved plugins in the format of `fastify -> plugin-A -> plugin-B`.
1111
+
1112
+ #### hasPlugin
1113
+ <a id="hasPlugin"></a>
1114
+
1115
+ Method to check if a specific plugin has been registered.
1116
+ Relies on the plugin metadata name.
1117
+ Returns `true` if the plugin is registered.
1118
+ Otherwise, returns `false`.
1119
+
1120
+ ```js
1121
+ const fastify = require('fastify')()
1122
+ fastify.register(require('@fastify/cookie'), {
1123
+ secret: 'my-secret',
1124
+ parseOptions: {}
1125
+ })
1126
+
1127
+ fastify.ready(() => {
1128
+ fastify.hasPlugin('@fastify/cookie') // true
1129
+ })
1130
+ ```
1082
1131
 
1083
1132
  #### log
1084
1133
  <a id="log"></a>
@@ -56,7 +56,7 @@ in a blank http Fastify server.
56
56
  npx tsc --init
57
57
  ```
58
58
  or use one of the [recommended
59
- ones](https://github.com/tsconfig/bases#node-10-tsconfigjson).
59
+ ones](https://github.com/tsconfig/bases#node-14-tsconfigjson).
60
60
 
61
61
  *Note: Set `target` property in `tsconfig.json` to `es2017` or greater to avoid
62
62
  [FastifyDeprecation](https://github.com/fastify/fastify/issues/3284) warning.*
@@ -1284,7 +1284,7 @@ handlers respectfully.
1284
1284
 
1285
1285
  [src](https://github.com/fastify/fastify/blob/main/types/route.d.ts#L78)
1286
1286
 
1287
- An interface than extends RouteShorthandOptions and adds the follow three
1287
+ An interface that extends RouteShorthandOptions and adds the following three
1288
1288
  required properties:
1289
1289
  1. `method` which corresponds to a singular [HTTPMethod][HTTPMethods] or a list
1290
1290
  of [HTTPMethods][HTTPMethods]
@@ -567,10 +567,20 @@ fastify.post('/the/url', { schema }, handler)
567
567
  ```
568
568
 
569
569
  As you can see, the response schema is based on the status code. If you want to
570
- use the same schema for multiple status codes, you can use `'2xx'`, for example:
570
+ use the same schema for multiple status codes, you can use `'2xx'` or `default`,
571
+ for example:
571
572
  ```js
572
573
  const schema = {
573
574
  response: {
575
+ default: {
576
+ type: 'object',
577
+ properties: {
578
+ error: {
579
+ type: 'boolean',
580
+ default: true
581
+ }
582
+ }
583
+ },
574
584
  '2xx': {
575
585
  type: 'object',
576
586
  properties: {
package/fastify.d.ts CHANGED
@@ -106,10 +106,11 @@ export type FastifyServerOptions<
106
106
  Logger extends FastifyBaseLogger = FastifyLoggerInstance
107
107
  > = {
108
108
  ignoreTrailingSlash?: boolean,
109
+ ignoreDuplicateSlashes?: boolean,
109
110
  connectionTimeout?: number,
110
111
  keepAliveTimeout?: number,
111
112
  maxRequestsPerSocket?: number,
112
- forceCloseConnections?: boolean,
113
+ forceCloseConnections?: boolean | 'idle',
113
114
  requestTimeout?: number,
114
115
  pluginTimeout?: number,
115
116
  bodyLimit?: number,