fastify 2.12.1 → 2.14.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/.dependabot/config.yml +28 -0
- package/README.md +43 -26
- package/docs/Decorators.md +136 -75
- package/docs/Ecosystem.md +3 -0
- package/docs/Errors.md +5 -0
- package/docs/Hooks.md +9 -1
- package/docs/LTS.md +13 -10
- package/docs/Logging.md +1 -1
- package/docs/Middleware.md +1 -1
- package/docs/Recommendations.md +4 -0
- package/docs/Reply.md +3 -2
- package/docs/Routes.md +6 -6
- package/docs/Server.md +26 -0
- package/docs/Validation-and-Serialization.md +87 -3
- package/fastify.d.ts +10 -36
- package/fastify.js +25 -1
- package/lib/decorate.js +13 -2
- package/lib/errors.js +7 -1
- package/lib/handleRequest.js +9 -3
- package/lib/reply.js +20 -6
- package/lib/route.js +18 -23
- package/package.json +23 -34
- package/test/404s.test.js +64 -1
- package/test/content-length.js +26 -0
- package/test/content-length.test.js +5 -0
- package/test/decorator.test.js +32 -0
- package/test/http2/head.js +35 -0
- package/test/http2/http2.test.js +1 -0
- package/test/internals/decorator.test.js +37 -2
- package/test/internals/handleRequest.test.js +8 -1
- package/test/reply-error.test.js +1 -1
- package/test/route.test.js +24 -24
- package/test/router-options.test.js +34 -0
- package/test/schemas.test.js +44 -14
- package/test/types/index.ts +20 -0
- package/test/validation-error-handling.test.js +100 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
version: 1
|
|
2
|
+
update_configs:
|
|
3
|
+
- package_manager: "javascript"
|
|
4
|
+
directory: "/"
|
|
5
|
+
update_schedule: "daily"
|
|
6
|
+
ignored_updates:
|
|
7
|
+
- match:
|
|
8
|
+
dependency_name: "autocannon"
|
|
9
|
+
- match:
|
|
10
|
+
dependency_name: "boom"
|
|
11
|
+
- match:
|
|
12
|
+
dependency_name: "joi"
|
|
13
|
+
- match:
|
|
14
|
+
dependency_name: "@types/*"
|
|
15
|
+
- match:
|
|
16
|
+
dependency_name: "semver"
|
|
17
|
+
- match:
|
|
18
|
+
dependency_name: "tap"
|
|
19
|
+
- match:
|
|
20
|
+
dependency_name: "tap-mocha-reporter"
|
|
21
|
+
- match:
|
|
22
|
+
dependency_name: "@typescript-eslint/eslint-plugin"
|
|
23
|
+
- match:
|
|
24
|
+
dependency_name: "lolex"
|
|
25
|
+
- match:
|
|
26
|
+
dependency_name: "pino"
|
|
27
|
+
- match:
|
|
28
|
+
dependency_name: "fast-json-stringify"
|
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
<div align="center">
|
|
17
17
|
|
|
18
18
|
[](https://www.npmjs.com/package/fastify)
|
|
19
|
-
[](https://www.npmjs.com/package/fastify)
|
|
19
|
+
[](https://www.npmjs.com/package/fastify)
|
|
20
20
|
[](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md)
|
|
22
22
|
|
|
@@ -29,8 +29,47 @@ How can you efficiently handle the resources of your server, knowing that you ar
|
|
|
29
29
|
|
|
30
30
|
Enter Fastify. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.
|
|
31
31
|
|
|
32
|
+
### Quick start
|
|
33
|
+
|
|
34
|
+
Create a folder and make it your current working directory:
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
mkdir my-app
|
|
38
|
+
cd my-app
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Generate a fastify project with `npm init`:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
npm init fastify
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Install dependencies:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
npm install
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To start the app in dev mode:
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
npm run dev
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For production mode:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
npm start
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Under the hood `npm init` downloads and runs [Fastify Create](https://github.com/fastify/create-fastify),
|
|
66
|
+
which in turn uses the generate functionality of [Fastify CLI](https://github.com/fastify/fastify-cli).
|
|
67
|
+
|
|
68
|
+
|
|
32
69
|
### Install
|
|
33
70
|
|
|
71
|
+
If installing in an existing project, then Fastify can be installed into the project as a dependency:
|
|
72
|
+
|
|
34
73
|
Install with npm:
|
|
35
74
|
```
|
|
36
75
|
npm i fastify --save
|
|
@@ -80,29 +119,6 @@ fastify.listen(3000, (err, address) => {
|
|
|
80
119
|
|
|
81
120
|
Do you want to know more? Head to the <a href="https://github.com/fastify/fastify/blob/master/docs/Getting-Started.md"><code><b>Getting Started</b></code></a>.
|
|
82
121
|
|
|
83
|
-
### Quick start with Fastify CLI
|
|
84
|
-
|
|
85
|
-
Good tools make API development quicker and easier to maintain than doing everything manually.
|
|
86
|
-
|
|
87
|
-
The [Fastify CLI](https://github.com/fastify/fastify-cli) is a command line interface tool that can create new projects, manage plugins, and perform a variety of development tasks testing and running the application.
|
|
88
|
-
|
|
89
|
-
The goal in this guide is to build and run a simple Fastify project, using the [Fastify CLI](https://github.com/fastify/fastify-cli), while adhering to the Style Guide recommendations that benefit every Fastify project.
|
|
90
|
-
|
|
91
|
-
### Example
|
|
92
|
-
|
|
93
|
-
Open a terminal window.
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
npm install fastify-cli --global
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Generate a new project and default app by running the following command:
|
|
100
|
-
|
|
101
|
-
```
|
|
102
|
-
fastify generate
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
For more information, see the [Fastify CLI documentation](https://github.com/fastify/fastify-cli).
|
|
106
122
|
|
|
107
123
|
### Fastify v1.x
|
|
108
124
|
|
|
@@ -176,8 +192,8 @@ matters to you.
|
|
|
176
192
|
- [Live Examples](https://github.com/fastify/example) - Multirepo with a broad set of real working examples.
|
|
177
193
|
|
|
178
194
|
## Support
|
|
179
|
-
|
|
180
|
-
|
|
195
|
+
Please visit [Fastify help](https://github.com/fastify/help) to view prior
|
|
196
|
+
support issues and to ask new support questions.
|
|
181
197
|
|
|
182
198
|
## Team
|
|
183
199
|
|
|
@@ -191,6 +207,7 @@ Team members are listed in alphabetical order.
|
|
|
191
207
|
### Fastify Core team
|
|
192
208
|
* [__Tommaso Allevi__](https://github.com/allevo), <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
|
|
193
209
|
* [__Ethan Arrowood__](https://github.com/Ethan-Arrowood/), <https://twitter.com/arrowoodtech>, <https://www.npmjs.com/~ethan_arrowood>
|
|
210
|
+
* [__David Mark Clements__](https://github.com/davidmarkclements), <https://twitter.com/davidmarkclem>, <https://www.npmjs.com/~davidmarkclements>
|
|
194
211
|
* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
|
195
212
|
* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
|
196
213
|
* [__Dustin Deus__](https://github.com/StarpTech), <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
|
package/docs/Decorators.md
CHANGED
|
@@ -2,22 +2,81 @@
|
|
|
2
2
|
|
|
3
3
|
## Decorators
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
The decorators API allows customization of the core Fastify objects, such as
|
|
6
|
+
the server instance itself and any request and reply objects used during the
|
|
7
|
+
HTTP request lifecycle. The decorators API can be used to attach any type of
|
|
8
|
+
property to the core objects, e.g. functions, plain objects, or native types.
|
|
9
|
+
|
|
10
|
+
This API is a *synchronous* API. Attempting to define a decoration
|
|
11
|
+
asynchronously could result in the Fastify instance booting prior to the
|
|
12
|
+
decoration completing its initialization. To avoid this issue, and register an
|
|
13
|
+
asynchronous decoration, the `register` API, in combination with
|
|
14
|
+
`fastify-plugin`, must be used instead. To learn more, see the
|
|
15
|
+
[Plugins](Plugins.md) documentation.
|
|
16
|
+
|
|
17
|
+
Decorating core objects with this API allows the underlying JavaScript engine
|
|
18
|
+
to optimize handling of the server, request, and reply objects. This is
|
|
19
|
+
accomplished by defining the shape of all such object instances before they are
|
|
20
|
+
instantiated and used. As an example, the following is not recommended because
|
|
21
|
+
it will change the shape of objects during their lifecycle:
|
|
6
22
|
|
|
7
|
-
|
|
23
|
+
```js
|
|
24
|
+
// Bad example! Continue reading.
|
|
25
|
+
|
|
26
|
+
// Attach a user property to the incoming request before the request
|
|
27
|
+
// handler is invoked.
|
|
28
|
+
fastify.addHook('preHandler', function (req, reply, done) {
|
|
29
|
+
req.user = 'Bob Dylan'
|
|
30
|
+
done()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
// Use the attached user property in the request handler.
|
|
34
|
+
fastify.get('/', function (req, reply) {
|
|
35
|
+
reply.send(`Hello, ${req.user}`)
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Since the above example mutates the request object after it has already
|
|
40
|
+
been instantiated, the JavaScript engine must deoptimize access to the request
|
|
41
|
+
object. By using the decoration API this deoptimization is avoided:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
// Decorate request with a 'user' property
|
|
45
|
+
fastify.decorateRequest('user', '')
|
|
46
|
+
|
|
47
|
+
// Update our property
|
|
48
|
+
fastify.addHook('preHandler', (req, reply, done) => {
|
|
49
|
+
req.user = 'Bob Dylan'
|
|
50
|
+
done()
|
|
51
|
+
})
|
|
52
|
+
// And finally access it
|
|
53
|
+
fastify.get('/', (req, reply) => {
|
|
54
|
+
reply.send(`Hello, ${req.user}!`)
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
See
|
|
59
|
+
[JavaScript engine fundamentals: Shapes and Inline Caches](https://web.archive.org/web/20200201163000/https://mathiasbynens.be/notes/shapes-ics)
|
|
60
|
+
for more information on this topic.
|
|
8
61
|
|
|
9
|
-
<a name="usage"></a>
|
|
10
62
|
### Usage
|
|
63
|
+
<a name="usage"></a>
|
|
64
|
+
|
|
65
|
+
#### `decorate(name, value, [dependencies])`
|
|
11
66
|
<a name="decorate"></a>
|
|
12
|
-
|
|
13
|
-
|
|
67
|
+
|
|
68
|
+
This method is used to customize the Fastify [server](Server.md) instance.
|
|
69
|
+
|
|
70
|
+
For example, to attach a new method to the server instance:
|
|
71
|
+
|
|
14
72
|
```js
|
|
15
|
-
fastify.decorate('utility', ()
|
|
73
|
+
fastify.decorate('utility', function () {
|
|
16
74
|
// Something very useful
|
|
17
75
|
})
|
|
18
76
|
```
|
|
19
77
|
|
|
20
|
-
As mentioned above,
|
|
78
|
+
As mentioned above, non-function values can be attached:
|
|
79
|
+
|
|
21
80
|
```js
|
|
22
81
|
fastify.decorate('conf', {
|
|
23
82
|
db: 'some.db',
|
|
@@ -25,39 +84,95 @@ fastify.decorate('conf', {
|
|
|
25
84
|
})
|
|
26
85
|
```
|
|
27
86
|
|
|
28
|
-
|
|
87
|
+
To access decorated properties, simply use the name provided to the
|
|
88
|
+
decoration API:
|
|
89
|
+
|
|
29
90
|
```js
|
|
30
91
|
fastify.utility()
|
|
31
92
|
|
|
32
93
|
console.log(fastify.conf.db)
|
|
33
94
|
```
|
|
34
95
|
|
|
96
|
+
The `dependencies` parameter is an optional list of decorators that the
|
|
97
|
+
decorator being defined relies upon. This list is simply a list of string names
|
|
98
|
+
of other decorators. In the following example, the "utility" decorator depends
|
|
99
|
+
upon "greet" and "log" decorators:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
fastify.decorate('utility', fn, ['greet', 'log'])
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
If a dependency is not satisfied, the `decorate` method will throw an exception.
|
|
106
|
+
The dependency check is peformed before the server instance is booted. Thus,
|
|
107
|
+
it cannot occur during runtime.
|
|
108
|
+
|
|
109
|
+
#### `decorateReply(name, value, [dependencies])`
|
|
35
110
|
<a name="decorate-reply"></a>
|
|
36
|
-
|
|
37
|
-
As the name suggests, this API
|
|
111
|
+
|
|
112
|
+
As the name suggests, this API is used to add new methods/properties to the core
|
|
113
|
+
`Reply` object:
|
|
114
|
+
|
|
38
115
|
```js
|
|
39
116
|
fastify.decorateReply('utility', function () {
|
|
40
117
|
// Something very useful
|
|
41
118
|
})
|
|
42
119
|
```
|
|
43
120
|
|
|
44
|
-
Note: using an arrow function will break the binding of `this` to the Fastify
|
|
121
|
+
Note: using an arrow function will break the binding of `this` to the Fastify
|
|
122
|
+
`Reply` instance.
|
|
45
123
|
|
|
124
|
+
See [`decorate`](#decorate) for information about the `dependencies` parameter.
|
|
125
|
+
|
|
126
|
+
#### `decorateRequest(name, value, [dependencies])`
|
|
46
127
|
<a name="decorate-request"></a>
|
|
47
|
-
|
|
48
|
-
As above, this API is
|
|
128
|
+
|
|
129
|
+
As above with [`decorateReply`](#decorate-reply), this API is used add new
|
|
130
|
+
methods/properties to the core `Request` object:
|
|
131
|
+
|
|
49
132
|
```js
|
|
50
133
|
fastify.decorateRequest('utility', function () {
|
|
51
134
|
// something very useful
|
|
52
135
|
})
|
|
53
136
|
```
|
|
54
137
|
|
|
55
|
-
Note: using an arrow function will break the binding of `this` to the Fastify
|
|
138
|
+
Note: using an arrow function will break the binding of `this` to the Fastify
|
|
139
|
+
`Request` instance.
|
|
140
|
+
|
|
141
|
+
See [`decorate`](#decorate) for information about the `dependencies` parameter.
|
|
142
|
+
|
|
143
|
+
#### `hasDecorator(name)`
|
|
144
|
+
<a name="has-decorator"></a>
|
|
145
|
+
|
|
146
|
+
Used to check for the existence of a server instance decoration:
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
fastify.hasDecorator('utility')
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### hasRequestDecorator
|
|
153
|
+
<a name="has-request-decorator"></a>
|
|
154
|
+
|
|
155
|
+
Used to check for the existence of a Request decoration:
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
fastify.hasRequestDecorator('utility')
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### hasReplyDecorator
|
|
162
|
+
<a name="has-reply-decorator"></a>
|
|
163
|
+
|
|
164
|
+
Used to check for the existence of a Reply decoration:
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
fastify.hasReplyDecorator('utility')
|
|
168
|
+
```
|
|
56
169
|
|
|
170
|
+
### Decorators and Encapsulation
|
|
57
171
|
<a name="decorators-encapsulation"></a>
|
|
58
|
-
#### Decorators and Encapsulation
|
|
59
172
|
|
|
60
|
-
|
|
173
|
+
Defining a decorator (using `decorate`, `decorateRequest` or `decorateReply`)
|
|
174
|
+
with the same name more than once in the same **encapsulated** context will
|
|
175
|
+
throw an exception.
|
|
61
176
|
|
|
62
177
|
As an example, the following will throw:
|
|
63
178
|
|
|
@@ -107,10 +222,12 @@ server.register(async function (server, opts) {
|
|
|
107
222
|
server.listen(3000)
|
|
108
223
|
```
|
|
109
224
|
|
|
225
|
+
### Getters and Setters
|
|
110
226
|
<a name="getters-setters"></a>
|
|
111
|
-
#### Getters and Setters
|
|
112
227
|
|
|
113
|
-
Decorators accept special "getter/setter" objects. These objects have functions
|
|
228
|
+
Decorators accept special "getter/setter" objects. These objects have functions
|
|
229
|
+
named `getter` and `setter` (though, the `setter` function is optional). This
|
|
230
|
+
allows defining properties via decorators. For example:
|
|
114
231
|
|
|
115
232
|
```js
|
|
116
233
|
fastify.decorate('foo', {
|
|
@@ -120,64 +237,8 @@ fastify.decorate('foo', {
|
|
|
120
237
|
})
|
|
121
238
|
```
|
|
122
239
|
|
|
123
|
-
Will define the `foo` property on the
|
|
240
|
+
Will define the `foo` property on the Fastify instance:
|
|
124
241
|
|
|
125
242
|
```js
|
|
126
243
|
console.log(fastify.foo) // 'a getter'
|
|
127
244
|
```
|
|
128
|
-
|
|
129
|
-
<a name="usage_notes"></a>
|
|
130
|
-
#### Usage Notes
|
|
131
|
-
`decorateReply` and `decorateRequest` are used to modify the `Reply` and `Request` constructors respectively by adding methods or properties. To update these properties you should directly access the desired property of the `Reply` or `Request` object.
|
|
132
|
-
|
|
133
|
-
As an example let's add a user property to the `Request` object:
|
|
134
|
-
|
|
135
|
-
```js
|
|
136
|
-
// Decorate request with a 'user' property
|
|
137
|
-
fastify.decorateRequest('user', '')
|
|
138
|
-
|
|
139
|
-
// Update our property
|
|
140
|
-
fastify.addHook('preHandler', (req, reply, done) => {
|
|
141
|
-
req.user = 'Bob Dylan'
|
|
142
|
-
done()
|
|
143
|
-
})
|
|
144
|
-
// And finally access it
|
|
145
|
-
fastify.get('/', (req, reply) => {
|
|
146
|
-
reply.send(`Hello ${req.user}!`)
|
|
147
|
-
})
|
|
148
|
-
```
|
|
149
|
-
Note: The usage of `decorateReply` and `decorateRequest` is optional in this case but will allow Fastify to optimize for performance.
|
|
150
|
-
|
|
151
|
-
<a name="sync-async"></a>
|
|
152
|
-
#### Sync and Async
|
|
153
|
-
`decorate` is a *synchronous* API. If you need to add a decorator that has an *asynchronous* bootstrap, Fastify could boot up before your decorator is ready. To avoid this issue, you must use the `register` API in combination with `fastify-plugin`. To learn more, check out the [Plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins.md) documentation as well.
|
|
154
|
-
|
|
155
|
-
<a name="dependencies"></a>
|
|
156
|
-
#### Dependencies
|
|
157
|
-
If your decorator depends on another decorator, you can easily declare the other decorator as a dependency. You just need to add an array of strings (representing the names of the decorators on which yours depends) as the third parameter:
|
|
158
|
-
```js
|
|
159
|
-
fastify.decorate('utility', fn, ['greet', 'log'])
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
If a dependency is not satisfied, `decorate` will throw an exception, but don't worry: the dependency check is executed before the server boots up, so it won't ever happen at runtime.
|
|
163
|
-
|
|
164
|
-
<a name="has-decorator"></a>
|
|
165
|
-
#### hasDecorator
|
|
166
|
-
You can check for the presence of a decorator with the `hasDecorator` API:
|
|
167
|
-
```js
|
|
168
|
-
fastify.hasDecorator('utility')
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
<a name="has-request-decorator"></a>
|
|
172
|
-
#### hasRequestDecorator
|
|
173
|
-
You can check for the presence of a Request decorator with the `hasRequestDecorator` API:
|
|
174
|
-
```js
|
|
175
|
-
fastify.hasRequestDecorator('utility')
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
<a name="has-reply-decorator"></a>
|
|
179
|
-
#### hasReplyDecorator
|
|
180
|
-
You can check for the presence of a Reply decorator with the `hasReplyDecorator` API:
|
|
181
|
-
```js
|
|
182
|
-
fastify.hasReplyDecorator('utility')
|
|
183
|
-
```
|
package/docs/Ecosystem.md
CHANGED
|
@@ -76,6 +76,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
76
76
|
- [`fastify-file-upload`](https://github.com/huangang/fastify-file-upload) Fastify plugin for uploading files.
|
|
77
77
|
- [`fastify-firebase-auth`](https://github.com/oxsav/fastify-firebase-auth) Firebase Authentication for Fastify supporting all of the methods relating to the authentication API.
|
|
78
78
|
- [`fastify-firestore`](https://github.com/now-ims/fastify-firestore) Fastify plugin for [Google Cloud Firestore](https://cloud.google.com/nodejs/docs/reference/firestore/0.13.x/).
|
|
79
|
+
- [`fastify-gcloud-trace`](https://github.com/mkinoshi/fastify-gcloud-trace) [Google Cloud Trace API](https://cloud.google.com/trace/docs/reference) Connector for Fastify.
|
|
79
80
|
- [`fastify-google-cloud-storage`](https://github.com/carlozamagni/fastify-google-cloud-storage) Fastify plugin that exposes a GCP Cloud Storage client instance.
|
|
80
81
|
- [`fastify-gql`](https://github.com/mcollina/fastify-gql) A GraphQL server implementation for Fastify with caching and [`graphql-jit`](https://github.com/ruiaraujo/graphql-jit).
|
|
81
82
|
- [`fastify-graceful-shutdown`](https://github.com/hemerajs/fastify-graceful-shutdown) Shutdown Fastify gracefully and asynchronously.
|
|
@@ -94,6 +95,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
94
95
|
- [`fastify-loader`](https://github.com/TheNoim/fastify-loader) Load routes from a directory and inject the fastify instance in each file.
|
|
95
96
|
- [`fastify-lured`](https://github.com/lependu/fastify-lured) Plugin to load lua scripts with [fastify-redis](https://github.com/fastify/fastify-redis) and [lured](https://github.com/enobufs/lured).
|
|
96
97
|
- [`fastify-markdown`](https://github.com/freezestudio/fastify-markdown) Plugin to markdown support.
|
|
98
|
+
- [`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.
|
|
97
99
|
- [`fastify-metrics`](https://gitlab.com/m03geek/fastify-metrics) Plugin for exporting [Prometheus](https://prometheus.io) metrics.
|
|
98
100
|
- [`fastify-mongo-memory`](https://github.com/chapuletta/fastify-mongo-memory) Fastify MongoDB in Memory Plugin for testing support.
|
|
99
101
|
- [`fastify-mongoose-api`](https://github.com/jeka-kiselyov/fastify-mongoose-api) Fastify plugin to create REST API methods based on Mongoose MongoDB models.
|
|
@@ -108,6 +110,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
108
110
|
- [`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
|
|
109
111
|
- [`fastify-oracle`](https://github.com/cemremengu/fastify-oracle) Attaches an [`oracledb`](https://github.com/oracle/node-oracledb) connection pool to a Fastify server instance.
|
|
110
112
|
- [`fastify-orientdb`](https://github.com/mahmed8003/fastify-orientdb) Fastify OrientDB connection plugin, with which you can share the OrientDB connection across every part of your server.
|
|
113
|
+
- [`fastify-qrcode`](https://github.com/chonla/fastify-qrcode) This plugin utilizes [qrcode](https://github.com/soldair/node-qrcode) to generate QR Code.
|
|
111
114
|
- [`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).
|
|
112
115
|
- [`fastify-rbac`](https://gitlab.com/m03geek/fastify-rbac) Fastify role-based access control plugin.
|
|
113
116
|
- [`fastify-register-routes`](https://github.com/israeleriston/fastify-register-routes) Plugin to automatically load routes from a specified path and optionally limit loaded file names by a regular expression.
|
package/docs/Errors.md
CHANGED
|
@@ -16,6 +16,11 @@ If routes are declared as `async` though - the error will safely be caught by th
|
|
|
16
16
|
<a name="fastify-error-codes"></a>
|
|
17
17
|
### Fastify Error Codes
|
|
18
18
|
|
|
19
|
+
<a name="FST_ERR_BAD_URL"></a>
|
|
20
|
+
#### FST_ERR_BAD_URL
|
|
21
|
+
|
|
22
|
+
The router received an invalid url.
|
|
23
|
+
|
|
19
24
|
<a name="FST_ERR_CTP_ALREADY_PRESENT"></a>
|
|
20
25
|
#### FST_ERR_CTP_ALREADY_PRESENT
|
|
21
26
|
|
package/docs/Hooks.md
CHANGED
|
@@ -15,6 +15,8 @@ By using hooks you can interact directly with the lifecycle of Fastify. There ar
|
|
|
15
15
|
- [onError](#onerror)
|
|
16
16
|
- [onSend](#onsend)
|
|
17
17
|
- [onResponse](#onresponse)
|
|
18
|
+
- [Manage Errors from a hook](#manage-errors-from-a-hook)
|
|
19
|
+
- [Respond to a request from a hook](#respond-to-a-request-from-a-hook)
|
|
18
20
|
- [Application Hooks](#application-hooks)
|
|
19
21
|
- [onClose](#onclose)
|
|
20
22
|
- [onRoute](#onroute)
|
|
@@ -206,9 +208,15 @@ fastify.addHook('preHandler', (request, reply, done) => {
|
|
|
206
208
|
done(new Error('Some error'))
|
|
207
209
|
})
|
|
208
210
|
```
|
|
209
|
-
|
|
210
211
|
*The error will be handled by [`Reply`](https://github.com/fastify/fastify/blob/master/docs/Reply.md#errors).*
|
|
211
212
|
|
|
213
|
+
Or if you're using `async/await` you can just throw an error:
|
|
214
|
+
```js
|
|
215
|
+
fastify.addHook('onResponse', async (request, reply) => {
|
|
216
|
+
throw new Error('Some error')
|
|
217
|
+
})
|
|
218
|
+
```
|
|
219
|
+
|
|
212
220
|
### Respond to a request from a hook
|
|
213
221
|
|
|
214
222
|
If needed, you can respond to a request before you reach the route handler,
|
package/docs/LTS.md
CHANGED
|
@@ -31,18 +31,21 @@ A "month" is to be a period of 30 consecutive days.
|
|
|
31
31
|
|
|
32
32
|
### Schedule
|
|
33
33
|
|
|
34
|
-
| Version | Release Date | End Of LTS Date | Node.js
|
|
35
|
-
| :------ | :----------- | :-------------- |
|
|
36
|
-
| 1.0.0 | 2018-03-06 | 2019-09-01 | 6, 8, 9, 10, 11
|
|
37
|
-
| 2.0.0 | 2019-02-25 | TBD | 6, 8, 10, 11
|
|
34
|
+
| Version | Release Date | End Of LTS Date | Node.js |
|
|
35
|
+
| :------ | :----------- | :-------------- | :------------------- |
|
|
36
|
+
| 1.0.0 | 2018-03-06 | 2019-09-01 | 6, 8, 9, 10, 11 |
|
|
37
|
+
| 2.0.0 | 2019-02-25 | TBD | 6, 8, 10, 11, 12, 13 |
|
|
38
38
|
|
|
39
39
|
<a name="supported-os"></a>
|
|
40
40
|
|
|
41
41
|
### CI tested operating systems
|
|
42
42
|
|
|
43
|
-
|
|
|
44
|
-
|
|
45
|
-
|
|
|
46
|
-
|
|
|
47
|
-
|
|
|
48
|
-
|
|
|
43
|
+
| OS | Version | Package Manager | Node.js |
|
|
44
|
+
|---------|------------------------|---------------------------|-----------|
|
|
45
|
+
| Linux | Ubuntu 16.04 | npm | 6,8,10,12 |
|
|
46
|
+
| Linux | Ubuntu 16.04 | pnpm | 8,10,12 |
|
|
47
|
+
| Linux | Ubuntu 16.04 | yarn | 8,10,12 |
|
|
48
|
+
| Windows | Windows Server 2016 R2 | npm | 6,8,10,12 |
|
|
49
|
+
| MacOS | macOS X Mojave 10.14 | npm | 6,8,10,12 |
|
|
50
|
+
|
|
51
|
+
Using yarn might require passing the `--ignore-engines` flag.
|
package/docs/Logging.md
CHANGED
|
@@ -101,7 +101,7 @@ const fastify = require('fastify')({
|
|
|
101
101
|
}
|
|
102
102
|
});
|
|
103
103
|
```
|
|
104
|
-
**Note**: The body
|
|
104
|
+
**Note**: The body cannot be serialized inside `req` method because the request is serialized when we create the child logger. At that time, the body is not yet parsed.
|
|
105
105
|
|
|
106
106
|
See an approach to log `req.body`
|
|
107
107
|
|
package/docs/Middleware.md
CHANGED
|
@@ -32,7 +32,7 @@ fastify.register(helmet)
|
|
|
32
32
|
|
|
33
33
|
Remember that middleware can be encapsulated, this means that you can decide where your middleware should run by using `register` as explained in the [plugins guide](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md).
|
|
34
34
|
|
|
35
|
-
Fastify middleware also do not expose the `send` method or other methods specific to the Fastify [Reply](
|
|
35
|
+
Fastify middleware also do not expose the `send` method or other methods specific to the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps the incoming `req` and `res` Node instances using the [Request](./Request.md#request) and [Reply](./Reply.md#reply) objects internally, but this is done after the middleware phase. If you need to create middleware, you have to use the Node `req` and `res` instances. Otherwise, you can use the `preHandler` hook which already has the [Request](./Request.md#request) and [Reply](./Reply.md#reply) Fastify instances. For more information, see [Hooks](./Hooks.md#hooks).
|
|
36
36
|
|
|
37
37
|
<a name="restrict-usage"></a>
|
|
38
38
|
#### Restrict middleware execution to a certain path(s)
|
package/docs/Recommendations.md
CHANGED
|
@@ -67,6 +67,10 @@ defaults
|
|
|
67
67
|
option dontlognull
|
|
68
68
|
retries 3
|
|
69
69
|
option redispatch
|
|
70
|
+
# The following option make haproxy close connections to backend servers
|
|
71
|
+
# instead of keeping them open. This can alleviate unexpected connection
|
|
72
|
+
# reset errors in the Node process.
|
|
73
|
+
option http-server-close
|
|
70
74
|
maxconn 2000
|
|
71
75
|
timeout connect 5000
|
|
72
76
|
timeout client 50000
|
package/docs/Reply.md
CHANGED
|
@@ -147,7 +147,8 @@ reply.code(303).redirect(302, '/home')
|
|
|
147
147
|
|
|
148
148
|
<a name="call-not-found"></a>
|
|
149
149
|
### .callNotFound()
|
|
150
|
-
Invokes the custom not found handler.
|
|
150
|
+
Invokes the custom not found handler. Note that it will only call `preHandler` hook specified in [`setNotFoundHandler`](https://github.com/fastify/fastify/blob/master/docs/Server.md#set-not-found-handler).
|
|
151
|
+
|
|
151
152
|
```js
|
|
152
153
|
reply.callNotFound()
|
|
153
154
|
```
|
|
@@ -338,7 +339,7 @@ fastify.get('/async-await', options, async function (request, reply) {
|
|
|
338
339
|
})
|
|
339
340
|
```
|
|
340
341
|
|
|
341
|
-
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.
|
|
342
|
+
Rejected promises default to a `500` HTTP status code. Reject the promise, or `throw` in an `async function`, with an _Error_ object that has `statusCode` (or `status`) and `message` properties to modify the reply. Throwing plain objects is not supported, it must be an instance of _Error_, see:
|
|
342
343
|
|
|
343
344
|
```js
|
|
344
345
|
fastify.get('/teapot', async function (request, reply) => {
|
package/docs/Routes.md
CHANGED
|
@@ -154,28 +154,28 @@ To register a **parametric** path, use the *colon* before the parameter name. Fo
|
|
|
154
154
|
|
|
155
155
|
```js
|
|
156
156
|
// parametric
|
|
157
|
-
fastify.get('/example/:userId', (request, reply) => {})
|
|
158
|
-
fastify.get('/example/:userId/:secretToken', (request, reply) => {})
|
|
157
|
+
fastify.get('/example/:userId', (request, reply) => {})
|
|
158
|
+
fastify.get('/example/:userId/:secretToken', (request, reply) => {})
|
|
159
159
|
|
|
160
160
|
// wildcard
|
|
161
|
-
fastify.get('/example/*', (request, reply) => {})
|
|
161
|
+
fastify.get('/example/*', (request, reply) => {})
|
|
162
162
|
```
|
|
163
163
|
|
|
164
164
|
Regular expression routes are supported as well, but pay attention, RegExp are very expensive in term of performance!
|
|
165
165
|
```js
|
|
166
166
|
// parametric with regexp
|
|
167
|
-
fastify.get('/example/:file(^\\d+).png', (request, reply) => {})
|
|
167
|
+
fastify.get('/example/:file(^\\d+).png', (request, reply) => {})
|
|
168
168
|
```
|
|
169
169
|
|
|
170
170
|
It's possible to define more than one parameter within the same couple of slash ("/"). Such as:
|
|
171
171
|
```js
|
|
172
|
-
fastify.get('/example/near/:lat-:lng/radius/:r', (request, reply) => {})
|
|
172
|
+
fastify.get('/example/near/:lat-:lng/radius/:r', (request, reply) => {})
|
|
173
173
|
```
|
|
174
174
|
*Remember in this case to use the dash ("-") as parameters separator.*
|
|
175
175
|
|
|
176
176
|
Finally it's possible to have multiple parameters with RegExp.
|
|
177
177
|
```js
|
|
178
|
-
fastify.get('/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (request, reply) => {})
|
|
178
|
+
fastify.get('/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (request, reply) => {})
|
|
179
179
|
```
|
|
180
180
|
In this case as parameter separator it's possible to use whatever character is not matched by the regular expression.
|
|
181
181
|
|
package/docs/Server.md
CHANGED
|
@@ -418,6 +418,30 @@ Set a default
|
|
|
418
418
|
Note that this is needed to offer the graceful "close" experience when
|
|
419
419
|
using http2. Node core defaults this to `0`.
|
|
420
420
|
|
|
421
|
+
<a name="framework-errors"></a>
|
|
422
|
+
### `frameworkErrors`
|
|
423
|
+
|
|
424
|
+
+ Default: `null`
|
|
425
|
+
|
|
426
|
+
Fastify provides default error handlers for the most common use cases.
|
|
427
|
+
Using this option it is possible to override one or more of those handlers with custom code.
|
|
428
|
+
|
|
429
|
+
*Note: Only `FST_ERR_BAD_URL` is implemented at the moment.*
|
|
430
|
+
|
|
431
|
+
```js
|
|
432
|
+
const fastify = require('fastify')({
|
|
433
|
+
frameworkErrors: function (error, req, res) {
|
|
434
|
+
if (error instanceof FST_ERR_BAD_URL) {
|
|
435
|
+
res.code(400)
|
|
436
|
+
return res.send("Provided url is not valid")
|
|
437
|
+
} else {
|
|
438
|
+
res.send(err)
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
})
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
|
|
421
445
|
## Instance
|
|
422
446
|
|
|
423
447
|
### Server Methods
|
|
@@ -696,6 +720,8 @@ This property can be used to set the schema compiler, it is a shortcut for the `
|
|
|
696
720
|
|
|
697
721
|
You can also register a [`preValidation`](https://www.fastify.io/docs/latest/Hooks/#route-hooks) and [preHandler](https://www.fastify.io/docs/latest/Hooks/#route-hooks) hook for the 404 handler.
|
|
698
722
|
|
|
723
|
+
_Note: The `preValidation` hook registered using this method will run for a route that Fastify does not recognize and **not** when a route handler manually calls [`reply.callNotFound`](https://github.com/fastify/fastify/blob/master/docs/Reply.md#call-not-found)_. In which case only preHandler will be run.
|
|
724
|
+
|
|
699
725
|
```js
|
|
700
726
|
fastify.setNotFoundHandler({
|
|
701
727
|
preValidation: (req, reply, done) => {
|