fastify 5.3.3 → 5.4.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 (70) hide show
  1. package/README.md +2 -0
  2. package/build/build-validation.js +2 -1
  3. package/docs/Guides/Delay-Accepting-Requests.md +3 -3
  4. package/docs/Guides/Ecosystem.md +9 -5
  5. package/docs/Reference/ContentTypeParser.md +1 -1
  6. package/docs/Reference/Errors.md +2 -2
  7. package/docs/Reference/Hooks.md +14 -14
  8. package/docs/Reference/Logging.md +3 -3
  9. package/docs/Reference/Middleware.md +1 -1
  10. package/docs/Reference/Reply.md +8 -8
  11. package/docs/Reference/Request.md +1 -1
  12. package/docs/Reference/Routes.md +3 -3
  13. package/docs/Reference/Server.md +35 -21
  14. package/docs/Reference/Validation-and-Serialization.md +1 -1
  15. package/fastify.d.ts +2 -1
  16. package/fastify.js +14 -2
  17. package/lib/configValidator.js +1 -1
  18. package/lib/errors.js +6 -0
  19. package/lib/pluginOverride.js +3 -1
  20. package/lib/reply.js +7 -11
  21. package/lib/request.js +3 -10
  22. package/lib/symbols.js +1 -0
  23. package/lib/warnings.js +8 -0
  24. package/package.json +8 -4
  25. package/test/404s.test.js +226 -325
  26. package/test/allow-unsafe-regex.test.js +19 -48
  27. package/test/als.test.js +28 -40
  28. package/test/async-await.test.js +11 -2
  29. package/test/body-limit.test.js +41 -65
  30. package/test/build-certificate.js +1 -1
  31. package/test/custom-parser-async.test.js +17 -22
  32. package/test/decorator-namespace.test._js_ +3 -4
  33. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  34. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  35. package/test/helper.js +1 -1
  36. package/test/hooks-async.test.js +248 -218
  37. package/test/hooks.test.js +910 -769
  38. package/test/http-methods/lock.test.js +31 -31
  39. package/test/http-methods/mkcol.test.js +5 -9
  40. package/test/http-methods/proppatch.test.js +23 -29
  41. package/test/http-methods/report.test.js +44 -69
  42. package/test/http-methods/search.test.js +67 -82
  43. package/test/http2/closing.test.js +38 -20
  44. package/test/http2/secure-with-fallback.test.js +28 -27
  45. package/test/https/https.test.js +56 -53
  46. package/test/internals/errors.test.js +1 -1
  47. package/test/internals/handle-request.test.js +49 -66
  48. package/test/issue-4959.test.js +12 -3
  49. package/test/listen.4.test.js +31 -43
  50. package/test/nullable-validation.test.js +33 -46
  51. package/test/output-validation.test.js +24 -26
  52. package/test/plugin.2.test.js +104 -86
  53. package/test/plugin.3.test.js +56 -35
  54. package/test/plugin.4.test.js +124 -119
  55. package/test/proto-poisoning.test.js +78 -97
  56. package/test/request-error.test.js +0 -46
  57. package/test/route-hooks.test.js +112 -92
  58. package/test/route-prefix.test.js +194 -133
  59. package/test/schema-serialization.test.js +177 -154
  60. package/test/schema-special-usage.test.js +165 -132
  61. package/test/schema-validation.test.js +242 -205
  62. package/test/set-error-handler.test.js +58 -1
  63. package/test/skip-reply-send.test.js +64 -69
  64. package/test/trust-proxy.test.js +32 -58
  65. package/test/types/fastify.test-d.ts +3 -0
  66. package/test/types/request.test-d.ts +1 -0
  67. package/test/url-rewriting.test.js +45 -62
  68. package/types/request.d.ts +1 -0
  69. package/.taprc +0 -7
  70. package/.vscode/settings.json +0 -22
@@ -1,12 +1,12 @@
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('../fastify')
6
5
  const sget = require('simple-get').concat
7
6
  const fp = require('fastify-plugin')
7
+ const { waitForCb } = require('./toolkit')
8
8
 
9
- test('if a plugin raises an error and there is not a callback to handle it, the server must not start', t => {
9
+ test('if a plugin raises an error and there is not a callback to handle it, the server must not start', (t, testDone) => {
10
10
  t.plan(2)
11
11
  const fastify = Fastify()
12
12
 
@@ -15,14 +15,16 @@ test('if a plugin raises an error and there is not a callback to handle it, the
15
15
  })
16
16
 
17
17
  fastify.listen({ port: 0 }, err => {
18
- t.ok(err instanceof Error)
19
- t.equal(err.message, 'err')
18
+ t.assert.ok(err instanceof Error)
19
+ t.assert.strictEqual(err.message, 'err')
20
+ testDone()
20
21
  })
21
22
  })
22
23
 
23
- test('add hooks after route declaration', t => {
24
+ test('add hooks after route declaration', (t, testDone) => {
24
25
  t.plan(3)
25
26
  const fastify = Fastify()
27
+ t.after(() => fastify.close())
26
28
 
27
29
  function plugin (instance, opts, done) {
28
30
  instance.decorateRequest('check', null)
@@ -58,25 +60,25 @@ test('add hooks after route declaration', t => {
58
60
  })
59
61
 
60
62
  fastify.listen({ port: 0 }, err => {
61
- t.error(err)
63
+ t.assert.ifError(err)
62
64
 
63
65
  sget({
64
66
  method: 'GET',
65
67
  url: 'http://localhost:' + fastify.server.address().port
66
68
  }, (err, response, body) => {
67
- t.error(err)
68
- t.same(JSON.parse(body), { hook1: true, hook2: true, hook3: true })
69
- fastify.close()
69
+ t.assert.ifError(err)
70
+ t.assert.deepStrictEqual(JSON.parse(body), { hook1: true, hook2: true, hook3: true })
71
+ testDone()
70
72
  })
71
73
  })
72
74
  })
73
75
 
74
- test('nested plugins', t => {
76
+ test('nested plugins', (t, testDone) => {
75
77
  t.plan(5)
76
78
 
77
79
  const fastify = Fastify()
78
80
 
79
- t.teardown(fastify.close.bind(fastify))
81
+ t.after(() => fastify.close())
80
82
 
81
83
  fastify.register(function (fastify, opts, done) {
82
84
  fastify.register((fastify, opts, done) => {
@@ -97,32 +99,39 @@ test('nested plugins', t => {
97
99
  }, { prefix: '/parent' })
98
100
 
99
101
  fastify.listen({ port: 0 }, err => {
100
- t.error(err)
102
+ t.assert.ifError(err)
103
+
104
+ const completion = waitForCb({
105
+ steps: 2
106
+ })
101
107
 
102
108
  sget({
103
109
  method: 'GET',
104
110
  url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
105
111
  }, (err, response, body) => {
106
- t.error(err)
107
- t.same(body.toString(), 'I am child 1')
112
+ t.assert.ifError(err)
113
+ t.assert.deepStrictEqual(body.toString(), 'I am child 1')
114
+ completion.stepIn()
108
115
  })
109
-
110
116
  sget({
111
117
  method: 'GET',
112
118
  url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
113
119
  }, (err, response, body) => {
114
- t.error(err)
115
- t.same(body.toString(), 'I am child 2')
120
+ t.assert.ifError(err)
121
+ t.assert.deepStrictEqual(body.toString(), 'I am child 2')
122
+ completion.stepIn()
116
123
  })
124
+
125
+ completion.patience.then(testDone)
117
126
  })
118
127
  })
119
128
 
120
- test('nested plugins awaited', t => {
129
+ test('nested plugins awaited', (t, testDone) => {
121
130
  t.plan(5)
122
131
 
123
132
  const fastify = Fastify()
124
133
 
125
- t.teardown(fastify.close.bind(fastify))
134
+ t.after(() => fastify.close())
126
135
 
127
136
  fastify.register(async function wrap (fastify, opts) {
128
137
  await fastify.register(async function child1 (fastify, opts) {
@@ -139,27 +148,34 @@ test('nested plugins awaited', t => {
139
148
  }, { prefix: '/parent' })
140
149
 
141
150
  fastify.listen({ port: 0 }, err => {
142
- t.error(err)
151
+ t.assert.ifError(err)
152
+
153
+ const completion = waitForCb({
154
+ steps: 2
155
+ })
143
156
 
144
157
  sget({
145
158
  method: 'GET',
146
159
  url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
147
160
  }, (err, response, body) => {
148
- t.error(err)
149
- t.same(body.toString(), 'I am child 1')
161
+ t.assert.ifError(err)
162
+ t.assert.deepStrictEqual(body.toString(), 'I am child 1')
163
+ completion.stepIn()
150
164
  })
151
165
 
152
166
  sget({
153
167
  method: 'GET',
154
168
  url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
155
169
  }, (err, response, body) => {
156
- t.error(err)
157
- t.same(body.toString(), 'I am child 2')
170
+ t.assert.ifError(err)
171
+ t.assert.deepStrictEqual(body.toString(), 'I am child 2')
172
+ completion.stepIn()
158
173
  })
174
+ completion.patience.then(testDone)
159
175
  })
160
176
  })
161
177
 
162
- test('plugin metadata - decorators', t => {
178
+ test('plugin metadata - decorators', (t, testDone) => {
163
179
  t.plan(1)
164
180
  const fastify = Fastify()
165
181
 
@@ -179,7 +195,8 @@ test('plugin metadata - decorators', t => {
179
195
  fastify.register(plugin)
180
196
 
181
197
  fastify.ready(() => {
182
- t.ok(fastify.plugin)
198
+ t.assert.ok(fastify.plugin)
199
+ testDone()
183
200
  })
184
201
 
185
202
  function plugin (instance, opts, done) {
@@ -188,7 +205,7 @@ test('plugin metadata - decorators', t => {
188
205
  }
189
206
  })
190
207
 
191
- test('plugin metadata - decorators - should throw', t => {
208
+ test('plugin metadata - decorators - should throw', (t, testDone) => {
192
209
  t.plan(1)
193
210
  const fastify = Fastify()
194
211
 
@@ -206,7 +223,8 @@ test('plugin metadata - decorators - should throw', t => {
206
223
 
207
224
  fastify.register(plugin)
208
225
  fastify.ready((err) => {
209
- t.equal(err.message, "The decorator 'plugin1' is not present in Request")
226
+ t.assert.strictEqual(err.message, "The decorator 'plugin1' is not present in Request")
227
+ testDone()
210
228
  })
211
229
 
212
230
  function plugin (instance, opts, done) {
@@ -215,7 +233,7 @@ test('plugin metadata - decorators - should throw', t => {
215
233
  }
216
234
  })
217
235
 
218
- test('plugin metadata - decorators - should throw with plugin name', t => {
236
+ test('plugin metadata - decorators - should throw with plugin name', (t, testDone) => {
219
237
  t.plan(1)
220
238
  const fastify = Fastify()
221
239
 
@@ -234,7 +252,8 @@ test('plugin metadata - decorators - should throw with plugin name', t => {
234
252
 
235
253
  fastify.register(plugin)
236
254
  fastify.ready((err) => {
237
- t.equal(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
255
+ t.assert.strictEqual(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
256
+ testDone()
238
257
  })
239
258
 
240
259
  function plugin (instance, opts, done) {
@@ -243,7 +262,7 @@ test('plugin metadata - decorators - should throw with plugin name', t => {
243
262
  }
244
263
  })
245
264
 
246
- test('plugin metadata - dependencies', t => {
265
+ test('plugin metadata - dependencies', (t, testDone) => {
247
266
  t.plan(1)
248
267
  const fastify = Fastify()
249
268
 
@@ -261,7 +280,8 @@ test('plugin metadata - dependencies', t => {
261
280
  fastify.register(plugin)
262
281
 
263
282
  fastify.ready(() => {
264
- t.pass('everything right')
283
+ t.assert.ok('everything right')
284
+ testDone()
265
285
  })
266
286
 
267
287
  function dependency (instance, opts, done) {
@@ -273,7 +293,7 @@ test('plugin metadata - dependencies', t => {
273
293
  }
274
294
  })
275
295
 
276
- test('plugin metadata - dependencies (nested)', t => {
296
+ test('plugin metadata - dependencies (nested)', (t, testDone) => {
277
297
  t.plan(1)
278
298
  const fastify = Fastify()
279
299
 
@@ -291,7 +311,8 @@ test('plugin metadata - dependencies (nested)', t => {
291
311
  fastify.register(plugin)
292
312
 
293
313
  fastify.ready(() => {
294
- t.pass('everything right')
314
+ t.assert.ok('everything right')
315
+ testDone()
295
316
  })
296
317
 
297
318
  function dependency (instance, opts, done) {