fastify 4.0.0-rc.5 → 4.0.2
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/.markdownlint-cli2.yaml +22 -0
- package/GOVERNANCE.md +30 -20
- package/PROJECT_CHARTER.md +48 -17
- package/README.md +164 -77
- package/SECURITY.md +55 -44
- package/build/build-error-serializer.js +12 -7
- package/docs/Guides/Benchmarking.md +2 -0
- package/docs/Guides/Delay-Accepting-Requests.md +98 -90
- package/docs/Guides/Ecosystem.md +48 -38
- package/docs/Guides/Index.md +2 -0
- package/docs/Guides/Migration-Guide-V3.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +55 -0
- package/docs/Guides/Plugins-Guide.md +3 -3
- package/docs/Guides/Prototype-Poisoning.md +1 -1
- package/docs/Guides/Recommendations.md +2 -2
- package/docs/Guides/Serverless.md +17 -16
- package/docs/Guides/Write-Plugin.md +3 -3
- package/docs/Reference/ContentTypeParser.md +17 -13
- package/docs/Reference/Errors.md +6 -5
- package/docs/Reference/Middleware.md +3 -3
- package/docs/Reference/Plugins.md +8 -6
- package/docs/Reference/Reply.md +30 -16
- package/docs/Reference/Request.md +3 -3
- package/docs/Reference/Routes.md +113 -38
- package/docs/Reference/Server.md +109 -72
- package/docs/Reference/Type-Providers.md +28 -8
- package/docs/Reference/TypeScript.md +12 -6
- package/docs/Reference/Validation-and-Serialization.md +47 -35
- package/fastify.js +1 -1
- package/lib/error-serializer.js +32 -204
- package/lib/pluginUtils.js +10 -0
- package/lib/reply.js +1 -1
- package/lib/schemas.js +3 -0
- package/lib/validation.js +1 -1
- package/package.json +6 -4
- package/test/build/error-serializer.test.js +28 -0
- package/test/{internals → build}/version.test.js +1 -1
- package/test/plugin.test.js +32 -0
- package/test/reply-error.test.js +7 -1
- package/test/schema-serialization.test.js +30 -0
- package/docs/Migration-Guide-V4.md +0 -12
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -12,14 +12,22 @@ section.
|
|
|
12
12
|
[accepts](https://www.npmjs.com/package/accepts) in your request object.
|
|
13
13
|
- [`@fastify/accepts-serializer`](https://github.com/fastify/fastify-accepts-serializer)
|
|
14
14
|
to serialize to output according to `Accept` header.
|
|
15
|
+
- [`@fastify/any-schema`](https://github.com/fastify/any-schema-you-like) Save
|
|
16
|
+
multiple schemas and decide which one to use to serialize the payload
|
|
15
17
|
- [`@fastify/auth`](https://github.com/fastify/fastify-auth) Run multiple auth
|
|
16
18
|
functions in Fastify.
|
|
17
19
|
- [`@fastify/autoload`](https://github.com/fastify/fastify-autoload) Require all
|
|
18
20
|
plugins in a directory.
|
|
21
|
+
- [`@fastify/awilix`](https://github.com/fastify/fastify-awilix) Dependency
|
|
22
|
+
injection support for Fastify, based on
|
|
23
|
+
[awilix](https://github.com/jeffijoe/awilix).
|
|
24
|
+
- [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) allows
|
|
25
|
+
you to easily build serverless web applications/services and RESTful APIs
|
|
26
|
+
using Fastify on top of AWS Lambda and Amazon API Gateway.
|
|
19
27
|
- [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) Basic
|
|
20
28
|
auth plugin for Fastify.
|
|
21
|
-
- [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth)
|
|
22
|
-
auth plugin for Fastify.
|
|
29
|
+
- [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth)
|
|
30
|
+
Bearer auth plugin for Fastify.
|
|
23
31
|
- [`@fastify/caching`](https://github.com/fastify/fastify-caching) General
|
|
24
32
|
server-side cache and ETag support.
|
|
25
33
|
- [`@fastify/circuit-breaker`](https://github.com/fastify/fastify-circuit-breaker)
|
|
@@ -30,7 +38,8 @@ section.
|
|
|
30
38
|
cookie headers.
|
|
31
39
|
- [`@fastify/cors`](https://github.com/fastify/fastify-cors) Enables the use of
|
|
32
40
|
CORS in a Fastify application.
|
|
33
|
-
- [`@fastify/csrf-protection`](https://github.com/fastify/csrf-protection) A
|
|
41
|
+
- [`@fastify/csrf-protection`](https://github.com/fastify/csrf-protection) A
|
|
42
|
+
plugin for adding
|
|
34
43
|
[CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection to
|
|
35
44
|
Fastify.
|
|
36
45
|
- [`@fastify/diagnostics-channel`](https://github.com/fastify/fastify-diagnostics-channel)
|
|
@@ -51,13 +60,16 @@ section.
|
|
|
51
60
|
function.
|
|
52
61
|
- [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) Important
|
|
53
62
|
security headers for Fastify.
|
|
54
|
-
- [`@fastify/hotwire`](https://github.com/fastify/fastify-hotwire) Use the
|
|
63
|
+
- [`@fastify/hotwire`](https://github.com/fastify/fastify-hotwire) Use the
|
|
64
|
+
Hotwire pattern with Fastify.
|
|
55
65
|
- [`@fastify/http-proxy`](https://github.com/fastify/fastify-http-proxy) Proxy
|
|
56
66
|
your HTTP requests to another server, with hooks.
|
|
57
|
-
- [`@fastify/jwt`](https://github.com/fastify/fastify-jwt) JWT utils for
|
|
58
|
-
internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
67
|
+
- [`@fastify/jwt`](https://github.com/fastify/fastify-jwt) JWT utils for
|
|
68
|
+
Fastify, internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
59
69
|
- [`@fastify/leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to
|
|
60
70
|
share a common LevelDB connection across Fastify.
|
|
71
|
+
- [`@fastify/middie`](https://github.com/fastify/middie) Middleware engine for
|
|
72
|
+
Fastify.
|
|
61
73
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
62
74
|
MongoDB connection plugin, with which you can share the same MongoDB
|
|
63
75
|
connection pool across every part of your server.
|
|
@@ -87,8 +99,11 @@ section.
|
|
|
87
99
|
to forward the current HTTP request to another server.
|
|
88
100
|
- [`@fastify/routes`](https://github.com/fastify/fastify-routes) Plugin that
|
|
89
101
|
provides a `Map` of routes.
|
|
90
|
-
- [`@fastify/
|
|
91
|
-
|
|
102
|
+
- [`@fastify/schedule`](https://github.com/fastify/fastify-schedule) Plugin for
|
|
103
|
+
scheduling periodic jobs, based on
|
|
104
|
+
[toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
105
|
+
- [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults
|
|
106
|
+
for Fastify that everyone can agree on. It adds some useful decorators such as
|
|
92
107
|
HTTP errors and assertions, but also more request and reply methods.
|
|
93
108
|
- [`@fastify/session`](https://github.com/fastify/session) a session plugin for
|
|
94
109
|
Fastify.
|
|
@@ -97,26 +112,15 @@ section.
|
|
|
97
112
|
- [`@fastify/swagger`](https://github.com/fastify/fastify-swagger) Plugin for
|
|
98
113
|
serving Swagger/OpenAPI documentation for Fastify, supporting dynamic
|
|
99
114
|
generation.
|
|
115
|
+
- [`@fastify/under-pressure`](https://github.com/fastify/under-pressure) Measure
|
|
116
|
+
process load with automatic handling of _"Service Unavailable"_ plugin for
|
|
117
|
+
Fastify.
|
|
118
|
+
- [`@fastify/url-data`](https://github.com/fastify/fastify-url-data) Decorate
|
|
119
|
+
the `Request` object with a method to access raw URL components.
|
|
120
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
121
|
+
rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
|
|
100
122
|
- [`@fastify/websocket`](https://github.com/fastify/fastify-websocket) WebSocket
|
|
101
123
|
support for Fastify. Built upon [ws](https://github.com/websockets/ws).
|
|
102
|
-
- [`@fastify/url-data`](https://github.com/fastify/fastify-url-data) Decorate the
|
|
103
|
-
`Request` object with a method to access raw URL components.
|
|
104
|
-
- [`any-schema-you-like`](https://github.com/fastify/any-schema-you-like) Save multiple
|
|
105
|
-
schemas and decide which one to use to serialize the payload
|
|
106
|
-
- [`aws-lambda-fastify`](https://github.com/fastify/aws-lambda-fastify) allows you to
|
|
107
|
-
easily build serverless web applications/services and RESTful APIs using Fastify
|
|
108
|
-
on top of AWS Lambda and Amazon API Gateway.
|
|
109
|
-
- [`fastify-awilix`](https://github.com/fastify/fastify-awilix) Dependency
|
|
110
|
-
injection support for Fastify, based on
|
|
111
|
-
[awilix](https://github.com/jeffijoe/awilix).
|
|
112
|
-
- [`fastify-schedule`](https://github.com/fastify/fastify-schedule) Plugin for
|
|
113
|
-
scheduling periodic jobs, based on
|
|
114
|
-
[toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
115
|
-
- [`middie`](https://github.com/fastify/middie) Middleware engine for Fastify.
|
|
116
|
-
- [`point-of-view`](https://github.com/fastify/point-of-view) Templates
|
|
117
|
-
rendering (_ejs, pug, handlebars, marko_) plugin support for Fastify.
|
|
118
|
-
- [`under-pressure`](https://github.com/fastify/under-pressure) Measure process
|
|
119
|
-
load with automatic handling of _"Service Unavailable"_ plugin for Fastify.
|
|
120
124
|
|
|
121
125
|
#### [Community](#community)
|
|
122
126
|
|
|
@@ -134,7 +138,9 @@ section.
|
|
|
134
138
|
- [`@immobiliarelabs/fastify-metrics`](https://github.com/immobiliare/fastify-metrics)
|
|
135
139
|
Minimalistic and opinionated plugin that collects usage/process metrics and
|
|
136
140
|
dispatches to [statsd](https://github.com/statsd/statsd).
|
|
137
|
-
- [`@immobiliarelabs/fastify-sentry`](https://github.com/immobiliare/fastify-sentry)
|
|
141
|
+
- [`@immobiliarelabs/fastify-sentry`](https://github.com/immobiliare/fastify-sentry)
|
|
142
|
+
Sentry errors handler that just works! Install, add your DSN and you're good
|
|
143
|
+
to go!
|
|
138
144
|
- [`@mgcrea/fastify-graceful-exit`](https://github.com/mgcrea/fastify-graceful-exit)
|
|
139
145
|
A plugin to close the server gracefully
|
|
140
146
|
- [`@mgcrea/fastify-request-logger`](https://github.com/mgcrea/fastify-request-logger)
|
|
@@ -250,8 +256,8 @@ section.
|
|
|
250
256
|
Fastify feature flags plugin with multiple providers support (e.g. env,
|
|
251
257
|
[config](https://lorenwest.github.io/node-config/),
|
|
252
258
|
[unleash](https://unleash.github.io/)).
|
|
253
|
-
- [`fastify-file-routes`](https://github.com/spa5k/fastify-file-routes)
|
|
254
|
-
|
|
259
|
+
- [`fastify-file-routes`](https://github.com/spa5k/fastify-file-routes) Get
|
|
260
|
+
Next.js based file system routing into fastify.
|
|
255
261
|
- [`fastify-file-upload`](https://github.com/huangang/fastify-file-upload)
|
|
256
262
|
Fastify plugin for uploading files.
|
|
257
263
|
- [`fastify-firebase`](https://github.com/now-ims/fastify-firebase) Fastify
|
|
@@ -302,8 +308,8 @@ section.
|
|
|
302
308
|
An error handling plugin for Fastify that uses enhanced HTTP errors.
|
|
303
309
|
- [`fastify-https-redirect`](https://github.com/tomsvogel/fastify-https-redirect)
|
|
304
310
|
Fastify plugin for auto-redirect from HTTP to HTTPS.
|
|
305
|
-
- [`fatify-impressions`](https://github.com/manju4ever/fastify-impressions)
|
|
306
|
-
plugin to track impressions of all the routes.
|
|
311
|
+
- [`fatify-impressions`](https://github.com/manju4ever/fastify-impressions)
|
|
312
|
+
Fastify plugin to track impressions of all the routes.
|
|
307
313
|
- [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify
|
|
308
314
|
InfluxDB plugin connecting to an InfluxDB instance via the Influx default
|
|
309
315
|
package.
|
|
@@ -353,7 +359,8 @@ section.
|
|
|
353
359
|
for handling multipart/form-data, which is primarily used for uploading files.
|
|
354
360
|
- [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share
|
|
355
361
|
[NATS](https://nats.io) client across Fastify.
|
|
356
|
-
- [`fastify-next-auth`](https://github.com/wobsoriano/fastify-next-auth)
|
|
362
|
+
- [`fastify-next-auth`](https://github.com/wobsoriano/fastify-next-auth)
|
|
363
|
+
NextAuth.js plugin for Fastify.
|
|
357
364
|
- [`fastify-no-additional-properties`](https://github.com/greguz/fastify-no-additional-properties)
|
|
358
365
|
Add `additionalProperties: false` by default to your JSON Schemas.
|
|
359
366
|
- [`fastify-no-icon`](https://github.com/jsumners/fastify-no-icon) Plugin to
|
|
@@ -412,7 +419,8 @@ section.
|
|
|
412
419
|
- [`fastify-qs`](https://github.com/webdevium/fastify-qs) A plugin for Fastify
|
|
413
420
|
that adds support for parsing URL query parameters with
|
|
414
421
|
[qs](https://github.com/ljharb/qs).
|
|
415
|
-
- [`fastify-racing`](https://github.com/metcoder95/fastify-racing) Fastify's
|
|
422
|
+
- [`fastify-racing`](https://github.com/metcoder95/fastify-racing) Fastify's
|
|
423
|
+
plugin that adds support to handle an aborted request asynchronous.
|
|
416
424
|
- [`fastify-raw-body`](https://github.com/Eomm/fastify-raw-body) Add the
|
|
417
425
|
`request.rawBody` field.
|
|
418
426
|
- [`fastify-rbac`](https://gitlab.com/m03geek/fastify-rbac) Fastify role-based
|
|
@@ -460,7 +468,9 @@ section.
|
|
|
460
468
|
client plugin for Fastify.
|
|
461
469
|
- [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) a
|
|
462
470
|
Socket.io plugin for Fastify.
|
|
463
|
-
- [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator)
|
|
471
|
+
- [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator)
|
|
472
|
+
Small plugin to allow you use multiple validators in one route based on each
|
|
473
|
+
HTTP part of the request.
|
|
464
474
|
- [`fastify-sse`](https://github.com/lolo32/fastify-sse) to provide Server-Sent
|
|
465
475
|
Events with `reply.sse( … )` to Fastify.
|
|
466
476
|
- [`fastify-sse-v2`](https://github.com/nodefactoryio/fastify-sse-v2) to provide
|
|
@@ -499,8 +509,8 @@ section.
|
|
|
499
509
|
waterline. Decorates Fastify with waterline models.
|
|
500
510
|
- [`fastify-webpack-hmr`](https://github.com/lependu/fastify-webpack-hmr)
|
|
501
511
|
Webpack hot module reloading plugin for Fastify.
|
|
502
|
-
- [`fastify-webpack-hot`](https://github.com/gajus/fastify-webpack-hot)
|
|
503
|
-
|
|
512
|
+
- [`fastify-webpack-hot`](https://github.com/gajus/fastify-webpack-hot) Webpack
|
|
513
|
+
Hot Module Replacement for Fastify.
|
|
504
514
|
- [`fastify-ws`](https://github.com/gj/fastify-ws) WebSocket integration for
|
|
505
515
|
Fastify — with support for WebSocket lifecycle hooks instead of a single
|
|
506
516
|
handler function. Built upon [ws](https://github.com/websockets/ws) and
|
|
@@ -531,5 +541,5 @@ section.
|
|
|
531
541
|
and lightweight Sequelize plugin for Fastify.
|
|
532
542
|
|
|
533
543
|
#### [Community Tools](#community-tools)
|
|
534
|
-
- [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
|
|
535
|
-
directory structure.
|
|
544
|
+
- [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
|
|
545
|
+
generator by directory structure.
|
package/docs/Guides/Index.md
CHANGED
|
@@ -21,6 +21,8 @@ This table of contents is in alphabetical order.
|
|
|
21
21
|
written with a fluent API and used in Fastify.
|
|
22
22
|
+ [Getting Started](./Getting-Started.md): Introduction tutorial for Fastify.
|
|
23
23
|
This is where beginners should start.
|
|
24
|
+
+ [Migration Guide (v4)](./Migration-Guide-V4.md): Details how to migrate to
|
|
25
|
+
Fastify v4 from earlier versions.
|
|
24
26
|
+ [Migration Guide (v3)](./Migration-Guide-V3.md): Details how to migrate to
|
|
25
27
|
Fastify v3 from earlier versions.
|
|
26
28
|
+ [Plugins Guide](./Plugins-Guide.md): An informal introduction to writing
|
|
@@ -15,7 +15,7 @@ framework itself.
|
|
|
15
15
|
|
|
16
16
|
If you use Express middleware in your application, please install and register
|
|
17
17
|
the [`@fastify/express`](https://github.com/fastify/fastify-express) or
|
|
18
|
-
[
|
|
18
|
+
[`@fastify/middie`](https://github.com/fastify/middie) plugin before doing so.
|
|
19
19
|
|
|
20
20
|
**v2:**
|
|
21
21
|
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# V4 Migration Guide
|
|
2
|
+
|
|
3
|
+
This guide is intended to help with migration from Fastify v3 to v4.
|
|
4
|
+
|
|
5
|
+
Before migrating to v4, please ensure that you have fixed all deprecation
|
|
6
|
+
warnings from v3. All v3 deprecations have been removed and they will no longer
|
|
7
|
+
work after upgrading.
|
|
8
|
+
|
|
9
|
+
## Breaking Changes
|
|
10
|
+
|
|
11
|
+
### Deprecation of `app.use()`
|
|
12
|
+
|
|
13
|
+
Starting this version of Fastify, we have deprecated the use of `app.use()`. We
|
|
14
|
+
have decided not to support the use of middlewares. Both
|
|
15
|
+
[`@fastify/middie`](https://github.com/fastify/middie) and
|
|
16
|
+
[`@fastify/express`](https://github.com/fastify/fastify-express) will still be
|
|
17
|
+
there and maintained. Use Fastify's [hooks](../Reference/Hooks.md) instead.
|
|
18
|
+
|
|
19
|
+
## Non Breaking Changes
|
|
20
|
+
|
|
21
|
+
### Change of schema for multiple types
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Since Fastiy v4 has upgraded to Ajv v8. The "type" keywords with multiple types
|
|
25
|
+
(other than with "null") are prohibited. Read more
|
|
26
|
+
['here'](https://ajv.js.org/strict-mode.html#strict-types)
|
|
27
|
+
|
|
28
|
+
You may encounter a console warning such as
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
strict mode: use allowUnionTypes to allow union type keyword at "#/properties/image" (strictTypes)
|
|
32
|
+
```
|
|
33
|
+
So schemas like below will need to be changed from
|
|
34
|
+
```
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
api_key: { type: 'string' },
|
|
38
|
+
image: { type: ['object', 'array'] }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
to
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
type: 'object',
|
|
46
|
+
properties: {
|
|
47
|
+
api_key: { type: 'string' },
|
|
48
|
+
image: {
|
|
49
|
+
anyOf: [
|
|
50
|
+
{ type: 'array' },
|
|
51
|
+
{ type: 'object' }
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
@@ -458,10 +458,10 @@ console.log(new CustomError())
|
|
|
458
458
|
|
|
459
459
|
If you want to deprecate an API, or you want to warn the user about a specific
|
|
460
460
|
use case, you can use the
|
|
461
|
-
[`
|
|
461
|
+
[`process-warning`](https://github.com/fastify/process-warning) module.
|
|
462
462
|
|
|
463
463
|
```js
|
|
464
|
-
const warning = require('
|
|
464
|
+
const warning = require('process-warning')()
|
|
465
465
|
warning.create('FastifyDeprecation', 'FST_ERROR_CODE', 'message')
|
|
466
466
|
warning.emit('FST_ERROR_CODE')
|
|
467
467
|
```
|
|
@@ -475,7 +475,7 @@ will add it to the [*ecosystem*](https://github.com/fastify/fastify#ecosystem)
|
|
|
475
475
|
section of our documentation!
|
|
476
476
|
|
|
477
477
|
If you want to see some real-world examples, check out:
|
|
478
|
-
- [
|
|
478
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
479
479
|
rendering (*ejs, pug, handlebars, marko*) plugin support for Fastify.
|
|
480
480
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
481
481
|
MongoDB connection plugin, with this you can share the same MongoDB connection
|
|
@@ -359,7 +359,7 @@ address.
|
|
|
359
359
|
We also got lucky by having full access to mitigate it at the source — didn't
|
|
360
360
|
need to send emails to some unknown framework maintainer and hope for a quick
|
|
361
361
|
answer. hapi's total control over all of its dependencies proved its usefulness
|
|
362
|
-
and security again. Not using [hapi](
|
|
362
|
+
and security again. Not using [hapi](https://hapi.dev)? [Maybe you
|
|
363
363
|
should](https://hueniverse.com/why-you-should-consider-hapi-6163689bd7c2).
|
|
364
364
|
|
|
365
365
|
### The after in happy ever-after
|
|
@@ -172,7 +172,7 @@ backend static-backend
|
|
|
172
172
|
# with 2 primary servers distributed via round-robin
|
|
173
173
|
# and one backup which is used when the first 2 are not reachable
|
|
174
174
|
# This also assumes your fastify servers are listening on port 80.
|
|
175
|
-
# more info:
|
|
175
|
+
# more info: https://nginx.org/en/docs/http/ngx_http_upstream_module.html
|
|
176
176
|
upstream fastify_app {
|
|
177
177
|
server 10.10.11.1:80;
|
|
178
178
|
server 10.10.11.2:80;
|
|
@@ -257,7 +257,7 @@ server {
|
|
|
257
257
|
# trustProxy to the address of your NGINX server so the X-Forwarded
|
|
258
258
|
# fields are used by fastify.
|
|
259
259
|
location / {
|
|
260
|
-
# more info:
|
|
260
|
+
# more info: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
|
|
261
261
|
proxy_http_version 1.1;
|
|
262
262
|
proxy_cache_bypass $http_upgrade;
|
|
263
263
|
proxy_set_header Upgrade $http_upgrade;
|
|
@@ -36,8 +36,9 @@ The sample provided allows you to easily build serverless web
|
|
|
36
36
|
applications/services and RESTful APIs using Fastify on top of AWS Lambda and
|
|
37
37
|
Amazon API Gateway.
|
|
38
38
|
|
|
39
|
-
*Note: Using
|
|
40
|
-
is just one
|
|
39
|
+
*Note: Using
|
|
40
|
+
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) is just one
|
|
41
|
+
possible way.*
|
|
41
42
|
|
|
42
43
|
### app.js
|
|
43
44
|
|
|
@@ -64,8 +65,7 @@ if (require.main === module) {
|
|
|
64
65
|
|
|
65
66
|
When executed in your lambda function we do not need to listen to a specific
|
|
66
67
|
port, so we just export the wrapper function `init` in this case. The
|
|
67
|
-
[`lambda.js`](#lambdajs) file
|
|
68
|
-
will use this export.
|
|
68
|
+
[`lambda.js`](#lambdajs) file will use this export.
|
|
69
69
|
|
|
70
70
|
When you execute your Fastify application like always, i.e. `node app.js` *(the
|
|
71
71
|
detection for this could be `require.main === module`)*, you can normally listen
|
|
@@ -74,7 +74,7 @@ to your port, so you can still run your Fastify function locally.
|
|
|
74
74
|
### lambda.js
|
|
75
75
|
|
|
76
76
|
```js
|
|
77
|
-
const awsLambdaFastify = require('aws-lambda
|
|
77
|
+
const awsLambdaFastify = require('@fastify/aws-lambda')
|
|
78
78
|
const init = require('./app');
|
|
79
79
|
|
|
80
80
|
const proxy = awsLambdaFastify(init())
|
|
@@ -91,14 +91,13 @@ exports.handler = proxy;
|
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
We just require
|
|
94
|
-
[aws-lambda
|
|
95
|
-
you install the dependency `npm i aws-lambda
|
|
96
|
-
[`app.js`](#appjs) file and call
|
|
97
|
-
the
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
[aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify).
|
|
94
|
+
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) (make sure
|
|
95
|
+
you install the dependency `npm i @fastify/aws-lambda`) and our
|
|
96
|
+
[`app.js`](#appjs) file and call the exported `awsLambdaFastify` function with
|
|
97
|
+
the `app` as the only parameter. The resulting `proxy` function has the correct
|
|
98
|
+
signature to be used as a lambda `handler` function. This way all the incoming
|
|
99
|
+
events (API Gateway requests) are passed to the `proxy` function of
|
|
100
|
+
[@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify).
|
|
102
101
|
|
|
103
102
|
### Example
|
|
104
103
|
|
|
@@ -237,7 +236,9 @@ gcloud functions logs read
|
|
|
237
236
|
|
|
238
237
|
#### Example request to `/hello` endpoint
|
|
239
238
|
```bash
|
|
240
|
-
curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me
|
|
239
|
+
curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me \
|
|
240
|
+
-H "Content-Type: application/json" \
|
|
241
|
+
-d '{ "name": "Fastify" }'
|
|
241
242
|
{"message":"Hello Fastify!"}
|
|
242
243
|
```
|
|
243
244
|
|
|
@@ -443,8 +444,8 @@ Then it should work fine
|
|
|
443
444
|
## Vercel
|
|
444
445
|
|
|
445
446
|
[Vercel](https://vercel.com) provides zero-configuration deployment for Node.js
|
|
446
|
-
applications. To use it now, it is as simple as configuring your
|
|
447
|
-
|
|
447
|
+
applications. To use it now, it is as simple as configuring your `vercel.json`
|
|
448
|
+
file like the following:
|
|
448
449
|
|
|
449
450
|
```json
|
|
450
451
|
{
|
|
@@ -36,8 +36,8 @@ at:
|
|
|
36
36
|
- [`@fastify/caching`](https://github.com/fastify/fastify-caching)
|
|
37
37
|
- [`@fastify/compress`](https://github.com/fastify/fastify-compress)
|
|
38
38
|
- [`@fastify/cookie`](https://github.com/fastify/fastify-cookie)
|
|
39
|
-
- [
|
|
40
|
-
- [`
|
|
39
|
+
- [`@fastify/under-pressure`](https://github.com/fastify/under-pressure)
|
|
40
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view)
|
|
41
41
|
|
|
42
42
|
## License
|
|
43
43
|
You can license your plugin as you prefer, we do not enforce any kind of
|
|
@@ -91,7 +91,7 @@ it to the [ecosystem](https://github.com/fastify/fastify#ecosystem) section of
|
|
|
91
91
|
our documentation!
|
|
92
92
|
|
|
93
93
|
If you want to see some real world examples, check out:
|
|
94
|
-
- [
|
|
94
|
+
- [`@fastify/view`](https://github.com/fastify/point-of-view) Templates
|
|
95
95
|
rendering (*ejs, pug, handlebars, marko*) plugin support for Fastify.
|
|
96
96
|
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
97
97
|
MongoDB connection plugin, with this you can share the same MongoDB connection
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## `Content-Type` Parser
|
|
4
4
|
Natively, Fastify only supports `'application/json'` and `'text/plain'` content
|
|
5
|
-
types. If the content type is not one of these, an
|
|
6
|
-
error will be thrown.
|
|
5
|
+
types. If the content type is not one of these, an
|
|
6
|
+
`FST_ERR_CTP_INVALID_MEDIA_TYPE` error will be thrown.
|
|
7
7
|
|
|
8
|
-
The default charset is `utf-8`. If you need to support different content
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
The default charset is `utf-8`. If you need to support different content types,
|
|
9
|
+
you can use the `addContentTypeParser` API. *The default JSON and/or plain text
|
|
10
|
+
parser can be changed or removed.*
|
|
11
11
|
|
|
12
12
|
*Note: If you decide to specify your own content type with the `Content-Type`
|
|
13
13
|
header, UTF-8 will not be the default. Be sure to include UTF-8 like this
|
|
@@ -21,8 +21,11 @@ available only in that scope and its children.
|
|
|
21
21
|
Fastify automatically adds the parsed request payload to the [Fastify
|
|
22
22
|
request](./Request.md) object which you can access with `request.body`.
|
|
23
23
|
|
|
24
|
-
Note that for `GET` and `HEAD` requests the payload is never parsed. For
|
|
25
|
-
|
|
24
|
+
Note that for `GET` and `HEAD` requests the payload is never parsed. For
|
|
25
|
+
`OPTIONS` and `DELETE` requests the payload is only parsed if the content type
|
|
26
|
+
is given in the content-type header. If it is not given, the
|
|
27
|
+
[catch-all](#catch-all) parser is not executed as with `POST`, `PUT` and
|
|
28
|
+
`PATCH`, but the payload is simply not parsed.
|
|
26
29
|
|
|
27
30
|
### Usage
|
|
28
31
|
```js
|
|
@@ -69,7 +72,8 @@ more specific one, like in the example below.
|
|
|
69
72
|
fastify.addContentTypeParser('application/vnd.custom+xml', (request, body, done) => {} )
|
|
70
73
|
fastify.addContentTypeParser('application/vnd.custom', (request, body, done) => {} )
|
|
71
74
|
|
|
72
|
-
// Here the desired behavior is achieved because fastify first tries to match the
|
|
75
|
+
// Here the desired behavior is achieved because fastify first tries to match the
|
|
76
|
+
// `application/vnd.custom+xml` content type parser
|
|
73
77
|
fastify.addContentTypeParser('application/vnd.custom', (request, body, done) => {} )
|
|
74
78
|
fastify.addContentTypeParser('application/vnd.custom+xml', (request, body, done) => {} )
|
|
75
79
|
```
|
|
@@ -115,9 +119,9 @@ fastify.removeContentTypeParser(['application/json', 'text/plain'])
|
|
|
115
119
|
In the example from just above, it is noticeable that we need to specify each
|
|
116
120
|
content type that we want to remove. To solve this problem Fastify provides the
|
|
117
121
|
`removeAllContentTypeParsers` API. This can be used to remove all currently
|
|
118
|
-
existing content type parsers. In the example below we achieve the same
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
existing content type parsers. In the example below we achieve the same as in
|
|
123
|
+
the example above except that we do not need to specify each content type to
|
|
124
|
+
delete. Just like `removeContentTypeParser`, this API supports encapsulation.
|
|
121
125
|
The API is especially useful if you want to register a [catch-all content type
|
|
122
126
|
parser](#catch-all) that should be executed for every content type and the
|
|
123
127
|
built-in parsers should be ignored as well.
|
|
@@ -223,8 +227,8 @@ fastify.route({
|
|
|
223
227
|
For piping file uploads you may want to check out [this
|
|
224
228
|
plugin](https://github.com/fastify/fastify-multipart).
|
|
225
229
|
|
|
226
|
-
If you want the content type parser to be executed on all content types
|
|
227
|
-
|
|
230
|
+
If you want the content type parser to be executed on all content types and not
|
|
231
|
+
only on those that don't have a specific one, you should call the
|
|
228
232
|
`removeAllContentTypeParsers` method first.
|
|
229
233
|
|
|
230
234
|
```js
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -54,9 +54,10 @@ When a custom error handler has been defined through
|
|
|
54
54
|
receive the error passed to the `done()` callback (or through other supported
|
|
55
55
|
automatic error handling mechanisms). If `setErrorHandler` has been used
|
|
56
56
|
multiple times to define multiple handlers, the error will be routed to the most
|
|
57
|
-
precedent handler defined within the error [encapsulation
|
|
58
|
-
Error handlers are fully encapsulated, so a
|
|
59
|
-
plugin will limit the error handler to that
|
|
57
|
+
precedent handler defined within the error [encapsulation
|
|
58
|
+
context](./Encapsulation.md). Error handlers are fully encapsulated, so a
|
|
59
|
+
`setErrorHandler` call within a plugin will limit the error handler to that
|
|
60
|
+
plugin's context.
|
|
60
61
|
|
|
61
62
|
The root error handler is Fastify's generic error handler. This error handler
|
|
62
63
|
will use the headers and status code in the `Error` object, if they exist. The
|
|
@@ -72,8 +73,8 @@ Some things to consider in your custom error handler:
|
|
|
72
73
|
- strings, buffers, and streams are sent to the client, with appropriate
|
|
73
74
|
headers (no serialization)
|
|
74
75
|
|
|
75
|
-
- You can throw a new error in your custom error handler
|
|
76
|
-
|
|
76
|
+
- You can throw a new error in your custom error handler - errors (new error or
|
|
77
|
+
the received error parameter re-thrown) - will call the parent `errorHandler`.
|
|
77
78
|
- `onError` hook will be triggered once only for the first error being thrown.
|
|
78
79
|
- an error will not be triggered twice from a lifecycle hook - Fastify
|
|
79
80
|
internally monitors the error invocation to avoid infinite loops for errors
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
Starting with Fastify v3.0.0, middleware is not supported out of the box and
|
|
6
6
|
requires an external plugin such as
|
|
7
7
|
[`@fastify/express`](https://github.com/fastify/fastify-express) or
|
|
8
|
-
[
|
|
8
|
+
[`@fastify/middie`](https://github.com/fastify/middie).
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
An example of registering the
|
|
@@ -22,11 +22,11 @@ fastify.use(require('ienoopen')())
|
|
|
22
22
|
fastify.use(require('x-xss-protection')())
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
You can also use [
|
|
25
|
+
You can also use [`@fastify/middie`](https://github.com/fastify/middie), which provides
|
|
26
26
|
support for simple Express-style middleware but with improved performance:
|
|
27
27
|
|
|
28
28
|
```js
|
|
29
|
-
await fastify.register(require('middie'))
|
|
29
|
+
await fastify.register(require('@fastify/middie'))
|
|
30
30
|
fastify.use(require('cors')())
|
|
31
31
|
```
|
|
32
32
|
|
|
@@ -9,10 +9,12 @@ By default, `register` creates a *new scope*, this means that if you make some
|
|
|
9
9
|
changes to the Fastify instance (via `decorate`), this change will not be
|
|
10
10
|
reflected by the current context ancestors, but only by its descendants. This
|
|
11
11
|
feature allows us to achieve plugin *encapsulation* and *inheritance*, in this
|
|
12
|
-
way we create a *directed acyclic graph* (DAG) and we will not have issues
|
|
13
|
-
by cross dependencies.
|
|
12
|
+
way we create a *directed acyclic graph* (DAG) and we will not have issues
|
|
13
|
+
caused by cross dependencies.
|
|
14
14
|
|
|
15
|
-
You may have already seen in the [Getting
|
|
15
|
+
You may have already seen in the [Getting
|
|
16
|
+
Started]((../Guides/Getting-Started.md#your-first-plugin)) guide how easy it is
|
|
17
|
+
to use this API:
|
|
16
18
|
```
|
|
17
19
|
fastify.register(plugin, [options])
|
|
18
20
|
```
|
|
@@ -96,9 +98,9 @@ If you pass an option with the key `prefix` with a `string` value, Fastify will
|
|
|
96
98
|
use it to prefix all the routes inside the register, for more info check
|
|
97
99
|
[here](./Routes.md#route-prefixing).
|
|
98
100
|
|
|
99
|
-
Be aware that if you
|
|
100
|
-
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) this option will
|
|
101
|
-
not work.
|
|
101
|
+
Be aware that if you wrap your routes with
|
|
102
|
+
[`fastify-plugin`](https://github.com/fastify/fastify-plugin), this option will
|
|
103
|
+
not work (there is a [workaround](./Routes.md#fastify-plugin) available).
|
|
102
104
|
|
|
103
105
|
#### Error handling
|
|
104
106
|
<a id="error-handling"></a>
|