fastify 4.0.0-alpha.3 → 4.0.0-rc.3

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 (90) hide show
  1. package/README.md +10 -11
  2. package/build/build-validation.js +2 -0
  3. package/docs/Guides/Benchmarking.md +1 -1
  4. package/docs/Guides/Database.md +17 -17
  5. package/docs/Guides/Delay-Accepting-Requests.md +597 -0
  6. package/docs/Guides/Ecosystem.md +41 -41
  7. package/docs/Guides/Getting-Started.md +56 -10
  8. package/docs/Guides/Index.md +6 -1
  9. package/docs/Guides/Migration-Guide-V3.md +6 -6
  10. package/docs/Guides/Plugins-Guide.md +8 -8
  11. package/docs/Guides/Recommendations.md +3 -3
  12. package/docs/Guides/Serverless.md +6 -6
  13. package/docs/Guides/Style-Guide.md +1 -1
  14. package/docs/Guides/Testing.md +1 -1
  15. package/docs/Guides/Write-Plugin.md +10 -10
  16. package/docs/Migration-Guide-V4.md +1 -1
  17. package/docs/Reference/ContentTypeParser.md +4 -4
  18. package/docs/Reference/Encapsulation.md +4 -4
  19. package/docs/Reference/Errors.md +0 -7
  20. package/docs/Reference/Hooks.md +4 -4
  21. package/docs/Reference/LTS.md +6 -6
  22. package/docs/Reference/Middleware.md +7 -7
  23. package/docs/Reference/Plugins.md +4 -5
  24. package/docs/Reference/Reply.md +6 -6
  25. package/docs/Reference/Routes.md +46 -25
  26. package/docs/Reference/Server.md +74 -15
  27. package/docs/{Type-Providers.md → Reference/Type-Providers.md} +5 -5
  28. package/docs/Reference/TypeScript.md +13 -3
  29. package/docs/Reference/Validation-and-Serialization.md +8 -9
  30. package/fastify.d.ts +3 -3
  31. package/fastify.js +19 -10
  32. package/lib/configValidator.js +216 -187
  33. package/lib/contentTypeParser.js +1 -1
  34. package/lib/errors.js +1 -1
  35. package/lib/fourOhFour.js +16 -17
  36. package/lib/hooks.js +4 -0
  37. package/lib/pluginUtils.js +2 -1
  38. package/lib/reply.js +10 -6
  39. package/lib/route.js +33 -7
  40. package/lib/server.js +52 -1
  41. package/package.json +6 -6
  42. package/test/404s.test.js +19 -4
  43. package/test/allowUnsafeRegex.test.js +117 -0
  44. package/test/async-await.test.js +3 -3
  45. package/test/bodyLimit.test.js +1 -1
  46. package/test/constrained-routes.test.js +262 -4
  47. package/test/content-parser.test.js +21 -1
  48. package/test/custom-parser.test.js +3 -3
  49. package/test/decorator.test.js +9 -9
  50. package/test/delete.test.js +1 -1
  51. package/test/get.test.js +1 -1
  52. package/test/head.test.js +1 -1
  53. package/test/helper.js +1 -1
  54. package/test/hooks-async.test.js +1 -1
  55. package/test/hooks.test.js +76 -17
  56. package/test/http2/closing.test.js +2 -2
  57. package/test/http2/constraint.test.js +1 -1
  58. package/test/http2/head.test.js +1 -1
  59. package/test/http2/plain.test.js +1 -1
  60. package/test/http2/secure-with-fallback.test.js +1 -1
  61. package/test/http2/secure.test.js +1 -1
  62. package/test/http2/unknown-http-method.test.js +1 -1
  63. package/test/https/https.test.js +1 -1
  64. package/test/input-validation.js +1 -1
  65. package/test/internals/handleRequest.test.js +6 -3
  66. package/test/internals/initialConfig.test.js +10 -10
  67. package/test/internals/server.test.js +5 -5
  68. package/test/listen.test.js +11 -2
  69. package/test/logger.test.js +19 -12
  70. package/test/nullable-validation.test.js +3 -3
  71. package/test/output-validation.test.js +1 -1
  72. package/test/plugin.test.js +30 -6
  73. package/test/promises.test.js +1 -1
  74. package/test/register.test.js +1 -1
  75. package/test/reply-trailers.test.js +14 -7
  76. package/test/request-error.test.js +1 -1
  77. package/test/route.test.js +2 -2
  78. package/test/router-options.test.js +1 -1
  79. package/test/stream.test.js +13 -14
  80. package/test/types/hooks.test-d.ts +1 -1
  81. package/test/types/instance.test-d.ts +27 -0
  82. package/test/types/plugin.test-d.ts +1 -1
  83. package/test/types/request.test-d.ts +2 -1
  84. package/test/types/route.test-d.ts +1 -1
  85. package/test/unsupported-httpversion.test.js +57 -0
  86. package/test/versioned-routes.test.js +7 -11
  87. package/types/hooks.d.ts +1 -1
  88. package/types/instance.d.ts +11 -4
  89. package/types/logger.d.ts +1 -1
  90. package/types/route.d.ts +1 -1
package/README.md CHANGED
@@ -8,7 +8,6 @@
8
8
  [![CI](https://github.com/fastify/fastify/workflows/ci/badge.svg)](https://github.com/fastify/fastify/actions/workflows/ci.yml)
9
9
  [![Package Manager CI](https://github.com/fastify/fastify/workflows/package-manager-ci/badge.svg)](https://github.com/fastify/fastify/actions/workflows/package-manager-ci.yml)
10
10
  [![Web SIte](https://github.com/fastify/fastify/workflows/website/badge.svg)](https://github.com/fastify/fastify/actions/workflows/website.yml)
11
- [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify/badge.svg)](https://snyk.io/test/github/fastify/fastify)
12
11
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
13
12
 
14
13
  </div>
@@ -18,7 +17,7 @@
18
17
  [![NPM version](https://img.shields.io/npm/v/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
19
18
  [![NPM downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
20
19
  [![Security Responsible
21
- Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/nodejs/security-wg/blob/HEAD/processes/responsible_disclosure_template.md)
20
+ Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/fastify/fastify/blob/main/SECURITY.md)
22
21
  [![Discord](https://img.shields.io/discord/725613461949906985)](https://discord.gg/fastify)
23
22
 
24
23
  </div>
@@ -63,7 +62,7 @@ npm init fastify
63
62
  Install dependencies:
64
63
 
65
64
  ```sh
66
- npm install
65
+ npm i
67
66
  ```
68
67
 
69
68
  To start the app in dev mode:
@@ -88,7 +87,7 @@ If installing in an existing project, then Fastify can be installed into the pro
88
87
 
89
88
  Install with npm:
90
89
  ```sh
91
- npm i fastify@next --save
90
+ npm i fastify@next
92
91
  ```
93
92
  Install with yarn:
94
93
  ```sh
@@ -174,13 +173,13 @@ __Method:__: `autocannon -c 100 -d 40 -p 10 localhost:3000` * 2, taking the seco
174
173
 
175
174
  | Framework | Version | Router? | Requests/sec |
176
175
  | :----------------- | :------------------------- | :----------: | ------------: |
177
- | Express | 4.17.1 | &#10003; | 15,978 |
178
- | hapi | 19.1.0 | &#10003; | 45,815 |
179
- | Restify | 8.5.1 | &#10003; | 49,279 |
180
- | Koa | 2.13.0 | &#10007; | 54,848 |
181
- | **Fastify** | **3.0.0** | **&#10003;** | **78,956** |
176
+ | Express | 4.17.3 | &#10003; | 14,200 |
177
+ | hapi | 20.2.1 | &#10003; | 42,284 |
178
+ | Restify | 8.6.1 | &#10003; | 50,363 |
179
+ | Koa | 2.13.0 | &#10007; | 54,272 |
180
+ | **Fastify** | **4.0.0** | **&#10003;** | **77,193** |
182
181
  | - | | | |
183
- | `http.Server` | 12.18.2 | &#10007; | 70,380 |
182
+ | `http.Server` | 16.14.2 | &#10007; | 74,513 |
184
183
 
185
184
  Benchmarks taken using https://github.com/fastify/benchmarks. This is a
186
185
  synthetic, "hello world" benchmark that aims to evaluate the framework
@@ -282,7 +281,7 @@ Great contributors on a specific area in the Fastify ecosystem will be invited t
282
281
 
283
282
  ## Hosted by
284
283
 
285
- [<img src="https://github.com/openjs-foundation/cross-project-council/blob/HEAD/logos/openjsf-color.png?raw=true" width="250px;"/>](https://openjsf.org/projects/#growth)
284
+ [<img src="https://github.com/openjs-foundation/artwork/blob/main/openjs_foundation/openjs_foundation-logo-horizontal-color.png?raw=true" width="250px;"/>](https://openjsf.org/projects/#growth)
286
285
 
287
286
  We are a [Growth Project](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/PROJECT_PROGRESSION.md#growth-stage) in the [OpenJS Foundation](https://openjsf.org/).
288
287
 
@@ -29,6 +29,7 @@ const defaultInitOptions = {
29
29
  requestTimeout: 0, // no limit
30
30
  bodyLimit: 1024 * 1024, // 1 MiB
31
31
  caseSensitive: true,
32
+ allowUnsafeRegex: false,
32
33
  disableRequestLogging: false,
33
34
  jsonShorthand: true,
34
35
  ignoreTrailingSlash: false,
@@ -53,6 +54,7 @@ const schema = {
53
54
  requestTimeout: { type: 'integer', default: defaultInitOptions.requestTimeout },
54
55
  bodyLimit: { type: 'integer', default: defaultInitOptions.bodyLimit },
55
56
  caseSensitive: { type: 'boolean', default: defaultInitOptions.caseSensitive },
57
+ allowUnsafeRegex: { type: 'boolean', default: defaultInitOptions.allowUnsafeRegex },
56
58
  http2: { type: 'boolean' },
57
59
  https: {
58
60
  if: {
@@ -15,7 +15,7 @@ The modules we will use:
15
15
  - [Concurrently](https://github.com/kimmobrunfeldt/concurrently): Run commands
16
16
  concurrently.
17
17
  - [Npx](https://github.com/npm/npx): NPM package runner used to run scripts
18
- against different Node.js Versions and to execute local binaries. Shipped with
18
+ against different Node.js Versions and execute local binaries. Shipped with
19
19
  npm@5.2.0.
20
20
 
21
21
  ## Simple
@@ -17,14 +17,14 @@ plugins maintained within the Fastify organization.
17
17
 
18
18
  ### [MySQL](https://github.com/fastify/fastify-mysql)
19
19
 
20
- Install the plugin by running `npm i fastify-mysql --save`.
20
+ Install the plugin by running `npm i @fastify/mysql`.
21
21
 
22
22
  *Usage:*
23
23
 
24
24
  ```javascript
25
25
  const fastify = require('fastify')()
26
26
 
27
- fastify.register(require('fastify-mysql'), {
27
+ fastify.register(require('@fastify/mysql'), {
28
28
  connectionString: 'mysql://root@localhost/mysql'
29
29
  })
30
30
 
@@ -44,14 +44,14 @@ fastify.listen(3000, err => {
44
44
  ```
45
45
 
46
46
  ### [Postgres](https://github.com/fastify/fastify-postgres)
47
- Install the plugin by running `npm i pg fastify-postgres --save`.
47
+ Install the plugin by running `npm i pg @fastify/postgres`.
48
48
 
49
49
  *Example*:
50
50
 
51
51
  ```javascript
52
52
  const fastify = require('fastify')()
53
53
 
54
- fastify.register(require('fastify-postgres'), {
54
+ fastify.register(require('@fastify/postgres'), {
55
55
  connectionString: 'postgres://postgres@localhost/postgres'
56
56
  })
57
57
 
@@ -71,7 +71,7 @@ fastify.listen(3000, err => {
71
71
  ```
72
72
 
73
73
  ### [Redis](https://github.com/fastify/fastify-redis)
74
- Install the plugin by running `npm i fastify-redis --save`
74
+ Install the plugin by running `npm i @fastify/redis`
75
75
 
76
76
  *Usage:*
77
77
 
@@ -80,9 +80,9 @@ Install the plugin by running `npm i fastify-redis --save`
80
80
 
81
81
  const fastify = require('fastify')()
82
82
 
83
- fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
83
+ fastify.register(require('@fastify/redis'), { host: '127.0.0.1' })
84
84
  // or
85
- fastify.register(require('fastify-redis'), { url: 'redis://127.0.0.1', /* other redis options */ })
85
+ fastify.register(require('@fastify/redis'), { url: 'redis://127.0.0.1', /* other redis options */ })
86
86
 
87
87
  fastify.get('/foo', function (req, reply) {
88
88
  const { redis } = fastify
@@ -104,25 +104,25 @@ fastify.listen(3000, err => {
104
104
  })
105
105
  ```
106
106
 
107
- By default `fastify-redis` doesn't close
107
+ By default `@fastify/redis` doesn't close
108
108
  the client connection when Fastify server shuts down.
109
109
  To opt-in to this behavior, register the client like so:
110
110
 
111
111
  ```javascript
112
- fastify.register(require('fastify-redis'), {
112
+ fastify.register(require('@fastify/redis'), {
113
113
  client: redis,
114
114
  closeClient: true
115
115
  })
116
116
  ```
117
117
 
118
118
  ### [Mongo](https://github.com/fastify/fastify-mongodb)
119
- Install the plugin by running `npm i fastify-mongodb --save`
119
+ Install the plugin by running `npm i @fastify/mongodb`
120
120
 
121
121
  *Usage:*
122
122
  ```javascript
123
123
  const fastify = require('fastify')()
124
124
 
125
- fastify.register(require('fastify-mongodb'), {
125
+ fastify.register(require('@fastify/mongodb'), {
126
126
  // force to close the mongodb connection when app stopped
127
127
  // the default value is false
128
128
  forceClose: true,
@@ -151,14 +151,14 @@ fastify.listen(3000, err => {
151
151
  ```
152
152
 
153
153
  ### [LevelDB](https://github.com/fastify/fastify-leveldb)
154
- Install the plugin by running `https://github.com/fastify/fastify-leveldb`
154
+ Install the plugin by running `npm i @fastify/leveldb`
155
155
 
156
156
  *Usage:*
157
157
  ```javascript
158
158
  const fastify = require('fastify')()
159
159
 
160
160
  fastify.register(
161
- require('fastify-leveldb'),
161
+ require('@fastify/leveldb'),
162
162
  { name: 'db' }
163
163
  )
164
164
 
@@ -235,12 +235,12 @@ export default fp(fastifyMysql, { name: 'fastify-mysql-example' })
235
235
 
236
236
  Database schema migrations are an integral part of database management and
237
237
  development. Migrations provide a repeatable and testable way to modify a
238
- database's schema and to prevent data loss.
238
+ database's schema and prevent data loss.
239
239
 
240
240
  As stated at the beginning of the guide, Fastify is database agnostic and any
241
241
  NodeJS database migration tool can be used with it. We will give an example of
242
242
  using [Postgrator](https://www.npmjs.com/package/postgrator) which has support
243
- for Postgres, MySQL and SQL Server. For MongoDB migrations, please check
243
+ for Postgres, MySQL, SQL Server and SQLite. For MongoDB migrations, please check
244
244
  [migrate-mongo](https://www.npmjs.com/package/migrate-mongo).
245
245
 
246
246
  #### [Postgrator](https://www.npmjs.com/package/postgrator)
@@ -258,10 +258,10 @@ reverts it. Think about it like `up` and `down` in other migration tools.
258
258
  optional, it should be used for all migrations as it makes it easier for
259
259
  everyone to know which changes are made in a migration.
260
260
 
261
- In our example we are going to have a single migration that creates a `users`
261
+ In our example, we are going to have a single migration that creates a `users`
262
262
  table and we are going to use `Postgrator` to run the migration.
263
263
 
264
- > Run `npm install pg postgrator` to install dependencies needed for the
264
+ > Run `npm i pg postgrator` to install dependencies needed for the
265
265
  > example.
266
266
 
267
267
  ```sql