fastify 3.29.0 → 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 +1 -1
- package/docs/Reference/TypeScript.md +1 -1
- package/fastify.js +1 -1
- package/lib/reply.js +1 -1
- package/package.json +1 -1
- package/test/stream.test.js +48 -0
- package/test/types/logger.test-d.ts +13 -1
- package/types/logger.d.ts +12 -1
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
|
@@ -483,7 +483,7 @@ be used to enhance the HTTP response.
|
|
|
483
483
|
|
|
484
484
|
Tip: you can simplify errors by using the
|
|
485
485
|
[`http-errors`](https://npm.im/http-errors) module or
|
|
486
|
-
[
|
|
486
|
+
[`@fastify/sensible`](https://github.com/fastify/fastify-sensible) plugin to
|
|
487
487
|
generate errors:
|
|
488
488
|
|
|
489
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.js
CHANGED
package/lib/reply.js
CHANGED
|
@@ -545,7 +545,7 @@ function sendStream (payload, res, reply) {
|
|
|
545
545
|
eos(payload, { readable: true, writable: false }, function (err) {
|
|
546
546
|
sourceOpen = false
|
|
547
547
|
if (err != null) {
|
|
548
|
-
if (res.headersSent) {
|
|
548
|
+
if (res.headersSent || reply.request.raw.aborted === true) {
|
|
549
549
|
if (!errorLogged) {
|
|
550
550
|
errorLogged = true
|
|
551
551
|
logStreamError(reply.log, err, res)
|
package/package.json
CHANGED
package/test/stream.test.js
CHANGED
|
@@ -683,3 +683,51 @@ test('should mark reply as sent before pumping the payload stream into response
|
|
|
683
683
|
fastify.close()
|
|
684
684
|
})
|
|
685
685
|
})
|
|
686
|
+
|
|
687
|
+
test('reply.send handles aborted requests', t => {
|
|
688
|
+
t.plan(2)
|
|
689
|
+
|
|
690
|
+
const spyLogger = {
|
|
691
|
+
level: 'error',
|
|
692
|
+
fatal: () => { },
|
|
693
|
+
error: () => {
|
|
694
|
+
t.fail('should not log an error')
|
|
695
|
+
},
|
|
696
|
+
warn: () => { },
|
|
697
|
+
info: () => { },
|
|
698
|
+
debug: () => { },
|
|
699
|
+
trace: () => { },
|
|
700
|
+
child: () => { return spyLogger }
|
|
701
|
+
}
|
|
702
|
+
const fastify = Fastify({
|
|
703
|
+
logger: spyLogger
|
|
704
|
+
})
|
|
705
|
+
|
|
706
|
+
fastify.get('/', (req, reply) => {
|
|
707
|
+
setTimeout(() => {
|
|
708
|
+
const stream = new Readable({
|
|
709
|
+
read: function () {
|
|
710
|
+
this.push(null)
|
|
711
|
+
}
|
|
712
|
+
})
|
|
713
|
+
reply.send(stream)
|
|
714
|
+
}, 6)
|
|
715
|
+
})
|
|
716
|
+
|
|
717
|
+
fastify.listen({ port: 0 }, err => {
|
|
718
|
+
t.error(err)
|
|
719
|
+
fastify.server.unref()
|
|
720
|
+
|
|
721
|
+
const port = fastify.server.address().port
|
|
722
|
+
const http = require('http')
|
|
723
|
+
const req = http.get(`http://localhost:${port}`)
|
|
724
|
+
.on('error', (err) => {
|
|
725
|
+
t.equal(err.code, 'ECONNRESET')
|
|
726
|
+
fastify.close()
|
|
727
|
+
})
|
|
728
|
+
|
|
729
|
+
setTimeout(() => {
|
|
730
|
+
req.abort()
|
|
731
|
+
}, 1)
|
|
732
|
+
})
|
|
733
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { expectType } from 'tsd'
|
|
1
|
+
import { expectError, expectType } from 'tsd'
|
|
2
2
|
import fastify, { FastifyLogFn, LogLevel, FastifyLoggerInstance, FastifyError, FastifyRequest, FastifyReply } from '../../fastify'
|
|
3
3
|
import { Server, IncomingMessage, ServerResponse } from 'http'
|
|
4
4
|
import pino from 'pino'
|
|
@@ -183,3 +183,15 @@ const passStreamAsOption = fastify({
|
|
|
183
183
|
stream: fs.createWriteStream('/tmp/stream.out')
|
|
184
184
|
}
|
|
185
185
|
})
|
|
186
|
+
|
|
187
|
+
const childParent = fastify().log
|
|
188
|
+
// we test different option variant here
|
|
189
|
+
expectType<FastifyLoggerInstance>(childParent.child({}, { level: 'info' }))
|
|
190
|
+
expectType<FastifyLoggerInstance>(childParent.child({}, { redact: ['pass', 'pin'] }))
|
|
191
|
+
expectType<FastifyLoggerInstance>(childParent.child({}, { serializers: { key: () => {} } }))
|
|
192
|
+
expectType<FastifyLoggerInstance>(childParent.child({}, { level: 'info', redact: ['pass', 'pin'], serializers: { key: () => {} } }))
|
|
193
|
+
|
|
194
|
+
// no option pass
|
|
195
|
+
expectError(childParent.child())
|
|
196
|
+
// wrong option
|
|
197
|
+
expectError(childParent.child({}, { nonExist: true }))
|
package/types/logger.d.ts
CHANGED
|
@@ -36,12 +36,23 @@ export type LogLevel = 'info' | 'error' | 'debug' | 'fatal' | 'warn' | 'trace'
|
|
|
36
36
|
|
|
37
37
|
export type SerializerFn = (value: unknown) => unknown;
|
|
38
38
|
|
|
39
|
+
export interface redactOptions {
|
|
40
|
+
paths: string[];
|
|
41
|
+
censor?: string | ((v: any) => any) | undefined;
|
|
42
|
+
remove?: boolean | undefined;
|
|
43
|
+
}
|
|
39
44
|
export interface Bindings {
|
|
40
45
|
level?: LogLevel | string;
|
|
41
46
|
serializers?: { [key: string]: SerializerFn };
|
|
42
47
|
[key: string]: unknown;
|
|
43
48
|
}
|
|
44
49
|
|
|
50
|
+
export interface ChildLoggerOptions {
|
|
51
|
+
level?: LogLevel | string;
|
|
52
|
+
redact?: string[] | redactOptions | undefined;
|
|
53
|
+
serializers?: { [key: string]: SerializerFn } | undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
45
56
|
export interface FastifyLoggerInstance {
|
|
46
57
|
info: FastifyLogFn;
|
|
47
58
|
warn: FastifyLogFn;
|
|
@@ -49,7 +60,7 @@ export interface FastifyLoggerInstance {
|
|
|
49
60
|
fatal: FastifyLogFn;
|
|
50
61
|
trace: FastifyLogFn;
|
|
51
62
|
debug: FastifyLogFn;
|
|
52
|
-
child(bindings: Bindings): FastifyLoggerInstance;
|
|
63
|
+
child(bindings: Bindings, options?: ChildLoggerOptions): FastifyLoggerInstance;
|
|
53
64
|
}
|
|
54
65
|
|
|
55
66
|
// This interface is accurate for pino 6.3 and was copied from the following permalink:
|