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.
- package/README.md +10 -11
- package/build/build-validation.js +2 -0
- package/docs/Guides/Benchmarking.md +1 -1
- package/docs/Guides/Database.md +17 -17
- package/docs/Guides/Delay-Accepting-Requests.md +597 -0
- package/docs/Guides/Ecosystem.md +41 -41
- package/docs/Guides/Getting-Started.md +56 -10
- package/docs/Guides/Index.md +6 -1
- package/docs/Guides/Migration-Guide-V3.md +6 -6
- package/docs/Guides/Plugins-Guide.md +8 -8
- package/docs/Guides/Recommendations.md +3 -3
- package/docs/Guides/Serverless.md +6 -6
- package/docs/Guides/Style-Guide.md +1 -1
- package/docs/Guides/Testing.md +1 -1
- package/docs/Guides/Write-Plugin.md +10 -10
- package/docs/Migration-Guide-V4.md +1 -1
- package/docs/Reference/ContentTypeParser.md +4 -4
- package/docs/Reference/Encapsulation.md +4 -4
- package/docs/Reference/Errors.md +0 -7
- package/docs/Reference/Hooks.md +4 -4
- package/docs/Reference/LTS.md +6 -6
- package/docs/Reference/Middleware.md +7 -7
- package/docs/Reference/Plugins.md +4 -5
- package/docs/Reference/Reply.md +6 -6
- package/docs/Reference/Routes.md +46 -25
- package/docs/Reference/Server.md +74 -15
- package/docs/{Type-Providers.md → Reference/Type-Providers.md} +5 -5
- package/docs/Reference/TypeScript.md +13 -3
- package/docs/Reference/Validation-and-Serialization.md +8 -9
- package/fastify.d.ts +3 -3
- package/fastify.js +19 -10
- package/lib/configValidator.js +216 -187
- package/lib/contentTypeParser.js +1 -1
- package/lib/errors.js +1 -1
- package/lib/fourOhFour.js +16 -17
- package/lib/hooks.js +4 -0
- package/lib/pluginUtils.js +2 -1
- package/lib/reply.js +10 -6
- package/lib/route.js +33 -7
- package/lib/server.js +52 -1
- package/package.json +6 -6
- package/test/404s.test.js +19 -4
- package/test/allowUnsafeRegex.test.js +117 -0
- package/test/async-await.test.js +3 -3
- package/test/bodyLimit.test.js +1 -1
- package/test/constrained-routes.test.js +262 -4
- package/test/content-parser.test.js +21 -1
- package/test/custom-parser.test.js +3 -3
- package/test/decorator.test.js +9 -9
- package/test/delete.test.js +1 -1
- package/test/get.test.js +1 -1
- package/test/head.test.js +1 -1
- package/test/helper.js +1 -1
- package/test/hooks-async.test.js +1 -1
- package/test/hooks.test.js +76 -17
- package/test/http2/closing.test.js +2 -2
- package/test/http2/constraint.test.js +1 -1
- package/test/http2/head.test.js +1 -1
- package/test/http2/plain.test.js +1 -1
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/http2/secure.test.js +1 -1
- package/test/http2/unknown-http-method.test.js +1 -1
- package/test/https/https.test.js +1 -1
- package/test/input-validation.js +1 -1
- package/test/internals/handleRequest.test.js +6 -3
- package/test/internals/initialConfig.test.js +10 -10
- package/test/internals/server.test.js +5 -5
- package/test/listen.test.js +11 -2
- package/test/logger.test.js +19 -12
- package/test/nullable-validation.test.js +3 -3
- package/test/output-validation.test.js +1 -1
- package/test/plugin.test.js +30 -6
- package/test/promises.test.js +1 -1
- package/test/register.test.js +1 -1
- package/test/reply-trailers.test.js +14 -7
- package/test/request-error.test.js +1 -1
- package/test/route.test.js +2 -2
- package/test/router-options.test.js +1 -1
- package/test/stream.test.js +13 -14
- package/test/types/hooks.test-d.ts +1 -1
- package/test/types/instance.test-d.ts +27 -0
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/request.test-d.ts +2 -1
- package/test/types/route.test-d.ts +1 -1
- package/test/unsupported-httpversion.test.js +57 -0
- package/test/versioned-routes.test.js +7 -11
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +11 -4
- package/types/logger.d.ts +1 -1
- package/types/route.d.ts +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
[](https://github.com/fastify/fastify/actions/workflows/ci.yml)
|
|
9
9
|
[](https://github.com/fastify/fastify/actions/workflows/package-manager-ci.yml)
|
|
10
10
|
[](https://github.com/fastify/fastify/actions/workflows/website.yml)
|
|
11
|
-
[](https://snyk.io/test/github/fastify/fastify)
|
|
12
11
|
[](https://standardjs.com/)
|
|
13
12
|
|
|
14
13
|
</div>
|
|
@@ -18,7 +17,7 @@
|
|
|
18
17
|
[](https://www.npmjs.com/package/fastify)
|
|
19
18
|
[](https://www.npmjs.com/package/fastify)
|
|
20
19
|
[](https://github.com/
|
|
20
|
+
Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/fastify/fastify/blob/main/SECURITY.md)
|
|
22
21
|
[](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
|
|
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
|
|
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.
|
|
178
|
-
| hapi |
|
|
179
|
-
| Restify | 8.
|
|
180
|
-
| Koa | 2.13.0 | ✗ | 54,
|
|
181
|
-
| **Fastify** | **
|
|
176
|
+
| Express | 4.17.3 | ✓ | 14,200 |
|
|
177
|
+
| hapi | 20.2.1 | ✓ | 42,284 |
|
|
178
|
+
| Restify | 8.6.1 | ✓ | 50,363 |
|
|
179
|
+
| Koa | 2.13.0 | ✗ | 54,272 |
|
|
180
|
+
| **Fastify** | **4.0.0** | **✓** | **77,193** |
|
|
182
181
|
| - | | | |
|
|
183
|
-
| `http.Server` |
|
|
182
|
+
| `http.Server` | 16.14.2 | ✗ | 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/
|
|
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
|
|
18
|
+
against different Node.js Versions and execute local binaries. Shipped with
|
|
19
19
|
npm@5.2.0.
|
|
20
20
|
|
|
21
21
|
## Simple
|
package/docs/Guides/Database.md
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
83
|
+
fastify.register(require('@fastify/redis'), { host: '127.0.0.1' })
|
|
84
84
|
// or
|
|
85
|
-
fastify.register(require('fastify
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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 `
|
|
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
|
|
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
|
|
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
|
|
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
|
|
264
|
+
> Run `npm i pg postgrator` to install dependencies needed for the
|
|
265
265
|
> example.
|
|
266
266
|
|
|
267
267
|
```sql
|