fastify 3.20.0 → 3.21.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/README.md +27 -0
- package/docs/ContentTypeParser.md +2 -2
- package/docs/Ecosystem.md +6 -2
- package/docs/Getting-Started.md +77 -4
- package/docs/Guides/Index.md +6 -0
- package/docs/Hooks.md +42 -0
- package/docs/Lifecycle.md +1 -1
- package/docs/Recommendations.md +80 -1
- package/docs/Reply.md +4 -4
- package/docs/Request.md +18 -1
- package/docs/Server.md +71 -5
- package/docs/TypeScript.md +2 -2
- package/docs/Validation-and-Serialization.md +47 -4
- package/fastify.d.ts +2 -0
- package/fastify.js +18 -5
- package/lib/contentTypeParser.js +3 -5
- package/lib/reply.js +6 -1
- package/lib/request.js +6 -0
- package/lib/route.js +4 -3
- package/lib/schema-controller.js +12 -7
- package/package.json +5 -4
- package/test/bundler/README.md +29 -0
- package/test/bundler/webpack/bundler-test.js +24 -0
- package/test/bundler/webpack/package.json +11 -0
- package/test/bundler/webpack/src/fail-plugin-version.js +14 -0
- package/test/bundler/webpack/src/index.js +9 -0
- package/test/bundler/webpack/webpack.config.js +13 -0
- package/test/custom-parser.test.js +111 -0
- package/test/diagnostics-channel.test.js +61 -0
- package/test/reply-error.test.js +71 -0
- package/test/request-error.test.js +72 -0
- package/test/route.test.js +22 -0
- package/test/schema-feature.test.js +1 -1
- package/test/schema-serialization.test.js +41 -0
- package/test/schema-special-usage.test.js +75 -2
- package/test/schema-validation.test.js +100 -0
- package/test/types/fastify.test-d.ts +4 -0
- package/test/types/instance.test-d.ts +10 -2
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/request.test-d.ts +4 -1
- package/types/.eslintrc.json +2 -1
- package/types/instance.d.ts +4 -3
- package/types/request.d.ts +1 -1
- package/types/schema.d.ts +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,19 @@ Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yello
|
|
|
28
28
|
An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users.
|
|
29
29
|
How can you efficiently handle the resources of your server, knowing that you are serving the highest number of requests as possible, without sacrificing security validations and handy development?
|
|
30
30
|
|
|
31
|
+
- [Quick start](./README.md#quick-start)
|
|
32
|
+
- [Install](./README.md#install)
|
|
33
|
+
- [Example](./README.md#example)
|
|
34
|
+
- [Fastify v1.x and v2.x](./README.md#fastify-v1x-and-v2x)
|
|
35
|
+
- [Core features](./README.md#core-features)
|
|
36
|
+
- [Benchmarks](./README.md#benchmarks)
|
|
37
|
+
- [Documentation](./README.md#documentation)
|
|
38
|
+
- [Ecosystem](./README.md#ecosystem)
|
|
39
|
+
- [Support](./README.md#support)
|
|
40
|
+
- [Team](./README.md#team)
|
|
41
|
+
- [Hosted by](./README.md#hosted-by)
|
|
42
|
+
- [License](./README.md#license)
|
|
43
|
+
|
|
31
44
|
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
45
|
|
|
33
46
|
### Quick start
|
|
@@ -84,6 +97,13 @@ yarn add fastify
|
|
|
84
97
|
|
|
85
98
|
```js
|
|
86
99
|
// Require the framework and instantiate it
|
|
100
|
+
|
|
101
|
+
// ESM
|
|
102
|
+
import Fastify from 'fastify'
|
|
103
|
+
const fastify = Fastify({
|
|
104
|
+
logger: true
|
|
105
|
+
})
|
|
106
|
+
// CommonJs
|
|
87
107
|
const fastify = require('fastify')({
|
|
88
108
|
logger: true
|
|
89
109
|
})
|
|
@@ -103,6 +123,12 @@ fastify.listen(3000, (err, address) => {
|
|
|
103
123
|
with async-await:
|
|
104
124
|
|
|
105
125
|
```js
|
|
126
|
+
// ESM
|
|
127
|
+
import Fastify from 'fastify'
|
|
128
|
+
const fastify = Fastify({
|
|
129
|
+
logger: true
|
|
130
|
+
})
|
|
131
|
+
// CommonJs
|
|
106
132
|
const fastify = require('fastify')({
|
|
107
133
|
logger: true
|
|
108
134
|
})
|
|
@@ -162,6 +188,7 @@ matters to you.
|
|
|
162
188
|
|
|
163
189
|
## Documentation
|
|
164
190
|
* <a href="./docs/Getting-Started.md"><code><b>Getting Started</b></code></a>
|
|
191
|
+
* <a href="./docs/Guides/Index.md"><code><b>Guides</b></code></a>
|
|
165
192
|
* <a href="./docs/Server.md"><code><b>Server</b></code></a>
|
|
166
193
|
* <a href="./docs/Routes.md"><code><b>Routes</b></code></a>
|
|
167
194
|
* <a href="./docs/Encapsulation.md"><code><b>Encapsulation</b></code></a>
|
|
@@ -85,7 +85,7 @@ fastify.addContentTypeParser('text/xml', function (request, payload, done) {
|
|
|
85
85
|
})
|
|
86
86
|
|
|
87
87
|
// Removes the both built-in content type parsers so that only the content type parser for text/html is available
|
|
88
|
-
|
|
88
|
+
fastify.removeContentTypeParser(['application/json', 'text/plain'])
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
#### removeAllContentTypeParsers
|
|
@@ -97,7 +97,7 @@ Just like `removeContentTypeParser`, this API supports encapsulation. The API is
|
|
|
97
97
|
[catch-all content type parser](#Catch-All) that should be executed for every content type and the built-in parsers should be ignored as well.
|
|
98
98
|
|
|
99
99
|
```js
|
|
100
|
-
|
|
100
|
+
fastify.removeAllContentTypeParsers()
|
|
101
101
|
|
|
102
102
|
fastify.addContentTypeParser('text/xml', function (request, payload, done) {
|
|
103
103
|
xmlParser(payload, function (err, body) {
|
package/docs/Ecosystem.md
CHANGED
|
@@ -44,9 +44,10 @@ Plugins maintained by the Fastify team are listed under [Core](#core) while plug
|
|
|
44
44
|
- [`fastify-routes`](https://github.com/fastify/fastify-routes) Plugin that provides a `Map` of routes.
|
|
45
45
|
- [`fastify-schedule`](https://github.com/fastify/fastify-schedule) Plugin for scheduling periodic jobs, based on [toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
46
46
|
- [`fastify-sensible`](https://github.com/fastify/fastify-sensible) Defaults for Fastify that everyone can agree on. It adds some useful decorators such as HTTP errors and assertions, but also more request and reply methods.
|
|
47
|
+
- [`@fastify/session`](https://github.com/fastify/session) a session plugin for Fastify.
|
|
47
48
|
- [`fastify-static`](https://github.com/fastify/fastify-static) Plugin for serving static files as fast as possible.
|
|
48
49
|
- [`fastify-swagger`](https://github.com/fastify/fastify-swagger) Plugin for serving Swagger/OpenAPI documentation for Fastify, supporting dynamic generation.
|
|
49
|
-
- [`fastify-websocket`](https://github.com/fastify/fastify-websocket) WebSocket support for Fastify. Built upon [
|
|
50
|
+
- [`fastify-websocket`](https://github.com/fastify/fastify-websocket) WebSocket support for Fastify. Built upon [ws](https://github.com/websockets/ws).
|
|
50
51
|
- [`fastify-url-data`](https://github.com/fastify/fastify-url-data) Decorate the `Request` object with a method to access raw URL components.
|
|
51
52
|
- [`point-of-view`](https://github.com/fastify/point-of-view) Templates rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
|
|
52
53
|
- [`under-pressure`](https://github.com/fastify/under-pressure) Measure process load with automatic handling of _"Service Unavailable"_ plugin for Fastify.
|
|
@@ -69,9 +70,11 @@ Plugins maintained by the Fastify team are listed under [Core](#core) while plug
|
|
|
69
70
|
- [`arecibo`](https://github.com/nucleode/arecibo) Fastify ping responder for Kubernetes Liveness and Readiness Probes.
|
|
70
71
|
- [`cls-rtracer`](https://github.com/puzpuzpuz/cls-rtracer) Fastify middleware for CLS-based request ID generation. An out-of-the-box solution for adding request IDs into your logs.
|
|
71
72
|
- [`fastify-405`](https://github.com/Eomm/fastify-405) Fastify plugin that adds 405 HTTP status to your routes
|
|
73
|
+
- [`fastify-allow`](https://github.com/mattbishop/fastify-allow) Fastify plugin that automatically adds an Allow header to responses with routes. Also sends 405 responses for routes that have a handler but not for the request's method.
|
|
72
74
|
- [`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).
|
|
73
75
|
- [`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
|
|
74
76
|
- [`fastify-api-key`](https://github.com/arkerone/fastify-api-key) Fastify plugin to authenticate HTTP requests based on api key and signature
|
|
77
|
+
- [`fastify-appwrite`](https://github.com/Dev-Manny/fastify-appwrite) Fastify Plugin for interacting with Appwrite server.
|
|
75
78
|
- [`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).
|
|
76
79
|
- [`fastify-autocrud`](https://github.com/paranoiasystem/fastify-autocrud) Plugin to auto-generate CRUD routes as fast as possible.
|
|
77
80
|
- [`fastify-autoroutes`](https://github.com/GiovanniCardamone/fastify-autoroutes) Plugin to scan and load routes based on filesystem path from a custom directory.
|
|
@@ -81,6 +84,7 @@ Plugins maintained by the Fastify team are listed under [Core](#core) while plug
|
|
|
81
84
|
- [`fastify-blipp`](https://github.com/PavelPolyakov/fastify-blipp) Prints your routes to the console, so you definitely know which endpoints are available.
|
|
82
85
|
- [`fastify-bookshelf`](https://github.com/butlerx/fastify-bookshelfjs) Fastify plugin to add [bookshelf.js](https://bookshelfjs.org/) ORM support.
|
|
83
86
|
- [`fastify-boom`](https://github.com/jeromemacias/fastify-boom) Fastify plugin to add [boom](https://github.com/hapijs/boom) support.
|
|
87
|
+
- [`fastify-bree`](https://github.com/climba03003/fastify-bree) Fastify plugin to add [bree](https://github.com/breejs/bree) support.
|
|
84
88
|
- [`fastify-casbin`](https://github.com/nearform/fastify-casbin) Casbin support for Fastify.
|
|
85
89
|
- [`fastify-casbin-rest`](https://github.com/nearform/fastify-casbin-rest) Casbin support for Fastify based on a RESTful model.
|
|
86
90
|
- [`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.
|
|
@@ -177,7 +181,7 @@ Plugins maintained by the Fastify team are listed under [Core](#core) while plug
|
|
|
177
181
|
- [`fastify-sentry`](https://github.com/alex-ppg/fastify-sentry) Fastify plugin to add the Sentry SDK error handler to requests.
|
|
178
182
|
- [`fastify-sequelize`](https://github.com/lyquocnam/fastify-sequelize) Fastify plugin work with Sequelize (adapter for NodeJS -> Sqlite, Mysql, Mssql, Postgres).
|
|
179
183
|
- [`fastify-server-session`](https://github.com/jsumners/fastify-server-session) A session plugin with support for arbitrary backing caches via `fastify-caching`.
|
|
180
|
-
- [`fastify-
|
|
184
|
+
- [`fastify-slonik`](https://github.com/Unbuttun/fastify-slonik) Fastify Slonik plugin, with this you can use slonik in every part of your server.
|
|
181
185
|
- [`fastify-soap-client`](https://github.com/fastify/fastify-soap-client) a SOAP client plugin for Fastify.
|
|
182
186
|
- [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) a Socket.io plugin for Fastify.
|
|
183
187
|
- [`fastify-sse`](https://github.com/lolo32/fastify-sse) to provide Server-Sent Events with `reply.sse( … )` to Fastify.
|
package/docs/Getting-Started.md
CHANGED
|
@@ -21,6 +21,13 @@ yarn add fastify
|
|
|
21
21
|
Let's write our first server:
|
|
22
22
|
```js
|
|
23
23
|
// Require the framework and instantiate it
|
|
24
|
+
|
|
25
|
+
// ESM
|
|
26
|
+
import Fastify from 'fastify'
|
|
27
|
+
const fastify = Fastify({
|
|
28
|
+
logger: true
|
|
29
|
+
})
|
|
30
|
+
// CommonJs
|
|
24
31
|
const fastify = require('fastify')({
|
|
25
32
|
logger: true
|
|
26
33
|
})
|
|
@@ -36,13 +43,19 @@ fastify.listen(3000, function (err, address) {
|
|
|
36
43
|
fastify.log.error(err)
|
|
37
44
|
process.exit(1)
|
|
38
45
|
}
|
|
39
|
-
|
|
46
|
+
// Server is now listening on ${address}
|
|
40
47
|
})
|
|
41
48
|
```
|
|
42
49
|
|
|
43
50
|
Do you prefer to use `async/await`? Fastify supports it out-of-the-box.<br>
|
|
44
51
|
*(We also suggest using [make-promises-safe](https://github.com/mcollina/make-promises-safe) to avoid file descriptor and memory leaks.)*
|
|
45
52
|
```js
|
|
53
|
+
// ESM
|
|
54
|
+
import Fastify from 'fastify'
|
|
55
|
+
const fastify = Fastify({
|
|
56
|
+
logger: true
|
|
57
|
+
})
|
|
58
|
+
// CommonJs
|
|
46
59
|
const fastify = require('fastify')({
|
|
47
60
|
logger: true
|
|
48
61
|
})
|
|
@@ -89,6 +102,26 @@ As with JavaScript, where everything is an object, with Fastify everything is a
|
|
|
89
102
|
Before digging into it, let's see how it works!<br>
|
|
90
103
|
Let's declare our basic server, but instead of declaring the route inside the entry point, we'll declare it in an external file (check out the [route declaration](Routes.md) docs).
|
|
91
104
|
```js
|
|
105
|
+
// ESM
|
|
106
|
+
import Fastify from 'fastify'
|
|
107
|
+
import firstRoute from './our-first-route'
|
|
108
|
+
const fastify = Fastify({
|
|
109
|
+
logger: true
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
fastify.register(firstRoute)
|
|
113
|
+
|
|
114
|
+
fastify.listen(3000, function (err, address) {
|
|
115
|
+
if (err) {
|
|
116
|
+
fastify.log.error(err)
|
|
117
|
+
process.exit(1)
|
|
118
|
+
}
|
|
119
|
+
// Server is now listening on ${address}
|
|
120
|
+
})
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
// CommonJs
|
|
92
125
|
const fastify = require('fastify')({
|
|
93
126
|
logger: true
|
|
94
127
|
})
|
|
@@ -100,7 +133,7 @@ fastify.listen(3000, function (err, address) {
|
|
|
100
133
|
fastify.log.error(err)
|
|
101
134
|
process.exit(1)
|
|
102
135
|
}
|
|
103
|
-
|
|
136
|
+
// Server is now listening on ${address}
|
|
104
137
|
})
|
|
105
138
|
```
|
|
106
139
|
|
|
@@ -132,6 +165,28 @@ npm i --save fastify-plugin fastify-mongodb
|
|
|
132
165
|
|
|
133
166
|
**server.js**
|
|
134
167
|
```js
|
|
168
|
+
// ESM
|
|
169
|
+
import Fastify from 'fastify'
|
|
170
|
+
import dbConnector from './our-db-connector'
|
|
171
|
+
import firstRoute from './our-first-route'
|
|
172
|
+
|
|
173
|
+
const fastify = Fastify({
|
|
174
|
+
logger: true
|
|
175
|
+
})
|
|
176
|
+
fastify.register(dbConnector)
|
|
177
|
+
fastify.register(firstRoute)
|
|
178
|
+
|
|
179
|
+
fastify.listen(3000, function (err, address) {
|
|
180
|
+
if (err) {
|
|
181
|
+
fastify.log.error(err)
|
|
182
|
+
process.exit(1)
|
|
183
|
+
}
|
|
184
|
+
// Server is now listening on ${address}
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
// CommonJs
|
|
135
190
|
const fastify = require('fastify')({
|
|
136
191
|
logger: true
|
|
137
192
|
})
|
|
@@ -144,13 +199,31 @@ fastify.listen(3000, function (err, address) {
|
|
|
144
199
|
fastify.log.error(err)
|
|
145
200
|
process.exit(1)
|
|
146
201
|
}
|
|
147
|
-
|
|
202
|
+
// Server is now listening on ${address}
|
|
148
203
|
})
|
|
149
204
|
|
|
150
205
|
```
|
|
151
206
|
|
|
152
207
|
**our-db-connector.js**
|
|
153
208
|
```js
|
|
209
|
+
// ESM
|
|
210
|
+
import fastifyPlugin from 'fastify-plugin'
|
|
211
|
+
import fastifyMongo from 'fastify-mongodb'
|
|
212
|
+
|
|
213
|
+
async function dbConnector (fastify, options) {
|
|
214
|
+
fastify.register(fastifyMongo, {
|
|
215
|
+
url: 'mongodb://localhost:27017/test_database'
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Wrapping a plugin function with fastify-plugin exposes the decorators
|
|
220
|
+
// and hooks, declared inside the plugin to the parent scope.
|
|
221
|
+
module.exports = fastifyPlugin(dbConnector)
|
|
222
|
+
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
```js
|
|
226
|
+
// CommonJs
|
|
154
227
|
const fastifyPlugin = require('fastify-plugin')
|
|
155
228
|
|
|
156
229
|
async function dbConnector (fastify, options) {
|
|
@@ -200,7 +273,7 @@ As you can see, we used `register` for both the database connector and the regis
|
|
|
200
273
|
This is one of the best features of Fastify, it will load your plugins in the same order you declare them, and it will load the next plugin only once the current one has been loaded. In this way, we can register the database connector in the first plugin and use it in the second *(read [here](Plugins.md#handle-the-scope) to understand how to handle the scope of a plugin)*.
|
|
201
274
|
Plugin loading starts when you call `fastify.listen()`, `fastify.inject()` or `fastify.ready()`
|
|
202
275
|
|
|
203
|
-
|
|
276
|
+
The MongoDB plugin uses the `decorate` API to add custom objects to the Fastify instance, making them available for use everywhere. Use of this API is encouraged to facilitate easy code reuse and to decrease code or logic duplication.
|
|
204
277
|
|
|
205
278
|
To dig deeper into how Fastify plugins work, how to develop new plugins, and for details on how to use the whole Fastify API to deal with the complexity of asynchronously bootstrapping an application, read [the hitchhiker's guide to plugins](Plugins-Guide.md).
|
|
206
279
|
|
package/docs/Hooks.md
CHANGED
|
@@ -564,3 +564,45 @@ fastify.route({
|
|
|
564
564
|
```
|
|
565
565
|
|
|
566
566
|
**Note**: both options also accept an array of functions.
|
|
567
|
+
|
|
568
|
+
## Diagnostics Channel Hooks
|
|
569
|
+
|
|
570
|
+
> **Note:** The `diagnostics_channel` is currently experimental on Node.js, so
|
|
571
|
+
> its API is subject to change even in semver-patch releases of Node.js. For
|
|
572
|
+
> versions of Node.js supported by Fastify where `diagnostics_channel` is
|
|
573
|
+
> unavailable, the hook will use the
|
|
574
|
+
> [polyfill](https://www.npmjs.com/package/diagnostics_channel) if it is
|
|
575
|
+
> available. Otherwise this feature will not be present.
|
|
576
|
+
|
|
577
|
+
Currently, one
|
|
578
|
+
[`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html) publish
|
|
579
|
+
event, `'fastify.initialization'`, happens at initialization time. The Fastify
|
|
580
|
+
instance is passed into the hook as a property of the object passed in. At this
|
|
581
|
+
point, the instance can be interacted with to add hooks, plugins, routes or any
|
|
582
|
+
other sort of modification.
|
|
583
|
+
|
|
584
|
+
For example, a tracing package might do something like the following (which is,
|
|
585
|
+
of course, a simplification). This would be in a file loaded in the
|
|
586
|
+
initialization of the tracking package, in the typical "require instrumentation
|
|
587
|
+
tools first" fashion.
|
|
588
|
+
|
|
589
|
+
```js
|
|
590
|
+
const tracer = /* retrieved from elsehwere in the package */
|
|
591
|
+
const dc = require('diagnostics_channel')
|
|
592
|
+
const channel = dc.channel('fastify.initialization')
|
|
593
|
+
const spans = new WeakMap()
|
|
594
|
+
|
|
595
|
+
channel.subscribe(function ({ fastify }) {
|
|
596
|
+
fastify.addHook('onRequest', (request, reply, done) => {
|
|
597
|
+
const span = tracer.startSpan('fastify.request')
|
|
598
|
+
spans.set(request, span)
|
|
599
|
+
done()
|
|
600
|
+
})
|
|
601
|
+
|
|
602
|
+
fastify.addHook('onResponse', (request, reply, done) => {
|
|
603
|
+
const span = spans.get(request)
|
|
604
|
+
span.finish()
|
|
605
|
+
done()
|
|
606
|
+
})
|
|
607
|
+
})
|
|
608
|
+
```
|
package/docs/Lifecycle.md
CHANGED
package/docs/Recommendations.md
CHANGED
|
@@ -39,7 +39,9 @@ For a concrete example, consider the situation where:
|
|
|
39
39
|
|
|
40
40
|
There are many reverse proxy solutions available, and your environment may
|
|
41
41
|
dictate the solution to use, e.g. AWS or GCP. Given the above, we could use
|
|
42
|
-
[HAProxy][haproxy] to solve these requirements:
|
|
42
|
+
[HAProxy][haproxy] or [Nginx][nginx] to solve these requirements:
|
|
43
|
+
|
|
44
|
+
### HAProxy
|
|
43
45
|
|
|
44
46
|
```conf
|
|
45
47
|
# The global section defines base HAProxy (engine) instance configuration.
|
|
@@ -161,6 +163,83 @@ backend static-backend
|
|
|
161
163
|
[why-use]: https://web.archive.org/web/20190821102906/https://medium.com/intrinsic/why-should-i-use-a-reverse-proxy-if-node-js-is-production-ready-5a079408b2ca
|
|
162
164
|
[haproxy]: https://www.haproxy.org/
|
|
163
165
|
|
|
166
|
+
### Nginx
|
|
167
|
+
|
|
168
|
+
```nginx
|
|
169
|
+
upstream fastify_app {
|
|
170
|
+
# more info: http://nginx.org/en/docs/http/ngx_http_upstream_module.html
|
|
171
|
+
server 10.10.11.1:80;
|
|
172
|
+
server 10.10.11.2:80;
|
|
173
|
+
server 10.10.11.3:80 backup;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
server {
|
|
177
|
+
# default server
|
|
178
|
+
listen 80 default_server;
|
|
179
|
+
listen [::]:80 default_server;
|
|
180
|
+
|
|
181
|
+
# specify host
|
|
182
|
+
# listen 80;
|
|
183
|
+
# listen [::]:80;
|
|
184
|
+
# server_name example.tld;
|
|
185
|
+
|
|
186
|
+
location / {
|
|
187
|
+
return 301 https://$host$request_uri;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
server {
|
|
192
|
+
# default server
|
|
193
|
+
listen 443 ssl http2 default_server;
|
|
194
|
+
listen [::]:443 ssl http2 default_server;
|
|
195
|
+
|
|
196
|
+
# specify host
|
|
197
|
+
# listen 443 ssl http2;
|
|
198
|
+
# listen [::]:443 ssl http2;
|
|
199
|
+
# server_name example.tld;
|
|
200
|
+
|
|
201
|
+
# public private keys
|
|
202
|
+
ssl_certificate /path/to/fullchain.pem;
|
|
203
|
+
ssl_certificate_key /path/to/private.pem;
|
|
204
|
+
ssl_trusted_certificate /path/to/chain.pem;
|
|
205
|
+
|
|
206
|
+
# use https://ssl-config.mozilla.org/ for best practice configuration
|
|
207
|
+
ssl_session_timeout 1d;
|
|
208
|
+
ssl_session_cache shared:FastifyApp:10m;
|
|
209
|
+
ssl_session_tickets off;
|
|
210
|
+
|
|
211
|
+
# modern configuration
|
|
212
|
+
ssl_protocols TLSv1.3;
|
|
213
|
+
ssl_prefer_server_ciphers off;
|
|
214
|
+
|
|
215
|
+
# HSTS (ngx_http_headers_module is required) (63072000 seconds)
|
|
216
|
+
add_header Strict-Transport-Security "max-age=63072000" always;
|
|
217
|
+
|
|
218
|
+
# OCSP stapling
|
|
219
|
+
ssl_stapling on;
|
|
220
|
+
ssl_stapling_verify on;
|
|
221
|
+
|
|
222
|
+
# custom resolver
|
|
223
|
+
# resolver 127.0.0.1;
|
|
224
|
+
|
|
225
|
+
location / {
|
|
226
|
+
# more info: http://nginx.org/en/docs/http/ngx_http_proxy_module.html
|
|
227
|
+
proxy_http_version 1.1;
|
|
228
|
+
proxy_cache_bypass $http_upgrade;
|
|
229
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
230
|
+
proxy_set_header Connection 'upgrade';
|
|
231
|
+
proxy_set_header Host $host;
|
|
232
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
233
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
234
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
235
|
+
|
|
236
|
+
proxy_pass http://fastify_app:3000;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
[nginx]: http://nginx.org/
|
|
242
|
+
|
|
164
243
|
## Kubernetes
|
|
165
244
|
<a id="kubernetes"></a>
|
|
166
245
|
|
package/docs/Reply.md
CHANGED
|
@@ -53,8 +53,8 @@ and properties:
|
|
|
53
53
|
- `.serializer(function)` - Sets a custom serializer for the payload.
|
|
54
54
|
- `.send(payload)` - Sends the payload to the user, could be a plain text, a buffer, JSON, stream, or an Error object.
|
|
55
55
|
- `.sent` - A boolean value that you can use if you need to know if `send` has already been called.
|
|
56
|
-
- `.raw` - The [`http.ServerResponse`](https://nodejs.org/dist/latest/docs/api/http.html#http_class_http_serverresponse) from Node core.
|
|
57
|
-
- `.res` *(deprecated, use `.raw` instead)* - The [`http.ServerResponse`](https://nodejs.org/dist/latest/docs/api/http.html#http_class_http_serverresponse) from Node core.
|
|
56
|
+
- `.raw` - The [`http.ServerResponse`](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_class_http_serverresponse) from Node core.
|
|
57
|
+
- `.res` *(deprecated, use `.raw` instead)* - The [`http.ServerResponse`](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_class_http_serverresponse) from Node core.
|
|
58
58
|
- `.log` - The logger instance of the incoming request.
|
|
59
59
|
- `.request` - The incoming request.
|
|
60
60
|
- `.context` - Access the [Request's context](Request.md#Request) property.
|
|
@@ -109,7 +109,7 @@ fastify.get('/', async function (req, rep) {
|
|
|
109
109
|
Sets a response header. If the value is omitted or undefined, it is coerced
|
|
110
110
|
to `''`.
|
|
111
111
|
|
|
112
|
-
For more information, see [`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest/docs/api/http.html#http_response_setheader_name_value).
|
|
112
|
+
For more information, see [`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_response_setheader_name_value).
|
|
113
113
|
|
|
114
114
|
<a name="headers"></a>
|
|
115
115
|
### .headers(object)
|
|
@@ -228,7 +228,7 @@ See [`.send()`](#send) for more information on sending different types of values
|
|
|
228
228
|
|
|
229
229
|
<a name="raw"></a>
|
|
230
230
|
### .raw
|
|
231
|
-
This is the [`http.ServerResponse`](https://nodejs.org/dist/latest/docs/api/http.html#http_class_http_serverresponse) from Node core. Whilst you are using the Fastify `Reply` object, the use of `Reply.raw` functions is at your own risk as you are skipping all the Fastify
|
|
231
|
+
This is the [`http.ServerResponse`](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_class_http_serverresponse) from Node core. Whilst you are using the Fastify `Reply` object, the use of `Reply.raw` functions is at your own risk as you are skipping all the Fastify
|
|
232
232
|
logic of handling the HTTP response. e.g.:
|
|
233
233
|
|
|
234
234
|
```js
|
package/docs/Request.md
CHANGED
|
@@ -6,7 +6,7 @@ Request is a core Fastify object containing the following fields:
|
|
|
6
6
|
- `query` - the parsed querystring
|
|
7
7
|
- `body` - the body
|
|
8
8
|
- `params` - the params matching the URL
|
|
9
|
-
- `headers` - the headers
|
|
9
|
+
- [`headers`](#headers) - the headers getter and setter
|
|
10
10
|
- `raw` - the incoming HTTP request from Node core
|
|
11
11
|
- `req` *(deprecated, use `.raw` instead)* - the incoming HTTP request from Node core
|
|
12
12
|
- `server` - The Fastify server instance, scoped to the current [encapsulation context](Encapsulation.md)
|
|
@@ -26,6 +26,20 @@ Request is a core Fastify object containing the following fields:
|
|
|
26
26
|
- `context` - A Fastify internal object. You should not use it directly or modify it. It is usefull to access one special key:
|
|
27
27
|
- `context.config` - The route [`config`](Routes.md#routes-config) object.
|
|
28
28
|
|
|
29
|
+
### Headers
|
|
30
|
+
|
|
31
|
+
The `request.headers` is a getter that return an Object with the headers of the incoming request.
|
|
32
|
+
You can set custom headers like this:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
request.headers = {
|
|
36
|
+
'foo': 'bar',
|
|
37
|
+
'baz': 'qux'
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This operation will add to the request headers the new values that can be read calling `request.headers.bar`.
|
|
42
|
+
Moreover, you can still access the standard request's headers with the `request.raw.headers` property.
|
|
29
43
|
|
|
30
44
|
```js
|
|
31
45
|
fastify.post('/:params', options, function (request, reply) {
|
|
@@ -40,6 +54,9 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
40
54
|
console.log(request.ips)
|
|
41
55
|
console.log(request.hostname)
|
|
42
56
|
console.log(request.protocol)
|
|
57
|
+
console.log(request.url)
|
|
58
|
+
console.log(request.routerMethod)
|
|
59
|
+
console.log(request.routerPath)
|
|
43
60
|
request.log.info('some info')
|
|
44
61
|
})
|
|
45
62
|
```
|
package/docs/Server.md
CHANGED
|
@@ -8,10 +8,42 @@ The Fastify module exports a factory function that is used to create new
|
|
|
8
8
|
an options object which is used to customize the resulting instance. This
|
|
9
9
|
document describes the properties available in that options object.
|
|
10
10
|
|
|
11
|
+
- [http2](./Server.md#http2)
|
|
12
|
+
- [https](./Server.md#https)
|
|
13
|
+
- [connectionTimeout](./Server.md#connectiontimeout)
|
|
14
|
+
- [keepAliveTimeout](./Server.md#keepalivetimeout)
|
|
15
|
+
- [ignoreTrailingSlash](./Server.md#ignoretrailingslash)
|
|
16
|
+
- [maxParamLength](./Server.md#maxparamlength)
|
|
17
|
+
- [onProtoPoisoning](./Server.md#onprotopoisoning)
|
|
18
|
+
- [onConstructorPoisoning](./Server.md#onconstructorpoisoning)
|
|
19
|
+
- [logger](./Server.md#logger)
|
|
20
|
+
- [serverFactory](./Server.md#serverfactory)
|
|
21
|
+
- [jsonShorthand](./Server.md#jsonshorthand)
|
|
22
|
+
- [caseSensitive](./Server.md#casesensitive)
|
|
23
|
+
- [requestIdHeader](./Server.md#requestidheader)
|
|
24
|
+
- [requestIdLogLabel](./Server.md#requestidloglabel)
|
|
25
|
+
- [genReqId](./Server.md#genreqid)
|
|
26
|
+
- [trustProxy](./Server.md#trustProxy)
|
|
27
|
+
- [pluginTimeout](./Server.md#plugintimeout)
|
|
28
|
+
- [querystringParser](./Server.md#querystringParser)
|
|
29
|
+
- [exposeHeadRoutes](./Server.md#exposeheadroutes)
|
|
30
|
+
- [constraints](./Server.md#constraints)
|
|
31
|
+
- [return503OnClosing](./Server.md#return503onclosing)
|
|
32
|
+
- [ajv](./Server.md#ajv)
|
|
33
|
+
- [serializerOpts](./Server.md#serializeropts)
|
|
34
|
+
- [http2SessionTimeout](./Server.md#http2sessiontimeout)
|
|
35
|
+
- [frameworkErrors](./Server.md#frameworkerrors)
|
|
36
|
+
- [clientErrorHandler](./Server.md#clienterrorhandler)
|
|
37
|
+
- [rewriteUrl](./Server.md#rewriteurl)
|
|
38
|
+
- [Instance](./Server.md#instance)
|
|
39
|
+
- [Server Methods](./Server.md#server-methods)
|
|
40
|
+
- [initialConfig](./Server.md#initialConfig)
|
|
41
|
+
|
|
42
|
+
|
|
11
43
|
<a name="factory-http2"></a>
|
|
12
44
|
### `http2`
|
|
13
45
|
|
|
14
|
-
If `true` Node.js core's [HTTP/2](https://nodejs.org/dist/latest-
|
|
46
|
+
If `true` Node.js core's [HTTP/2](https://nodejs.org/dist/latest-v14.x/docs/api/http2.html) module is used for binding the socket.
|
|
15
47
|
|
|
16
48
|
+ Default: `false`
|
|
17
49
|
|
|
@@ -20,7 +52,7 @@ If `true` Node.js core's [HTTP/2](https://nodejs.org/dist/latest-v8.x/docs/api/h
|
|
|
20
52
|
|
|
21
53
|
An object used to configure the server's listening socket for TLS. The options
|
|
22
54
|
are the same as the Node.js core
|
|
23
|
-
[`createServer` method](https://nodejs.org/dist/latest-
|
|
55
|
+
[`createServer` method](https://nodejs.org/dist/latest-v14.x/docs/api/https.html#https_https_createserver_options_requestlistener).
|
|
24
56
|
When this property is `null`, the socket will not be configured for TLS.
|
|
25
57
|
|
|
26
58
|
This option also applies when the
|
|
@@ -402,7 +434,7 @@ If `false`, the server routes the incoming request as usual.
|
|
|
402
434
|
<a name="factory-ajv"></a>
|
|
403
435
|
### `ajv`
|
|
404
436
|
|
|
405
|
-
Configure the Ajv instance used by Fastify without providing a custom one.
|
|
437
|
+
Configure the Ajv v6 instance used by Fastify without providing a custom one.
|
|
406
438
|
|
|
407
439
|
+ Default:
|
|
408
440
|
|
|
@@ -426,8 +458,8 @@ const fastify = require('fastify')({
|
|
|
426
458
|
nullable: false // Refer to [ajv options](https://ajv.js.org/#options)
|
|
427
459
|
},
|
|
428
460
|
plugins: [
|
|
429
|
-
require('ajv-merge-patch')
|
|
430
|
-
[require('ajv-keywords'), 'instanceof']
|
|
461
|
+
require('ajv-merge-patch'),
|
|
462
|
+
[require('ajv-keywords'), 'instanceof']
|
|
431
463
|
// Usage: [plugin, pluginOptions] - Plugin with options
|
|
432
464
|
// Usage: plugin - Plugin without options
|
|
433
465
|
]
|
|
@@ -895,6 +927,9 @@ It can be useful when your schemas are stored in another data structure that is
|
|
|
895
927
|
See [issue #2446](https://github.com/fastify/fastify/issues/2446) for an example of what
|
|
896
928
|
this property helps to resolve.
|
|
897
929
|
|
|
930
|
+
Another use case is to tweak all the schemas processing.
|
|
931
|
+
Doing so it is possible to use Ajv v8, instead of the default v6! We will see an example of this later.
|
|
932
|
+
|
|
898
933
|
```js
|
|
899
934
|
const fastify = Fastify({
|
|
900
935
|
schemaController: {
|
|
@@ -968,6 +1003,37 @@ const fastify = Fastify({
|
|
|
968
1003
|
});
|
|
969
1004
|
```
|
|
970
1005
|
|
|
1006
|
+
##### Ajv 8 as default schema validator
|
|
1007
|
+
|
|
1008
|
+
Ajv 8 is the evolution of Ajv 6, and it has a lot of improvements and new features.
|
|
1009
|
+
To use the new Ajv 8 features such as JTD or the Standalone mode, refers to the [`@fastify/ajv-compiler` documentation](https://github.com/fastify/ajv-compiler#usage).
|
|
1010
|
+
|
|
1011
|
+
To use Ajv 8 as default schema validator, you can use the following code:
|
|
1012
|
+
|
|
1013
|
+
```js
|
|
1014
|
+
const AjvCompiler = require('@fastify/ajv-compiler') // It must be the v2.x.x version
|
|
1015
|
+
|
|
1016
|
+
// Note that the `format` schema's keyword is no longer supported on Ajv 8 by default.
|
|
1017
|
+
// So you need to add it manually.
|
|
1018
|
+
const ajvFormats = require('ajv-formats')
|
|
1019
|
+
|
|
1020
|
+
const app = fastify({
|
|
1021
|
+
ajv: {
|
|
1022
|
+
customOptions: {
|
|
1023
|
+
validateFormats: true
|
|
1024
|
+
},
|
|
1025
|
+
plugins: [ajvFormats]
|
|
1026
|
+
},
|
|
1027
|
+
schemaController: {
|
|
1028
|
+
compilersFactory: {
|
|
1029
|
+
buildValidator: AjvCompiler()
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
})
|
|
1033
|
+
|
|
1034
|
+
// Done! You can now use Ajv 8 options and keywords into your schemas!
|
|
1035
|
+
```
|
|
1036
|
+
|
|
971
1037
|
<a name="set-not-found-handler"></a>
|
|
972
1038
|
#### setNotFoundHandler
|
|
973
1039
|
|
package/docs/TypeScript.md
CHANGED
|
@@ -85,7 +85,7 @@ The type system heavily relies on generic properties to provide the most accurat
|
|
|
85
85
|
'h-Custom': string;
|
|
86
86
|
}
|
|
87
87
|
```
|
|
88
|
-
3. Using the two interfaces, define a new API route and pass them as generics. The shorthand route methods (i.e. `.get`) accept a generic object `
|
|
88
|
+
3. Using the two interfaces, define a new API route and pass them as generics. The shorthand route methods (i.e. `.get`) accept a generic object `RouteGenericInterface` containing five named properties: `Body`, `Querystring`, `Params`, `Headers` and `Reply`. The interfaces `Body`, `Querystring`, `Params` and `Headers` will be passed down through the route method into the route method handler `request` instance and the `Reply` interface to the `reply` instance.
|
|
89
89
|
```typescript
|
|
90
90
|
server.get<{
|
|
91
91
|
Querystring: IQuerystring,
|
|
@@ -599,7 +599,7 @@ The Fastify API is powered by the `fastify()` method. In JavaScript you would im
|
|
|
599
599
|
const f = fastify()
|
|
600
600
|
f.listen(8080, () => { console.log('running') })
|
|
601
601
|
```
|
|
602
|
-
- Destructuring is
|
|
602
|
+
- Destructuring is supported and will resolve types properly
|
|
603
603
|
```typescript
|
|
604
604
|
const { fastify } = require('fastify')
|
|
605
605
|
|