fastify 3.27.4 → 3.29.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/docs/Guides/Ecosystem.md +39 -39
- package/docs/Guides/Getting-Started.md +4 -4
- package/docs/Guides/Migration-Guide-V3.md +2 -2
- package/docs/Guides/Plugins-Guide.md +5 -5
- package/docs/Guides/Write-Plugin.md +6 -6
- package/docs/Reference/Encapsulation.md +3 -3
- package/docs/Reference/Hooks.md +1 -1
- package/docs/Reference/Middleware.md +6 -6
- package/docs/Reference/Reply.md +50 -1
- package/docs/Reference/TypeScript.md +1 -1
- package/fastify.d.ts +3 -3
- package/fastify.js +1 -1
- package/lib/errors.js +9 -1
- package/lib/fourOhFour.js +20 -1
- package/lib/reply.js +97 -10
- package/lib/symbols.js +1 -0
- package/package.json +11 -11
- package/test/404s.test.js +52 -0
- package/test/internals/reply.test.js +3 -0
- package/test/logger.test.js +20 -0
- package/test/reply-trailers.test.js +277 -0
- package/test/stream.test.js +48 -0
- package/test/types/hooks.test-d.ts +53 -3
- package/test/types/logger.test-d.ts +13 -1
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/request.test-d.ts +41 -1
- package/test/types/route.test-d.ts +1 -1
- package/types/hooks.d.ts +86 -62
- package/types/instance.d.ts +55 -39
- package/types/logger.d.ts +13 -2
- package/types/request.d.ts +2 -1
- package/types/route.d.ts +36 -31
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -8,105 +8,105 @@ section.
|
|
|
8
8
|
|
|
9
9
|
#### [Core](#core)
|
|
10
10
|
|
|
11
|
-
- [
|
|
11
|
+
- [`@fastify/accepts`](https://github.com/fastify/fastify-accepts) to have
|
|
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
|
-
- [
|
|
15
|
+
- [`@fastify/auth`](https://github.com/fastify/fastify-auth) Run multiple auth
|
|
16
16
|
functions in Fastify.
|
|
17
|
-
- [
|
|
17
|
+
- [`@fastify/autoload`](https://github.com/fastify/fastify-autoload) Require all
|
|
18
18
|
plugins in a directory.
|
|
19
19
|
- [`fastify-awilix`](https://github.com/fastify/fastify-awilix) Dependency
|
|
20
20
|
injection support for Fastify, based on
|
|
21
21
|
[awilix](https://github.com/jeffijoe/awilix).
|
|
22
|
-
- [
|
|
22
|
+
- [`@fastify/bankai`](https://github.com/fastify/fastify-bankai)
|
|
23
23
|
[Bankai](https://github.com/yoshuawuyts/bankai) assets compiler for Fastify.
|
|
24
|
-
- [
|
|
24
|
+
- [`@fastify/basic-auth`](https://github.com/fastify/fastify-basic-auth) Basic
|
|
25
25
|
auth plugin for Fastify.
|
|
26
|
-
- [
|
|
26
|
+
- [`@fastify/bearer-auth`](https://github.com/fastify/fastify-bearer-auth) Bearer
|
|
27
27
|
auth plugin for Fastify.
|
|
28
|
-
- [
|
|
28
|
+
- [`@fastify/caching`](https://github.com/fastify/fastify-caching) General
|
|
29
29
|
server-side cache and ETag support.
|
|
30
|
-
- [
|
|
30
|
+
- [`@fastify/circuit-breaker`](https://github.com/fastify/fastify-circuit-breaker)
|
|
31
31
|
A low overhead circuit breaker for your routes.
|
|
32
|
-
- [
|
|
32
|
+
- [`@fastify/compress`](https://github.com/fastify/fastify-compress) Fastify
|
|
33
33
|
compression utils.
|
|
34
|
-
- [
|
|
34
|
+
- [`@fastify/cookie`](https://github.com/fastify/fastify-cookie) Parse and set
|
|
35
35
|
cookie headers.
|
|
36
|
-
- [
|
|
36
|
+
- [`@fastify/cors`](https://github.com/fastify/fastify-cors) Enables the use of
|
|
37
37
|
CORS in a Fastify application.
|
|
38
38
|
- [`fastify-csrf`](https://github.com/fastify/fastify-csrf) A plugin for adding
|
|
39
39
|
[CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection to
|
|
40
40
|
Fastify.
|
|
41
|
-
- [
|
|
41
|
+
- [`@fastify/diagnostics-channel`](https://github.com/fastify/fastify-diagnostics-channel)
|
|
42
42
|
Plugin to deal with `diagnostics_channel` on Fastify
|
|
43
|
-
- [
|
|
43
|
+
- [`@fastify/elasticsearch`](https://github.com/fastify/fastify-elasticsearch)
|
|
44
44
|
Plugin to share the same ES client.
|
|
45
|
-
- [
|
|
45
|
+
- [`@fastify/env`](https://github.com/fastify/fastify-env) Load and check
|
|
46
46
|
configuration.
|
|
47
|
-
- [
|
|
47
|
+
- [`@fastify/etag`](https://github.com/fastify/fastify-etag) Automatically
|
|
48
48
|
generate ETags for HTTP responses.
|
|
49
|
-
- [
|
|
49
|
+
- [`@fastify/flash`](https://github.com/fastify/fastify-flash) Set and get flash
|
|
50
50
|
messages using the session.
|
|
51
|
-
- [
|
|
51
|
+
- [`@fastify/formbody`](https://github.com/fastify/fastify-formbody) Plugin to
|
|
52
52
|
parse x-www-form-urlencoded bodies.
|
|
53
|
-
- [
|
|
53
|
+
- [`@fastify/funky`](https://github.com/fastify/fastify-funky) Makes functional
|
|
54
54
|
programming in Fastify more convenient. Adds support for Fastify routes
|
|
55
55
|
returning functional structures, such as Either, Task or plain parameterless
|
|
56
56
|
function.
|
|
57
|
-
- [
|
|
57
|
+
- [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) Important
|
|
58
58
|
security headers for Fastify.
|
|
59
|
-
- [
|
|
59
|
+
- [`@fastify/http-proxy`](https://github.com/fastify/fastify-http-proxy) Proxy
|
|
60
60
|
your HTTP requests to another server, with hooks.
|
|
61
|
-
- [
|
|
61
|
+
- [`@fastify/jwt`](https://github.com/fastify/fastify-jwt) JWT utils for Fastify,
|
|
62
62
|
internally uses [fast-jwt](https://github.com/nearform/fast-jwt).
|
|
63
|
-
- [
|
|
63
|
+
- [`@fastify/leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to
|
|
64
64
|
share a common LevelDB connection across Fastify.
|
|
65
|
-
- [
|
|
65
|
+
- [`@fastify/mongodb`](https://github.com/fastify/fastify-mongodb) Fastify
|
|
66
66
|
MongoDB connection plugin, with which you can share the same MongoDB
|
|
67
67
|
connection pool across every part of your server.
|
|
68
|
-
- [
|
|
68
|
+
- [`@fastify/multipart`](https://github.com/fastify/fastify-multipart) Multipart
|
|
69
69
|
support for Fastify.
|
|
70
|
-
- [
|
|
70
|
+
- [`@fastify/oauth2`](https://github.com/fastify/fastify-oauth2) Wrap around
|
|
71
71
|
[`simple-oauth2`](https://github.com/lelylan/simple-oauth2).
|
|
72
|
-
- [
|
|
72
|
+
- [`@fastify/postgres`](https://github.com/fastify/fastify-postgres) Fastify
|
|
73
73
|
PostgreSQL connection plugin, with this you can share the same PostgreSQL
|
|
74
74
|
connection pool in every part of your server.
|
|
75
|
-
- [
|
|
75
|
+
- [`@fastify/rate-limit`](https://github.com/fastify/fastify-rate-limit) A low
|
|
76
76
|
overhead rate limiter for your routes.
|
|
77
|
-
- [
|
|
77
|
+
- [`@fastify/request-context`](https://github.com/fastify/fastify-request-context)
|
|
78
78
|
Request-scoped storage, based on
|
|
79
79
|
[AsyncLocalStorage](https://nodejs.org/api/async_hooks.html#async_hooks_class_asynclocalstorage)
|
|
80
80
|
(with fallback to [cls-hooked](https://github.com/Jeff-Lewis/cls-hooked)),
|
|
81
81
|
providing functionality similar to thread-local storages.
|
|
82
|
-
- [
|
|
82
|
+
- [`@fastify/response-validation`](https://github.com/fastify/fastify-response-validation)
|
|
83
83
|
A simple plugin that enables response validation for Fastify.
|
|
84
|
-
- [
|
|
84
|
+
- [`@fastify/nextjs`](https://github.com/fastify/fastify-nextjs) React
|
|
85
85
|
server-side rendering support for Fastify with
|
|
86
86
|
[Next](https://github.com/zeit/next.js/).
|
|
87
|
-
- [
|
|
87
|
+
- [`@fastify/redis`](https://github.com/fastify/fastify-redis) Fastify Redis
|
|
88
88
|
connection plugin, with which you can share the same Redis connection across
|
|
89
89
|
every part of your server.
|
|
90
|
-
- [
|
|
90
|
+
- [`@fastify/reply-from`](https://github.com/fastify/fastify-reply-from) Plugin
|
|
91
91
|
to forward the current HTTP request to another server.
|
|
92
|
-
- [
|
|
92
|
+
- [`@fastify/routes`](https://github.com/fastify/fastify-routes) Plugin that
|
|
93
93
|
provides a `Map` of routes.
|
|
94
94
|
- [`fastify-schedule`](https://github.com/fastify/fastify-schedule) Plugin for
|
|
95
95
|
scheduling periodic jobs, based on
|
|
96
96
|
[toad-scheduler](https://github.com/kibertoad/toad-scheduler).
|
|
97
|
-
- [
|
|
97
|
+
- [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults for
|
|
98
98
|
Fastify that everyone can agree on. It adds some useful decorators such as
|
|
99
99
|
HTTP errors and assertions, but also more request and reply methods.
|
|
100
100
|
- [`@fastify/session`](https://github.com/fastify/session) a session plugin for
|
|
101
101
|
Fastify.
|
|
102
|
-
- [
|
|
102
|
+
- [`@fastify/static`](https://github.com/fastify/fastify-static) Plugin for
|
|
103
103
|
serving static files as fast as possible.
|
|
104
|
-
- [
|
|
104
|
+
- [`@fastify/swagger`](https://github.com/fastify/fastify-swagger) Plugin for
|
|
105
105
|
serving Swagger/OpenAPI documentation for Fastify, supporting dynamic
|
|
106
106
|
generation.
|
|
107
|
-
- [
|
|
107
|
+
- [`@fastify/websocket`](https://github.com/fastify/fastify-websocket) WebSocket
|
|
108
108
|
support for Fastify. Built upon [ws](https://github.com/websockets/ws).
|
|
109
|
-
- [
|
|
109
|
+
- [`@fastify/url-data`](https://github.com/fastify/fastify-url-data) Decorate the
|
|
110
110
|
`Request` object with a method to access raw URL components.
|
|
111
111
|
- [`middie`](https://github.com/fastify/middie) Middleware engine for Fastify.
|
|
112
112
|
- [`point-of-view`](https://github.com/fastify/point-of-view) Templates
|
|
@@ -195,10 +195,10 @@ Fastify handles this internally, with minimum effort!
|
|
|
195
195
|
Let's rewrite the above example with a database connection.
|
|
196
196
|
|
|
197
197
|
|
|
198
|
-
First, install `fastify-plugin` and
|
|
198
|
+
First, install `fastify-plugin` and `@fastify/mongodb`:
|
|
199
199
|
|
|
200
200
|
```
|
|
201
|
-
npm i --save fastify-plugin fastify
|
|
201
|
+
npm i --save fastify-plugin @fastify/mongodb
|
|
202
202
|
```
|
|
203
203
|
|
|
204
204
|
**server.js**
|
|
@@ -246,7 +246,7 @@ fastify.listen(3000, function (err, address) {
|
|
|
246
246
|
```js
|
|
247
247
|
// ESM
|
|
248
248
|
import fastifyPlugin from 'fastify-plugin'
|
|
249
|
-
import fastifyMongo from 'fastify
|
|
249
|
+
import fastifyMongo from '@fastify/mongodb'
|
|
250
250
|
|
|
251
251
|
async function dbConnector (fastify, options) {
|
|
252
252
|
fastify.register(fastifyMongo, {
|
|
@@ -265,7 +265,7 @@ module.exports = fastifyPlugin(dbConnector)
|
|
|
265
265
|
const fastifyPlugin = require('fastify-plugin')
|
|
266
266
|
|
|
267
267
|
async function dbConnector (fastify, options) {
|
|
268
|
-
fastify.register(require('fastify
|
|
268
|
+
fastify.register(require('@fastify/mongodb'), {
|
|
269
269
|
url: 'mongodb://localhost:27017/test_database'
|
|
270
270
|
})
|
|
271
271
|
}
|
|
@@ -14,7 +14,7 @@ From Fastify v3, middleware support does not come out-of-the-box with the
|
|
|
14
14
|
framework itself.
|
|
15
15
|
|
|
16
16
|
If you use Express middleware in your application, please install and register
|
|
17
|
-
the [
|
|
17
|
+
the [`@fastify/express`](https://github.com/fastify/fastify-express) or
|
|
18
18
|
[`middie`](https://github.com/fastify/middie) plugin before doing so.
|
|
19
19
|
|
|
20
20
|
**v2:**
|
|
@@ -28,7 +28,7 @@ fastify.use(require('cors')());
|
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
30
|
// Using the Express `cors` middleware in Fastify v3.
|
|
31
|
-
await fastify.register(require('fastify
|
|
31
|
+
await fastify.register(require('@fastify/express'));
|
|
32
32
|
fastify.use(require('cors')());
|
|
33
33
|
```
|
|
34
34
|
|
|
@@ -445,10 +445,10 @@ fastify
|
|
|
445
445
|
|
|
446
446
|
If your plugin needs to expose custom errors, you can easily generate consistent
|
|
447
447
|
error objects across your codebase and plugins with the
|
|
448
|
-
[
|
|
448
|
+
[`@fastify/error`](https://github.com/fastify/fastify-error) module.
|
|
449
449
|
|
|
450
450
|
```js
|
|
451
|
-
const createError = require('fastify
|
|
451
|
+
const createError = require('@fastify/error')
|
|
452
452
|
const CustomError = createError('ERROR_CODE', 'message')
|
|
453
453
|
console.log(new CustomError())
|
|
454
454
|
```
|
|
@@ -477,12 +477,12 @@ section of our documentation!
|
|
|
477
477
|
If you want to see some real-world examples, check out:
|
|
478
478
|
- [`point-of-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
|
|
482
482
|
pool in every part of your server.
|
|
483
|
-
- [
|
|
483
|
+
- [`@fastify/multipart`](https://github.com/fastify/fastify-multipart) Multipart
|
|
484
484
|
support for Fastify
|
|
485
|
-
- [
|
|
485
|
+
- [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) Important
|
|
486
486
|
security headers for Fastify
|
|
487
487
|
|
|
488
488
|
|
|
@@ -33,9 +33,9 @@ unused.
|
|
|
33
33
|
|
|
34
34
|
If you want to see some good examples on how to document a plugin take a look
|
|
35
35
|
at:
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
38
|
-
- [
|
|
36
|
+
- [`@fastify/caching`](https://github.com/fastify/fastify-caching)
|
|
37
|
+
- [`@fastify/compress`](https://github.com/fastify/fastify-compress)
|
|
38
|
+
- [`@fastify/cookie`](https://github.com/fastify/fastify-cookie)
|
|
39
39
|
- [`point-of-view`](https://github.com/fastify/point-of-view)
|
|
40
40
|
- [`under-pressure`](https://github.com/fastify/under-pressure)
|
|
41
41
|
|
|
@@ -93,10 +93,10 @@ our documentation!
|
|
|
93
93
|
If you want to see some real world examples, check out:
|
|
94
94
|
- [`point-of-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
|
|
98
98
|
pool in every part of your server.
|
|
99
|
-
- [
|
|
99
|
+
- [`@fastify/multipart`](https://github.com/fastify/fastify-multipart) Multipart
|
|
100
100
|
support for Fastify.
|
|
101
|
-
- [
|
|
101
|
+
- [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) Important
|
|
102
102
|
security headers for Fastify.
|
|
@@ -33,7 +33,7 @@ this example into concrete terms, consider a basic scenario of a REST API
|
|
|
33
33
|
server that has three routes: the first route (`/one`) requires authentication,
|
|
34
34
|
the second route (`/two`) does not, and the third route (`/three`) has
|
|
35
35
|
access to the same context as the second route. Using
|
|
36
|
-
[fastify
|
|
36
|
+
[@fastify/bearer-auth][bearer] to provide the authentication, the code for this
|
|
37
37
|
example is as follows:
|
|
38
38
|
|
|
39
39
|
```js
|
|
@@ -44,7 +44,7 @@ const fastify = require('fastify')()
|
|
|
44
44
|
fastify.decorateRequest('answer', 42)
|
|
45
45
|
|
|
46
46
|
fastify.register(async function authenticatedContext (childServer) {
|
|
47
|
-
childServer.register(require('fastify
|
|
47
|
+
childServer.register(require('@fastify/bearer-auth'), { keys: ['abc123'] })
|
|
48
48
|
|
|
49
49
|
childServer.route({
|
|
50
50
|
path: '/one',
|
|
@@ -103,7 +103,7 @@ original diagram:
|
|
|
103
103
|
1. Each _child context_ (`authenticatedContext`, `publicContext`, and
|
|
104
104
|
`grandchildContext`) has access to the `answer` request decorator defined in
|
|
105
105
|
the _root context_.
|
|
106
|
-
2. Only the `authenticatedContext` has access to the
|
|
106
|
+
2. Only the `authenticatedContext` has access to the `@fastify/bearer-auth`
|
|
107
107
|
plugin.
|
|
108
108
|
3. Both the `publicContext` and `grandchildContext` have access to the `foo`
|
|
109
109
|
request decorator.
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -351,7 +351,7 @@ fastify.addHook('preHandler', async (request, reply) => {
|
|
|
351
351
|
})
|
|
352
352
|
|
|
353
353
|
fastify.addHook('preHandler', async (request, reply) => {
|
|
354
|
-
// the fastify
|
|
354
|
+
// the @fastify/static plugin will send a file asynchronously,
|
|
355
355
|
// so we should return reply
|
|
356
356
|
reply.sendFile('myfile')
|
|
357
357
|
return reply
|
|
@@ -4,16 +4,16 @@
|
|
|
4
4
|
|
|
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
|
[`middie`](https://github.com/fastify/middie).
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
An example of registering the
|
|
12
|
-
[
|
|
12
|
+
[`@fastify/express`](https://github.com/fastify/fastify-express) plugin to `use`
|
|
13
13
|
Express middleware:
|
|
14
14
|
|
|
15
15
|
```js
|
|
16
|
-
await fastify.register(require('fastify
|
|
16
|
+
await fastify.register(require('@fastify/express'))
|
|
17
17
|
fastify.use(require('cors')())
|
|
18
18
|
fastify.use(require('dns-prefetch-control')())
|
|
19
19
|
fastify.use(require('frameguard')())
|
|
@@ -70,9 +70,9 @@ fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
|
|
|
70
70
|
### Alternatives
|
|
71
71
|
|
|
72
72
|
Fastify offers some alternatives to the most commonly used middleware, such as
|
|
73
|
-
[
|
|
73
|
+
[`@fastify/helmet`](https://github.com/fastify/fastify-helmet) in case of
|
|
74
74
|
[`helmet`](https://github.com/helmetjs/helmet),
|
|
75
|
-
[
|
|
75
|
+
[`@fastify/cors`](https://github.com/fastify/fastify-cors) for
|
|
76
76
|
[`cors`](https://github.com/expressjs/cors), and
|
|
77
|
-
[
|
|
77
|
+
[`@fastify/static`](https://github.com/fastify/fastify-static) for
|
|
78
78
|
[`serve-static`](https://github.com/expressjs/serve-static).
|
package/docs/Reference/Reply.md
CHANGED
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
- [.getHeaders()](#getheaders)
|
|
14
14
|
- [.removeHeader(key)](#removeheaderkey)
|
|
15
15
|
- [.hasHeader(key)](#hasheaderkey)
|
|
16
|
+
- [.trailer(key, function)](#trailerkey-function)
|
|
17
|
+
- [.hasTrailer(key)](#hastrailerkey)
|
|
18
|
+
- [.removeTrailer(key)](#removetrailerkey)
|
|
16
19
|
- [.redirect([code,] dest)](#redirectcode--dest)
|
|
17
20
|
- [.callNotFound()](#callnotfound)
|
|
18
21
|
- [.getResponseTime()](#getresponsetime)
|
|
@@ -47,6 +50,9 @@ object that exposes the following functions and properties:
|
|
|
47
50
|
- `.getHeaders()` - Gets a shallow copy of all current response headers.
|
|
48
51
|
- `.removeHeader(key)` - Remove the value of a previously set header.
|
|
49
52
|
- `.hasHeader(name)` - Determine if a header has been set.
|
|
53
|
+
- `.trailer(key, function)` - Sets a response trailer.
|
|
54
|
+
- `.hasTrailer(key)` - Determine if a trailer has been set.
|
|
55
|
+
- `.removeTrailer(key)` - Remove the value of a previously set trailer.
|
|
50
56
|
- `.type(value)` - Sets the header `Content-Type`.
|
|
51
57
|
- `.redirect([code,] dest)` - Redirect to the specified url, the status code is
|
|
52
58
|
optional (default to `302`).
|
|
@@ -199,6 +205,49 @@ reply.getHeader('x-foo') // undefined
|
|
|
199
205
|
|
|
200
206
|
Returns a boolean indicating if the specified header has been set.
|
|
201
207
|
|
|
208
|
+
### .trailer(key, function)
|
|
209
|
+
<a id="trailer"></a>
|
|
210
|
+
|
|
211
|
+
Sets a response trailer. Trailer usually used when you want some header that require heavy resources to be sent after the `data`, for example `Server-Timing`, `Etag`. It can ensure the client get the response data as soon as possible.
|
|
212
|
+
|
|
213
|
+
*Note: The header `Transfer-Encoding: chunked` will be added once you use the trailer. It is a hard requipment for using trailer in Node.js.*
|
|
214
|
+
|
|
215
|
+
*Note: Currently, the computation function only supports synchronous function. That means `async-await` and `promise` are not supported.*
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
reply.trailer('server-timing', function() {
|
|
219
|
+
return 'db;dur=53, app;dur=47.2'
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
const { createHash } = require('crypto')
|
|
223
|
+
// trailer function also recieve two argument
|
|
224
|
+
// @param {object} reply fastify reply
|
|
225
|
+
// @param {string|Buffer|null} payload payload that already sent, note that it will be null when stream is sent
|
|
226
|
+
reply.trailer('content-md5', function(reply, payload) {
|
|
227
|
+
const hash = createHash('md5')
|
|
228
|
+
hash.update(payload)
|
|
229
|
+
return hash.disgest('hex')
|
|
230
|
+
})
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### .hasTrailer(key)
|
|
234
|
+
<a id="hasTrailer"></a>
|
|
235
|
+
|
|
236
|
+
Returns a boolean indicating if the specified trailer has been set.
|
|
237
|
+
|
|
238
|
+
### .removeTrailer(key)
|
|
239
|
+
<a id="removeTrailer"></a>
|
|
240
|
+
|
|
241
|
+
Remove the value of a previously set trailer.
|
|
242
|
+
```js
|
|
243
|
+
reply.trailer('server-timing', function() {
|
|
244
|
+
return 'db;dur=53, app;dur=47.2'
|
|
245
|
+
})
|
|
246
|
+
reply.removeTrailer('server-timing')
|
|
247
|
+
reply.getTrailer('server-timing') // undefined
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
|
|
202
251
|
### .redirect([code ,] dest)
|
|
203
252
|
<a id="redirect"></a>
|
|
204
253
|
|
|
@@ -434,7 +483,7 @@ be used to enhance the HTTP response.
|
|
|
434
483
|
|
|
435
484
|
Tip: you can simplify errors by using the
|
|
436
485
|
[`http-errors`](https://npm.im/http-errors) module or
|
|
437
|
-
[
|
|
486
|
+
[`@fastify/sensible`](https://github.com/fastify/fastify-sensible) plugin to
|
|
438
487
|
generate errors:
|
|
439
488
|
|
|
440
489
|
```js
|
|
@@ -604,7 +604,7 @@ newer, automatically adds `.default` property and a named export to the exported
|
|
|
604
604
|
plugin. Be sure to `export default` and `export const myPlugin` in your typings
|
|
605
605
|
to provide the best developer experience. For a complete example you can check
|
|
606
606
|
out
|
|
607
|
-
[fastify
|
|
607
|
+
[@fastify/swagger](https://github.com/fastify/fastify-swagger/blob/master/index.d.ts).
|
|
608
608
|
|
|
609
609
|
With those files completed, the plugin is now ready to be consumed by any
|
|
610
610
|
TypeScript project!
|
package/fastify.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { FastifyLoggerInstance, FastifyLoggerOptions } from './types/logger'
|
|
|
9
9
|
import { FastifyInstance } from './types/instance'
|
|
10
10
|
import { FastifyServerFactory } from './types/serverFactory'
|
|
11
11
|
import { Options as AjvOptions } from '@fastify/ajv-compiler'
|
|
12
|
-
import { FastifyError } from 'fastify
|
|
12
|
+
import { FastifyError } from '@fastify/error'
|
|
13
13
|
import { FastifyReply } from './types/reply'
|
|
14
14
|
import { FastifySchemaValidationError } from './types/schema'
|
|
15
15
|
import { ConstructorAction, ProtoAction } from "./types/content-type-parser";
|
|
@@ -163,7 +163,7 @@ export type FastifyServerOptions<
|
|
|
163
163
|
|
|
164
164
|
type TrustProxyFunction = (address: string, hop: number) => boolean
|
|
165
165
|
|
|
166
|
-
declare module 'fastify
|
|
166
|
+
declare module '@fastify/error' {
|
|
167
167
|
interface FastifyError {
|
|
168
168
|
validation?: ValidationResult[];
|
|
169
169
|
}
|
|
@@ -188,7 +188,7 @@ export { FastifyContext, FastifyContextConfig } from './types/context'
|
|
|
188
188
|
export { RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler } from './types/route'
|
|
189
189
|
export * from './types/register'
|
|
190
190
|
export { FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction } from './types/content-type-parser'
|
|
191
|
-
export { FastifyError } from 'fastify
|
|
191
|
+
export { FastifyError } from '@fastify/error'
|
|
192
192
|
export { FastifySchema, FastifySchemaCompiler } from './types/schema'
|
|
193
193
|
export { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
|
|
194
194
|
export * from './types/hooks'
|
package/fastify.js
CHANGED
package/lib/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const createError = require('fastify
|
|
3
|
+
const createError = require('@fastify/error')
|
|
4
4
|
const codes = {
|
|
5
5
|
/**
|
|
6
6
|
* Basic
|
|
@@ -147,6 +147,14 @@ const codes = {
|
|
|
147
147
|
'FST_ERR_BAD_STATUS_CODE',
|
|
148
148
|
'Called reply with an invalid status code: %s'
|
|
149
149
|
),
|
|
150
|
+
FST_ERR_BAD_TRAILER_NAME: createError(
|
|
151
|
+
'FST_ERR_BAD_TRAILER_NAME',
|
|
152
|
+
'Called reply.trailer with an invalid header name: %s'
|
|
153
|
+
),
|
|
154
|
+
FST_ERR_BAD_TRAILER_VALUE: createError(
|
|
155
|
+
'FST_ERR_BAD_TRAILER_VALUE',
|
|
156
|
+
"Called reply.trailer('%s', fn) with an invalid type: %s. Expected a function."
|
|
157
|
+
),
|
|
150
158
|
|
|
151
159
|
/**
|
|
152
160
|
* schemas
|
package/lib/fourOhFour.js
CHANGED
|
@@ -37,7 +37,8 @@ function fourOhFour (options) {
|
|
|
37
37
|
const { logger, genReqId } = options
|
|
38
38
|
|
|
39
39
|
// 404 router, used for handling encapsulated 404 handlers
|
|
40
|
-
const router = FindMyWay({ defaultRoute: fourOhFourFallBack })
|
|
40
|
+
const router = FindMyWay({ onBadUrl: createOnBadUrl(), defaultRoute: fourOhFourFallBack })
|
|
41
|
+
let _onBadUrlHandler = null
|
|
41
42
|
|
|
42
43
|
return { router, setNotFoundHandler, setContext, arrange404 }
|
|
43
44
|
|
|
@@ -45,6 +46,8 @@ function fourOhFour (options) {
|
|
|
45
46
|
// Change the pointer of the fastify instance to itself, so register + prefix can add new 404 handler
|
|
46
47
|
instance[kFourOhFourLevelInstance] = instance
|
|
47
48
|
instance[kCanSetNotFoundHandler] = true
|
|
49
|
+
// we need to bind instance for the context
|
|
50
|
+
router.onBadUrl = router.onBadUrl.bind(instance)
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
function basic404 (request, reply) {
|
|
@@ -58,6 +61,18 @@ function fourOhFour (options) {
|
|
|
58
61
|
})
|
|
59
62
|
}
|
|
60
63
|
|
|
64
|
+
function createOnBadUrl () {
|
|
65
|
+
return function onBadUrl (path, req, res) {
|
|
66
|
+
const id = genReqId(req)
|
|
67
|
+
const childLogger = logger.child({ reqId: id })
|
|
68
|
+
const fourOhFourContext = this[kFourOhFourLevelInstance][kFourOhFourContext]
|
|
69
|
+
const request = new Request(id, null, req, null, childLogger, fourOhFourContext)
|
|
70
|
+
const reply = new Reply(res, request, childLogger)
|
|
71
|
+
|
|
72
|
+
_onBadUrlHandler(request, reply)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
61
76
|
function setContext (instance, context) {
|
|
62
77
|
const _404Context = Object.assign({}, instance[kFourOhFourContext])
|
|
63
78
|
_404Context.onSend = context.onSend
|
|
@@ -107,8 +122,12 @@ function fourOhFour (options) {
|
|
|
107
122
|
if (handler) {
|
|
108
123
|
this[kFourOhFourLevelInstance][kCanSetNotFoundHandler] = false
|
|
109
124
|
handler = handler.bind(this)
|
|
125
|
+
// update onBadUrl handler
|
|
126
|
+
_onBadUrlHandler = handler
|
|
110
127
|
} else {
|
|
111
128
|
handler = basic404
|
|
129
|
+
// update onBadUrl handler
|
|
130
|
+
_onBadUrlHandler = basic404
|
|
112
131
|
}
|
|
113
132
|
|
|
114
133
|
this.after((notHandledErr, done) => {
|