fastify 5.3.2 → 5.3.3

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 (48) hide show
  1. package/.vscode/settings.json +22 -0
  2. package/docs/Guides/Ecosystem.md +7 -2
  3. package/docs/Guides/Serverless.md +28 -69
  4. package/docs/Reference/Errors.md +0 -2
  5. package/docs/Reference/Server.md +17 -1
  6. package/eslint.config.js +17 -9
  7. package/fastify.js +6 -2
  8. package/lib/decorate.js +2 -2
  9. package/lib/errors.js +0 -8
  10. package/lib/logger-factory.js +1 -1
  11. package/lib/logger-pino.js +2 -2
  12. package/lib/reply.js +2 -2
  13. package/lib/request.js +1 -1
  14. package/lib/server.js +30 -51
  15. package/package.json +4 -4
  16. package/test/close-pipelining.test.js +5 -4
  17. package/test/decorator.test.js +422 -341
  18. package/test/helper.js +107 -69
  19. package/test/hooks.on-listen.test.js +255 -239
  20. package/test/hooks.on-ready.test.js +110 -92
  21. package/test/inject.test.js +114 -97
  22. package/test/input-validation.js +63 -53
  23. package/test/internals/errors.test.js +1 -11
  24. package/test/internals/hooks.test.js +17 -0
  25. package/test/issue-4959.test.js +2 -2
  26. package/test/logger/response.test.js +19 -20
  27. package/test/options.error-handler.test.js +1 -1
  28. package/test/options.test.js +1 -1
  29. package/test/output-validation.test.js +49 -70
  30. package/test/patch.error-handler.test.js +1 -1
  31. package/test/patch.test.js +1 -1
  32. package/test/plugin.1.test.js +71 -60
  33. package/test/promises.test.js +36 -30
  34. package/test/put.error-handler.test.js +1 -1
  35. package/test/put.test.js +1 -1
  36. package/test/reply-error.test.js +169 -148
  37. package/test/reply-trailers.test.js +119 -108
  38. package/test/schema-feature.test.js +309 -238
  39. package/test/schema-validation.test.js +44 -2
  40. package/test/stream.1.test.js +30 -27
  41. package/test/stream.2.test.js +20 -10
  42. package/test/stream.3.test.js +37 -31
  43. package/test/types/errors.test-d.ts +0 -1
  44. package/test/types/plugin.test-d.ts +1 -1
  45. package/test/types/register.test-d.ts +1 -1
  46. package/test/use-semicolon-delimiter.test.js +1 -1
  47. package/types/errors.d.ts +0 -1
  48. package/test/http2/missing-http2-module.test.js +0 -17
@@ -1,41 +1,44 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('../fastify')
5
5
  const immediate = require('node:util').promisify(setImmediate)
6
6
 
7
- t.test('onReady should be called in order', t => {
7
+ test('onReady should be called in order', (t, done) => {
8
8
  t.plan(7)
9
9
  const fastify = Fastify()
10
10
 
11
11
  let order = 0
12
12
 
13
13
  fastify.addHook('onReady', function (done) {
14
- t.equal(order++, 0, 'called in root')
15
- t.equal(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
14
+ t.assert.strictEqual(order++, 0, 'called in root')
15
+ t.assert.strictEqual(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
16
16
  done()
17
17
  })
18
18
 
19
19
  fastify.register(async (childOne, o) => {
20
20
  childOne.addHook('onReady', function (done) {
21
- t.equal(order++, 1, 'called in childOne')
22
- t.equal(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
21
+ t.assert.strictEqual(order++, 1, 'called in childOne')
22
+ t.assert.strictEqual(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
23
23
  done()
24
24
  })
25
25
 
26
26
  childOne.register(async (childTwo, o) => {
27
27
  childTwo.addHook('onReady', async function () {
28
28
  await immediate()
29
- t.equal(order++, 2, 'called in childTwo')
30
- t.equal(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
29
+ t.assert.strictEqual(order++, 2, 'called in childTwo')
30
+ t.assert.strictEqual(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
31
31
  })
32
32
  })
33
33
  })
34
34
 
35
- fastify.ready(err => t.error(err))
35
+ fastify.ready(err => {
36
+ t.assert.ifError(err)
37
+ done()
38
+ })
36
39
  })
37
40
 
38
- t.test('onReady should be called once', async (t) => {
41
+ test('onReady should be called once', async (t) => {
39
42
  const app = Fastify()
40
43
  let counter = 0
41
44
 
@@ -47,11 +50,11 @@ t.test('onReady should be called once', async (t) => {
47
50
 
48
51
  const result = await Promise.race(promises)
49
52
 
50
- t.strictSame(result, 1, 'Should resolve in order')
51
- t.equal(counter, 1, 'Should call onReady only once')
53
+ t.assert.strictEqual(result, 1, 'Should resolve in order')
54
+ t.assert.strictEqual(counter, 1, 'Should call onReady only once')
52
55
  })
53
56
 
54
- t.test('async onReady should be called in order', async t => {
57
+ test('async onReady should be called in order', async t => {
55
58
  t.plan(7)
56
59
  const fastify = Fastify()
57
60
 
@@ -59,31 +62,31 @@ t.test('async onReady should be called in order', async t => {
59
62
 
60
63
  fastify.addHook('onReady', async function () {
61
64
  await immediate()
62
- t.equal(order++, 0, 'called in root')
63
- t.equal(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
65
+ t.assert.strictEqual(order++, 0, 'called in root')
66
+ t.assert.strictEqual(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
64
67
  })
65
68
 
66
69
  fastify.register(async (childOne, o) => {
67
70
  childOne.addHook('onReady', async function () {
68
71
  await immediate()
69
- t.equal(order++, 1, 'called in childOne')
70
- t.equal(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
72
+ t.assert.strictEqual(order++, 1, 'called in childOne')
73
+ t.assert.strictEqual(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
71
74
  })
72
75
 
73
76
  childOne.register(async (childTwo, o) => {
74
77
  childTwo.addHook('onReady', async function () {
75
78
  await immediate()
76
- t.equal(order++, 2, 'called in childTwo')
77
- t.equal(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
79
+ t.assert.strictEqual(order++, 2, 'called in childTwo')
80
+ t.assert.strictEqual(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
78
81
  })
79
82
  })
80
83
  })
81
84
 
82
85
  await fastify.ready()
83
- t.pass('ready')
86
+ t.assert.ok('ready')
84
87
  })
85
88
 
86
- t.test('mix ready and onReady', async t => {
89
+ test('mix ready and onReady', async t => {
87
90
  t.plan(2)
88
91
  const fastify = Fastify()
89
92
  let order = 0
@@ -94,13 +97,13 @@ t.test('mix ready and onReady', async t => {
94
97
  })
95
98
 
96
99
  await fastify.ready()
97
- t.equal(order, 1)
100
+ t.assert.strictEqual(order, 1)
98
101
 
99
102
  await fastify.ready()
100
- t.equal(order, 1, 'ready hooks execute once')
103
+ t.assert.strictEqual(order, 1, 'ready hooks execute once')
101
104
  })
102
105
 
103
- t.test('listen and onReady order', async t => {
106
+ test('listen and onReady order', async t => {
104
107
  t.plan(9)
105
108
 
106
109
  const fastify = Fastify()
@@ -127,19 +130,19 @@ t.test('listen and onReady order', async t => {
127
130
  fastify.addHook('onReady', checkOrder.bind(null, 3))
128
131
 
129
132
  await fastify.ready()
130
- t.pass('trigger the onReady')
133
+ t.assert.ok('trigger the onReady')
131
134
  await fastify.listen({ port: 0 })
132
- t.pass('do not trigger the onReady')
135
+ t.assert.ok('do not trigger the onReady')
133
136
 
134
137
  await fastify.close()
135
138
 
136
139
  function checkOrder (shouldbe) {
137
- t.equal(order, shouldbe)
140
+ t.assert.strictEqual(order, shouldbe)
138
141
  order++
139
142
  }
140
143
  })
141
144
 
142
- t.test('multiple ready calls', async t => {
145
+ test('multiple ready calls', async t => {
143
146
  t.plan(11)
144
147
 
145
148
  const fastify = Fastify()
@@ -154,7 +157,7 @@ t.test('multiple ready calls', async t => {
154
157
  subinstance.addHook('onReady', checkOrder.bind(null, 7))
155
158
  })
156
159
 
157
- t.equal(order, 0, 'ready and hooks not triggered yet')
160
+ t.assert.strictEqual(order, 0, 'ready and hooks not triggered yet')
158
161
  order++
159
162
  })
160
163
 
@@ -163,139 +166,145 @@ t.test('multiple ready calls', async t => {
163
166
  fastify.addHook('onReady', checkOrder.bind(null, 5))
164
167
 
165
168
  await fastify.ready()
166
- t.pass('trigger the onReady')
169
+ t.assert.ok('trigger the onReady')
167
170
 
168
171
  await fastify.ready()
169
- t.pass('do not trigger the onReady')
172
+ t.assert.ok('do not trigger the onReady')
170
173
 
171
174
  await fastify.ready()
172
- t.pass('do not trigger the onReady')
175
+ t.assert.ok('do not trigger the onReady')
173
176
 
174
177
  function checkOrder (shouldbe) {
175
- t.equal(order, shouldbe)
178
+ t.assert.strictEqual(order, shouldbe)
176
179
  order++
177
180
  }
178
181
  })
179
182
 
180
- t.test('onReady should manage error in sync', t => {
183
+ test('onReady should manage error in sync', (t, done) => {
181
184
  t.plan(4)
182
185
  const fastify = Fastify()
183
186
 
184
187
  fastify.addHook('onReady', function (done) {
185
- t.pass('called in root')
188
+ t.assert.ok('called in root')
186
189
  done()
187
190
  })
188
191
 
189
192
  fastify.register(async (childOne, o) => {
190
193
  childOne.addHook('onReady', function (done) {
191
- t.pass('called in childOne')
194
+ t.assert.ok('called in childOne')
192
195
  done(new Error('FAIL ON READY'))
193
196
  })
194
197
 
195
198
  childOne.register(async (childTwo, o) => {
196
199
  childTwo.addHook('onReady', async function () {
197
- t.fail('should not be called')
200
+ t.assert.fail('should not be called')
198
201
  })
199
202
  })
200
203
  })
201
204
 
202
205
  fastify.ready(err => {
203
- t.ok(err)
204
- t.equal(err.message, 'FAIL ON READY')
206
+ t.assert.ok(err)
207
+ t.assert.strictEqual(err.message, 'FAIL ON READY')
208
+ done()
205
209
  })
206
210
  })
207
211
 
208
- t.test('onReady should manage error in async', t => {
212
+ test('onReady should manage error in async', (t, done) => {
209
213
  t.plan(4)
210
214
  const fastify = Fastify()
211
215
 
212
216
  fastify.addHook('onReady', function (done) {
213
- t.pass('called in root')
217
+ t.assert.ok('called in root')
214
218
  done()
215
219
  })
216
220
 
217
221
  fastify.register(async (childOne, o) => {
218
222
  childOne.addHook('onReady', async function () {
219
- t.pass('called in childOne')
223
+ t.assert.ok('called in childOne')
220
224
  throw new Error('FAIL ON READY')
221
225
  })
222
226
 
223
227
  childOne.register(async (childTwo, o) => {
224
228
  childTwo.addHook('onReady', async function () {
225
- t.fail('should not be called')
229
+ t.assert.fail('should not be called')
226
230
  })
227
231
  })
228
232
  })
229
233
 
230
234
  fastify.ready(err => {
231
- t.ok(err)
232
- t.equal(err.message, 'FAIL ON READY')
235
+ t.assert.ok(err)
236
+ t.assert.strictEqual(err.message, 'FAIL ON READY')
237
+ done()
233
238
  })
234
239
  })
235
240
 
236
- t.test('onReady should manage sync error', t => {
241
+ test('onReady should manage sync error', (t, done) => {
237
242
  t.plan(4)
238
243
  const fastify = Fastify()
239
244
 
240
245
  fastify.addHook('onReady', function (done) {
241
- t.pass('called in root')
246
+ t.assert.ok('called in root')
242
247
  done()
243
248
  })
244
249
 
245
250
  fastify.register(async (childOne, o) => {
246
251
  childOne.addHook('onReady', function (done) {
247
- t.pass('called in childOne')
252
+ t.assert.ok('called in childOne')
248
253
  throw new Error('FAIL UNWANTED SYNC EXCEPTION')
249
254
  })
250
255
 
251
256
  childOne.register(async (childTwo, o) => {
252
257
  childTwo.addHook('onReady', async function () {
253
- t.fail('should not be called')
258
+ t.assert.fail('should not be called')
254
259
  })
255
260
  })
256
261
  })
257
262
 
258
263
  fastify.ready(err => {
259
- t.ok(err)
260
- t.equal(err.message, 'FAIL UNWANTED SYNC EXCEPTION')
264
+ t.assert.ok(err)
265
+ t.assert.strictEqual(err.message, 'FAIL UNWANTED SYNC EXCEPTION')
266
+ done()
261
267
  })
262
268
  })
263
269
 
264
- t.test('onReady can not add decorators or application hooks', t => {
270
+ test('onReady can not add decorators or application hooks', (t, done) => {
265
271
  t.plan(3)
266
272
  const fastify = Fastify()
267
273
 
268
274
  fastify.addHook('onReady', function (done) {
269
- t.pass('called in root')
275
+ t.assert.ok('called in root')
270
276
  fastify.decorate('test', () => {})
271
277
 
272
278
  fastify.addHook('onReady', async function () {
273
- t.fail('it will be not called')
279
+ t.assert.fail('it will be not called')
274
280
  })
275
281
  done()
276
282
  })
277
283
 
278
284
  fastify.addHook('onReady', function (done) {
279
- t.ok(this.hasDecorator('test'))
285
+ t.assert.ok(this.hasDecorator('test'))
280
286
  done()
281
287
  })
282
288
 
283
- fastify.ready(err => { t.error(err) })
289
+ fastify.ready(err => {
290
+ t.assert.ifError(err)
291
+ done()
292
+ })
284
293
  })
285
294
 
286
- t.test('onReady cannot add lifecycle hooks', t => {
295
+ test('onReady cannot add lifecycle hooks', (t, done) => {
287
296
  t.plan(5)
288
297
  const fastify = Fastify()
289
298
 
290
299
  fastify.addHook('onReady', function (done) {
291
- t.pass('called in root')
300
+ t.assert.ok('called in root')
292
301
  try {
293
302
  fastify.addHook('onRequest', (request, reply, done) => {})
294
303
  } catch (error) {
295
- t.ok(error)
296
- t.equal(error.message, 'Root plugin has already booted')
304
+ t.assert.ok(error)
305
+ t.assert.strictEqual(error.message, 'Root plugin has already booted')
297
306
  // TODO: look where the error pops up
298
- t.equal(error.code, 'AVV_ERR_ROOT_PLG_BOOTED')
307
+ t.assert.strictEqual(error.code, 'AVV_ERR_ROOT_PLG_BOOTED')
299
308
  done(error)
300
309
  }
301
310
  })
@@ -303,93 +312,102 @@ t.test('onReady cannot add lifecycle hooks', t => {
303
312
  fastify.addHook('onRequest', (request, reply, done) => {})
304
313
  fastify.get('/', async () => 'hello')
305
314
 
306
- fastify.ready((err) => { t.ok(err) })
315
+ fastify.ready((err) => {
316
+ t.assert.ok(err)
317
+ done()
318
+ })
307
319
  })
308
320
 
309
- t.test('onReady throw loading error', t => {
321
+ test('onReady throw loading error', t => {
310
322
  t.plan(2)
311
323
  const fastify = Fastify()
312
324
 
313
325
  try {
314
326
  fastify.addHook('onReady', async function (done) {})
315
327
  } catch (e) {
316
- t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
317
- t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
328
+ t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
329
+ t.assert.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
318
330
  }
319
331
  })
320
332
 
321
- t.test('onReady does not call done', t => {
333
+ test('onReady does not call done', (t, done) => {
322
334
  t.plan(6)
323
335
  const fastify = Fastify({ pluginTimeout: 500 })
324
336
 
325
337
  fastify.addHook('onReady', function someHookName (done) {
326
- t.pass('called in root')
338
+ t.assert.ok('called in root')
327
339
  // done() // don't call done to test timeout
328
340
  })
329
341
 
330
342
  fastify.ready(err => {
331
- t.ok(err)
332
- t.equal(err.message, 'A callback for \'onReady\' hook "someHookName" timed out. You may have forgotten to call \'done\' function or to resolve a Promise')
333
- t.equal(err.code, 'FST_ERR_HOOK_TIMEOUT')
334
- t.ok(err.cause)
335
- t.equal(err.cause.code, 'AVV_ERR_READY_TIMEOUT')
343
+ t.assert.ok(err)
344
+ t.assert.strictEqual(err.message, 'A callback for \'onReady\' hook "someHookName" timed out. You may have forgotten to call \'done\' function or to resolve a Promise')
345
+ t.assert.strictEqual(err.code, 'FST_ERR_HOOK_TIMEOUT')
346
+ t.assert.ok(err.cause)
347
+ t.assert.strictEqual(err.cause.code, 'AVV_ERR_READY_TIMEOUT')
348
+ done()
336
349
  })
337
350
  })
338
351
 
339
- t.test('onReady execution order', t => {
352
+ test('onReady execution order', (t, done) => {
340
353
  t.plan(3)
341
354
  const fastify = Fastify({ })
342
355
 
343
356
  let i = 0
344
- fastify.ready(() => { i++; t.equal(i, 1) })
345
- fastify.ready(() => { i++; t.equal(i, 2) })
346
- fastify.ready(() => { i++; t.equal(i, 3) })
357
+ fastify.ready(() => { i++; t.assert.strictEqual(i, 1) })
358
+ fastify.ready(() => { i++; t.assert.strictEqual(i, 2) })
359
+ fastify.ready(() => {
360
+ i++
361
+ t.assert.strictEqual(i, 3)
362
+ done()
363
+ })
347
364
  })
348
365
 
349
- t.test('ready return the server with callback', t => {
366
+ test('ready return the server with callback', (t, done) => {
350
367
  t.plan(2)
351
368
  const fastify = Fastify()
352
369
 
353
370
  fastify.ready((err, instance) => {
354
- t.error(err)
355
- t.same(instance, fastify)
371
+ t.assert.ifError(err)
372
+ t.assert.deepStrictEqual(instance, fastify)
373
+ done()
356
374
  })
357
375
  })
358
376
 
359
- t.test('ready return the server with Promise', t => {
377
+ test('ready return the server with Promise', async t => {
360
378
  t.plan(1)
361
379
  const fastify = Fastify()
362
380
 
363
- fastify.ready()
364
- .then(instance => { t.same(instance, fastify) })
365
- .catch(err => { t.fail(err) })
381
+ await fastify.ready()
382
+ .then(instance => { t.assert.deepStrictEqual(instance, fastify) })
383
+ .catch(err => { t.assert.fail(err) })
366
384
  })
367
385
 
368
- t.test('ready return registered', t => {
386
+ test('ready return registered', async t => {
369
387
  t.plan(4)
370
388
  const fastify = Fastify()
371
389
 
372
390
  fastify.register((one, opts, done) => {
373
- one.ready().then(itself => { t.same(itself, one) })
391
+ one.ready().then(itself => { t.assert.deepStrictEqual(itself, one) })
374
392
  done()
375
393
  })
376
394
 
377
395
  fastify.register((two, opts, done) => {
378
- two.ready().then(itself => { t.same(itself, two) })
396
+ two.ready().then(itself => { t.assert.deepStrictEqual(itself, two) })
379
397
 
380
398
  two.register((twoDotOne, opts, done) => {
381
- twoDotOne.ready().then(itself => { t.same(itself, twoDotOne) })
399
+ twoDotOne.ready().then(itself => { t.assert.deepStrictEqual(itself, twoDotOne) })
382
400
  done()
383
401
  })
384
402
  done()
385
403
  })
386
404
 
387
- fastify.ready()
388
- .then(instance => { t.same(instance, fastify) })
389
- .catch(err => { t.fail(err) })
405
+ await fastify.ready()
406
+ .then(instance => { t.assert.deepStrictEqual(instance, fastify) })
407
+ .catch(err => { t.assert.fail(err) })
390
408
  })
391
409
 
392
- t.test('do not crash with error in follow up onReady hook', async t => {
410
+ test('do not crash with error in follow up onReady hook', async t => {
393
411
  const fastify = Fastify()
394
412
 
395
413
  fastify.addHook('onReady', async function () {
@@ -399,5 +417,5 @@ t.test('do not crash with error in follow up onReady hook', async t => {
399
417
  throw new Error('kaboom')
400
418
  })
401
419
 
402
- await t.rejects(fastify.ready())
420
+ await t.assert.rejects(fastify.ready())
403
421
  })