fastify 3.27.3 → 4.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/.taprc +3 -0
  2. package/README.md +7 -7
  3. package/build/build-error-serializer.js +27 -0
  4. package/build/build-validation.js +47 -35
  5. package/docs/Guides/Database.md +320 -0
  6. package/docs/Guides/Getting-Started.md +7 -7
  7. package/docs/Guides/Plugins-Guide.md +1 -1
  8. package/docs/Guides/Serverless.md +3 -3
  9. package/docs/Guides/Testing.md +2 -2
  10. package/docs/Migration-Guide-V4.md +12 -0
  11. package/docs/Reference/ContentTypeParser.md +4 -0
  12. package/docs/Reference/Decorators.md +2 -2
  13. package/docs/Reference/Encapsulation.md +2 -2
  14. package/docs/Reference/Errors.md +51 -6
  15. package/docs/Reference/HTTP2.md +3 -3
  16. package/docs/Reference/Hooks.md +4 -7
  17. package/docs/Reference/LTS.md +5 -4
  18. package/docs/Reference/Plugins.md +3 -3
  19. package/docs/Reference/Reply.md +23 -22
  20. package/docs/Reference/Request.md +1 -3
  21. package/docs/Reference/Routes.md +22 -15
  22. package/docs/Reference/Server.md +69 -119
  23. package/docs/Reference/TypeScript.md +20 -22
  24. package/docs/Reference/Validation-and-Serialization.md +30 -55
  25. package/docs/Type-Providers.md +257 -0
  26. package/examples/asyncawait.js +1 -1
  27. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  28. package/examples/benchmark/hooks-benchmark.js +1 -1
  29. package/examples/benchmark/simple.js +1 -1
  30. package/examples/hooks.js +2 -2
  31. package/examples/http2.js +1 -1
  32. package/examples/https.js +1 -1
  33. package/examples/parser.js +1 -1
  34. package/examples/route-prefix.js +1 -1
  35. package/examples/shared-schema.js +1 -1
  36. package/examples/simple-stream.js +18 -0
  37. package/examples/simple.js +1 -1
  38. package/examples/simple.mjs +1 -1
  39. package/examples/typescript-server.ts +1 -1
  40. package/examples/use-plugin.js +1 -1
  41. package/fastify.d.ts +34 -22
  42. package/fastify.js +40 -36
  43. package/lib/configValidator.js +902 -1023
  44. package/lib/contentTypeParser.js +6 -16
  45. package/lib/context.js +36 -10
  46. package/lib/decorate.js +3 -1
  47. package/lib/error-handler.js +158 -0
  48. package/lib/error-serializer.js +257 -0
  49. package/lib/errors.js +43 -9
  50. package/lib/fourOhFour.js +31 -20
  51. package/lib/handleRequest.js +10 -13
  52. package/lib/hooks.js +14 -9
  53. package/lib/pluginOverride.js +0 -3
  54. package/lib/pluginUtils.js +3 -2
  55. package/lib/reply.js +29 -158
  56. package/lib/request.js +13 -10
  57. package/lib/route.js +131 -138
  58. package/lib/schema-controller.js +2 -2
  59. package/lib/schemas.js +27 -1
  60. package/lib/server.js +241 -116
  61. package/lib/symbols.js +4 -3
  62. package/lib/validation.js +2 -1
  63. package/lib/warnings.js +4 -12
  64. package/lib/wrapThenable.js +4 -11
  65. package/package.json +37 -39
  66. package/test/404s.test.js +258 -125
  67. package/test/500s.test.js +3 -3
  68. package/test/als.test.js +1 -1
  69. package/test/async-await.test.js +20 -76
  70. package/test/bodyLimit.test.js +1 -1
  71. package/test/build-certificate.js +6 -7
  72. package/test/case-insensitive.test.js +4 -4
  73. package/test/close-pipelining.test.js +2 -2
  74. package/test/close.test.js +11 -11
  75. package/test/content-parser.test.js +32 -0
  76. package/test/context-config.test.js +52 -0
  77. package/test/custom-http-server.test.js +14 -7
  78. package/test/custom-parser-async.test.js +1 -66
  79. package/test/custom-parser.test.js +92 -159
  80. package/test/custom-querystring-parser.test.js +3 -3
  81. package/test/decorator.test.js +11 -13
  82. package/test/delete.test.js +6 -6
  83. package/test/encapsulated-error-handler.test.js +50 -0
  84. package/test/esm/index.test.js +0 -14
  85. package/test/fastify-instance.test.js +4 -4
  86. package/test/fluent-schema.test.js +4 -4
  87. package/test/genReqId.test.js +1 -1
  88. package/test/get.test.js +4 -4
  89. package/test/handler-context.test.js +2 -2
  90. package/test/head.test.js +1 -1
  91. package/test/helper.js +19 -4
  92. package/test/hooks-async.test.js +15 -48
  93. package/test/hooks.on-ready.test.js +10 -5
  94. package/test/hooks.test.js +78 -119
  95. package/test/http2/closing.test.js +10 -16
  96. package/test/http2/constraint.test.js +1 -1
  97. package/test/http2/head.test.js +1 -1
  98. package/test/http2/plain.test.js +1 -1
  99. package/test/http2/secure-with-fallback.test.js +1 -1
  100. package/test/http2/secure.test.js +1 -1
  101. package/test/http2/unknown-http-method.test.js +4 -10
  102. package/test/https/custom-https-server.test.js +12 -6
  103. package/test/https/https.test.js +1 -1
  104. package/test/input-validation.js +3 -3
  105. package/test/internals/handleRequest.test.js +6 -43
  106. package/test/internals/initialConfig.test.js +41 -12
  107. package/test/internals/logger.test.js +2 -2
  108. package/test/internals/reply.test.js +281 -40
  109. package/test/internals/request.test.js +13 -7
  110. package/test/internals/server.test.js +88 -0
  111. package/test/listen.deprecated.test.js +202 -0
  112. package/test/listen.test.js +118 -150
  113. package/test/logger.test.js +82 -42
  114. package/test/maxRequestsPerSocket.test.js +8 -6
  115. package/test/middleware.test.js +2 -25
  116. package/test/nullable-validation.test.js +53 -16
  117. package/test/output-validation.test.js +1 -1
  118. package/test/plugin.test.js +47 -21
  119. package/test/pretty-print.test.js +22 -10
  120. package/test/promises.test.js +1 -1
  121. package/test/proto-poisoning.test.js +6 -6
  122. package/test/register.test.js +3 -3
  123. package/test/reply-error.test.js +126 -15
  124. package/test/request-error.test.js +3 -6
  125. package/test/route-hooks.test.js +18 -18
  126. package/test/route-prefix.test.js +2 -1
  127. package/test/route.test.js +206 -22
  128. package/test/router-options.test.js +2 -2
  129. package/test/schema-examples.test.js +11 -5
  130. package/test/schema-feature.test.js +25 -20
  131. package/test/schema-serialization.test.js +9 -9
  132. package/test/schema-special-usage.test.js +5 -153
  133. package/test/schema-validation.test.js +9 -9
  134. package/test/skip-reply-send.test.js +2 -2
  135. package/test/stream.test.js +82 -23
  136. package/test/throw.test.js +8 -5
  137. package/test/trust-proxy.test.js +6 -6
  138. package/test/type-provider.test.js +20 -0
  139. package/test/types/fastify.test-d.ts +10 -18
  140. package/test/types/import.js +2 -0
  141. package/test/types/import.ts +1 -0
  142. package/test/types/instance.test-d.ts +68 -17
  143. package/test/types/logger.test-d.ts +44 -15
  144. package/test/types/reply.test-d.ts +2 -1
  145. package/test/types/route.test-d.ts +8 -2
  146. package/test/types/schema.test-d.ts +2 -39
  147. package/test/types/type-provider.test-d.ts +417 -0
  148. package/test/url-rewriting.test.js +3 -3
  149. package/test/validation-error-handling.test.js +8 -8
  150. package/test/versioned-routes.test.js +30 -18
  151. package/test/wrapThenable.test.js +7 -6
  152. package/types/content-type-parser.d.ts +17 -8
  153. package/types/hooks.d.ts +102 -59
  154. package/types/instance.d.ts +244 -118
  155. package/types/logger.d.ts +18 -104
  156. package/types/plugin.d.ts +10 -4
  157. package/types/reply.d.ts +18 -12
  158. package/types/request.d.ts +10 -5
  159. package/types/route.d.ts +42 -31
  160. package/types/schema.d.ts +1 -1
  161. package/types/type-provider.d.ts +99 -0
  162. package/types/utils.d.ts +1 -1
  163. package/lib/schema-compilers.js +0 -12
  164. package/test/emit-warning.test.js +0 -166
@@ -0,0 +1,202 @@
1
+ 'use strict'
2
+
3
+ // Tests for deprecated `.listen` signature. This file should be
4
+ // removed when the deprecation is complete.
5
+
6
+ const { test, before } = require('tap')
7
+ const dns = require('dns').promises
8
+ const Fastify = require('..')
9
+
10
+ let localhost
11
+ let localhostForURL
12
+
13
+ process.removeAllListeners('warning')
14
+
15
+ before(async function (t) {
16
+ const lookup = await dns.lookup('localhost')
17
+ localhost = lookup.address
18
+ if (lookup.family === 6) {
19
+ localhostForURL = `[${lookup.address}]`
20
+ } else {
21
+ localhostForURL = localhost
22
+ }
23
+ })
24
+
25
+ test('listen accepts a port and a callback', t => {
26
+ t.plan(2)
27
+ const fastify = Fastify()
28
+ t.teardown(fastify.close.bind(fastify))
29
+ fastify.listen(0, (err) => {
30
+ t.equal(fastify.server.address().address, localhost)
31
+ t.error(err)
32
+ })
33
+ })
34
+
35
+ test('listen accepts a port and a callback with (err, address)', t => {
36
+ t.plan(2)
37
+ const fastify = Fastify()
38
+ t.teardown(fastify.close.bind(fastify))
39
+ fastify.listen(0, (err, address) => {
40
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
41
+ t.error(err)
42
+ })
43
+ })
44
+
45
+ test('listen accepts a port, address, and callback', t => {
46
+ t.plan(1)
47
+ const fastify = Fastify()
48
+ t.teardown(fastify.close.bind(fastify))
49
+ fastify.listen(0, localhost, (err) => {
50
+ t.error(err)
51
+ })
52
+ })
53
+
54
+ test('listen accepts options, backlog and a callback', t => {
55
+ t.plan(1)
56
+ const fastify = Fastify()
57
+ t.teardown(fastify.close.bind(fastify))
58
+ fastify.listen({
59
+ port: 0,
60
+ host: 'localhost'
61
+ }, 511, (err) => {
62
+ t.error(err)
63
+ })
64
+ })
65
+
66
+ test('listen accepts options (no port), backlog and a callback', t => {
67
+ t.plan(1)
68
+ const fastify = Fastify()
69
+ t.teardown(fastify.close.bind(fastify))
70
+ fastify.listen({
71
+ host: 'localhost'
72
+ }, 511, (err) => {
73
+ t.error(err)
74
+ })
75
+ })
76
+
77
+ test('listen accepts options (no host), backlog and a callback', t => {
78
+ t.plan(1)
79
+ const fastify = Fastify()
80
+ t.teardown(fastify.close.bind(fastify))
81
+ fastify.listen({
82
+ port: 0
83
+ }, 511, (err) => {
84
+ t.error(err)
85
+ })
86
+ })
87
+
88
+ test('listen accepts options (no port, no host), backlog and a callback', t => {
89
+ t.plan(1)
90
+ const fastify = Fastify()
91
+ t.teardown(fastify.close.bind(fastify))
92
+ fastify.listen({
93
+ ipv6Only: false
94
+ }, 511, (err) => {
95
+ t.error(err)
96
+ })
97
+ })
98
+
99
+ test('listen accepts a port, address and a callback with (err, address)', t => {
100
+ t.plan(2)
101
+ const fastify = Fastify()
102
+ t.teardown(fastify.close.bind(fastify))
103
+ fastify.listen(0, localhost, (err, address) => {
104
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
105
+ t.error(err)
106
+ })
107
+ })
108
+
109
+ test('listen accepts a port, address, backlog and callback', t => {
110
+ t.plan(1)
111
+ const fastify = Fastify()
112
+ t.teardown(fastify.close.bind(fastify))
113
+ fastify.listen(0, localhost, 511, (err) => {
114
+ t.error(err)
115
+ })
116
+ })
117
+
118
+ test('listen accepts a port, address, backlog and callback with (err, address)', t => {
119
+ t.plan(2)
120
+ const fastify = Fastify()
121
+ t.teardown(fastify.close.bind(fastify))
122
+ fastify.listen(0, localhost, 511, (err, address) => {
123
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
124
+ t.error(err)
125
+ })
126
+ })
127
+
128
+ test('listen without callback (port zero)', t => {
129
+ t.plan(1)
130
+ const fastify = Fastify()
131
+ t.teardown(fastify.close.bind(fastify))
132
+ fastify.listen(0)
133
+ .then(() => {
134
+ t.equal(fastify.server.address().address, localhost)
135
+ })
136
+ })
137
+
138
+ test('listen without callback (port not given)', t => {
139
+ t.plan(1)
140
+ const fastify = Fastify()
141
+ t.teardown(fastify.close.bind(fastify))
142
+ fastify.listen()
143
+ .then(() => {
144
+ t.equal(fastify.server.address().address, localhost)
145
+ })
146
+ })
147
+
148
+ test('listen null without callback with (address)', t => {
149
+ t.plan(1)
150
+ const fastify = Fastify()
151
+ t.teardown(fastify.close.bind(fastify))
152
+ fastify.listen(null)
153
+ .then(address => {
154
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
155
+ })
156
+ })
157
+
158
+ test('listen without port without callback with (address)', t => {
159
+ t.plan(1)
160
+ const fastify = Fastify()
161
+ t.teardown(fastify.close.bind(fastify))
162
+ fastify.listen()
163
+ .then(address => {
164
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
165
+ })
166
+ })
167
+
168
+ test('listen with undefined without callback with (address)', t => {
169
+ t.plan(1)
170
+ const fastify = Fastify()
171
+ t.teardown(fastify.close.bind(fastify))
172
+ fastify.listen(undefined)
173
+ .then(address => {
174
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
175
+ })
176
+ })
177
+
178
+ test('listen when firstArg is string(pipe) and without backlog', async t => {
179
+ t.plan(1)
180
+ const fastify = Fastify()
181
+ t.teardown(fastify.close.bind(fastify))
182
+ const address = await fastify.listen('\\\\.\\pipe\\testPipe')
183
+ t.equal(address, '\\\\.\\pipe\\testPipe')
184
+ })
185
+
186
+ test('listen when firstArg is string(pipe) and with backlog', async t => {
187
+ t.plan(1)
188
+ const fastify = Fastify()
189
+ t.teardown(fastify.close.bind(fastify))
190
+ const address = await fastify.listen('\\\\.\\pipe\\testPipe2', 511)
191
+ t.equal(address, '\\\\.\\pipe\\testPipe2')
192
+ })
193
+
194
+ test('listen when firstArg is { path: string(pipe) } and with backlog and callback', t => {
195
+ t.plan(2)
196
+ const fastify = Fastify()
197
+ t.teardown(fastify.close.bind(fastify))
198
+ fastify.listen({ path: '\\\\.\\pipe\\testPipe3' }, 511, (err, address) => {
199
+ t.error(err)
200
+ t.equal(address, '\\\\.\\pipe\\testPipe3')
201
+ })
202
+ })
@@ -4,8 +4,10 @@ const os = require('os')
4
4
  const path = require('path')
5
5
  const fs = require('fs')
6
6
  const { test, before } = require('tap')
7
- const Fastify = require('..')
8
7
  const dns = require('dns').promises
8
+ const dnsCb = require('dns')
9
+ const sget = require('simple-get').concat
10
+ const Fastify = require('..')
9
11
 
10
12
  let localhost
11
13
  let localhostForURL
@@ -20,46 +22,38 @@ before(async function () {
20
22
  }
21
23
  })
22
24
 
23
- test('listen accepts a callback', t => {
24
- t.plan(2)
25
- const fastify = Fastify()
26
- t.teardown(fastify.close.bind(fastify))
27
- fastify.listen((err) => {
28
- t.equal(fastify.server.address().address, localhost)
29
- t.error(err)
25
+ test('listen works without arguments', async t => {
26
+ process.on('warning', () => {
27
+ t.fail('should not be deprecated')
30
28
  })
31
- })
32
29
 
33
- test('listen accepts a port and a callback', t => {
34
- t.plan(2)
35
30
  const fastify = Fastify()
36
31
  t.teardown(fastify.close.bind(fastify))
37
- fastify.listen(0, (err) => {
38
- t.equal(fastify.server.address().address, localhost)
39
- t.error(err)
40
- })
32
+ await fastify.listen()
33
+ const address = fastify.server.address()
34
+ t.equal(address.address, localhost)
35
+ t.ok(address.port > 0)
41
36
  })
42
37
 
43
- test('listen accepts a port and a callback with (err, address)', t => {
44
- t.plan(2)
45
- const fastify = Fastify()
46
- t.teardown(fastify.close.bind(fastify))
47
- fastify.listen(0, (err, address) => {
48
- t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
49
- t.error(err)
38
+ test('listen accepts a callback', t => {
39
+ process.on('warning', () => {
40
+ t.fail('should not be deprecated')
50
41
  })
51
- })
52
42
 
53
- test('listen accepts a port, address, and callback', t => {
54
- t.plan(1)
43
+ t.plan(2)
55
44
  const fastify = Fastify()
56
45
  t.teardown(fastify.close.bind(fastify))
57
- fastify.listen(0, localhost, (err) => {
46
+ fastify.listen({ port: 0 }, (err) => {
47
+ t.equal(fastify.server.address().address, localhost)
58
48
  t.error(err)
59
49
  })
60
50
  })
61
51
 
62
52
  test('listen accepts options and a callback', t => {
53
+ process.on('warning', () => {
54
+ t.fail('should not be deprecated')
55
+ })
56
+
63
57
  t.plan(1)
64
58
  const fastify = Fastify()
65
59
  t.teardown(fastify.close.bind(fastify))
@@ -76,54 +70,13 @@ test('listen accepts options and a callback', t => {
76
70
  })
77
71
  })
78
72
 
79
- test('listen accepts options, backlog and a callback', t => {
80
- t.plan(1)
81
- const fastify = Fastify()
82
- t.teardown(fastify.close.bind(fastify))
83
- fastify.listen({
84
- port: 0,
85
- host: 'localhost'
86
- }, 511, (err) => {
87
- t.error(err)
88
- })
89
- })
90
-
91
- test('listen accepts a port, address and a callback with (err, address)', t => {
92
- t.plan(2)
93
- const fastify = Fastify()
94
- t.teardown(fastify.close.bind(fastify))
95
- fastify.listen(0, localhost, (err, address) => {
96
- t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
97
- t.error(err)
98
- })
99
- })
100
-
101
- test('listen accepts a port, address, backlog and callback', t => {
102
- t.plan(1)
103
- const fastify = Fastify()
104
- t.teardown(fastify.close.bind(fastify))
105
- fastify.listen(0, localhost, 511, (err) => {
106
- t.error(err)
107
- })
108
- })
109
-
110
- test('listen accepts a port, address, backlog and callback with (err, address)', t => {
111
- t.plan(2)
112
- const fastify = Fastify()
113
- t.teardown(fastify.close.bind(fastify))
114
- fastify.listen(0, localhost, 511, (err, address) => {
115
- t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
116
- t.error(err)
117
- })
118
- })
119
-
120
73
  test('listen after Promise.resolve()', t => {
121
74
  t.plan(2)
122
75
  const f = Fastify()
123
76
  t.teardown(f.close.bind(f))
124
77
  Promise.resolve()
125
78
  .then(() => {
126
- f.listen(0, (err, address) => {
79
+ f.listen({ port: 0 }, (err, address) => {
127
80
  f.server.unref()
128
81
  t.equal(address, `http://${localhostForURL}:${f.server.address().port}`)
129
82
  t.error(err)
@@ -153,9 +106,9 @@ test('double listen errors', t => {
153
106
  t.plan(3)
154
107
  const fastify = Fastify()
155
108
  t.teardown(fastify.close.bind(fastify))
156
- fastify.listen(0, (err) => {
109
+ fastify.listen({ port: 0 }, (err) => {
157
110
  t.error(err)
158
- fastify.listen(fastify.server.address().port, (err, address) => {
111
+ fastify.listen({ port: fastify.server.address().port }, (err, address) => {
159
112
  t.equal(address, null)
160
113
  t.ok(err)
161
114
  })
@@ -166,10 +119,10 @@ test('double listen errors callback with (err, address)', t => {
166
119
  t.plan(4)
167
120
  const fastify = Fastify()
168
121
  t.teardown(fastify.close.bind(fastify))
169
- fastify.listen(0, (err1, address1) => {
122
+ fastify.listen({ port: 0 }, (err1, address1) => {
170
123
  t.equal(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
171
124
  t.error(err1)
172
- fastify.listen(fastify.server.address().port, (err2, address2) => {
125
+ fastify.listen({ port: fastify.server.address().port }, (err2, address2) => {
173
126
  t.equal(address2, null)
174
127
  t.ok(err2)
175
128
  })
@@ -180,12 +133,12 @@ test('listen twice on the same port', t => {
180
133
  t.plan(4)
181
134
  const fastify = Fastify()
182
135
  t.teardown(fastify.close.bind(fastify))
183
- fastify.listen(0, (err1, address1) => {
136
+ fastify.listen({ port: 0 }, (err1, address1) => {
184
137
  t.equal(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
185
138
  t.error(err1)
186
139
  const s2 = Fastify()
187
140
  t.teardown(s2.close.bind(s2))
188
- s2.listen(fastify.server.address().port, (err2, address2) => {
141
+ s2.listen({ port: fastify.server.address().port }, (err2, address2) => {
189
142
  t.equal(address2, null)
190
143
  t.ok(err2)
191
144
  })
@@ -196,13 +149,13 @@ test('listen twice on the same port callback with (err, address)', t => {
196
149
  t.plan(4)
197
150
  const fastify = Fastify()
198
151
  t.teardown(fastify.close.bind(fastify))
199
- fastify.listen(0, (err1, address1) => {
152
+ fastify.listen({ port: 0 }, (err1, address1) => {
200
153
  const _port = fastify.server.address().port
201
154
  t.equal(address1, `http://${localhostForURL}:${_port}`)
202
155
  t.error(err1)
203
156
  const s2 = Fastify()
204
157
  t.teardown(s2.close.bind(s2))
205
- s2.listen(_port, (err2, address2) => {
158
+ s2.listen({ port: _port }, (err2, address2) => {
206
159
  t.equal(address2, null)
207
160
  t.ok(err2)
208
161
  })
@@ -221,7 +174,7 @@ if (os.platform() !== 'win32') {
221
174
  fs.unlinkSync(sockFile)
222
175
  } catch (e) { }
223
176
 
224
- fastify.listen(sockFile, (err, address) => {
177
+ fastify.listen({ path: sockFile }, (err, address) => {
225
178
  t.error(err)
226
179
  t.equal(sockFile, fastify.server.address())
227
180
  t.equal(address, sockFile)
@@ -229,61 +182,11 @@ if (os.platform() !== 'win32') {
229
182
  })
230
183
  }
231
184
 
232
- test('listen without callback (port zero)', t => {
233
- t.plan(1)
234
- const fastify = Fastify()
235
- t.teardown(fastify.close.bind(fastify))
236
- fastify.listen(0)
237
- .then(() => {
238
- t.equal(fastify.server.address().address, localhost)
239
- })
240
- })
241
-
242
- test('listen without callback (port not given)', t => {
243
- t.plan(1)
244
- const fastify = Fastify()
245
- t.teardown(fastify.close.bind(fastify))
246
- fastify.listen()
247
- .then(() => {
248
- t.equal(fastify.server.address().address, localhost)
249
- })
250
- })
251
-
252
- test('listen null without callback with (address)', t => {
253
- t.plan(1)
254
- const fastify = Fastify()
255
- t.teardown(fastify.close.bind(fastify))
256
- fastify.listen(null)
257
- .then(address => {
258
- t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
259
- })
260
- })
261
-
262
- test('listen without port without callback with (address)', t => {
263
- t.plan(1)
264
- const fastify = Fastify()
265
- t.teardown(fastify.close.bind(fastify))
266
- fastify.listen()
267
- .then(address => {
268
- t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
269
- })
270
- })
271
-
272
- test('listen with undefined without callback with (address)', t => {
273
- t.plan(1)
274
- const fastify = Fastify()
275
- t.teardown(fastify.close.bind(fastify))
276
- fastify.listen(undefined)
277
- .then(address => {
278
- t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
279
- })
280
- })
281
-
282
185
  test('listen without callback with (address)', t => {
283
186
  t.plan(1)
284
187
  const fastify = Fastify()
285
188
  t.teardown(fastify.close.bind(fastify))
286
- fastify.listen(0)
189
+ fastify.listen({ port: 0 })
287
190
  .then(address => {
288
191
  t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
289
192
  })
@@ -293,9 +196,9 @@ test('double listen without callback rejects', t => {
293
196
  t.plan(1)
294
197
  const fastify = Fastify()
295
198
  t.teardown(fastify.close.bind(fastify))
296
- fastify.listen(0)
199
+ fastify.listen({ port: 0 })
297
200
  .then(() => {
298
- fastify.listen(0)
201
+ fastify.listen({ port: 0 })
299
202
  .catch(err => {
300
203
  t.ok(err)
301
204
  })
@@ -307,10 +210,10 @@ test('double listen without callback with (address)', t => {
307
210
  t.plan(2)
308
211
  const fastify = Fastify()
309
212
  t.teardown(fastify.close.bind(fastify))
310
- fastify.listen(0)
213
+ fastify.listen({ port: 0 })
311
214
  .then(address => {
312
215
  t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
313
- fastify.listen(0)
216
+ fastify.listen({ port: 0 })
314
217
  .catch(err => {
315
218
  t.ok(err)
316
219
  })
@@ -323,11 +226,11 @@ test('listen twice on the same port without callback rejects', t => {
323
226
  const fastify = Fastify()
324
227
  t.teardown(fastify.close.bind(fastify))
325
228
 
326
- fastify.listen(0)
229
+ fastify.listen({ port: 0 })
327
230
  .then(() => {
328
231
  const s2 = Fastify()
329
232
  t.teardown(s2.close.bind(s2))
330
- s2.listen(fastify.server.address().port)
233
+ s2.listen({ port: fastify.server.address().port })
331
234
  .catch(err => {
332
235
  t.ok(err)
333
236
  })
@@ -339,12 +242,12 @@ test('listen twice on the same port without callback rejects with (address)', t
339
242
  t.plan(2)
340
243
  const fastify = Fastify()
341
244
  t.teardown(fastify.close.bind(fastify))
342
- fastify.listen(0)
245
+ fastify.listen({ port: 0 })
343
246
  .then(address => {
344
247
  const s2 = Fastify()
345
248
  t.teardown(s2.close.bind(s2))
346
249
  t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
347
- s2.listen(fastify.server.address().port)
250
+ s2.listen({ port: fastify.server.address().port })
348
251
  .catch(err => {
349
252
  t.ok(err)
350
253
  })
@@ -355,7 +258,7 @@ test('listen twice on the same port without callback rejects with (address)', t
355
258
  test('listen on invalid port without callback rejects', t => {
356
259
  const fastify = Fastify()
357
260
  t.teardown(fastify.close.bind(fastify))
358
- return fastify.listen(-1)
261
+ return fastify.listen({ port: -1 })
359
262
  .catch(err => {
360
263
  t.ok(err)
361
264
  return true
@@ -372,24 +275,89 @@ test('listen logs the port as info', t => {
372
275
  msgs.push(msg)
373
276
  }
374
277
 
375
- fastify.listen(0)
278
+ fastify.listen({ port: 0 })
376
279
  .then(() => {
377
280
  t.ok(/http:\/\//.test(msgs[0]))
378
281
  })
379
282
  })
380
283
 
381
- test('listen when firstArg is string(pipe) and without backlog', async t => {
382
- t.plan(1)
383
- const fastify = Fastify()
384
- t.teardown(fastify.close.bind(fastify))
385
- const address = await fastify.listen('\\\\.\\pipe\\testPipe')
386
- t.equal(address, '\\\\.\\pipe\\testPipe')
284
+ test('listen on localhost binds IPv4 and IPv6 - promise interface', async t => {
285
+ const lookups = await dns.lookup('localhost', { all: true })
286
+ t.plan(2 * lookups.length)
287
+
288
+ const app = Fastify()
289
+ app.get('/', async () => 'hello localhost')
290
+ t.teardown(app.close.bind(app))
291
+ await app.listen({ port: 0, host: 'localhost' })
292
+
293
+ for (const lookup of lookups) {
294
+ await new Promise((resolve, reject) => {
295
+ sget({
296
+ method: 'GET',
297
+ url: getUrl(app, lookup)
298
+ }, (err, response, body) => {
299
+ if (err) { return reject(err) }
300
+ t.equal(response.statusCode, 200)
301
+ t.same(body.toString(), 'hello localhost')
302
+ resolve()
303
+ })
304
+ })
305
+ }
387
306
  })
388
307
 
389
- test('listen when firstArg is string(pipe) and with backlog', async t => {
390
- t.plan(1)
391
- const fastify = Fastify()
392
- t.teardown(fastify.close.bind(fastify))
393
- const address = await fastify.listen('\\\\.\\pipe\\testPipe', 511)
394
- t.equal(address, '\\\\.\\pipe\\testPipe')
308
+ test('listen on localhost binds to all interfaces (both IPv4 and IPv6 if present) - callback interface', t => {
309
+ dnsCb.lookup('localhost', { all: true }, (err, lookups) => {
310
+ t.plan(2 + (3 * lookups.length))
311
+ t.error(err)
312
+
313
+ const app = Fastify()
314
+ app.get('/', async () => 'hello localhost')
315
+ app.listen({ port: 0, host: 'localhost' }, (err) => {
316
+ t.error(err)
317
+ t.teardown(app.close.bind(app))
318
+
319
+ for (const lookup of lookups) {
320
+ sget({
321
+ method: 'GET',
322
+ url: getUrl(app, lookup)
323
+ }, (err, response, body) => {
324
+ t.error(err)
325
+ t.equal(response.statusCode, 200)
326
+ t.same(body.toString(), 'hello localhost')
327
+ })
328
+ }
329
+ })
330
+ })
331
+ })
332
+
333
+ test('addresses getter', async t => {
334
+ t.plan(4)
335
+ const app = Fastify()
336
+ app.get('/', async () => 'hello localhost')
337
+
338
+ t.same(app.addresses(), [], 'before ready')
339
+ await app.ready()
340
+
341
+ t.same(app.addresses(), [], 'after ready')
342
+ await app.listen({ port: 0, host: 'localhost' })
343
+ const { port } = app.server.address()
344
+ const localAddresses = await dns.lookup('localhost', { all: true })
345
+ for (const address of localAddresses) {
346
+ address.port = port
347
+ address.family = 'IPv' + address.family
348
+ }
349
+ localAddresses.sort((a, b) => a.address.localeCompare(b.address))
350
+ t.same(app.addresses().sort((a, b) => a.address.localeCompare(b.address)), localAddresses, 'after listen')
351
+
352
+ await app.close()
353
+ t.same(app.addresses(), [], 'after close')
395
354
  })
355
+
356
+ function getUrl (fastify, lookup) {
357
+ const { port } = fastify.server.address()
358
+ if (lookup.family === 6) {
359
+ return `http://[${lookup.address}]:${port}/`
360
+ } else {
361
+ return `http://${lookup.address}:${port}/`
362
+ }
363
+ }