fastify 3.23.0 → 3.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/build/sync-version.js +11 -0
- package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
- package/docs/Guides/Ecosystem.md +508 -0
- package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
- package/docs/{Getting-Started.md → Guides/Getting-Started.md} +191 -57
- package/docs/Guides/Index.md +30 -4
- package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
- package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
- package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
- package/docs/{Serverless.md → Guides/Serverless.md} +200 -42
- package/docs/Guides/Style-Guide.md +246 -0
- package/docs/{Testing.md → Guides/Testing.md} +25 -11
- package/docs/Guides/Write-Plugin.md +102 -0
- package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
- package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
- package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
- package/docs/{Errors.md → Reference/Errors.md} +77 -47
- package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
- package/docs/{Hooks.md → Reference/Hooks.md} +148 -69
- package/docs/Reference/Index.md +71 -0
- package/docs/{LTS.md → Reference/LTS.md} +31 -32
- package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
- package/docs/{Logging.md → Reference/Logging.md} +68 -28
- package/docs/Reference/Middleware.md +78 -0
- package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
- package/docs/{Reply.md → Reference/Reply.md} +205 -94
- package/docs/{Request.md → Reference/Request.md} +32 -16
- package/docs/{Routes.md → Reference/Routes.md} +243 -111
- package/docs/{Server.md → Reference/Server.md} +516 -267
- package/docs/{TypeScript.md → Reference/TypeScript.md} +447 -187
- package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
- package/docs/index.md +24 -0
- package/examples/typescript-server.ts +1 -1
- package/fastify.js +4 -23
- package/lib/decorate.js +6 -3
- package/lib/logger.js +2 -2
- package/lib/pluginUtils.js +1 -3
- package/lib/reply.js +4 -1
- package/lib/route.js +1 -1
- package/lib/schema-controller.js +11 -2
- package/lib/server.js +9 -8
- package/lib/symbols.js +1 -0
- package/package.json +10 -4
- package/test/bundler/webpack/bundler-test.js +2 -6
- package/test/constrained-routes.test.js +220 -0
- package/test/custom-parser.test.js +11 -2
- package/test/decorator.test.js +24 -0
- package/test/handler-context.test.js +11 -4
- package/test/http2/closing.test.js +14 -5
- package/test/internals/logger.test.js +20 -0
- package/test/internals/reply.test.js +42 -0
- package/test/internals/version.test.js +6 -34
- package/test/listen.test.js +36 -22
- package/test/logger.test.js +16 -0
- package/test/maxRequestsPerSocket.test.js +10 -0
- package/test/request-error.test.js +2 -8
- package/test/requestTimeout.test.js +4 -1
- package/test/route-hooks.test.js +55 -0
- package/test/router-options.test.js +10 -1
- package/test/schema-feature.test.js +461 -0
- package/test/stream.test.js +14 -3
- package/test/trust-proxy.test.js +15 -7
- package/test/types/instance.test-d.ts +52 -1
- package/test/types/request.test-d.ts +7 -1
- package/test/types/route.test-d.ts +21 -0
- package/types/instance.d.ts +11 -6
- package/types/request.d.ts +4 -1
- package/types/route.d.ts +1 -1
- package/docs/Ecosystem.md +0 -211
- package/docs/Middleware.md +0 -53
- package/docs/Style-Guide.md +0 -185
- package/docs/Write-Plugin.md +0 -58
package/test/listen.test.js
CHANGED
|
@@ -3,15 +3,29 @@
|
|
|
3
3
|
const os = require('os')
|
|
4
4
|
const path = require('path')
|
|
5
5
|
const fs = require('fs')
|
|
6
|
-
const test = require('tap')
|
|
6
|
+
const { test, before } = require('tap')
|
|
7
7
|
const Fastify = require('..')
|
|
8
|
+
const dns = require('dns').promises
|
|
9
|
+
|
|
10
|
+
let localhost
|
|
11
|
+
let localhostForURL
|
|
12
|
+
|
|
13
|
+
before(async function () {
|
|
14
|
+
const lookup = await dns.lookup('localhost')
|
|
15
|
+
localhost = lookup.address
|
|
16
|
+
if (lookup.family === 6) {
|
|
17
|
+
localhostForURL = `[${lookup.address}]`
|
|
18
|
+
} else {
|
|
19
|
+
localhostForURL = localhost
|
|
20
|
+
}
|
|
21
|
+
})
|
|
8
22
|
|
|
9
23
|
test('listen accepts a callback', t => {
|
|
10
24
|
t.plan(2)
|
|
11
25
|
const fastify = Fastify()
|
|
12
26
|
t.teardown(fastify.close.bind(fastify))
|
|
13
27
|
fastify.listen((err) => {
|
|
14
|
-
t.equal(fastify.server.address().address,
|
|
28
|
+
t.equal(fastify.server.address().address, localhost)
|
|
15
29
|
t.error(err)
|
|
16
30
|
})
|
|
17
31
|
})
|
|
@@ -21,7 +35,7 @@ test('listen accepts a port and a callback', t => {
|
|
|
21
35
|
const fastify = Fastify()
|
|
22
36
|
t.teardown(fastify.close.bind(fastify))
|
|
23
37
|
fastify.listen(0, (err) => {
|
|
24
|
-
t.equal(fastify.server.address().address,
|
|
38
|
+
t.equal(fastify.server.address().address, localhost)
|
|
25
39
|
t.error(err)
|
|
26
40
|
})
|
|
27
41
|
})
|
|
@@ -31,7 +45,7 @@ test('listen accepts a port and a callback with (err, address)', t => {
|
|
|
31
45
|
const fastify = Fastify()
|
|
32
46
|
t.teardown(fastify.close.bind(fastify))
|
|
33
47
|
fastify.listen(0, (err, address) => {
|
|
34
|
-
t.equal(address,
|
|
48
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
35
49
|
t.error(err)
|
|
36
50
|
})
|
|
37
51
|
})
|
|
@@ -40,7 +54,7 @@ test('listen accepts a port, address, and callback', t => {
|
|
|
40
54
|
t.plan(1)
|
|
41
55
|
const fastify = Fastify()
|
|
42
56
|
t.teardown(fastify.close.bind(fastify))
|
|
43
|
-
fastify.listen(0,
|
|
57
|
+
fastify.listen(0, localhost, (err) => {
|
|
44
58
|
t.error(err)
|
|
45
59
|
})
|
|
46
60
|
})
|
|
@@ -78,8 +92,8 @@ test('listen accepts a port, address and a callback with (err, address)', t => {
|
|
|
78
92
|
t.plan(2)
|
|
79
93
|
const fastify = Fastify()
|
|
80
94
|
t.teardown(fastify.close.bind(fastify))
|
|
81
|
-
fastify.listen(0,
|
|
82
|
-
t.equal(address,
|
|
95
|
+
fastify.listen(0, localhost, (err, address) => {
|
|
96
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
83
97
|
t.error(err)
|
|
84
98
|
})
|
|
85
99
|
})
|
|
@@ -88,7 +102,7 @@ test('listen accepts a port, address, backlog and callback', t => {
|
|
|
88
102
|
t.plan(1)
|
|
89
103
|
const fastify = Fastify()
|
|
90
104
|
t.teardown(fastify.close.bind(fastify))
|
|
91
|
-
fastify.listen(0,
|
|
105
|
+
fastify.listen(0, localhost, 511, (err) => {
|
|
92
106
|
t.error(err)
|
|
93
107
|
})
|
|
94
108
|
})
|
|
@@ -97,8 +111,8 @@ test('listen accepts a port, address, backlog and callback with (err, address)',
|
|
|
97
111
|
t.plan(2)
|
|
98
112
|
const fastify = Fastify()
|
|
99
113
|
t.teardown(fastify.close.bind(fastify))
|
|
100
|
-
fastify.listen(0,
|
|
101
|
-
t.equal(address,
|
|
114
|
+
fastify.listen(0, localhost, 511, (err, address) => {
|
|
115
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
102
116
|
t.error(err)
|
|
103
117
|
})
|
|
104
118
|
})
|
|
@@ -111,7 +125,7 @@ test('listen after Promise.resolve()', t => {
|
|
|
111
125
|
.then(() => {
|
|
112
126
|
f.listen(0, (err, address) => {
|
|
113
127
|
f.server.unref()
|
|
114
|
-
t.equal(address,
|
|
128
|
+
t.equal(address, `http://${localhostForURL}:${f.server.address().port}`)
|
|
115
129
|
t.error(err)
|
|
116
130
|
})
|
|
117
131
|
})
|
|
@@ -153,7 +167,7 @@ test('double listen errors callback with (err, address)', t => {
|
|
|
153
167
|
const fastify = Fastify()
|
|
154
168
|
t.teardown(fastify.close.bind(fastify))
|
|
155
169
|
fastify.listen(0, (err1, address1) => {
|
|
156
|
-
t.equal(address1,
|
|
170
|
+
t.equal(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
157
171
|
t.error(err1)
|
|
158
172
|
fastify.listen(fastify.server.address().port, (err2, address2) => {
|
|
159
173
|
t.equal(address2, null)
|
|
@@ -167,7 +181,7 @@ test('listen twice on the same port', t => {
|
|
|
167
181
|
const fastify = Fastify()
|
|
168
182
|
t.teardown(fastify.close.bind(fastify))
|
|
169
183
|
fastify.listen(0, (err1, address1) => {
|
|
170
|
-
t.equal(address1,
|
|
184
|
+
t.equal(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
171
185
|
t.error(err1)
|
|
172
186
|
const s2 = Fastify()
|
|
173
187
|
t.teardown(s2.close.bind(s2))
|
|
@@ -184,7 +198,7 @@ test('listen twice on the same port callback with (err, address)', t => {
|
|
|
184
198
|
t.teardown(fastify.close.bind(fastify))
|
|
185
199
|
fastify.listen(0, (err1, address1) => {
|
|
186
200
|
const _port = fastify.server.address().port
|
|
187
|
-
t.equal(address1,
|
|
201
|
+
t.equal(address1, `http://${localhostForURL}:${_port}`)
|
|
188
202
|
t.error(err1)
|
|
189
203
|
const s2 = Fastify()
|
|
190
204
|
t.teardown(s2.close.bind(s2))
|
|
@@ -221,7 +235,7 @@ test('listen without callback (port zero)', t => {
|
|
|
221
235
|
t.teardown(fastify.close.bind(fastify))
|
|
222
236
|
fastify.listen(0)
|
|
223
237
|
.then(() => {
|
|
224
|
-
t.equal(fastify.server.address().address,
|
|
238
|
+
t.equal(fastify.server.address().address, localhost)
|
|
225
239
|
})
|
|
226
240
|
})
|
|
227
241
|
|
|
@@ -231,7 +245,7 @@ test('listen without callback (port not given)', t => {
|
|
|
231
245
|
t.teardown(fastify.close.bind(fastify))
|
|
232
246
|
fastify.listen()
|
|
233
247
|
.then(() => {
|
|
234
|
-
t.equal(fastify.server.address().address,
|
|
248
|
+
t.equal(fastify.server.address().address, localhost)
|
|
235
249
|
})
|
|
236
250
|
})
|
|
237
251
|
|
|
@@ -241,7 +255,7 @@ test('listen null without callback with (address)', t => {
|
|
|
241
255
|
t.teardown(fastify.close.bind(fastify))
|
|
242
256
|
fastify.listen(null)
|
|
243
257
|
.then(address => {
|
|
244
|
-
t.equal(address,
|
|
258
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
245
259
|
})
|
|
246
260
|
})
|
|
247
261
|
|
|
@@ -251,7 +265,7 @@ test('listen without port without callback with (address)', t => {
|
|
|
251
265
|
t.teardown(fastify.close.bind(fastify))
|
|
252
266
|
fastify.listen()
|
|
253
267
|
.then(address => {
|
|
254
|
-
t.equal(address,
|
|
268
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
255
269
|
})
|
|
256
270
|
})
|
|
257
271
|
|
|
@@ -261,7 +275,7 @@ test('listen with undefined without callback with (address)', t => {
|
|
|
261
275
|
t.teardown(fastify.close.bind(fastify))
|
|
262
276
|
fastify.listen(undefined)
|
|
263
277
|
.then(address => {
|
|
264
|
-
t.equal(address,
|
|
278
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
265
279
|
})
|
|
266
280
|
})
|
|
267
281
|
|
|
@@ -271,7 +285,7 @@ test('listen without callback with (address)', t => {
|
|
|
271
285
|
t.teardown(fastify.close.bind(fastify))
|
|
272
286
|
fastify.listen(0)
|
|
273
287
|
.then(address => {
|
|
274
|
-
t.equal(address,
|
|
288
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
275
289
|
})
|
|
276
290
|
})
|
|
277
291
|
|
|
@@ -295,7 +309,7 @@ test('double listen without callback with (address)', t => {
|
|
|
295
309
|
t.teardown(fastify.close.bind(fastify))
|
|
296
310
|
fastify.listen(0)
|
|
297
311
|
.then(address => {
|
|
298
|
-
t.equal(address,
|
|
312
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
299
313
|
fastify.listen(0)
|
|
300
314
|
.catch(err => {
|
|
301
315
|
t.ok(err)
|
|
@@ -329,7 +343,7 @@ test('listen twice on the same port without callback rejects with (address)', t
|
|
|
329
343
|
.then(address => {
|
|
330
344
|
const s2 = Fastify()
|
|
331
345
|
t.teardown(s2.close.bind(s2))
|
|
332
|
-
t.equal(address,
|
|
346
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
333
347
|
s2.listen(fastify.server.address().port)
|
|
334
348
|
.catch(err => {
|
|
335
349
|
t.ok(err)
|
package/test/logger.test.js
CHANGED
|
@@ -1516,3 +1516,19 @@ test('should create a default logger if provided one is invalid', t => {
|
|
|
1516
1516
|
|
|
1517
1517
|
t.pass()
|
|
1518
1518
|
})
|
|
1519
|
+
|
|
1520
|
+
test('should not throw error when serializing custom req', t => {
|
|
1521
|
+
t.plan(1)
|
|
1522
|
+
|
|
1523
|
+
const lines = []
|
|
1524
|
+
const dest = new stream.Writable({
|
|
1525
|
+
write: function (chunk, enc, cb) {
|
|
1526
|
+
lines.push(JSON.parse(chunk))
|
|
1527
|
+
cb()
|
|
1528
|
+
}
|
|
1529
|
+
})
|
|
1530
|
+
const fastify = Fastify({ logger: { level: 'info', stream: dest } })
|
|
1531
|
+
fastify.log.info({ req: {} })
|
|
1532
|
+
|
|
1533
|
+
t.same(lines[0].req, {})
|
|
1534
|
+
})
|
|
@@ -102,3 +102,13 @@ test('maxRequestsPerSocket should 0', async (t) => {
|
|
|
102
102
|
const initialConfig = Fastify().initialConfig
|
|
103
103
|
t.same(initialConfig.maxRequestsPerSocket, 0)
|
|
104
104
|
})
|
|
105
|
+
|
|
106
|
+
test('requestTimeout passed to server', t => {
|
|
107
|
+
t.plan(2)
|
|
108
|
+
|
|
109
|
+
const httpServer = Fastify({ maxRequestsPerSocket: 5 }).server
|
|
110
|
+
t.equal(httpServer.maxRequestsPerSocket, 5)
|
|
111
|
+
|
|
112
|
+
const httpsServer = Fastify({ maxRequestsPerSocket: 5, https: true }).server
|
|
113
|
+
t.equal(httpsServer.maxRequestsPerSocket, 5)
|
|
114
|
+
})
|
|
@@ -157,10 +157,7 @@ test('default clientError handler destroys sockets in writable state', t => {
|
|
|
157
157
|
|
|
158
158
|
const fastify = Fastify({
|
|
159
159
|
bodyLimit: 1,
|
|
160
|
-
keepAliveTimeout: 100
|
|
161
|
-
logger: {
|
|
162
|
-
level: 'trace'
|
|
163
|
-
}
|
|
160
|
+
keepAliveTimeout: 100
|
|
164
161
|
})
|
|
165
162
|
|
|
166
163
|
fastify.server.emit('clientError', new Error(), {
|
|
@@ -181,10 +178,7 @@ test('default clientError handler destroys http sockets in non-writable state',
|
|
|
181
178
|
|
|
182
179
|
const fastify = Fastify({
|
|
183
180
|
bodyLimit: 1,
|
|
184
|
-
keepAliveTimeout: 100
|
|
185
|
-
logger: {
|
|
186
|
-
level: 'trace'
|
|
187
|
-
}
|
|
181
|
+
keepAliveTimeout: 100
|
|
188
182
|
})
|
|
189
183
|
|
|
190
184
|
fastify.server.emit('clientError', new Error(), {
|
|
@@ -5,7 +5,7 @@ const { test } = require('tap')
|
|
|
5
5
|
const Fastify = require('../fastify')
|
|
6
6
|
|
|
7
7
|
test('requestTimeout passed to server', t => {
|
|
8
|
-
t.plan(
|
|
8
|
+
t.plan(5)
|
|
9
9
|
|
|
10
10
|
try {
|
|
11
11
|
Fastify({ requestTimeout: 500.1 })
|
|
@@ -24,6 +24,9 @@ test('requestTimeout passed to server', t => {
|
|
|
24
24
|
const httpServer = Fastify({ requestTimeout: 1000 }).server
|
|
25
25
|
t.equal(httpServer.requestTimeout, 1000)
|
|
26
26
|
|
|
27
|
+
const httpsServer = Fastify({ requestTimeout: 1000, https: true }).server
|
|
28
|
+
t.equal(httpsServer.requestTimeout, 1000)
|
|
29
|
+
|
|
27
30
|
const serverFactory = (handler, _) => {
|
|
28
31
|
const server = http.createServer((req, res) => {
|
|
29
32
|
handler(req, res)
|
package/test/route-hooks.test.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { Readable } = require('stream')
|
|
4
4
|
const test = require('tap').test
|
|
5
|
+
const sget = require('simple-get').concat
|
|
5
6
|
const Fastify = require('../')
|
|
6
7
|
|
|
7
8
|
process.removeAllListeners('warning')
|
|
@@ -496,3 +497,57 @@ test('onRequest option should be called before preParsing', t => {
|
|
|
496
497
|
t.same(payload, { hello: 'world' })
|
|
497
498
|
})
|
|
498
499
|
})
|
|
500
|
+
|
|
501
|
+
test('onTimeout on route', t => {
|
|
502
|
+
t.plan(4)
|
|
503
|
+
const fastify = Fastify({ connectionTimeout: 500 })
|
|
504
|
+
|
|
505
|
+
fastify.get('/timeout', {
|
|
506
|
+
async handler (request, reply) { },
|
|
507
|
+
onTimeout (request, reply, done) {
|
|
508
|
+
t.pass('onTimeout called')
|
|
509
|
+
done()
|
|
510
|
+
}
|
|
511
|
+
})
|
|
512
|
+
|
|
513
|
+
fastify.listen(0, (err, address) => {
|
|
514
|
+
t.error(err)
|
|
515
|
+
t.teardown(() => fastify.close())
|
|
516
|
+
|
|
517
|
+
sget({
|
|
518
|
+
method: 'GET',
|
|
519
|
+
url: `${address}/timeout`
|
|
520
|
+
}, (err, response, body) => {
|
|
521
|
+
t.type(err, Error)
|
|
522
|
+
t.equal(err.message, 'socket hang up')
|
|
523
|
+
})
|
|
524
|
+
})
|
|
525
|
+
})
|
|
526
|
+
|
|
527
|
+
test('onError on route', t => {
|
|
528
|
+
t.plan(3)
|
|
529
|
+
|
|
530
|
+
const fastify = Fastify()
|
|
531
|
+
|
|
532
|
+
const err = new Error('kaboom')
|
|
533
|
+
|
|
534
|
+
fastify.get('/',
|
|
535
|
+
{
|
|
536
|
+
onError (request, reply, error, done) {
|
|
537
|
+
t.match(error, err)
|
|
538
|
+
done()
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
(req, reply) => {
|
|
542
|
+
reply.send(err)
|
|
543
|
+
})
|
|
544
|
+
|
|
545
|
+
fastify.inject('/', (err, res) => {
|
|
546
|
+
t.error(err)
|
|
547
|
+
t.same(JSON.parse(res.payload), {
|
|
548
|
+
error: 'Internal Server Error',
|
|
549
|
+
message: 'kaboom',
|
|
550
|
+
statusCode: 500
|
|
551
|
+
})
|
|
552
|
+
})
|
|
553
|
+
})
|
|
@@ -5,6 +5,15 @@ const sget = require('simple-get')
|
|
|
5
5
|
const Fastify = require('../')
|
|
6
6
|
const { FST_ERR_BAD_URL } = require('../lib/errors')
|
|
7
7
|
|
|
8
|
+
function getUrl (app) {
|
|
9
|
+
const { address, port } = app.server.address()
|
|
10
|
+
if (address === '::1') {
|
|
11
|
+
return `http://[${address}]:${port}`
|
|
12
|
+
} else {
|
|
13
|
+
return `http://${address}:${port}`
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
8
17
|
test('Should honor ignoreTrailingSlash option', t => {
|
|
9
18
|
t.plan(4)
|
|
10
19
|
const fastify = Fastify({
|
|
@@ -19,7 +28,7 @@ test('Should honor ignoreTrailingSlash option', t => {
|
|
|
19
28
|
fastify.server.unref()
|
|
20
29
|
if (err) t.threw(err)
|
|
21
30
|
|
|
22
|
-
const baseUrl =
|
|
31
|
+
const baseUrl = getUrl(fastify)
|
|
23
32
|
|
|
24
33
|
sget.concat(baseUrl + '/test', (err, res, data) => {
|
|
25
34
|
if (err) t.threw(err)
|