fastify 5.0.0 → 5.1.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 (138) hide show
  1. package/.borp.yaml +3 -0
  2. package/.vscode/settings.json +22 -0
  3. package/docs/Guides/Ecosystem.md +1 -8
  4. package/docs/Guides/Migration-Guide-V5.md +128 -4
  5. package/docs/Guides/Testing.md +51 -50
  6. package/docs/Guides/Write-Plugin.md +1 -1
  7. package/docs/Reference/ContentTypeParser.md +4 -4
  8. package/docs/Reference/Errors.md +1 -1
  9. package/docs/Reference/Reply.md +1 -5
  10. package/docs/Reference/Request.md +2 -1
  11. package/docs/Reference/Routes.md +9 -6
  12. package/docs/Reference/Server.md +1 -1
  13. package/fastify.d.ts +10 -1
  14. package/fastify.js +8 -6
  15. package/lib/contentTypeParser.js +9 -7
  16. package/lib/context.js +1 -2
  17. package/lib/fourOhFour.js +1 -1
  18. package/lib/{logger.js → logger-factory.js} +70 -122
  19. package/lib/logger-pino.js +68 -0
  20. package/lib/pluginOverride.js +1 -1
  21. package/lib/pluginUtils.js +2 -2
  22. package/lib/reply.js +4 -5
  23. package/lib/request.js +15 -9
  24. package/lib/route.js +23 -22
  25. package/lib/validation.js +2 -2
  26. package/package.json +12 -13
  27. package/test/404s.test.js +675 -629
  28. package/test/500s.test.js +72 -63
  29. package/test/allowUnsafeRegex.test.js +30 -26
  30. package/test/als.test.js +48 -45
  31. package/test/async-await.test.js +148 -134
  32. package/test/async-dispose.test.js +4 -4
  33. package/test/async_hooks.test.js +30 -28
  34. package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
  35. package/test/buffer.test.js +9 -10
  36. package/test/build/error-serializer.test.js +3 -4
  37. package/test/build/version.test.js +2 -3
  38. package/test/bundler/esbuild/bundler-test.js +10 -9
  39. package/test/bundler/webpack/bundler-test.js +10 -9
  40. package/test/case-insensitive.test.js +31 -28
  41. package/test/chainable.test.js +4 -5
  42. package/test/check.test.js +8 -9
  43. package/test/childLoggerFactory.test.js +56 -19
  44. package/test/client-timeout.test.js +5 -5
  45. package/test/close-pipelining.test.js +6 -7
  46. package/test/conditional-pino.test.js +47 -0
  47. package/test/connectionTimeout.test.js +10 -11
  48. package/test/constrained-routes.test.js +243 -236
  49. package/test/content-parser.test.js +17 -0
  50. package/test/custom-http-server.test.js +16 -20
  51. package/test/diagnostics-channel/404.test.js +15 -15
  52. package/test/diagnostics-channel/async-delay-request.test.js +25 -25
  53. package/test/diagnostics-channel/async-request.test.js +24 -24
  54. package/test/diagnostics-channel/error-before-handler.test.js +4 -5
  55. package/test/diagnostics-channel/error-request.test.js +19 -19
  56. package/test/diagnostics-channel/error-status.test.js +8 -8
  57. package/test/diagnostics-channel/init.test.js +6 -7
  58. package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
  59. package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
  60. package/test/diagnostics-channel/sync-request.test.js +19 -19
  61. package/test/esm/errorCodes.test.mjs +5 -5
  62. package/test/esm/esm.test.mjs +3 -3
  63. package/test/esm/named-exports.mjs +3 -3
  64. package/test/esm/other.mjs +2 -2
  65. package/test/hooks.test.js +6 -6
  66. package/test/http-methods/copy.test.js +22 -24
  67. package/test/http-methods/custom-http-methods.test.js +24 -21
  68. package/test/http-methods/get.test.js +97 -84
  69. package/test/http-methods/head.test.js +63 -54
  70. package/test/http-methods/lock.test.js +21 -20
  71. package/test/http-methods/mkcalendar.test.js +31 -27
  72. package/test/http-methods/mkcol.test.js +10 -10
  73. package/test/http-methods/move.test.js +11 -11
  74. package/test/http-methods/propfind.test.js +32 -27
  75. package/test/http-methods/proppatch.test.js +21 -19
  76. package/test/http-methods/report.test.js +32 -27
  77. package/test/http-methods/search.test.js +52 -47
  78. package/test/http-methods/trace.test.js +3 -4
  79. package/test/http-methods/unlock.test.js +10 -10
  80. package/test/http2/closing.test.js +50 -58
  81. package/test/http2/constraint.test.js +47 -50
  82. package/test/http2/head.test.js +18 -19
  83. package/test/http2/missing-http2-module.test.js +4 -5
  84. package/test/http2/plain.test.js +31 -31
  85. package/test/http2/secure-with-fallback.test.js +61 -61
  86. package/test/http2/secure.test.js +28 -31
  87. package/test/http2/unknown-http-method.test.js +13 -14
  88. package/test/https/custom-https-server.test.js +6 -7
  89. package/test/https/https.test.js +78 -78
  90. package/test/imports.test.js +5 -6
  91. package/test/internals/all.test.js +8 -11
  92. package/test/internals/contentTypeParser.test.js +5 -6
  93. package/test/internals/context.test.js +9 -11
  94. package/test/internals/decorator.test.js +20 -21
  95. package/test/internals/errors.test.js +427 -427
  96. package/test/internals/handleRequest.test.js +53 -42
  97. package/test/internals/hookRunner.test.js +99 -100
  98. package/test/internals/hooks.test.js +31 -35
  99. package/test/internals/initialConfig.test.js +92 -80
  100. package/test/internals/logger.test.js +28 -28
  101. package/test/internals/plugin.test.js +17 -18
  102. package/test/internals/reply-serialize.test.js +106 -106
  103. package/test/internals/reply.test.js +620 -585
  104. package/test/internals/reqIdGenFactory.test.js +31 -31
  105. package/test/internals/request-validate.test.js +218 -221
  106. package/test/internals/request.test.js +225 -107
  107. package/test/internals/server.test.js +15 -12
  108. package/test/internals/validation.test.js +35 -36
  109. package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
  110. package/test/noop-set.test.js +5 -5
  111. package/test/request-error.test.js +122 -0
  112. package/test/request-header-host.test.js +197 -0
  113. package/test/route.1.test.js +79 -72
  114. package/test/route.2.test.js +17 -16
  115. package/test/route.3.test.js +30 -26
  116. package/test/route.4.test.js +21 -25
  117. package/test/route.5.test.js +45 -64
  118. package/test/route.6.test.js +70 -89
  119. package/test/route.7.test.js +61 -65
  120. package/test/route.8.test.js +62 -0
  121. package/test/same-shape.test.js +5 -5
  122. package/test/serialize-response.test.js +9 -10
  123. package/test/server.test.js +67 -70
  124. package/test/test-reporter.mjs +68 -0
  125. package/test/types/fastify.test-d.ts +12 -0
  126. package/test/types/hooks.test-d.ts +2 -1
  127. package/test/types/instance.test-d.ts +10 -12
  128. package/test/types/plugin.test-d.ts +15 -7
  129. package/test/types/register.test-d.ts +20 -4
  130. package/test/types/route.test-d.ts +33 -3
  131. package/test/versioned-routes.test.js +126 -113
  132. package/test/web-api.test.js +48 -37
  133. package/test/wrapThenable.test.js +10 -9
  134. package/types/hooks.d.ts +1 -0
  135. package/types/register.d.ts +12 -3
  136. package/types/request.d.ts +2 -2
  137. package/types/utils.d.ts +10 -2
  138. package/test/types/import.js +0 -2
@@ -1,18 +1,17 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
 
6
5
  test('should import as default', t => {
7
6
  t.plan(2)
8
7
  const fastify = require('..')
9
- t.ok(fastify)
10
- t.equal(typeof fastify, 'function')
8
+ t.assert.ok(fastify)
9
+ t.assert.equal(typeof fastify, 'function')
11
10
  })
12
11
 
13
12
  test('should import as esm', t => {
14
13
  t.plan(2)
15
14
  const { fastify } = require('..')
16
- t.ok(fastify)
17
- t.equal(typeof fastify, 'function')
15
+ t.assert.ok(fastify)
16
+ t.assert.equal(typeof fastify, 'function')
18
17
  })
@@ -1,10 +1,9 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../..')
6
5
 
7
- test('fastify.all should add all the methods to the same url', t => {
6
+ test('fastify.all should add all the methods to the same url', async t => {
8
7
  const fastify = Fastify()
9
8
 
10
9
  const requirePayload = [
@@ -14,15 +13,15 @@ test('fastify.all should add all the methods to the same url', t => {
14
13
  ]
15
14
 
16
15
  const supportedMethods = fastify.supportedMethods
17
- t.plan(supportedMethods.length * 2)
16
+ t.plan(supportedMethods.length)
18
17
 
19
18
  fastify.all('/', (req, reply) => {
20
19
  reply.send({ method: req.raw.method })
21
20
  })
22
21
 
23
- supportedMethods.forEach(injectRequest)
22
+ await Promise.all(supportedMethods.map(async method => injectRequest(method)))
24
23
 
25
- function injectRequest (method) {
24
+ async function injectRequest (method) {
26
25
  const options = {
27
26
  url: '/',
28
27
  method
@@ -32,10 +31,8 @@ test('fastify.all should add all the methods to the same url', t => {
32
31
  options.payload = { hello: 'world' }
33
32
  }
34
33
 
35
- fastify.inject(options, (err, res) => {
36
- t.error(err)
37
- const payload = JSON.parse(res.payload)
38
- t.same(payload, { method })
39
- })
34
+ const res = await fastify.inject(options)
35
+ const payload = JSON.parse(res.payload)
36
+ t.assert.deepStrictEqual(payload, { method })
40
37
  }
41
38
  })
@@ -1,8 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
3
+ const { test } = require('node:test')
4
4
  const proxyquire = require('proxyquire')
5
- const test = t.test
6
5
  const { Readable } = require('node:stream')
7
6
  const { kTestInternals, kRouteContext } = require('../../lib/symbols')
8
7
  const Request = require('../../lib/request')
@@ -17,8 +16,8 @@ test('rawBody function', t => {
17
16
  asString: true,
18
17
  asBuffer: false,
19
18
  fn (req, bodyInString, done) {
20
- t.equal(bodyInString, body.toString())
21
- t.equal(typeof done, 'function')
19
+ t.assert.strictEqual(bodyInString, body.toString())
20
+ t.assert.strictEqual(typeof done, 'function')
22
21
  return {
23
22
  then (cb) {
24
23
  cb()
@@ -70,8 +69,8 @@ test('Should support Webpack and faux modules', t => {
70
69
  asString: true,
71
70
  asBuffer: false,
72
71
  fn (req, bodyInString, done) {
73
- t.equal(bodyInString, body.toString())
74
- t.equal(typeof done, 'function')
72
+ t.assert.strictEqual(bodyInString, body.toString())
73
+ t.assert.strictEqual(typeof done, 'function')
75
74
  return {
76
75
  then (cb) {
77
76
  cb()
@@ -1,23 +1,23 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
4
-
3
+ const { test } = require('node:test')
5
4
  const { kRouteContext } = require('../../lib/symbols')
6
5
  const Context = require('../../lib/context')
7
6
 
8
7
  const Fastify = require('../..')
9
8
 
10
- test('context', context => {
9
+ test('context', async context => {
11
10
  context.plan(1)
12
11
 
13
- context.test('Should not contain undefined as key prop', async t => {
12
+ await context.test('Should not contain undefined as key prop', async t => {
13
+ t.plan(4)
14
14
  const app = Fastify()
15
15
 
16
16
  app.get('/', (req, reply) => {
17
- t.type(req[kRouteContext], Context)
18
- t.type(reply[kRouteContext], Context)
19
- t.notOk('undefined' in reply[kRouteContext])
20
- t.notOk('undefined' in req[kRouteContext])
17
+ t.assert.ok(req[kRouteContext] instanceof Context)
18
+ t.assert.ok(reply[kRouteContext] instanceof Context)
19
+ t.assert.ok(!('undefined' in reply[kRouteContext]))
20
+ t.assert.ok(!('undefined' in req[kRouteContext]))
21
21
 
22
22
  reply.send('hello world!')
23
23
  })
@@ -25,9 +25,7 @@ test('context', context => {
25
25
  try {
26
26
  await app.inject('/')
27
27
  } catch (e) {
28
- t.fail(e)
28
+ t.assert.fail(e)
29
29
  }
30
-
31
- t.plan(4)
32
30
  })
33
31
  })
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const decorator = require('../../lib/decorate')
6
5
  const {
7
6
  kState
@@ -22,7 +21,7 @@ test('decorate should add the given method to its instance', t => {
22
21
 
23
22
  const server = build()
24
23
  server.add('test', () => {})
25
- t.ok(server.test)
24
+ t.assert.ok(server.test)
26
25
  })
27
26
 
28
27
  test('decorate is chainable', t => {
@@ -44,15 +43,15 @@ test('decorate is chainable', t => {
44
43
  .add('test2', () => {})
45
44
  .add('test3', () => {})
46
45
 
47
- t.ok(server.test1)
48
- t.ok(server.test2)
49
- t.ok(server.test3)
46
+ t.assert.ok(server.test1)
47
+ t.assert.ok(server.test2)
48
+ t.assert.ok(server.test3)
50
49
  })
51
50
 
52
51
  test('checkExistence should check if a property is part of the given instance', t => {
53
52
  t.plan(1)
54
53
  const instance = { test: () => {} }
55
- t.ok(decorator.exist(instance, 'test'))
54
+ t.assert.ok(decorator.exist(instance, 'test'))
56
55
  })
57
56
 
58
57
  test('checkExistence should find the instance if not given', t => {
@@ -71,7 +70,7 @@ test('checkExistence should find the instance if not given', t => {
71
70
 
72
71
  const server = build()
73
72
  server.add('test', () => {})
74
- t.ok(server.check('test'))
73
+ t.assert.ok(server.check('test'))
75
74
  })
76
75
 
77
76
  test('checkExistence should check the prototype as well', t => {
@@ -80,7 +79,7 @@ test('checkExistence should check the prototype as well', t => {
80
79
  Instance.prototype.test = () => {}
81
80
 
82
81
  const instance = new Instance()
83
- t.ok(decorator.exist(instance, 'test'))
82
+ t.assert.ok(decorator.exist(instance, 'test'))
84
83
  })
85
84
 
86
85
  test('checkDependencies should throw if a dependency is not present', t => {
@@ -88,10 +87,10 @@ test('checkDependencies should throw if a dependency is not present', t => {
88
87
  const instance = {}
89
88
  try {
90
89
  decorator.dependencies(instance, 'foo', ['test'])
91
- t.fail()
90
+ t.assert.fail()
92
91
  } catch (e) {
93
- t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
94
- t.equal(e.message, 'The decorator is missing dependency \'test\'.')
92
+ t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
93
+ t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')
95
94
  }
96
95
  })
97
96
 
@@ -112,10 +111,10 @@ test('decorate should internally call checkDependencies', t => {
112
111
 
113
112
  try {
114
113
  server.add('method', () => {}, ['test'])
115
- t.fail()
114
+ t.assert.fail()
116
115
  } catch (e) {
117
- t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
118
- t.equal(e.message, 'The decorator is missing dependency \'test\'.')
116
+ t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
117
+ t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')
119
118
  }
120
119
  })
121
120
 
@@ -132,14 +131,14 @@ test('decorate should recognize getter/setter objects', t => {
132
131
  decorator.add.call(one, 'foo', {
133
132
  getter: () => this._a,
134
133
  setter: (val) => {
135
- t.pass()
134
+ t.assert.ok(true)
136
135
  this._a = val
137
136
  }
138
137
  })
139
- t.equal(Object.hasOwn(one, 'foo'), true)
140
- t.equal(one.foo, undefined)
138
+ t.assert.strictEqual(Object.hasOwn(one, 'foo'), true)
139
+ t.assert.strictEqual(one.foo, undefined)
141
140
  one.foo = 'a'
142
- t.equal(one.foo, 'a')
141
+ t.assert.strictEqual(one.foo, 'a')
143
142
 
144
143
  // getter only
145
144
  const two = {
@@ -152,6 +151,6 @@ test('decorate should recognize getter/setter objects', t => {
152
151
  decorator.add.call(two, 'foo', {
153
152
  getter: () => 'a getter'
154
153
  })
155
- t.equal(Object.hasOwn(two, 'foo'), true)
156
- t.equal(two.foo, 'a getter')
154
+ t.assert.strictEqual(Object.hasOwn(two, 'foo'), true)
155
+ t.assert.strictEqual(two.foo, 'a getter')
157
156
  })