fastify 5.0.0 → 5.2.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.
Files changed (201) hide show
  1. package/.borp.yaml +3 -0
  2. package/.vscode/settings.json +22 -0
  3. package/README.md +12 -7
  4. package/docs/Guides/Database.md +15 -15
  5. package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
  6. package/docs/Guides/Ecosystem.md +14 -15
  7. package/docs/Guides/Index.md +1 -1
  8. package/docs/Guides/Migration-Guide-V4.md +11 -11
  9. package/docs/Guides/Migration-Guide-V5.md +133 -9
  10. package/docs/Guides/Plugins-Guide.md +1 -1
  11. package/docs/Guides/Prototype-Poisoning.md +3 -3
  12. package/docs/Guides/Recommendations.md +9 -9
  13. package/docs/Guides/Serverless.md +5 -5
  14. package/docs/Guides/Testing.md +58 -57
  15. package/docs/Guides/Write-Plugin.md +2 -2
  16. package/docs/Guides/Write-Type-Provider.md +3 -3
  17. package/docs/Reference/ContentTypeParser.md +4 -4
  18. package/docs/Reference/Decorators.md +2 -2
  19. package/docs/Reference/Errors.md +3 -3
  20. package/docs/Reference/Hooks.md +7 -7
  21. package/docs/Reference/LTS.md +8 -0
  22. package/docs/Reference/Logging.md +5 -4
  23. package/docs/Reference/Reply.md +55 -58
  24. package/docs/Reference/Request.md +49 -42
  25. package/docs/Reference/Routes.md +16 -13
  26. package/docs/Reference/Server.md +32 -28
  27. package/docs/Reference/TypeScript.md +9 -9
  28. package/docs/Reference/Validation-and-Serialization.md +5 -5
  29. package/examples/typescript-server.ts +1 -1
  30. package/fastify.d.ts +14 -5
  31. package/fastify.js +8 -6
  32. package/lib/contentTypeParser.js +9 -7
  33. package/lib/context.js +1 -2
  34. package/lib/error-handler.js +9 -9
  35. package/lib/errors.js +1 -1
  36. package/lib/fourOhFour.js +1 -1
  37. package/lib/hooks.js +4 -1
  38. package/lib/{logger.js → logger-factory.js} +70 -122
  39. package/lib/logger-pino.js +68 -0
  40. package/lib/pluginOverride.js +1 -1
  41. package/lib/pluginUtils.js +2 -2
  42. package/lib/reply.js +4 -5
  43. package/lib/request.js +16 -9
  44. package/lib/route.js +23 -22
  45. package/lib/validation.js +2 -2
  46. package/package.json +13 -15
  47. package/test/404s.test.js +675 -629
  48. package/test/500s.test.js +72 -63
  49. package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
  50. package/test/als.test.js +48 -45
  51. package/test/async-await.test.js +148 -134
  52. package/test/async-dispose.test.js +4 -5
  53. package/test/async_hooks.test.js +30 -28
  54. package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
  55. package/test/buffer.test.js +9 -10
  56. package/test/build/error-serializer.test.js +3 -4
  57. package/test/build/version.test.js +2 -3
  58. package/test/build-certificate.js +1 -1
  59. package/test/bundler/README.md +5 -5
  60. package/test/bundler/esbuild/bundler-test.js +10 -9
  61. package/test/bundler/webpack/bundler-test.js +10 -9
  62. package/test/case-insensitive.test.js +31 -28
  63. package/test/chainable.test.js +4 -5
  64. package/test/check.test.js +8 -10
  65. package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
  66. package/test/client-timeout.test.js +5 -5
  67. package/test/close-pipelining.test.js +6 -8
  68. package/test/conditional-pino.test.js +47 -0
  69. package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
  70. package/test/constrained-routes.test.js +243 -236
  71. package/test/content-length.test.js +53 -68
  72. package/test/content-parser.test.js +186 -158
  73. package/test/content-type.test.js +8 -9
  74. package/test/context-config.test.js +44 -54
  75. package/test/custom-http-server.test.js +16 -20
  76. package/test/custom-parser.5.test.js +32 -32
  77. package/test/diagnostics-channel/404.test.js +15 -15
  78. package/test/diagnostics-channel/async-delay-request.test.js +25 -25
  79. package/test/diagnostics-channel/async-request.test.js +24 -24
  80. package/test/diagnostics-channel/error-before-handler.test.js +4 -5
  81. package/test/diagnostics-channel/error-request.test.js +19 -19
  82. package/test/diagnostics-channel/error-status.test.js +8 -8
  83. package/test/diagnostics-channel/init.test.js +6 -7
  84. package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
  85. package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
  86. package/test/diagnostics-channel/sync-request.test.js +19 -19
  87. package/test/encapsulated-child-logger-factory.test.js +8 -8
  88. package/test/encapsulated-error-handler.test.js +20 -20
  89. package/test/esm/errorCodes.test.mjs +5 -5
  90. package/test/esm/esm.test.mjs +3 -3
  91. package/test/esm/named-exports.mjs +3 -3
  92. package/test/esm/other.mjs +2 -2
  93. package/test/fastify-instance.test.js +33 -34
  94. package/test/{findRoute.test.js → find-route.test.js} +11 -10
  95. package/test/fluent-schema.test.js +33 -36
  96. package/test/handler-context.test.js +11 -11
  97. package/test/has-route.test.js +12 -15
  98. package/test/header-overflow.test.js +13 -12
  99. package/test/hooks.on-ready.test.js +2 -2
  100. package/test/hooks.test.js +25 -25
  101. package/test/http-methods/copy.test.js +22 -24
  102. package/test/http-methods/custom-http-methods.test.js +24 -21
  103. package/test/http-methods/get.test.js +97 -84
  104. package/test/http-methods/head.test.js +63 -57
  105. package/test/http-methods/lock.test.js +21 -20
  106. package/test/http-methods/mkcalendar.test.js +31 -27
  107. package/test/http-methods/mkcol.test.js +10 -10
  108. package/test/http-methods/move.test.js +11 -11
  109. package/test/http-methods/propfind.test.js +32 -27
  110. package/test/http-methods/proppatch.test.js +21 -19
  111. package/test/http-methods/report.test.js +32 -27
  112. package/test/http-methods/search.test.js +52 -47
  113. package/test/http-methods/trace.test.js +3 -4
  114. package/test/http-methods/unlock.test.js +10 -10
  115. package/test/http2/closing.test.js +50 -58
  116. package/test/http2/constraint.test.js +47 -50
  117. package/test/http2/head.test.js +18 -19
  118. package/test/http2/missing-http2-module.test.js +4 -5
  119. package/test/http2/plain.test.js +31 -31
  120. package/test/http2/secure-with-fallback.test.js +61 -61
  121. package/test/http2/secure.test.js +28 -31
  122. package/test/http2/unknown-http-method.test.js +13 -14
  123. package/test/https/custom-https-server.test.js +6 -7
  124. package/test/https/https.test.js +78 -78
  125. package/test/imports.test.js +5 -6
  126. package/test/internals/all.test.js +8 -11
  127. package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
  128. package/test/internals/context.test.js +9 -11
  129. package/test/internals/decorator.test.js +20 -21
  130. package/test/internals/errors.test.js +427 -427
  131. package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
  132. package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
  133. package/test/internals/hooks.test.js +31 -35
  134. package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
  135. package/test/internals/logger.test.js +28 -28
  136. package/test/internals/plugin.test.js +17 -18
  137. package/test/internals/reply-serialize.test.js +106 -106
  138. package/test/internals/reply.test.js +620 -585
  139. package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
  140. package/test/internals/request-validate.test.js +218 -221
  141. package/test/internals/request.test.js +225 -107
  142. package/test/internals/server.test.js +15 -12
  143. package/test/internals/validation.test.js +35 -36
  144. package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
  145. package/test/listen.5.test.js +9 -9
  146. package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
  147. package/test/middleware.test.js +4 -5
  148. package/test/noop-set.test.js +5 -5
  149. package/test/post-empty-body.test.js +18 -11
  150. package/test/pretty-print.test.js +59 -49
  151. package/test/proto-poisoning.test.js +42 -37
  152. package/test/reply-code.test.js +34 -32
  153. package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
  154. package/test/request-error.test.js +122 -0
  155. package/test/request-header-host.test.js +339 -0
  156. package/test/request-id.test.js +31 -25
  157. package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
  158. package/test/route.1.test.js +79 -72
  159. package/test/route.2.test.js +17 -16
  160. package/test/route.3.test.js +32 -27
  161. package/test/route.4.test.js +21 -25
  162. package/test/route.5.test.js +45 -64
  163. package/test/route.6.test.js +70 -89
  164. package/test/route.7.test.js +61 -65
  165. package/test/route.8.test.js +80 -18
  166. package/test/router-options.test.js +80 -77
  167. package/test/same-shape.test.js +5 -5
  168. package/test/schema-examples.test.js +72 -38
  169. package/test/serialize-response.test.js +9 -10
  170. package/test/server.test.js +75 -78
  171. package/test/set-error-handler.test.js +2 -3
  172. package/test/stream-serializers.test.js +10 -7
  173. package/test/sync-routes.test.js +18 -18
  174. package/test/test-reporter.mjs +68 -0
  175. package/test/trust-proxy.test.js +51 -45
  176. package/test/type-provider.test.js +8 -6
  177. package/test/types/content-type-parser.test-d.ts +1 -1
  178. package/test/types/fastify.test-d.ts +16 -4
  179. package/test/types/hooks.test-d.ts +2 -1
  180. package/test/types/instance.test-d.ts +13 -13
  181. package/test/types/logger.test-d.ts +2 -2
  182. package/test/types/plugin.test-d.ts +17 -9
  183. package/test/types/register.test-d.ts +22 -6
  184. package/test/types/reply.test-d.ts +1 -1
  185. package/test/types/route.test-d.ts +34 -4
  186. package/test/types/serverFactory.test-d.ts +1 -1
  187. package/test/types/type-provider.test-d.ts +1 -1
  188. package/test/url-rewriting.test.js +35 -38
  189. package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
  190. package/test/validation-error-handling.test.js +259 -285
  191. package/test/versioned-routes.test.js +126 -113
  192. package/test/web-api.test.js +48 -37
  193. package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
  194. package/types/hooks.d.ts +2 -1
  195. package/types/instance.d.ts +9 -2
  196. package/types/register.d.ts +12 -3
  197. package/types/reply.d.ts +1 -1
  198. package/types/request.d.ts +2 -6
  199. package/types/serverFactory.d.ts +3 -3
  200. package/types/utils.d.ts +13 -5
  201. package/test/types/import.js +0 -2
@@ -1,12 +1,13 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const { test, before } = t
3
+ const { test, before } = require('node:test')
5
4
  const sget = require('simple-get').concat
6
5
  const fastify = require('..')
7
6
  const helper = require('./helper')
8
7
 
9
- const sgetForwardedRequest = (app, forHeader, path, protoHeader) => {
8
+ const noop = () => {}
9
+
10
+ const sgetForwardedRequest = (app, forHeader, path, protoHeader, testCaseDone) => {
10
11
  const headers = {
11
12
  'X-Forwarded-For': forHeader,
12
13
  'X-Forwarded-Host': 'example.com'
@@ -18,30 +19,30 @@ const sgetForwardedRequest = (app, forHeader, path, protoHeader) => {
18
19
  method: 'GET',
19
20
  headers,
20
21
  url: 'http://localhost:' + app.server.address().port + path
21
- }, () => {})
22
+ }, testCaseDone || noop)
22
23
  }
23
24
 
24
25
  const testRequestValues = (t, req, options) => {
25
26
  if (options.ip) {
26
- t.ok(req.ip, 'ip is defined')
27
- t.equal(req.ip, options.ip, 'gets ip from x-forwarded-for')
27
+ t.assert.ok(req.ip, 'ip is defined')
28
+ t.assert.strictEqual(req.ip, options.ip, 'gets ip from x-forwarded-for')
28
29
  }
29
30
  if (options.host) {
30
- t.ok(req.host, 'host is defined')
31
- t.equal(req.host, options.host, 'gets host from x-forwarded-host')
32
- t.ok(req.hostname)
33
- t.equal(req.hostname, options.host, 'gets hostname from x-forwarded-host')
31
+ t.assert.ok(req.host, 'host is defined')
32
+ t.assert.strictEqual(req.host, options.host, 'gets host from x-forwarded-host')
33
+ t.assert.ok(req.hostname)
34
+ t.assert.strictEqual(req.hostname, options.host, 'gets hostname from x-forwarded-host')
34
35
  }
35
36
  if (options.ips) {
36
- t.same(req.ips, options.ips, 'gets ips from x-forwarded-for')
37
+ t.assert.deepStrictEqual(req.ips, options.ips, 'gets ips from x-forwarded-for')
37
38
  }
38
39
  if (options.protocol) {
39
- t.ok(req.protocol, 'protocol is defined')
40
- t.equal(req.protocol, options.protocol, 'gets protocol from x-forwarded-proto')
40
+ t.assert.ok(req.protocol, 'protocol is defined')
41
+ t.assert.strictEqual(req.protocol, options.protocol, 'gets protocol from x-forwarded-proto')
41
42
  }
42
43
  if (options.port) {
43
- t.ok(req.port, 'port is defined')
44
- t.equal(req.port, options.port, 'port is taken from x-forwarded-for or host')
44
+ t.assert.ok(req.port, 'port is defined')
45
+ t.assert.strictEqual(req.port, options.port, 'port is taken from x-forwarded-for or host')
45
46
  }
46
47
  }
47
48
 
@@ -50,11 +51,13 @@ before(async function () {
50
51
  [localhost] = await helper.getLoopbackHost()
51
52
  })
52
53
 
53
- test('trust proxy, not add properties to node req', (t) => {
54
+ test('trust proxy, not add properties to node req', (t, done) => {
54
55
  t.plan(14)
55
56
  const app = fastify({
56
57
  trustProxy: true
57
58
  })
59
+ t.after(() => app.close())
60
+
58
61
  app.get('/trustproxy', function (req, reply) {
59
62
  testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })
60
63
  reply.code(200).send({ ip: req.ip, host: req.host })
@@ -65,17 +68,23 @@ test('trust proxy, not add properties to node req', (t) => {
65
68
  reply.code(200).send({ ip: req.ip, host: req.host })
66
69
  })
67
70
 
68
- t.teardown(app.close.bind(app))
69
-
70
71
  app.listen({ port: 0 }, (err) => {
71
72
  app.server.unref()
72
- t.error(err)
73
- sgetForwardedRequest(app, '1.1.1.1', '/trustproxy')
74
- sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxychain')
73
+ t.assert.ifError(err)
74
+
75
+ sgetForwardedRequest(app, '1.1.1.1', '/trustproxy', undefined, completed)
76
+ sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxychain', undefined, completed)
77
+
78
+ let pending = 2
79
+ function completed () {
80
+ if (--pending === 0) {
81
+ done()
82
+ }
83
+ }
75
84
  })
76
85
  })
77
86
 
78
- test('trust proxy chain', (t) => {
87
+ test('trust proxy chain', (t, done) => {
79
88
  t.plan(9)
80
89
  const app = fastify({
81
90
  trustProxy: [localhost, '192.168.1.1']
@@ -86,16 +95,15 @@ test('trust proxy chain', (t) => {
86
95
  reply.code(200).send({ ip: req.ip, host: req.host })
87
96
  })
88
97
 
89
- t.teardown(app.close.bind(app))
90
-
91
98
  app.listen({ port: 0 }, (err) => {
92
99
  app.server.unref()
93
- t.error(err)
94
- sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain')
100
+ t.assert.ifError(err)
101
+ t.after(() => app.close())
102
+ sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined, done)
95
103
  })
96
104
  })
97
105
 
98
- test('trust proxy function', (t) => {
106
+ test('trust proxy function', (t, done) => {
99
107
  t.plan(9)
100
108
  const app = fastify({
101
109
  trustProxy: (address) => address === localhost
@@ -105,16 +113,15 @@ test('trust proxy function', (t) => {
105
113
  reply.code(200).send({ ip: req.ip, host: req.host })
106
114
  })
107
115
 
108
- t.teardown(app.close.bind(app))
109
-
110
116
  app.listen({ port: 0 }, (err) => {
111
117
  app.server.unref()
112
- t.error(err)
113
- sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc')
118
+ t.assert.ifError(err)
119
+ t.after(() => app.close())
120
+ sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc', undefined, done)
114
121
  })
115
122
  })
116
123
 
117
- test('trust proxy number', (t) => {
124
+ test('trust proxy number', (t, done) => {
118
125
  t.plan(10)
119
126
  const app = fastify({
120
127
  trustProxy: 1
@@ -124,16 +131,15 @@ test('trust proxy number', (t) => {
124
131
  reply.code(200).send({ ip: req.ip, host: req.host })
125
132
  })
126
133
 
127
- t.teardown(app.close.bind(app))
128
-
129
134
  app.listen({ port: 0 }, (err) => {
130
135
  app.server.unref()
131
- t.error(err)
132
- sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber')
136
+ t.assert.ifError(err)
137
+ t.after(() => app.close())
138
+ sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined, done)
133
139
  })
134
140
  })
135
141
 
136
- test('trust proxy IP addresses', (t) => {
142
+ test('trust proxy IP addresses', (t, done) => {
137
143
  t.plan(10)
138
144
  const app = fastify({
139
145
  trustProxy: `${localhost}, 2.2.2.2`
@@ -143,16 +149,15 @@ test('trust proxy IP addresses', (t) => {
143
149
  reply.code(200).send({ ip: req.ip, host: req.host })
144
150
  })
145
151
 
146
- t.teardown(app.close.bind(app))
147
-
148
152
  app.listen({ port: 0 }, (err) => {
149
153
  app.server.unref()
150
- t.error(err)
151
- sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs')
154
+ t.assert.ifError(err)
155
+ t.after(() => app.close())
156
+ sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined, done)
152
157
  })
153
158
  })
154
159
 
155
- test('trust proxy protocol', (t) => {
160
+ test('trust proxy protocol', (t, done) => {
156
161
  t.plan(31)
157
162
  const app = fastify({
158
163
  trustProxy: true
@@ -170,13 +175,14 @@ test('trust proxy protocol', (t) => {
170
175
  reply.code(200).send({ ip: req.ip, host: req.host })
171
176
  })
172
177
 
173
- t.teardown(app.close.bind(app))
178
+ t.after(() => app.close())
174
179
 
175
180
  app.listen({ port: 0 }, (err) => {
176
181
  app.server.unref()
177
- t.error(err)
182
+ t.assert.ifError(err)
178
183
  sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocol', 'lorem')
179
184
  sgetForwardedRequest(app, '1.1.1.1', '/trustproxynoprotocol')
180
- sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor')
185
+ // Allow for sgetForwardedRequest requests above to finish
186
+ setTimeout(() => sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor', done))
181
187
  })
182
188
  })
@@ -1,20 +1,22 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('..')
5
5
 
6
- test('Should export withTypeProvider function', t => {
6
+ test('Should export withTypeProvider function', (t, done) => {
7
7
  t.plan(1)
8
8
  try {
9
9
  Fastify().withTypeProvider()
10
- t.pass()
10
+ t.assert.ok('pass')
11
+ done()
11
12
  } catch (e) {
12
- t.fail()
13
+ t.assert.fail(e)
13
14
  }
14
15
  })
15
16
 
16
- test('Should return same instance', t => {
17
+ test('Should return same instance', (t, done) => {
17
18
  t.plan(1)
18
19
  const fastify = Fastify()
19
- t.equal(fastify, fastify.withTypeProvider())
20
+ t.assert.strictEqual(fastify, fastify.withTypeProvider())
21
+ done()
20
22
  })
@@ -1,6 +1,6 @@
1
1
  import fastify, { FastifyBodyParser } from '../../fastify'
2
2
  import { expectError, expectType } from 'tsd'
3
- import { IncomingMessage } from 'http'
3
+ import { IncomingMessage } from 'node:http'
4
4
  import { FastifyRequest } from '../../types/request'
5
5
 
6
6
  expectType<void>(fastify().addContentTypeParser('contentType', function (request, payload, done) {
@@ -1,8 +1,8 @@
1
1
  import { ErrorObject as AjvErrorObject } from 'ajv'
2
- import * as http from 'http'
3
- import * as http2 from 'http2'
4
- import * as https from 'https'
5
- import { Socket } from 'net'
2
+ import * as http from 'node:http'
3
+ import * as http2 from 'node:http2'
4
+ import * as https from 'node:https'
5
+ import { Socket } from 'node:net'
6
6
  import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'
7
7
  import fastify, {
8
8
  ConnectionError,
@@ -21,6 +21,7 @@ import fastify, {
21
21
  RouteGenericInterface,
22
22
  SafePromiseLike
23
23
  } from '../../fastify'
24
+ import { Bindings, ChildLoggerOptions } from '../../types/logger'
24
25
 
25
26
  // FastifyInstance
26
27
  // http server
@@ -219,6 +220,17 @@ expectAssignable<FastifyInstance>(fastify({
219
220
  }
220
221
  }))
221
222
 
223
+ expectAssignable<FastifyInstance>(fastify({
224
+ childLoggerFactory: function (this: FastifyInstance, logger: FastifyBaseLogger, bindings: Bindings, opts: ChildLoggerOptions, req: RawRequestDefaultExpression) {
225
+ expectType<FastifyBaseLogger>(logger)
226
+ expectType<Bindings>(bindings)
227
+ expectType<ChildLoggerOptions>(opts)
228
+ expectType<RawRequestDefaultExpression>(req)
229
+ expectAssignable<FastifyInstance>(this)
230
+ return logger.child(bindings, opts)
231
+ }
232
+ }))
233
+
222
234
  // Thenable
223
235
  expectAssignable<PromiseLike<FastifyInstance>>(fastify({ return503OnClosing: true }))
224
236
  fastify().then(fastifyInstance => expectAssignable<FastifyInstance>(fastifyInstance))
@@ -131,7 +131,8 @@ server.addHook('onRoute', function (opts) {
131
131
  expectType<RouteOptions & { routePath: string; path: string; prefix: string }>(opts)
132
132
  })
133
133
 
134
- server.addHook('onRegister', (instance, opts) => {
134
+ server.addHook('onRegister', function (instance, opts) {
135
+ expectType<FastifyInstance>(this)
135
136
  expectType<FastifyInstance>(instance)
136
137
  expectType<RegisterOptions & FastifyPluginOptions>(opts)
137
138
  })
@@ -13,7 +13,7 @@ import { HookHandlerDoneFunction } from '../../types/hooks'
13
13
  import { FastifyReply } from '../../types/reply'
14
14
  import { FastifyRequest } from '../../types/request'
15
15
  import { FastifySchemaControllerOptions, FastifySchemaCompiler, FastifySerializerCompiler } from '../../types/schema'
16
- import { AddressInfo } from 'net'
16
+ import { AddressInfo } from 'node:net'
17
17
  import { Bindings, ChildLoggerOptions } from '../../types/logger'
18
18
 
19
19
  const server = fastify()
@@ -193,6 +193,8 @@ function invalidSchemaErrorFormatter (err: Error) {
193
193
  }
194
194
  expectError(server.setSchemaErrorFormatter(invalidSchemaErrorFormatter))
195
195
 
196
+ expectType<FastifyInstance>(server.addHttpMethod('SEARCH', { hasBody: true }))
197
+
196
198
  // test listen opts objects
197
199
  expectAssignable<PromiseLike<string>>(server.listen())
198
200
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000 }))
@@ -370,12 +372,11 @@ expectError(server.decorate<string>('test', true))
370
372
  expectError(server.decorate<(myNumber: number) => number>('test', function (myNumber: number): string {
371
373
  return ''
372
374
  }))
373
- // TODO(mcollina): uncomment after https://github.com/tsdjs/tsd/pull/220 lands.
374
- // expectError(server.decorate<string>('test', {
375
- // getter () {
376
- // return true
377
- // }
378
- // }))
375
+ expectError(server.decorate<string>('test', {
376
+ getter () {
377
+ return true
378
+ }
379
+ }))
379
380
  expectError(server.decorate<string>('test', {
380
381
  setter (x) {}
381
382
  }))
@@ -419,12 +420,11 @@ server.decorate('typedTestProperty')
419
420
  server.decorate('typedTestProperty', null, ['foo'])
420
421
  expectError(server.decorate('typedTestProperty', null))
421
422
  expectError(server.decorate('typedTestProperty', 'foo'))
422
- // TODO(mcollina): uncomment after https://github.com/tsdjs/tsd/pull/220 lands.
423
- // expectError(server.decorate('typedTestProperty', {
424
- // getter () {
425
- // return 'foo'
426
- // }
427
- // }))
423
+ expectError(server.decorate('typedTestProperty', {
424
+ getter () {
425
+ return 'foo'
426
+ }
427
+ }))
428
428
  server.decorate('typedTestMethod', function (x) {
429
429
  expectType<string>(x)
430
430
  expectType<FastifyInstance>(this)
@@ -7,8 +7,8 @@ import fastify, {
7
7
  FastifyReply,
8
8
  FastifyBaseLogger
9
9
  } from '../../fastify'
10
- import { Server, IncomingMessage, ServerResponse } from 'http'
11
- import * as fs from 'fs'
10
+ import { Server, IncomingMessage, ServerResponse } from 'node:http'
11
+ import * as fs from 'node:fs'
12
12
  import P from 'pino'
13
13
  import { ResSerializerReply } from '../../types/logger'
14
14
 
@@ -1,6 +1,6 @@
1
1
  import fastify, { FastifyInstance, FastifyPluginOptions, SafePromiseLike } from '../../fastify'
2
- import * as http from 'http'
3
- import * as https from 'https'
2
+ import * as http from 'node:http'
3
+ import * as https from 'node:https'
4
4
  import { expectType, expectError, expectAssignable } from 'tsd'
5
5
  import { FastifyPluginCallback, FastifyPluginAsync } from '../../types/plugin'
6
6
  import { FastifyError } from '@fastify/error'
@@ -10,8 +10,16 @@ interface TestOptions extends FastifyPluginOptions {
10
10
  option1: string;
11
11
  option2: boolean;
12
12
  }
13
- const testPluginOpts: FastifyPluginCallback<TestOptions> = function (instance, opts, done) { }
14
- const testPluginOptsAsync: FastifyPluginAsync<TestOptions> = async function (instance, opts) { }
13
+ const testOptions: TestOptions = {
14
+ option1: 'a',
15
+ option2: false,
16
+ }
17
+ const testPluginOpts: FastifyPluginCallback<TestOptions> = function (instance, opts, done) {
18
+ expectType<TestOptions>(opts)
19
+ }
20
+ const testPluginOptsAsync: FastifyPluginAsync<TestOptions> = async function (instance, opts) {
21
+ expectType<TestOptions>(opts)
22
+ }
15
23
 
16
24
  const testPluginOptsWithType = (instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
17
25
  const testPluginOptsWithTypeAsync = async (instance: FastifyInstance, opts: FastifyPluginOptions) => { }
@@ -39,7 +47,7 @@ expectAssignable<FastifyInstance>(fastify().register(function (instance, opts):
39
47
  expectAssignable<FastifyInstance>(fastify().register(async function (instance, opts) { }, () => { }))
40
48
  expectAssignable<FastifyInstance>(fastify().register(async function (instance, opts) { }, { logLevel: 'info', prefix: 'foobar' }))
41
49
 
42
- expectError(fastify().register(function (instance, opts, done) { }, { logLevel: '' })) // must use a valid logLevel
50
+ expectError(fastify().register(function (instance, opts, done) { }, { ...testOptions, logLevel: '' })) // must use a valid logLevel
43
51
 
44
52
  const httpsServer = fastify({ https: {} })
45
53
  expectError<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & Promise<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(httpsServer)
@@ -48,7 +56,7 @@ expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerRespon
48
56
 
49
57
  // Chainable
50
58
  httpsServer
51
- .register(testPluginOpts)
59
+ .register(testPluginOpts, testOptions)
52
60
  .after((_error) => { })
53
61
  .ready((_error) => { })
54
62
  .close(() => { })
@@ -57,7 +65,7 @@ httpsServer
57
65
  expectAssignable<PromiseLike<undefined>>(httpsServer.after())
58
66
  expectAssignable<PromiseLike<undefined>>(httpsServer.close())
59
67
  expectAssignable<PromiseLike<undefined>>(httpsServer.ready())
60
- expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOpts))
68
+ expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOpts, testOptions))
61
69
  expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWithType))
62
70
  expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWithTypeAsync))
63
71
  expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWithType, { prefix: '/test' }))
@@ -66,6 +74,6 @@ expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWith
66
74
  /* eslint-disable @typescript-eslint/no-unused-vars */
67
75
  async function testAsync (): Promise<void> {
68
76
  await httpsServer
69
- .register(testPluginOpts)
70
- .register(testPluginOpts)
77
+ .register(testPluginOpts, testOptions)
78
+ .register(testPluginOpts, testOptions)
71
79
  }
@@ -1,6 +1,6 @@
1
1
  import { expectAssignable, expectError, expectType } from 'tsd'
2
- import { IncomingMessage, Server, ServerResponse } from 'http'
3
- import { Http2Server, Http2ServerRequest, Http2ServerResponse } from 'http2'
2
+ import { IncomingMessage, Server, ServerResponse } from 'node:http'
3
+ import { Http2Server, Http2ServerRequest, Http2ServerResponse } from 'node:http2'
4
4
  import fastify, { FastifyInstance, FastifyError, FastifyLoggerInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, RawServerDefault } from '../../fastify'
5
5
 
6
6
  const testPluginCallback: FastifyPluginCallback = function (instance, opts, done) { }
@@ -48,14 +48,22 @@ const testPluginWithHttp2: FastifyPluginCallback<TestOptions, Http2Server> = fun
48
48
  const testPluginWithHttp2Async: FastifyPluginAsync<TestOptions, Http2Server> = async function (instance, opts) { }
49
49
  const testPluginWithHttp2WithType = (instance: ServerWithHttp2, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
50
50
  const testPluginWithHttp2WithTypeAsync = async (instance: ServerWithHttp2, opts: FastifyPluginOptions) => { }
51
+ const testOptions: TestOptions = {
52
+ option1: 'a',
53
+ option2: false,
54
+ }
51
55
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginCallback))
52
56
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginAsync))
53
57
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOpts))
54
58
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOptsAsync))
55
59
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOptsWithType))
56
60
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOptsWithTypeAsync))
57
- expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2))
58
- expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2Async))
61
+ // @ts-expect-error
62
+ serverWithHttp2.register(testPluginWithHttp2)
63
+ expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2, testOptions))
64
+ // @ts-expect-error
65
+ serverWithHttp2.register(testPluginWithHttp2Async)
66
+ expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2Async, testOptions))
59
67
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2WithType))
60
68
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2WithTypeAsync))
61
69
  expectAssignable<ServerWithHttp2>(serverWithHttp2.register((instance) => {
@@ -85,8 +93,12 @@ expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPlu
85
93
  expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsAsync))
86
94
  expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsWithType))
87
95
  expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsWithTypeAsync))
88
- expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProvider))
89
- expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderAsync))
96
+ // @ts-expect-error
97
+ serverWithTypeProvider.register(testPluginWithTypeProvider)
98
+ expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProvider, testOptions))
99
+ // @ts-expect-error
100
+ serverWithTypeProvider.register(testPluginWithTypeProviderAsync)
101
+ expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderAsync, testOptions))
90
102
  expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderWithType))
91
103
  expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderWithTypeAsync))
92
104
  expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register((instance) => {
@@ -128,8 +140,12 @@ expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogge
128
140
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsAsync))
129
141
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsWithType))
130
142
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsWithTypeAsync))
143
+ // @ts-expect-error
131
144
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger))
145
+ expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger, testOptions))
146
+ // @ts-expect-error
132
147
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync))
148
+ expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync, testOptions))
133
149
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithType))
134
150
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithTypeAsync))
135
151
  expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register((instance) => {
@@ -1,4 +1,4 @@
1
- import { Buffer } from 'buffer'
1
+ import { Buffer } from 'node:buffer'
2
2
  import { expectAssignable, expectError, expectType } from 'tsd'
3
3
  import fastify, { FastifyContextConfig, FastifyReply, FastifyRequest, FastifySchema, FastifyTypeProviderDefault, RawRequestDefaultExpression, RouteHandler, RouteHandlerMethod } from '../../fastify'
4
4
  import { FastifyInstance } from '../../types/instance'
@@ -1,5 +1,5 @@
1
1
  import { FastifyError } from '@fastify/error'
2
- import * as http from 'http'
2
+ import * as http from 'node:http'
3
3
  import { expectAssignable, expectError, expectType } from 'tsd'
4
4
  import fastify, { FastifyInstance, FastifyReply, FastifyRequest, RouteHandlerMethod } from '../../fastify'
5
5
  import { RequestPayload } from '../../types/hooks'
@@ -398,9 +398,21 @@ type LowerCaseHTTPMethods = 'delete' | 'get' | 'head' | 'patch' | 'post' | 'put'
398
398
  })
399
399
  })
400
400
 
401
- expectError(fastify().route({
401
+ expectType<FastifyInstance>(fastify().route({
402
+ url: '/',
403
+ method: 'CONNECT', // not a valid method but could be implemented by the user
404
+ handler: routeHandler
405
+ }))
406
+
407
+ expectType<FastifyInstance>(fastify().route({
402
408
  url: '/',
403
- method: 'CONNECT', // not a valid method
409
+ method: 'OPTIONS',
410
+ handler: routeHandler
411
+ }))
412
+
413
+ expectType<FastifyInstance>(fastify().route({
414
+ url: '/',
415
+ method: 'OPTION', // OPTION is a typo for OPTIONS
404
416
  handler: routeHandler
405
417
  }))
406
418
 
@@ -410,7 +422,7 @@ expectType<FastifyInstance>(fastify().route({
410
422
  handler: routeHandler
411
423
  }))
412
424
 
413
- expectError(fastify().route({
425
+ expectType<FastifyInstance>(fastify().route({
414
426
  url: '/',
415
427
  method: ['GET', 'POST', 'OPTION'], // OPTION is a typo for OPTIONS
416
428
  handler: routeHandler
@@ -506,3 +518,21 @@ expectType<FastifyInstance>(fastify().route({
506
518
  method: ['put', 'patch'],
507
519
  handler: routeHandlerWithReturnValue
508
520
  }))
521
+
522
+ expectType<FastifyInstance>(fastify().route({
523
+ url: '/',
524
+ method: 'GET',
525
+ handler: (req) => {
526
+ expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.method)
527
+ expectAssignable<string | Array<string>>(req.routeOptions.method)
528
+ }
529
+ }))
530
+
531
+ expectType<FastifyInstance>(fastify().route({
532
+ url: '/',
533
+ method: ['HEAD', 'GET'],
534
+ handler: (req) => {
535
+ expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.method)
536
+ expectAssignable<string | Array<string>>(req.routeOptions.method)
537
+ }
538
+ }))
@@ -1,5 +1,5 @@
1
1
  import fastify, { FastifyServerFactory } from '../../fastify'
2
- import * as http from 'http'
2
+ import * as http from 'node:http'
3
3
  import { expectType } from 'tsd'
4
4
 
5
5
  // Custom Server
@@ -8,7 +8,7 @@ import fastify, {
8
8
  SafePromiseLike
9
9
  } from '../../fastify'
10
10
  import { expectAssignable, expectError, expectType } from 'tsd'
11
- import { IncomingHttpHeaders } from 'http'
11
+ import { IncomingHttpHeaders } from 'node:http'
12
12
  import { Type, TSchema, Static } from '@sinclair/typebox'
13
13
  import { FromSchema, JSONSchema } from 'json-schema-to-ts'
14
14