fastify 5.2.1 → 5.3.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/LICENSE +1 -1
- package/PROJECT_CHARTER.md +7 -7
- package/README.md +24 -25
- package/SPONSORS.md +1 -0
- package/docs/Guides/Benchmarking.md +4 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +10 -10
- package/docs/Guides/Ecosystem.md +7 -1
- package/docs/Guides/Fluent-Schema.md +1 -1
- package/docs/Guides/Getting-Started.md +9 -5
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +1 -1
- package/docs/Guides/Migration-Guide-V5.md +12 -2
- package/docs/Guides/Plugins-Guide.md +6 -6
- package/docs/Guides/Serverless.md +14 -48
- package/docs/Guides/Style-Guide.md +2 -2
- package/docs/Guides/Testing.md +2 -2
- package/docs/Guides/Write-Plugin.md +2 -3
- package/docs/Reference/ContentTypeParser.md +58 -78
- package/docs/Reference/Decorators.md +249 -60
- package/docs/Reference/Encapsulation.md +28 -33
- package/docs/Reference/Errors.md +52 -53
- package/docs/Reference/HTTP2.md +7 -7
- package/docs/Reference/Hooks.md +31 -30
- package/docs/Reference/LTS.md +10 -15
- package/docs/Reference/Lifecycle.md +19 -24
- package/docs/Reference/Logging.md +59 -56
- package/docs/Reference/Middleware.md +19 -19
- package/docs/Reference/Plugins.md +55 -71
- package/docs/Reference/Principles.md +25 -30
- package/docs/Reference/Reply.md +11 -10
- package/docs/Reference/Request.md +89 -98
- package/docs/Reference/Routes.md +108 -128
- package/docs/Reference/Server.md +18 -16
- package/docs/Reference/Type-Providers.md +19 -21
- package/docs/Reference/TypeScript.md +1 -18
- package/docs/Reference/Validation-and-Serialization.md +134 -159
- package/docs/Reference/Warnings.md +22 -25
- package/fastify.js +3 -2
- package/lib/contentTypeParser.js +7 -8
- package/lib/decorate.js +18 -3
- package/lib/error-handler.js +14 -12
- package/lib/errors.js +4 -0
- package/lib/headRoute.js +4 -2
- package/lib/pluginUtils.js +4 -2
- package/lib/reply.js +17 -2
- package/lib/request.js +28 -2
- package/lib/server.js +5 -0
- package/lib/validation.js +1 -1
- package/lib/warnings.js +9 -0
- package/lib/wrapThenable.js +8 -1
- package/package.json +12 -12
- package/test/bundler/esbuild/package.json +1 -1
- package/test/close.test.js +125 -108
- package/test/custom-parser-async.test.js +34 -36
- package/test/custom-parser.4.test.js +55 -38
- package/test/decorator.test.js +174 -4
- package/test/fastify-instance.test.js +12 -2
- package/test/genReqId.test.js +125 -174
- package/test/has-route.test.js +1 -3
- package/test/hooks.on-listen.test.js +17 -14
- package/test/internals/content-type-parser.test.js +1 -1
- package/test/internals/errors.test.js +14 -1
- package/test/issue-4959.test.js +84 -0
- package/test/listen.1.test.js +37 -34
- package/test/listen.2.test.js +50 -40
- package/test/listen.3.test.js +28 -32
- package/test/listen.4.test.js +61 -45
- package/test/listen.5.test.js +23 -0
- package/test/register.test.js +55 -50
- package/test/request-error.test.js +114 -94
- package/test/route-shorthand.test.js +36 -32
- package/test/stream.5.test.js +35 -33
- package/test/throw.test.js +87 -91
- package/test/toolkit.js +32 -0
- package/test/trust-proxy.test.js +23 -23
- package/test/types/instance.test-d.ts +4 -0
- package/test/types/reply.test-d.ts +1 -0
- package/test/types/request.test-d.ts +4 -0
- package/test/types/type-provider.test-d.ts +40 -0
- package/test/upgrade.test.js +32 -33
- package/types/instance.d.ts +6 -0
- package/types/reply.d.ts +1 -0
- package/types/request.d.ts +2 -0
- package/types/type-provider.d.ts +12 -3
|
@@ -6,6 +6,9 @@ const fp = require('fastify-plugin')
|
|
|
6
6
|
const split = require('split2')
|
|
7
7
|
const helper = require('./helper')
|
|
8
8
|
const { kState } = require('../lib/symbols')
|
|
9
|
+
const { networkInterfaces } = require('node:os')
|
|
10
|
+
|
|
11
|
+
const isIPv6Missing = !Object.values(networkInterfaces()).flat().some(({ family }) => family === 'IPv6')
|
|
9
12
|
|
|
10
13
|
let localhost
|
|
11
14
|
before(async function () {
|
|
@@ -405,7 +408,7 @@ test('localhost onListen encapsulation should be called in order and should log
|
|
|
405
408
|
})
|
|
406
409
|
})
|
|
407
410
|
|
|
408
|
-
test('non-localhost onListen should be called in order', t => {
|
|
411
|
+
test('non-localhost onListen should be called in order', { skip: isIPv6Missing }, t => {
|
|
409
412
|
t.plan(2)
|
|
410
413
|
|
|
411
414
|
const fastify = Fastify()
|
|
@@ -428,7 +431,7 @@ test('non-localhost onListen should be called in order', t => {
|
|
|
428
431
|
})
|
|
429
432
|
})
|
|
430
433
|
|
|
431
|
-
test('non-localhost async onListen should be called in order', async t => {
|
|
434
|
+
test('non-localhost async onListen should be called in order', { skip: isIPv6Missing }, async t => {
|
|
432
435
|
t.plan(2)
|
|
433
436
|
const fastify = Fastify()
|
|
434
437
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -448,7 +451,7 @@ test('non-localhost async onListen should be called in order', async t => {
|
|
|
448
451
|
})
|
|
449
452
|
})
|
|
450
453
|
|
|
451
|
-
test('non-localhost sync onListen should log errors as warnings and continue', t => {
|
|
454
|
+
test('non-localhost sync onListen should log errors as warnings and continue', { skip: isIPv6Missing }, t => {
|
|
452
455
|
t.plan(4)
|
|
453
456
|
const stream = split(JSON.parse)
|
|
454
457
|
const fastify = Fastify({
|
|
@@ -488,7 +491,7 @@ test('non-localhost sync onListen should log errors as warnings and continue', t
|
|
|
488
491
|
})
|
|
489
492
|
})
|
|
490
493
|
|
|
491
|
-
test('non-localhost async onListen should log errors as warnings and continue', async t => {
|
|
494
|
+
test('non-localhost async onListen should log errors as warnings and continue', { skip: isIPv6Missing }, async t => {
|
|
492
495
|
t.plan(6)
|
|
493
496
|
const stream = split(JSON.parse)
|
|
494
497
|
const fastify = Fastify({
|
|
@@ -529,7 +532,7 @@ test('non-localhost async onListen should log errors as warnings and continue',
|
|
|
529
532
|
})
|
|
530
533
|
})
|
|
531
534
|
|
|
532
|
-
test('non-localhost Register onListen hook after a plugin inside a plugin', t => {
|
|
535
|
+
test('non-localhost Register onListen hook after a plugin inside a plugin', { skip: isIPv6Missing }, t => {
|
|
533
536
|
t.plan(3)
|
|
534
537
|
const fastify = Fastify()
|
|
535
538
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -562,7 +565,7 @@ test('non-localhost Register onListen hook after a plugin inside a plugin', t =>
|
|
|
562
565
|
})
|
|
563
566
|
})
|
|
564
567
|
|
|
565
|
-
test('non-localhost Register onListen hook after a plugin inside a plugin should log errors as warnings and continue', t => {
|
|
568
|
+
test('non-localhost Register onListen hook after a plugin inside a plugin should log errors as warnings and continue', { skip: isIPv6Missing }, t => {
|
|
566
569
|
t.plan(6)
|
|
567
570
|
const stream = split(JSON.parse)
|
|
568
571
|
const fastify = Fastify({
|
|
@@ -608,7 +611,7 @@ test('non-localhost Register onListen hook after a plugin inside a plugin should
|
|
|
608
611
|
})
|
|
609
612
|
})
|
|
610
613
|
|
|
611
|
-
test('non-localhost onListen encapsulation should be called in order', t => {
|
|
614
|
+
test('non-localhost onListen encapsulation should be called in order', { skip: isIPv6Missing }, t => {
|
|
612
615
|
t.plan(6)
|
|
613
616
|
const fastify = Fastify()
|
|
614
617
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -640,7 +643,7 @@ test('non-localhost onListen encapsulation should be called in order', t => {
|
|
|
640
643
|
})
|
|
641
644
|
})
|
|
642
645
|
|
|
643
|
-
test('non-localhost onListen encapsulation should be called in order and should log errors as warnings and continue', t => {
|
|
646
|
+
test('non-localhost onListen encapsulation should be called in order and should log errors as warnings and continue', { skip: isIPv6Missing }, t => {
|
|
644
647
|
t.plan(7)
|
|
645
648
|
const stream = split(JSON.parse)
|
|
646
649
|
const fastify = Fastify({
|
|
@@ -874,7 +877,7 @@ test('onListen localhost with callback encapsulation should be called in order',
|
|
|
874
877
|
})
|
|
875
878
|
})
|
|
876
879
|
|
|
877
|
-
test('onListen non-localhost should work in order with callback in sync', t => {
|
|
880
|
+
test('onListen non-localhost should work in order with callback in sync', { skip: isIPv6Missing }, t => {
|
|
878
881
|
t.plan(4)
|
|
879
882
|
const fastify = Fastify()
|
|
880
883
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -896,7 +899,7 @@ test('onListen non-localhost should work in order with callback in sync', t => {
|
|
|
896
899
|
})
|
|
897
900
|
})
|
|
898
901
|
|
|
899
|
-
test('onListen non-localhost should work in order with callback in async', t => {
|
|
902
|
+
test('onListen non-localhost should work in order with callback in async', { skip: isIPv6Missing }, t => {
|
|
900
903
|
t.plan(4)
|
|
901
904
|
const fastify = Fastify()
|
|
902
905
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -916,7 +919,7 @@ test('onListen non-localhost should work in order with callback in async', t =>
|
|
|
916
919
|
})
|
|
917
920
|
})
|
|
918
921
|
|
|
919
|
-
test('onListen non-localhost sync with callback should log errors as warnings and continue', t => {
|
|
922
|
+
test('onListen non-localhost sync with callback should log errors as warnings and continue', { skip: isIPv6Missing }, t => {
|
|
920
923
|
t.plan(8)
|
|
921
924
|
|
|
922
925
|
const stream = split(JSON.parse)
|
|
@@ -960,7 +963,7 @@ test('onListen non-localhost sync with callback should log errors as warnings an
|
|
|
960
963
|
})
|
|
961
964
|
})
|
|
962
965
|
|
|
963
|
-
test('onListen non-localhost async with callback should log errors as warnings and continue', t => {
|
|
966
|
+
test('onListen non-localhost async with callback should log errors as warnings and continue', { skip: isIPv6Missing }, t => {
|
|
964
967
|
t.plan(8)
|
|
965
968
|
|
|
966
969
|
const stream = split(JSON.parse)
|
|
@@ -1002,7 +1005,7 @@ test('onListen non-localhost async with callback should log errors as warnings a
|
|
|
1002
1005
|
})
|
|
1003
1006
|
})
|
|
1004
1007
|
|
|
1005
|
-
test('Register onListen hook non-localhost with callback after a plugin inside a plugin', t => {
|
|
1008
|
+
test('Register onListen hook non-localhost with callback after a plugin inside a plugin', { skip: isIPv6Missing }, t => {
|
|
1006
1009
|
t.plan(5)
|
|
1007
1010
|
const fastify = Fastify()
|
|
1008
1011
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -1035,7 +1038,7 @@ test('Register onListen hook non-localhost with callback after a plugin inside a
|
|
|
1035
1038
|
})
|
|
1036
1039
|
})
|
|
1037
1040
|
|
|
1038
|
-
test('onListen non-localhost with callback encapsulation should be called in order', t => {
|
|
1041
|
+
test('onListen non-localhost with callback encapsulation should be called in order', { skip: isIPv6Missing }, t => {
|
|
1039
1042
|
t.plan(8)
|
|
1040
1043
|
const fastify = Fastify()
|
|
1041
1044
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -61,7 +61,7 @@ test('Should support Webpack and faux modules', t => {
|
|
|
61
61
|
t.plan(2)
|
|
62
62
|
|
|
63
63
|
const internals = proxyquire('../../lib/contentTypeParser', {
|
|
64
|
-
'
|
|
64
|
+
'toad-cache': { default: () => { } }
|
|
65
65
|
})[kTestInternals]
|
|
66
66
|
|
|
67
67
|
const body = Buffer.from('你好 世界')
|
|
@@ -5,7 +5,7 @@ const errors = require('../../lib/errors')
|
|
|
5
5
|
const { readFileSync } = require('node:fs')
|
|
6
6
|
const { resolve } = require('node:path')
|
|
7
7
|
|
|
8
|
-
const expectedErrors =
|
|
8
|
+
const expectedErrors = 85
|
|
9
9
|
|
|
10
10
|
test(`should expose ${expectedErrors} errors`, t => {
|
|
11
11
|
t.plan(1)
|
|
@@ -21,6 +21,7 @@ test(`should expose ${expectedErrors} errors`, t => {
|
|
|
21
21
|
|
|
22
22
|
test('ensure name and codes of Errors are identical', t => {
|
|
23
23
|
t.plan(expectedErrors)
|
|
24
|
+
|
|
24
25
|
const exportedKeys = Object.keys(errors)
|
|
25
26
|
for (const key of exportedKeys) {
|
|
26
27
|
if (errors[key].name === 'FastifyError') {
|
|
@@ -249,6 +250,16 @@ test('FST_ERR_DEC_REFERENCE_TYPE', t => {
|
|
|
249
250
|
t.assert.ok(error instanceof Error)
|
|
250
251
|
})
|
|
251
252
|
|
|
253
|
+
test('FST_ERR_DEC_UNDECLARED', t => {
|
|
254
|
+
t.plan(5)
|
|
255
|
+
const error = new errors.FST_ERR_DEC_UNDECLARED('myDecorator', 'request')
|
|
256
|
+
t.assert.strictEqual(error.name, 'FastifyError')
|
|
257
|
+
t.assert.strictEqual(error.code, 'FST_ERR_DEC_UNDECLARED')
|
|
258
|
+
t.assert.strictEqual(error.message, "No decorator 'myDecorator' has been declared on request.")
|
|
259
|
+
t.assert.strictEqual(error.statusCode, 500)
|
|
260
|
+
t.assert.ok(error instanceof Error)
|
|
261
|
+
})
|
|
262
|
+
|
|
252
263
|
test('FST_ERR_HOOK_INVALID_TYPE', t => {
|
|
253
264
|
t.plan(5)
|
|
254
265
|
const error = new errors.FST_ERR_HOOK_INVALID_TYPE()
|
|
@@ -881,6 +892,7 @@ test('FST_ERR_ERROR_HANDLER_NOT_FN', t => {
|
|
|
881
892
|
|
|
882
893
|
test('Ensure that all errors are in Errors.md TOC', t => {
|
|
883
894
|
t.plan(expectedErrors)
|
|
895
|
+
|
|
884
896
|
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
|
|
885
897
|
|
|
886
898
|
const exportedKeys = Object.keys(errors)
|
|
@@ -918,6 +930,7 @@ test('Ensure that all errors are in Errors.md documented', t => {
|
|
|
918
930
|
|
|
919
931
|
test('Ensure that non-existing errors are not in Errors.md documented', t => {
|
|
920
932
|
t.plan(expectedErrors)
|
|
933
|
+
|
|
921
934
|
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
|
|
922
935
|
|
|
923
936
|
const matchRE = /<a id="[0-9a-zA-Z_]+">([0-9a-zA-Z_]+)<\/a>/g
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const http = require('node:http')
|
|
5
|
+
const Fastify = require('../fastify')
|
|
6
|
+
|
|
7
|
+
function runBadClientCall (reqOptions, payload) {
|
|
8
|
+
let innerResolve, innerReject
|
|
9
|
+
const promise = new Promise((resolve, reject) => {
|
|
10
|
+
innerResolve = resolve
|
|
11
|
+
innerReject = reject
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const postData = JSON.stringify(payload)
|
|
15
|
+
|
|
16
|
+
const req = http.request({
|
|
17
|
+
...reqOptions,
|
|
18
|
+
headers: {
|
|
19
|
+
'Content-Type': 'application/json',
|
|
20
|
+
'Content-Length': Buffer.byteLength(postData),
|
|
21
|
+
}
|
|
22
|
+
}, () => {
|
|
23
|
+
innerReject(new Error('Request should have failed'))
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
// Kill the socket immediately (before sending data)
|
|
27
|
+
req.on('socket', (socket) => {
|
|
28
|
+
setTimeout(() => { socket.destroy() }, 5)
|
|
29
|
+
})
|
|
30
|
+
req.on('error', innerResolve)
|
|
31
|
+
req.write(postData)
|
|
32
|
+
req.end()
|
|
33
|
+
|
|
34
|
+
return promise
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('should handle a soket error', async (t) => {
|
|
38
|
+
t.plan(4)
|
|
39
|
+
const fastify = Fastify()
|
|
40
|
+
|
|
41
|
+
function shouldNotHappen () {
|
|
42
|
+
t.assert.fail('This should not happen')
|
|
43
|
+
}
|
|
44
|
+
process.on('unhandledRejection', shouldNotHappen)
|
|
45
|
+
|
|
46
|
+
t.after(() => {
|
|
47
|
+
fastify.close()
|
|
48
|
+
process.removeListener('unhandledRejection', shouldNotHappen)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
fastify.addHook('onRequest', async (request, reply) => {
|
|
52
|
+
t.assert.ok('onRequest hook called')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
fastify.addHook('onSend', async (request, reply, payload) => {
|
|
56
|
+
if (request.onSendCalled) {
|
|
57
|
+
t.assert.fail('onSend hook called more than once')
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
t.assert.ok('onSend hook called')
|
|
62
|
+
request.onSendCalled = true
|
|
63
|
+
|
|
64
|
+
// Introduce a delay
|
|
65
|
+
await new Promise(resolve => setTimeout(resolve, 5))
|
|
66
|
+
return payload
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// The handler must be async to trigger the error
|
|
70
|
+
fastify.put('/', async (request, reply) => {
|
|
71
|
+
t.assert.ok('PUT handler called')
|
|
72
|
+
return reply.send({ hello: 'world' })
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
await fastify.listen({ port: 0 })
|
|
76
|
+
|
|
77
|
+
const err = await runBadClientCall({
|
|
78
|
+
hostname: 'localhost',
|
|
79
|
+
port: fastify.server.address().port,
|
|
80
|
+
path: '/',
|
|
81
|
+
method: 'PUT',
|
|
82
|
+
}, { test: 'me' })
|
|
83
|
+
t.assert.equal(err.code, 'ECONNRESET')
|
|
84
|
+
})
|
package/test/listen.1.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test, before } = require('
|
|
3
|
+
const { test, before } = require('node:test')
|
|
4
4
|
const Fastify = require('..')
|
|
5
5
|
const helper = require('./helper')
|
|
6
6
|
|
|
@@ -13,69 +13,70 @@ before(async function () {
|
|
|
13
13
|
|
|
14
14
|
test('listen works without arguments', async t => {
|
|
15
15
|
const doNotWarn = () => {
|
|
16
|
-
t.fail('should not be deprecated')
|
|
16
|
+
t.assert.fail('should not be deprecated')
|
|
17
17
|
}
|
|
18
18
|
process.on('warning', doNotWarn)
|
|
19
19
|
|
|
20
20
|
const fastify = Fastify()
|
|
21
|
-
t.
|
|
21
|
+
t.after(() => {
|
|
22
22
|
fastify.close()
|
|
23
23
|
process.removeListener('warning', doNotWarn)
|
|
24
24
|
})
|
|
25
25
|
await fastify.listen()
|
|
26
26
|
const address = fastify.server.address()
|
|
27
|
-
t.
|
|
28
|
-
t.ok(address.port > 0)
|
|
27
|
+
t.assert.strictEqual(address.address, localhost)
|
|
28
|
+
t.assert.ok(address.port > 0)
|
|
29
29
|
})
|
|
30
30
|
|
|
31
31
|
test('Async/await listen with arguments', async t => {
|
|
32
32
|
const doNotWarn = () => {
|
|
33
|
-
t.fail('should not be deprecated')
|
|
33
|
+
t.assert.fail('should not be deprecated')
|
|
34
34
|
}
|
|
35
35
|
process.on('warning', doNotWarn)
|
|
36
36
|
|
|
37
37
|
const fastify = Fastify()
|
|
38
|
-
t.
|
|
38
|
+
t.after(() => {
|
|
39
39
|
fastify.close()
|
|
40
40
|
process.removeListener('warning', doNotWarn)
|
|
41
41
|
})
|
|
42
42
|
const addr = await fastify.listen({ port: 0, host: '0.0.0.0' })
|
|
43
43
|
const address = fastify.server.address()
|
|
44
|
-
t.
|
|
45
|
-
t.
|
|
44
|
+
t.assert.strictEqual(addr, `http://127.0.0.1:${address.port}`)
|
|
45
|
+
t.assert.deepEqual(address, {
|
|
46
46
|
address: '0.0.0.0',
|
|
47
47
|
family: 'IPv4',
|
|
48
48
|
port: address.port
|
|
49
49
|
})
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
test('listen accepts a callback', t => {
|
|
52
|
+
test('listen accepts a callback', (t, done) => {
|
|
53
53
|
t.plan(2)
|
|
54
54
|
const doNotWarn = () => {
|
|
55
|
-
t.fail('should not be deprecated')
|
|
55
|
+
t.assert.fail('should not be deprecated')
|
|
56
56
|
}
|
|
57
57
|
process.on('warning', doNotWarn)
|
|
58
58
|
|
|
59
59
|
const fastify = Fastify()
|
|
60
|
-
t.
|
|
60
|
+
t.after(() => {
|
|
61
61
|
fastify.close()
|
|
62
62
|
process.removeListener('warning', doNotWarn)
|
|
63
63
|
})
|
|
64
64
|
fastify.listen({ port: 0 }, (err) => {
|
|
65
|
-
t.
|
|
66
|
-
t.
|
|
65
|
+
t.assert.ifError(err)
|
|
66
|
+
t.assert.strictEqual(fastify.server.address().address, localhost)
|
|
67
|
+
done()
|
|
67
68
|
})
|
|
68
69
|
})
|
|
69
70
|
|
|
70
|
-
test('listen accepts options and a callback', t => {
|
|
71
|
+
test('listen accepts options and a callback', (t, done) => {
|
|
71
72
|
t.plan(1)
|
|
72
73
|
const doNotWarn = () => {
|
|
73
|
-
t.fail('should not be deprecated')
|
|
74
|
+
t.assert.fail('should not be deprecated')
|
|
74
75
|
}
|
|
75
76
|
process.on('warning', doNotWarn)
|
|
76
77
|
|
|
77
78
|
const fastify = Fastify()
|
|
78
|
-
t.
|
|
79
|
+
t.after(() => {
|
|
79
80
|
fastify.close()
|
|
80
81
|
process.removeListener('warning', doNotWarn)
|
|
81
82
|
})
|
|
@@ -88,40 +89,42 @@ test('listen accepts options and a callback', t => {
|
|
|
88
89
|
writableAll: false,
|
|
89
90
|
ipv6Only: false
|
|
90
91
|
}, (err) => {
|
|
91
|
-
t.
|
|
92
|
+
t.assert.ifError(err)
|
|
93
|
+
done()
|
|
92
94
|
})
|
|
93
95
|
})
|
|
94
96
|
|
|
95
|
-
test('listen after Promise.resolve()', t => {
|
|
97
|
+
test('listen after Promise.resolve()', (t, done) => {
|
|
96
98
|
t.plan(2)
|
|
97
|
-
const
|
|
98
|
-
t.
|
|
99
|
+
const fastify = Fastify()
|
|
100
|
+
t.after(() => fastify.close())
|
|
99
101
|
Promise.resolve()
|
|
100
102
|
.then(() => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
t.
|
|
104
|
-
t.
|
|
103
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
104
|
+
fastify.server.unref()
|
|
105
|
+
t.assert.strictEqual(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
106
|
+
t.assert.ifError(err)
|
|
107
|
+
done()
|
|
105
108
|
})
|
|
106
109
|
})
|
|
107
110
|
})
|
|
108
111
|
|
|
109
112
|
test('listen works with undefined host', async t => {
|
|
110
113
|
const doNotWarn = () => {
|
|
111
|
-
t.fail('should not be deprecated')
|
|
114
|
+
t.assert.fail('should not be deprecated')
|
|
112
115
|
}
|
|
113
116
|
process.on('warning', doNotWarn)
|
|
114
117
|
|
|
115
118
|
const fastify = Fastify()
|
|
116
|
-
t.
|
|
117
|
-
t.
|
|
119
|
+
t.after(() => fastify.close())
|
|
120
|
+
t.after(() => {
|
|
118
121
|
fastify.close()
|
|
119
122
|
process.removeListener('warning', doNotWarn)
|
|
120
123
|
})
|
|
121
124
|
await fastify.listen({ host: undefined, port: 0 })
|
|
122
125
|
const address = fastify.server.address()
|
|
123
|
-
t.
|
|
124
|
-
t.ok(address.port > 0)
|
|
126
|
+
t.assert.strictEqual(address.address, localhost)
|
|
127
|
+
t.assert.ok(address.port > 0)
|
|
125
128
|
})
|
|
126
129
|
|
|
127
130
|
test('listen works with null host', async t => {
|
|
@@ -131,13 +134,13 @@ test('listen works with null host', async t => {
|
|
|
131
134
|
process.on('warning', doNotWarn)
|
|
132
135
|
|
|
133
136
|
const fastify = Fastify()
|
|
134
|
-
t.
|
|
135
|
-
t.
|
|
137
|
+
t.after(() => fastify.close())
|
|
138
|
+
t.after(() => {
|
|
136
139
|
fastify.close()
|
|
137
140
|
process.removeListener('warning', doNotWarn)
|
|
138
141
|
})
|
|
139
142
|
await fastify.listen({ host: null, port: 0 })
|
|
140
143
|
const address = fastify.server.address()
|
|
141
|
-
t.
|
|
142
|
-
t.ok(address.port > 0)
|
|
144
|
+
t.assert.strictEqual(address.address, localhost)
|
|
145
|
+
t.assert.ok(address.port > 0)
|
|
143
146
|
})
|
package/test/listen.2.test.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test, before } = require('
|
|
3
|
+
const { test, before } = require('node:test')
|
|
4
4
|
const Fastify = require('..')
|
|
5
5
|
const helper = require('./helper')
|
|
6
|
+
const { networkInterfaces } = require('node:os')
|
|
7
|
+
|
|
8
|
+
const isIPv6Missing = !Object.values(networkInterfaces()).flat().some(({ family }) => family === 'IPv6')
|
|
6
9
|
|
|
7
10
|
let localhostForURL
|
|
8
11
|
|
|
@@ -10,94 +13,101 @@ before(async function () {
|
|
|
10
13
|
[, localhostForURL] = await helper.getLoopbackHost()
|
|
11
14
|
})
|
|
12
15
|
|
|
13
|
-
test('register after listen using Promise.resolve()', t => {
|
|
16
|
+
test('register after listen using Promise.resolve()', async t => {
|
|
14
17
|
t.plan(1)
|
|
15
|
-
const
|
|
18
|
+
const fastify = Fastify()
|
|
16
19
|
|
|
17
20
|
const handler = (req, res) => res.send({})
|
|
18
|
-
Promise.resolve()
|
|
21
|
+
await Promise.resolve()
|
|
19
22
|
.then(() => {
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
fastify.get('/', handler)
|
|
24
|
+
fastify.register((f2, options, done) => {
|
|
22
25
|
f2.get('/plugin', handler)
|
|
23
26
|
done()
|
|
24
27
|
})
|
|
25
|
-
return
|
|
28
|
+
return fastify.ready()
|
|
29
|
+
})
|
|
30
|
+
.catch((err) => {
|
|
31
|
+
t.assert.fail(err.message)
|
|
26
32
|
})
|
|
27
|
-
.
|
|
28
|
-
.then(() => t.pass('resolved'))
|
|
33
|
+
.then(() => t.assert.ok('resolved'))
|
|
29
34
|
})
|
|
30
35
|
|
|
31
|
-
test('double listen errors', t => {
|
|
36
|
+
test('double listen errors', (t, done) => {
|
|
32
37
|
t.plan(3)
|
|
33
38
|
const fastify = Fastify()
|
|
34
|
-
t.
|
|
39
|
+
t.after(() => fastify.close())
|
|
35
40
|
fastify.listen({ port: 0 }, (err) => {
|
|
36
|
-
t.
|
|
41
|
+
t.assert.ifError(err)
|
|
37
42
|
fastify.listen({ port: fastify.server.address().port }, (err, address) => {
|
|
38
|
-
t.
|
|
39
|
-
t.ok(err)
|
|
43
|
+
t.assert.strictEqual(address, null)
|
|
44
|
+
t.assert.ok(err)
|
|
45
|
+
done()
|
|
40
46
|
})
|
|
41
47
|
})
|
|
42
48
|
})
|
|
43
49
|
|
|
44
|
-
test('double listen errors callback with (err, address)', t => {
|
|
50
|
+
test('double listen errors callback with (err, address)', (t, done) => {
|
|
45
51
|
t.plan(4)
|
|
46
52
|
const fastify = Fastify()
|
|
47
|
-
t.
|
|
53
|
+
t.after(() => fastify.close())
|
|
48
54
|
fastify.listen({ port: 0 }, (err1, address1) => {
|
|
49
|
-
t.
|
|
50
|
-
t.
|
|
55
|
+
t.assert.strictEqual(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
56
|
+
t.assert.ifError(err1)
|
|
51
57
|
fastify.listen({ port: fastify.server.address().port }, (err2, address2) => {
|
|
52
|
-
t.
|
|
53
|
-
t.ok(err2)
|
|
58
|
+
t.assert.strictEqual(address2, null)
|
|
59
|
+
t.assert.ok(err2)
|
|
60
|
+
done()
|
|
54
61
|
})
|
|
55
62
|
})
|
|
56
63
|
})
|
|
57
64
|
|
|
58
|
-
test('nonlocalhost double listen errors callback with (err, address)', t => {
|
|
65
|
+
test('nonlocalhost double listen errors callback with (err, address)', { skip: isIPv6Missing }, (t, done) => {
|
|
59
66
|
t.plan(4)
|
|
60
67
|
const fastify = Fastify()
|
|
61
|
-
t.
|
|
68
|
+
t.after(() => fastify.close())
|
|
62
69
|
fastify.listen({ host: '::1', port: 0 }, (err, address) => {
|
|
63
|
-
t.
|
|
64
|
-
t.
|
|
70
|
+
t.assert.strictEqual(address, `http://${'[::1]'}:${fastify.server.address().port}`)
|
|
71
|
+
t.assert.ifError(err)
|
|
65
72
|
fastify.listen({ host: '::1', port: fastify.server.address().port }, (err2, address2) => {
|
|
66
|
-
t.
|
|
67
|
-
t.ok(err2)
|
|
73
|
+
t.assert.strictEqual(address2, null)
|
|
74
|
+
t.assert.ok(err2)
|
|
75
|
+
done()
|
|
68
76
|
})
|
|
69
77
|
})
|
|
70
78
|
})
|
|
71
79
|
|
|
72
|
-
test('listen twice on the same port', t => {
|
|
80
|
+
test('listen twice on the same port', (t, done) => {
|
|
73
81
|
t.plan(4)
|
|
74
82
|
const fastify = Fastify()
|
|
75
|
-
t.
|
|
83
|
+
t.after(() => fastify.close())
|
|
76
84
|
fastify.listen({ port: 0 }, (err1, address1) => {
|
|
77
|
-
t.
|
|
78
|
-
t.
|
|
85
|
+
t.assert.strictEqual(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
86
|
+
t.assert.ifError(err1)
|
|
79
87
|
const s2 = Fastify()
|
|
80
|
-
t.
|
|
88
|
+
t.after(() => fastify.close())
|
|
81
89
|
s2.listen({ port: fastify.server.address().port }, (err2, address2) => {
|
|
82
|
-
t.
|
|
83
|
-
t.ok(err2)
|
|
90
|
+
t.assert.strictEqual(address2, null)
|
|
91
|
+
t.assert.ok(err2)
|
|
92
|
+
done()
|
|
84
93
|
})
|
|
85
94
|
})
|
|
86
95
|
})
|
|
87
96
|
|
|
88
|
-
test('listen twice on the same port callback with (err, address)', t => {
|
|
97
|
+
test('listen twice on the same port callback with (err, address)', (t, done) => {
|
|
89
98
|
t.plan(4)
|
|
90
99
|
const fastify = Fastify()
|
|
91
|
-
t.
|
|
100
|
+
t.after(() => fastify.close())
|
|
92
101
|
fastify.listen({ port: 0 }, (err1, address1) => {
|
|
93
102
|
const _port = fastify.server.address().port
|
|
94
|
-
t.
|
|
95
|
-
t.
|
|
103
|
+
t.assert.strictEqual(address1, `http://${localhostForURL}:${_port}`)
|
|
104
|
+
t.assert.ifError(err1)
|
|
96
105
|
const s2 = Fastify()
|
|
97
|
-
t.
|
|
106
|
+
t.after(() => fastify.close())
|
|
98
107
|
s2.listen({ port: _port }, (err2, address2) => {
|
|
99
|
-
t.
|
|
100
|
-
t.ok(err2)
|
|
108
|
+
t.assert.strictEqual(address2, null)
|
|
109
|
+
t.assert.ok(err2)
|
|
110
|
+
done()
|
|
101
111
|
})
|
|
102
112
|
})
|
|
103
113
|
})
|