fastify 2.10.0 → 2.13.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.
- package/LICENSE +1 -1
- package/README.md +52 -28
- package/build/build-validation.js +12 -2
- package/docs/Decorators.md +136 -75
- package/docs/Ecosystem.md +13 -2
- package/docs/Errors.md +10 -0
- package/docs/Fluent-Schema.md +1 -3
- package/docs/Hooks.md +75 -37
- package/docs/Plugins-Guide.md +19 -2
- package/docs/Plugins.md +27 -0
- package/docs/Recommendations.md +159 -0
- package/docs/Reply.md +19 -2
- package/docs/Routes.md +96 -6
- package/docs/Server.md +86 -4
- package/docs/Serverless.md +1 -1
- package/docs/Testing.md +18 -3
- package/docs/TypeScript.md +37 -14
- package/docs/Validation-and-Serialization.md +214 -13
- package/fastify.d.ts +33 -40
- package/fastify.js +65 -6
- package/lib/configValidator.js +129 -52
- package/lib/contentTypeParser.js +4 -4
- package/lib/context.js +2 -1
- package/lib/decorate.js +13 -2
- package/lib/errors.js +8 -0
- package/lib/hooks.js +3 -1
- package/lib/logger.js +2 -2
- package/lib/reply.js +10 -4
- package/lib/route.js +37 -27
- package/lib/schemas.js +10 -2
- package/lib/server.js +11 -0
- package/lib/symbols.js +3 -1
- package/lib/validation.js +11 -5
- package/package.json +33 -29
- package/test/404s.test.js +40 -0
- package/test/decorator.test.js +80 -0
- package/test/esm/esm.mjs +13 -0
- package/test/esm/index.test.js +19 -0
- package/test/esm/other.mjs +7 -0
- package/test/esm/plugin.mjs +7 -0
- package/test/fastify-instance.test.js +29 -0
- package/test/genReqId.test.js +1 -1
- package/test/hooks-async.js +19 -0
- package/test/hooks.test.js +139 -7
- package/test/http2/closing.js +53 -0
- package/test/http2/plain.js +15 -0
- package/test/input-validation.js +63 -0
- package/test/input-validation.test.js +161 -0
- package/test/internals/decorator.test.js +37 -2
- package/test/internals/initialConfig.test.js +6 -2
- package/test/internals/reply.test.js +27 -3
- package/test/logger.test.js +310 -1
- package/test/proto-poisoning.test.js +76 -0
- package/test/reply-error.test.js +30 -0
- package/test/route-hooks.test.js +23 -14
- package/test/route.test.js +62 -24
- package/test/router-options.test.js +34 -0
- package/test/schemas.test.js +156 -0
- package/test/shared-schemas.test.js +65 -0
- package/test/types/index.ts +75 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|

|
|
8
8
|

|
|
9
9
|

|
|
10
|
-
[](https://dev.azure.com/fastify/fastify/_build/latest?definitionId=1)
|
|
11
10
|
[](https://snyk.io/test/github/fastify/fastify)
|
|
12
11
|
[](https://coveralls.io/github/fastify/fastify?branch=master)
|
|
13
12
|
[](http://standardjs.com/)
|
|
@@ -17,7 +16,7 @@
|
|
|
17
16
|
<div align="center">
|
|
18
17
|
|
|
19
18
|
[](https://www.npmjs.com/package/fastify)
|
|
20
|
-
[](https://www.npmjs.com/package/fastify)
|
|
19
|
+
[](https://www.npmjs.com/package/fastify)
|
|
21
20
|
[](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md)
|
|
23
22
|
|
|
@@ -30,8 +29,47 @@ How can you efficiently handle the resources of your server, knowing that you ar
|
|
|
30
29
|
|
|
31
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.
|
|
32
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/fastify-create),
|
|
66
|
+
which in turn uses the generate functionality of [Fastify CLI](https://github.com/fastify/fastify-cli).
|
|
67
|
+
|
|
68
|
+
|
|
33
69
|
### Install
|
|
34
70
|
|
|
71
|
+
If installing in an existing project, then Fastify can be installed into the project as a dependency:
|
|
72
|
+
|
|
35
73
|
Install with npm:
|
|
36
74
|
```
|
|
37
75
|
npm i fastify --save
|
|
@@ -81,29 +119,6 @@ fastify.listen(3000, (err, address) => {
|
|
|
81
119
|
|
|
82
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>.
|
|
83
121
|
|
|
84
|
-
### Quick start with Fastify CLI
|
|
85
|
-
|
|
86
|
-
Good tools make API development quicker and easier to maintain than doing everything manually.
|
|
87
|
-
|
|
88
|
-
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.
|
|
89
|
-
|
|
90
|
-
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.
|
|
91
|
-
|
|
92
|
-
### Example
|
|
93
|
-
|
|
94
|
-
Open a terminal window.
|
|
95
|
-
|
|
96
|
-
```
|
|
97
|
-
npm install fastify-cli --global
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Generate a new project and default app by running the following command:
|
|
101
|
-
|
|
102
|
-
```
|
|
103
|
-
fastify generate
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
For more information, see the [Fastify CLI documentation](https://github.com/fastify/fastify-cli).
|
|
107
122
|
|
|
108
123
|
### Fastify v1.x
|
|
109
124
|
|
|
@@ -115,7 +130,7 @@ Code for Fastify's **v1.x** is in [Branch 1.x](https://github.com/fastify/fastif
|
|
|
115
130
|
|
|
116
131
|
### Core features
|
|
117
132
|
|
|
118
|
-
- **Highly performant:** as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to
|
|
133
|
+
- **Highly performant:** as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second.
|
|
119
134
|
- **Extendible:** Fastify is fully extensible via its hooks, plugins and decorators.
|
|
120
135
|
- **Schema based:** even if it is not mandatory we recommend to use [JSON Schema](http://json-schema.org/) to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
|
|
121
136
|
- **Logging:** logs are extremely important but are costly; we chose the best logger to almost remove this cost, [Pino](https://github.com/pinojs/pino)!
|
|
@@ -152,6 +167,7 @@ matters to you.
|
|
|
152
167
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Hooks.md"><code><b>Hooks</b></code></a>
|
|
153
168
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Decorators.md"><code><b>Decorators</b></code></a>
|
|
154
169
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
|
|
170
|
+
* <a href="https://github.com/fastify/fastify/blob/master/docs/Fluent-Schema.md"><code><b>Fluent Schema</b></code></a>
|
|
155
171
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md"><code><b>Lifecycle</b></code></a>
|
|
156
172
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Reply.md"><code><b>Reply</b></code></a>
|
|
157
173
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Request.md"><code><b>Request</b></code></a>
|
|
@@ -166,6 +182,7 @@ matters to you.
|
|
|
166
182
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/LTS.md"><code><b>Long Term Support</b></code></a>
|
|
167
183
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/TypeScript.md"><code><b>TypeScript and types support</b></code></a>
|
|
168
184
|
* <a href="https://github.com/fastify/fastify/blob/master/docs/Serverless.md"><code><b>Serverless</b></code></a>
|
|
185
|
+
* <a href="https://github.com/fastify/fastify/blob/master/docs/Recommendations.md"><code><b>Recommendations</b></code></a>
|
|
169
186
|
|
|
170
187
|
中文文档[地址](https://github.com/fastify/docs-chinese/blob/master/README.md)
|
|
171
188
|
|
|
@@ -175,8 +192,8 @@ matters to you.
|
|
|
175
192
|
- [Live Examples](https://github.com/fastify/example) - Multirepo with a broad set of real working examples.
|
|
176
193
|
|
|
177
194
|
## Support
|
|
178
|
-
|
|
179
|
-
|
|
195
|
+
Please visit [Fastify help](https://github.com/fastify/help) to view prior
|
|
196
|
+
support issues and to ask new support questions.
|
|
180
197
|
|
|
181
198
|
## Team
|
|
182
199
|
|
|
@@ -190,6 +207,7 @@ Team members are listed in alphabetical order.
|
|
|
190
207
|
### Fastify Core team
|
|
191
208
|
* [__Tommaso Allevi__](https://github.com/allevo), <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
|
|
192
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>
|
|
193
211
|
* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
|
194
212
|
* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
|
195
213
|
* [__Dustin Deus__](https://github.com/StarpTech), <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
|
|
@@ -215,6 +233,12 @@ Great contributors on a specific area in the Fastify ecosystem will be invited t
|
|
|
215
233
|
* [__Trivikram Kamat__](https://github.com/trivikr), <https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
|
|
216
234
|
* [__Nathan Woltman__](https://github.com/nwoltman), <https://twitter.com/NathanWoltman>, <https://www.npmjs.com/~nwoltman>
|
|
217
235
|
|
|
236
|
+
## Hosted by
|
|
237
|
+
|
|
238
|
+
[<img src="https://github.com/openjs-foundation/cross-project-council/blob/master/logos/openjsf-color.png?raw=true" width="250px;"/>](https://openjsf.org/projects/#incubating)
|
|
239
|
+
|
|
240
|
+
We are currently an [incubated project](https://openjsf.org/blog/2019/11/20/web-framework-fastify-joins-openjs-foundation-as-an-incubating-project/) at the OpenJS Foundation.
|
|
241
|
+
|
|
218
242
|
## Acknowledgements
|
|
219
243
|
|
|
220
244
|
This project is kindly sponsored by:
|
|
@@ -15,12 +15,16 @@ const ajv = new Ajv({
|
|
|
15
15
|
const defaultInitOptions = {
|
|
16
16
|
bodyLimit: 1024 * 1024, // 1 MiB
|
|
17
17
|
caseSensitive: true,
|
|
18
|
+
disableRequestLogging: false,
|
|
18
19
|
ignoreTrailingSlash: false,
|
|
19
20
|
maxParamLength: 100,
|
|
20
21
|
onProtoPoisoning: 'error',
|
|
22
|
+
// TODO v3: default should be 'error'
|
|
23
|
+
onConstructorPoisoning: 'ignore',
|
|
21
24
|
pluginTimeout: 10000,
|
|
22
25
|
requestIdHeader: 'request-id',
|
|
23
|
-
requestIdLogLabel: 'reqId'
|
|
26
|
+
requestIdLogLabel: 'reqId',
|
|
27
|
+
http2SessionTimeout: 5000
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
function customRule0 (schemaParamValue, validatedParamValue, validationSchemaObject, currentDataPath, validatedParamObject, validatedParam) {
|
|
@@ -62,11 +66,17 @@ const schema = {
|
|
|
62
66
|
then: { setDefaultValue: true }
|
|
63
67
|
},
|
|
64
68
|
ignoreTrailingSlash: { type: 'boolean', default: defaultInitOptions.ignoreTrailingSlash },
|
|
69
|
+
disableRequestLogging: {
|
|
70
|
+
type: 'boolean',
|
|
71
|
+
default: false
|
|
72
|
+
},
|
|
65
73
|
maxParamLength: { type: 'integer', default: defaultInitOptions.maxParamLength },
|
|
66
74
|
onProtoPoisoning: { type: 'string', default: defaultInitOptions.onProtoPoisoning },
|
|
75
|
+
onConstructorPoisoning: { type: 'string', default: defaultInitOptions.onConstructorPoisoning },
|
|
67
76
|
pluginTimeout: { type: 'integer', default: defaultInitOptions.pluginTimeout },
|
|
68
77
|
requestIdHeader: { type: 'string', default: defaultInitOptions.requestIdHeader },
|
|
69
|
-
requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel }
|
|
78
|
+
requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel },
|
|
79
|
+
http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout }
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
|
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
|
@@ -31,6 +31,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
31
31
|
- [`fastify-objectionjs`](https://github.com/jarcodallo/fastify-objectionjs) Plugin for the `fastify` framework that provides integration with objectionjs ORM.
|
|
32
32
|
- [`fastify-postgres`](https://github.com/fastify/fastify-postgres) Fastify PostgreSQL connection plugin, with this you can share the same PostgreSQL connection pool in every part of your server.
|
|
33
33
|
- [`fastify-rate-limit`](https://github.com/fastify/fastify-rate-limit) A low overhead rate limiter for your routes.
|
|
34
|
+
- [`fastify-response-validation`](https://github.com/fastify/fastify-response-validation) A simple plugin that enables response validation for Fastify.
|
|
34
35
|
- [`fastify-nextjs`](https://github.com/fastify/fastify-nextjs) React server side rendering support for Fastify with [Next](https://github.com/zeit/next.js/).
|
|
35
36
|
- [`fastify-redis`](https://github.com/fastify/fastify-redis) Fastify Redis connection plugin, with which you can share the same Redis connection across every part of your server.
|
|
36
37
|
- [`fastify-reply-from`](https://github.com/fastify/fastify-reply-from) Plugin to forward the current http request to another server.
|
|
@@ -51,11 +52,14 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
51
52
|
- [`fastify-405`](https://github.com/Eomm/fastify-405) Fastify plugin that adds 405 HTTP status to your routes
|
|
52
53
|
- [`fastify-amqp`](https://github.com/RafaelGSS/fastify-amqp) Fastify AMQP connection plugin, to use with RabbitMQ or another connector. Just a wrapper to [`amqplib`](https://github.com/squaremo/amqp.node).
|
|
53
54
|
- [`fastify-angular-universal`](https://github.com/exequiel09/fastify-angular-universal) Angular server-side rendering support using [`@angular/platform-server`](https://github.com/angular/angular/tree/master/packages/platform-server) for Fastify
|
|
55
|
+
- [`fastify-auth0-verify`](https://github.com/nearform/fastify-auth0-verify): Auth0 verification plugin for Fastify, internally uses [fastify-jwt](https://npm.im/fastify-jwt) and [jsonwebtoken](https://npm.im/jsonwebtoken).
|
|
54
56
|
- [`fastify-autocrud`](https://github.com/paranoiasystem/fastify-autocrud) Plugin for autogenerate CRUD routes as fast as possible.
|
|
57
|
+
- [`fastify-axios`](https://github.com/davidedantonio/fastify-axios) Plugin to send HTTP requests via [axios](https://github.com/axios/axios).
|
|
55
58
|
- [`fastify-babel`](https://github.com/cfware/fastify-babel) Fastify plugin for development servers which require babel transformations of JavaScript sources.
|
|
56
59
|
- [`fastify-blipp`](https://github.com/PavelPolyakov/fastify-blipp) Prints your routes to the console, so you definitely know which endpoints are available.
|
|
57
60
|
- [`fastify-bookshelf`](https://github.com/butlerx/fastify-bookshelfjs) Fastify plugin to add [bookshelf.js](http://bookshelfjs.org/) orm support.
|
|
58
61
|
- [`fastify-boom`](https://github.com/jeromemacias/fastify-boom) Fastify plugin to add [boom](https://github.com/hapijs/boom) support.
|
|
62
|
+
- [`fastify-casl`](https://github.com/Inlecom/fastify-casl) Fastify [CASL](https://github.com/stalniy/casl) plugin that supports ACL-like protection of endpoints via either a preSerialization & preHandler hook, sanitizing the inputs and outputs of your application based on user rights.
|
|
59
63
|
- [`fastify-cloudevents`](https://github.com/smartiniOnGitHub/fastify-cloudevents) Fastify plugin to generate and forward Fastify events in the Cloudevents format.
|
|
60
64
|
- [`fastify-cockroachdb`](https://github.com/alex-ppg/fastify-cockroachdb) Fastify plugin to connect to a CockroachDB PostgreSQL instance via the Sequelize ORM.
|
|
61
65
|
- [`fastify-couchdb`](https://github.com/nigelhanlon/fastify-couchdb) Fastify plugin to add CouchDB support via [nano](https://github.com/apache/nano).
|
|
@@ -64,6 +68,9 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
64
68
|
- [`fastify-decorators`](https://github.com/L2jLiga/fastify-decorators) Fastify plugin that provides the set of TypeScript decorators.
|
|
65
69
|
- [`fastify-dynamodb`](https://github.com/matrus2/fastify-dynamodb) AWS DynamoDB plugin for Fastify. It exposes [AWS.DynamoDB.DocumentClient()](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) object.
|
|
66
70
|
- [`fastify-error-page`](https://github.com/hemerajs/fastify-error-page) Fastify plugin to print errors in structured HTML to the browser.
|
|
71
|
+
- [`fastify-errors-properties`](https://github.com/ShogunPanda/fastify-errors-properties) A error handling plugin for Fastify that enables additional properties in errors.
|
|
72
|
+
- [`fastify-explorer`](https://github.com/Eomm/fastify-explorer) Get control of your decorators across all the encapsulated contexts
|
|
73
|
+
- [`fastify-esso`](https://github.com/patrickpissurno/fastify-esso) The easiest authentication plugin for Fastify, with built-in support for Single sign-on (and great documentation)
|
|
67
74
|
- [`fastify-favicon`](https://github.com/smartiniOnGitHub/fastify-favicon) Fastify plugin to serve default favicon.
|
|
68
75
|
- [`fastify-feature-flags`](https://gitlab.com/m03geek/fastify-feature-flags) Fastify feature flags plugin with multiple providers support (e.g. env, [config](http://lorenwest.github.io/node-config/), [unleash](https://unleash.github.io/)).
|
|
69
76
|
- [`fastify-file-upload`](https://github.com/huangang/fastify-file-upload) Fastify plugin for uploading files.
|
|
@@ -75,6 +82,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
75
82
|
- [`fastify-healthcheck`](https://github.com/smartiniOnGitHub/fastify-healthcheck) Fastify plugin to serve an health check route and a probe script.
|
|
76
83
|
- [`fastify-hemera`](https://github.com/hemerajs/fastify-hemera) Fastify Hemera plugin, for writing reliable & fault-tolerant microservices with [nats.io](https://nats.io/).
|
|
77
84
|
- [`fastify-http2https`](https://github.com/lolo32/fastify-http2https) Redirect HTTP requests to HTTPS, both using the same port number, or different response on HTTP and HTTPS.
|
|
85
|
+
- [`fastify-https-redirect`](https://github.com/tomsvogel/fastify-https-redirect) Fastify plugin for auto redirect from http to https
|
|
78
86
|
- [`fastify-http-client`](https://github.com/kenuyx/fastify-http-client) Plugin to send HTTP(s) requests. Built upon [urllib](https://github.com/node-modules/urllib).
|
|
79
87
|
- [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify InfluxDB plugin connecting to an InfluxDB instance via the Influx default package.
|
|
80
88
|
- [`fastify-jwt-authz`](https://github.com/Ethan-Arrowood/fastify-jwt-authz) JWT user scope verifier.
|
|
@@ -90,7 +98,7 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
90
98
|
- [`fastify-mongo-memory`](https://github.com/chapuletta/fastify-mongo-memory) Fastify MongoDB in Memory Plugin for testing support.
|
|
91
99
|
- [`fastify-mongoose-api`](https://github.com/jeka-kiselyov/fastify-mongoose-api) Fastify plugin to create REST API methods based on Mongoose MongoDB models.
|
|
92
100
|
- [`fastify-mongoose-driver`](https://github.com/alex-ppg/fastify-mongoose) Fastify Mongoose plugin that connects to a MongoDB via the Mongoose plugin with support for Models.
|
|
93
|
-
- [`fastify-multer`](https://github.com/fox1t/multer) Multer is a plugin for handling multipart/form-data, which is primarily used for uploading files.
|
|
101
|
+
- [`fastify-multer`](https://github.com/fox1t/fastify-multer) Multer is a plugin for handling multipart/form-data, which is primarily used for uploading files.
|
|
94
102
|
- [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share [NATS](http://nats.io) client across Fastify.
|
|
95
103
|
- [`fastify-no-additional-properties`](https://github.com/greguz/fastify-no-additional-properties) Add `additionalProperties: false` by default to your JSON Schemas.
|
|
96
104
|
- [`fastify-no-icon`](https://github.com/jsumners/fastify-no-icon) Plugin to eliminate thrown errors for `/favicon.ico` requests.
|
|
@@ -100,12 +108,14 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
100
108
|
- [`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
|
|
101
109
|
- [`fastify-oracle`](https://github.com/cemremengu/fastify-oracle) Attaches an [`oracledb`](https://github.com/oracle/node-oracledb) connection pool to a Fastify server instance.
|
|
102
110
|
- [`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.
|
|
111
|
+
- [`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).
|
|
103
112
|
- [`fastify-rbac`](https://gitlab.com/m03geek/fastify-rbac) Fastify role-based access control plugin.
|
|
104
113
|
- [`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.
|
|
105
114
|
- [`fastify-response-time`](https://github.com/lolo32/fastify-response-time) Add `X-Response-Time` header at each request for Fastify, in milliseconds.
|
|
106
115
|
- [`fastify-reverse-routes`](https://github.com/dimonnwc3/fastify-reverse-routes) Fastify reverse routes plugin, allows to defined named routes and build path using name and parameters.
|
|
107
116
|
- [`fastify-rob-config`](https://github.com/jeromemacias/fastify-rob-config) Fastify Rob-Config integration.
|
|
108
117
|
- [`fastify-schema-constraint`](https://github.com/Eomm/fastify-schema-constraint) Choose the JSON schema to use based on request parameters.
|
|
118
|
+
- [`fastify-secure-session`](https://github.com/mcollina/fastify-secure-session) Create a secure stateless cookie session for Fastify
|
|
109
119
|
- [`fastify-sentry`](https://github.com/alex-ppg/fastify-sentry) Fastify plugin to add the Sentry SDK error handler to requests.
|
|
110
120
|
- [`fastify-sequelize`](https://github.com/lyquocnam/fastify-sequelize) Fastify plugin work with Sequelize (adapter for NodeJS -> Sqlite, Mysql, Mssql, Postgres).
|
|
111
121
|
- [`fastify-server-session`](https://github.com/jsumners/fastify-server-session) A session plugin with support for arbitrary backing caches via `fastify-caching`.
|
|
@@ -114,9 +124,10 @@ Plugins maintained by the fastify team are listed under [Core](#core) while plug
|
|
|
114
124
|
- [`fastify-sse`](https://github.com/lolo32/fastify-sse) to provide Server-Sent Events with `reply.sse( … )` to Fastify.
|
|
115
125
|
- [`fastify-tls-keygen`](https://gitlab.com/sebdeckers/fastify-tls-keygen) Automatically generate a browser-compatible, trusted, self-signed, localhost-only, TLS certificate.
|
|
116
126
|
- [`fastify-twitch-ebs-tools`](https://github.com/lukemnet/fastify-twitch-ebs-tools) Useful functions for Twitch Extension Backend Services (EBS).
|
|
127
|
+
- [`fastify-typeorm-plugin`](https://github.com/inthepocket/fastify-typeorm-plugin) Fastify plugin to work with TypeORM.
|
|
117
128
|
- [`fastify-vhost`](https://github.com/patrickpissurno/fastify-vhost) Proxy subdomain http requests to another server (useful if you want to point multiple subdomains to the same IP address, while running different servers on the same machine).
|
|
118
129
|
- [`fastify-xml-body-parser`](https://github.com/NaturalIntelligence/fastify-xml-body-parser) Parse XML payload / request body into JS / JSON object.
|
|
119
|
-
- [`fastify-vue-plugin`](https://github.com/TheNoim/fastify-vue) [Nuxt.js](https://nuxtjs.org) plugin for fastify.
|
|
130
|
+
- [`fastify-vue-plugin`](https://github.com/TheNoim/fastify-vue) [Nuxt.js](https://nuxtjs.org) plugin for fastify. Control the routes nuxt should use.
|
|
120
131
|
- [`fastify-wamp-router`](https://github.com/lependu/fastify-wamp-router) Web Application Messaging Protocol router for fastify.
|
|
121
132
|
- [`fast-water`](https://github.com/tswayne/fast-water) A fastify plugin for waterline. Decorates fastify with waterline models.
|
|
122
133
|
- [`fastify-webpack-hmr`](https://github.com/lependu/fastify-webpack-hmr) Webpack hot module reloading plugin for Fastify.
|
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
|
|
|
@@ -120,3 +125,8 @@ The JSON schema provided to one route is not valid.
|
|
|
120
125
|
#### FST_ERR_PROMISE_NOT_FULLFILLED
|
|
121
126
|
|
|
122
127
|
A promise may not be fulfilled with 'undefined' when statusCode is not 204.
|
|
128
|
+
|
|
129
|
+
<a name="FST_ERR_SEND_UNDEFINED_ERR"></a>
|
|
130
|
+
#### FST_ERR_SEND_UNDEFINED_ERR
|
|
131
|
+
|
|
132
|
+
Undefined error has occured.
|
package/docs/Fluent-Schema.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
The [Validation and Serialization](https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md) documentation outlines all parameters accepted by Fastify to set up JSON Schema Validation in order to validate the input, and JSON Schema Serialization in order to optimize the output.
|
|
6
6
|
|
|
7
|
-
[`fluent-schema`](fluent-schema
|
|
7
|
+
[`fluent-schema`](https://github.com/fastify/fluent-schema) can be used to simplify this task while allowing the reuse of constants.
|
|
8
8
|
|
|
9
9
|
### Basic settings
|
|
10
10
|
|
|
@@ -115,5 +115,3 @@ fastify.post('/the/url', { schema }, handler)
|
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
NB: you can mix up the `$ref-way` and the `replace-way` when using `fastify.addSchema`.
|
|
118
|
-
|
|
119
|
-
[fluent-schema-repo]: https://github.com/fastify/fluent-schema
|