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,15 +1,14 @@
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
  const sget = require('simple-get').concat
7
6
 
8
- test('Should rewrite url', t => {
7
+ test('Should rewrite url', (t, done) => {
9
8
  t.plan(5)
10
9
  const fastify = Fastify({
11
10
  rewriteUrl (req) {
12
- t.equal(req.url, '/this-would-404-without-url-rewrite')
11
+ t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
13
12
  this.log.info('rewriting url')
14
13
  return '/'
15
14
  }
@@ -24,26 +23,26 @@ test('Should rewrite url', t => {
24
23
  })
25
24
 
26
25
  fastify.listen({ port: 0 }, function (err) {
27
- t.error(err)
26
+ t.assert.ifError(err)
28
27
 
28
+ t.after(() => fastify.close())
29
29
  sget({
30
30
  method: 'GET',
31
31
  url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
32
32
  }, (err, response, body) => {
33
- t.error(err)
34
- t.same(JSON.parse(body), { hello: 'world' })
35
- t.equal(response.statusCode, 200)
33
+ t.assert.ifError(err)
34
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
35
+ t.assert.strictEqual(response.statusCode, 200)
36
+ done()
36
37
  })
37
38
  })
38
-
39
- t.teardown(() => fastify.close())
40
39
  })
41
40
 
42
- test('Should not rewrite if the url is the same', t => {
41
+ test('Should not rewrite if the url is the same', (t, done) => {
43
42
  t.plan(4)
44
43
  const fastify = Fastify({
45
44
  rewriteUrl (req) {
46
- t.equal(req.url, '/this-would-404-without-url-rewrite')
45
+ t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
47
46
  this.log.info('rewriting url')
48
47
  return req.url
49
48
  }
@@ -58,24 +57,24 @@ test('Should not rewrite if the url is the same', t => {
58
57
  })
59
58
 
60
59
  fastify.listen({ port: 0 }, function (err) {
61
- t.error(err)
62
-
60
+ t.assert.ifError(err)
61
+ t.after(() => fastify.close())
63
62
  sget({
64
63
  method: 'GET',
65
64
  url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
66
65
  }, (err, response, body) => {
67
- t.error(err)
68
- t.equal(response.statusCode, 404)
66
+ t.assert.ifError(err)
67
+ t.assert.strictEqual(response.statusCode, 404)
68
+ done()
69
69
  })
70
70
  })
71
-
72
- t.teardown(() => fastify.close())
73
71
  })
74
- test('Should throw an error', t => {
72
+
73
+ test('Should throw an error', (t, done) => {
75
74
  t.plan(5)
76
75
  const fastify = Fastify({
77
76
  rewriteUrl (req) {
78
- t.equal(req.url, '/this-would-404-without-url-rewrite')
77
+ t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
79
78
  this.log.info('rewriting url')
80
79
  return undefined
81
80
  }
@@ -90,27 +89,26 @@ test('Should throw an error', t => {
90
89
  })
91
90
 
92
91
  fastify.listen({ port: 0 }, function (err) {
93
- t.error(err)
94
-
92
+ t.assert.ifError(err)
93
+ t.after(() => fastify.close())
95
94
  sget({
96
95
  method: 'GET',
97
96
  url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
98
97
  }, (err, response, body) => {
99
- t.equal(err.code, 'ECONNRESET')
100
- t.equal(response, undefined)
101
- t.equal(body, undefined)
98
+ t.assert.strictEqual(err.code, 'ECONNRESET')
99
+ t.assert.strictEqual(response, undefined)
100
+ t.assert.strictEqual(body, undefined)
101
+ done()
102
102
  })
103
103
  })
104
-
105
- t.teardown(() => fastify.close())
106
104
  })
107
105
 
108
- test('Should rewrite url but keep originalUrl unchanged', t => {
106
+ test('Should rewrite url but keep originalUrl unchanged', (t, done) => {
109
107
  t.plan(7)
110
108
  const fastify = Fastify({
111
109
  rewriteUrl (req) {
112
- t.equal(req.url, '/this-would-404-without-url-rewrite')
113
- t.equal(req.originalUrl, '/this-would-404-without-url-rewrite')
110
+ t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
111
+ t.assert.strictEqual(req.originalUrl, '/this-would-404-without-url-rewrite')
114
112
  return '/'
115
113
  }
116
114
  })
@@ -120,23 +118,22 @@ test('Should rewrite url but keep originalUrl unchanged', t => {
120
118
  url: '/',
121
119
  handler: (req, reply) => {
122
120
  reply.send({ hello: 'world', hostname: req.hostname, port: req.port })
123
- t.equal(req.originalUrl, '/this-would-404-without-url-rewrite')
121
+ t.assert.strictEqual(req.originalUrl, '/this-would-404-without-url-rewrite')
124
122
  }
125
123
  })
126
124
 
127
125
  fastify.listen({ port: 0 }, function (err) {
128
- t.error(err)
129
-
126
+ t.assert.ifError(err)
127
+ t.after(() => fastify.close())
130
128
  sget({
131
129
  method: 'GET',
132
130
  url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
133
131
  }, (err, response, body) => {
134
- t.error(err)
132
+ t.assert.ifError(err)
135
133
  const parsedBody = JSON.parse(body)
136
- t.same(parsedBody, { hello: 'world', hostname: 'localhost', port: fastify.server.address().port })
137
- t.equal(response.statusCode, 200)
134
+ t.assert.deepStrictEqual(parsedBody, { hello: 'world', hostname: 'localhost', port: fastify.server.address().port })
135
+ t.assert.strictEqual(response.statusCode, 200)
136
+ done()
138
137
  })
139
138
  })
140
-
141
- t.teardown(() => fastify.close())
142
139
  })
@@ -1,110 +1,110 @@
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
  const sget = require('simple-get').concat
7
6
 
8
- test('use semicolon delimiter default false', t => {
7
+ test('use semicolon delimiter default false', (t, done) => {
9
8
  t.plan(4)
10
9
 
11
- const fastify = Fastify({})
12
-
13
- t.teardown(fastify.close.bind(fastify))
10
+ const fastify = Fastify()
14
11
 
15
12
  fastify.get('/1234;foo=bar', (req, reply) => {
16
13
  reply.send(req.query)
17
14
  })
18
15
 
19
- fastify.listen({ port: 0 }, err => {
20
- t.error(err)
21
-
16
+ fastify.listen({ port: 0, }, err => {
17
+ t.assert.ifError(err)
22
18
  sget({
23
19
  method: 'GET',
24
20
  url: 'http://localhost:' + fastify.server.address().port + '/1234;foo=bar'
25
21
  }, (err, response, body) => {
26
- t.error(err)
27
- t.equal(response.statusCode, 200)
28
- t.same(JSON.parse(body), {})
22
+ t.assert.ifError(err)
23
+ t.assert.strictEqual(response.statusCode, 200)
24
+ t.assert.deepStrictEqual(JSON.parse(body), {})
25
+ fastify.close()
26
+ done()
29
27
  })
30
28
  })
31
29
  })
32
30
 
33
- test('use semicolon delimiter set to true', t => {
31
+ test('use semicolon delimiter set to true', (t, done) => {
34
32
  t.plan(4)
35
-
36
33
  const fastify = Fastify({
37
34
  useSemicolonDelimiter: true
38
35
  })
39
- t.teardown(fastify.close.bind(fastify))
40
36
 
41
37
  fastify.get('/1234', (req, reply) => {
42
38
  reply.send(req.query)
43
39
  })
44
40
 
45
41
  fastify.listen({ port: 0 }, err => {
46
- t.error(err)
42
+ t.assert.ifError(err)
47
43
 
48
44
  sget({
49
45
  method: 'GET',
50
46
  url: 'http://localhost:' + fastify.server.address().port + '/1234;foo=bar'
51
47
  }, (err, response, body) => {
52
- t.error(err)
53
- t.equal(response.statusCode, 200)
54
- t.same(JSON.parse(body), {
48
+ t.assert.ifError(err)
49
+ t.assert.strictEqual(response.statusCode, 200)
50
+ t.assert.deepStrictEqual(JSON.parse(body), {
55
51
  foo: 'bar'
56
52
  })
53
+ fastify.close()
54
+ done()
57
55
  })
58
56
  })
59
57
  })
60
58
 
61
- test('use semicolon delimiter set to false', t => {
59
+ test('use semicolon delimiter set to false', (t, done) => {
62
60
  t.plan(4)
63
61
 
64
62
  const fastify = Fastify({
65
63
  useSemicolonDelimiter: false
66
64
  })
67
- t.teardown(fastify.close.bind(fastify))
68
65
 
69
66
  fastify.get('/1234;foo=bar', (req, reply) => {
70
67
  reply.send(req.query)
71
68
  })
72
69
 
73
70
  fastify.listen({ port: 0 }, err => {
74
- t.error(err)
71
+ t.assert.ifError(err)
75
72
 
76
73
  sget({
77
74
  method: 'GET',
78
75
  url: 'http://localhost:' + fastify.server.address().port + '/1234;foo=bar'
79
76
  }, (err, response, body) => {
80
- t.error(err)
81
- t.equal(response.statusCode, 200)
82
- t.same(JSON.parse(body), {})
77
+ t.assert.ifError(err)
78
+ t.assert.strictEqual(response.statusCode, 200)
79
+ t.assert.deepStrictEqual(JSON.parse(body), {})
80
+ fastify.close()
81
+ done()
83
82
  })
84
83
  })
85
84
  })
86
85
 
87
- test('use semicolon delimiter set to false return 404', t => {
86
+ test('use semicolon delimiter set to false return 404', (t, done) => {
88
87
  t.plan(3)
89
88
 
90
89
  const fastify = Fastify({
91
90
  useSemicolonDelimiter: false
92
91
  })
93
- t.teardown(fastify.close.bind(fastify))
94
92
 
95
93
  fastify.get('/1234', (req, reply) => {
96
94
  reply.send(req.query)
97
95
  })
98
96
 
99
97
  fastify.listen({ port: 0 }, err => {
100
- t.error(err)
98
+ t.assert.ifError(err)
101
99
 
102
100
  sget({
103
101
  method: 'GET',
104
102
  url: 'http://localhost:' + fastify.server.address().port + '/1234;foo=bar'
105
103
  }, (err, response, body) => {
106
- t.error(err)
107
- t.equal(response.statusCode, 404)
104
+ t.assert.ifError(err)
105
+ t.assert.strictEqual(response.statusCode, 404)
106
+ fastify.close()
107
+ done()
108
108
  })
109
109
  })
110
110
  })