fastify 3.9.2 → 3.12.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 (92) hide show
  1. package/GOVERNANCE.md +1 -1
  2. package/README.md +12 -8
  3. package/SECURITY.md +3 -3
  4. package/docs/ContentTypeParser.md +1 -1
  5. package/docs/Ecosystem.md +16 -6
  6. package/docs/Encapsulation.md +5 -2
  7. package/docs/Fluent-Schema.md +4 -4
  8. package/docs/Getting-Started.md +1 -1
  9. package/docs/Hooks.md +28 -1
  10. package/docs/Lifecycle.md +8 -1
  11. package/docs/Middleware.md +5 -4
  12. package/docs/Reply.md +13 -4
  13. package/docs/Routes.md +4 -3
  14. package/docs/Server.md +78 -4
  15. package/docs/Serverless.md +23 -51
  16. package/docs/TypeScript.md +35 -18
  17. package/docs/Validation-and-Serialization.md +4 -4
  18. package/docs/Write-Plugin.md +4 -4
  19. package/examples/hooks-benchmark.js +12 -12
  20. package/examples/hooks.js +16 -16
  21. package/examples/plugin.js +2 -2
  22. package/examples/route-prefix.js +4 -4
  23. package/fastify.d.ts +16 -1
  24. package/fastify.js +33 -16
  25. package/isolate-0x426d1e0-1227-v8.log +4019 -0
  26. package/isolate-0x4d4c7e0-1988-v8.log +4081 -0
  27. package/lib/errors.js +6 -0
  28. package/lib/headRoute.js +31 -0
  29. package/lib/pluginOverride.js +5 -5
  30. package/lib/pluginUtils.js +7 -6
  31. package/lib/reply.js +14 -2
  32. package/lib/reqIdGenFactory.js +5 -0
  33. package/lib/request.js +1 -1
  34. package/lib/route.js +66 -41
  35. package/lib/schema-compilers.js +5 -3
  36. package/lib/schema-controller.js +106 -0
  37. package/lib/schemas.js +14 -24
  38. package/lib/server.js +1 -0
  39. package/lib/symbols.js +1 -3
  40. package/lib/warnings.js +2 -0
  41. package/lib/wrapThenable.js +2 -1
  42. package/package.json +25 -21
  43. package/test/404s.test.js +120 -120
  44. package/test/500s.test.js +8 -8
  45. package/test/async-await.test.js +29 -1
  46. package/test/close.test.js +8 -8
  47. package/test/context-config.test.js +52 -0
  48. package/test/custom-parser.test.js +8 -8
  49. package/test/decorator.test.js +49 -49
  50. package/test/default-route.test.js +43 -0
  51. package/test/fastify-instance.test.js +2 -2
  52. package/test/fluent-schema.test.js +3 -3
  53. package/test/handler-context.test.js +2 -2
  54. package/test/hooks-async.test.js +3 -3
  55. package/test/hooks.on-ready.test.js +12 -12
  56. package/test/hooks.test.js +75 -32
  57. package/test/http2/closing.test.js +23 -1
  58. package/test/inject.test.js +6 -6
  59. package/test/input-validation.js +2 -2
  60. package/test/internals/hookRunner.test.js +50 -50
  61. package/test/internals/reply.test.js +47 -22
  62. package/test/internals/request.test.js +3 -9
  63. package/test/internals/version.test.js +2 -2
  64. package/test/logger.test.js +30 -30
  65. package/test/middleware.test.js +4 -4
  66. package/test/plugin.helper.js +2 -2
  67. package/test/plugin.test.js +154 -99
  68. package/test/register.test.js +11 -11
  69. package/test/request-error.test.js +2 -2
  70. package/test/route-hooks.test.js +24 -24
  71. package/test/route-prefix.test.js +81 -52
  72. package/test/route.test.js +568 -0
  73. package/test/schema-feature.test.js +168 -38
  74. package/test/schema-serialization.test.js +4 -4
  75. package/test/schema-special-usage.test.js +136 -0
  76. package/test/schema-validation.test.js +7 -7
  77. package/test/skip-reply-send.test.js +315 -0
  78. package/test/stream.test.js +6 -6
  79. package/test/throw.test.js +4 -4
  80. package/test/types/instance.test-d.ts +5 -3
  81. package/test/types/plugin.test-d.ts +7 -7
  82. package/test/types/reply.test-d.ts +1 -0
  83. package/test/types/schema.test-d.ts +15 -0
  84. package/test/validation-error-handling.test.js +5 -5
  85. package/test/versioned-routes.test.js +1 -1
  86. package/types/content-type-parser.d.ts +1 -1
  87. package/types/instance.d.ts +6 -3
  88. package/types/plugin.d.ts +1 -1
  89. package/types/reply.d.ts +1 -0
  90. package/types/route.d.ts +8 -2
  91. package/types/schema.d.ts +3 -0
  92. package/test/skip-reply-send.js +0 -98
package/test/404s.test.js CHANGED
@@ -198,7 +198,7 @@ test('setting a custom 404 handler multiple times is an error', t => {
198
198
 
199
199
  const fastify = Fastify()
200
200
 
201
- fastify.register((instance, options, next) => {
201
+ fastify.register((instance, options, done) => {
202
202
  instance.setNotFoundHandler(() => {})
203
203
 
204
204
  try {
@@ -209,7 +209,7 @@ test('setting a custom 404 handler multiple times is an error', t => {
209
209
  t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
210
210
  }
211
211
 
212
- next()
212
+ done()
213
213
  }, { prefix: '/prefix' })
214
214
 
215
215
  fastify.listen(0, err => {
@@ -223,7 +223,7 @@ test('setting a custom 404 handler multiple times is an error', t => {
223
223
 
224
224
  const fastify = Fastify()
225
225
 
226
- fastify.register((instance, options, next) => {
226
+ fastify.register((instance, options, done) => {
227
227
  try {
228
228
  instance.setNotFoundHandler(() => {})
229
229
  t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
@@ -231,7 +231,7 @@ test('setting a custom 404 handler multiple times is an error', t => {
231
231
  t.type(err, Error)
232
232
  t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
233
233
  }
234
- next()
234
+ done()
235
235
  })
236
236
 
237
237
  fastify.setNotFoundHandler(() => {})
@@ -247,10 +247,10 @@ test('setting a custom 404 handler multiple times is an error', t => {
247
247
 
248
248
  const fastify = Fastify()
249
249
 
250
- fastify.register((instance, options, next) => {
250
+ fastify.register((instance, options, done) => {
251
251
  instance.setNotFoundHandler(() => {})
252
252
 
253
- instance.register((instance2, options, next) => {
253
+ instance.register((instance2, options, done) => {
254
254
  try {
255
255
  instance2.setNotFoundHandler(() => {})
256
256
  t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
@@ -258,10 +258,10 @@ test('setting a custom 404 handler multiple times is an error', t => {
258
258
  t.type(err, Error)
259
259
  t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
260
260
  }
261
- next()
261
+ done()
262
262
  })
263
263
 
264
- next()
264
+ done()
265
265
  }, { prefix: '/prefix' })
266
266
 
267
267
  fastify.setNotFoundHandler(() => {})
@@ -277,13 +277,13 @@ test('setting a custom 404 handler multiple times is an error', t => {
277
277
 
278
278
  const fastify = Fastify()
279
279
 
280
- fastify.register((instance, options, next) => {
281
- instance.register((instance2A, options, next) => {
280
+ fastify.register((instance, options, done) => {
281
+ instance.register((instance2A, options, done) => {
282
282
  instance2A.setNotFoundHandler(() => {})
283
- next()
283
+ done()
284
284
  })
285
285
 
286
- instance.register((instance2B, options, next) => {
286
+ instance.register((instance2B, options, done) => {
287
287
  try {
288
288
  instance2B.setNotFoundHandler(() => {})
289
289
  t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
@@ -291,10 +291,10 @@ test('setting a custom 404 handler multiple times is an error', t => {
291
291
  t.type(err, Error)
292
292
  t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
293
293
  }
294
- next()
294
+ done()
295
295
  })
296
296
 
297
- next()
297
+ done()
298
298
  }, { prefix: '/prefix' })
299
299
 
300
300
  fastify.setNotFoundHandler(() => {})
@@ -320,25 +320,25 @@ test('encapsulated 404', t => {
320
320
  reply.code(404).send('this was not found')
321
321
  })
322
322
 
323
- fastify.register(function (f, opts, next) {
323
+ fastify.register(function (f, opts, done) {
324
324
  f.setNotFoundHandler(function (req, reply) {
325
325
  reply.code(404).send('this was not found 2')
326
326
  })
327
- next()
327
+ done()
328
328
  }, { prefix: '/test' })
329
329
 
330
- fastify.register(function (f, opts, next) {
330
+ fastify.register(function (f, opts, done) {
331
331
  f.setNotFoundHandler(function (req, reply) {
332
332
  reply.code(404).send('this was not found 3')
333
333
  })
334
- next()
334
+ done()
335
335
  }, { prefix: '/test2' })
336
336
 
337
- fastify.register(function (f, opts, next) {
337
+ fastify.register(function (f, opts, done) {
338
338
  f.setNotFoundHandler(function (request, reply) {
339
339
  reply.code(404).send('this was not found 4')
340
340
  })
341
- next()
341
+ done()
342
342
  }, { prefix: '/test3/' })
343
343
 
344
344
  t.tearDown(fastify.close.bind(fastify))
@@ -459,21 +459,21 @@ test('custom 404 hook and handler context', t => {
459
459
 
460
460
  fastify.decorate('foo', 42)
461
461
 
462
- fastify.addHook('onRequest', function (req, res, next) {
462
+ fastify.addHook('onRequest', function (req, res, done) {
463
463
  t.strictEqual(this.foo, 42)
464
- next()
464
+ done()
465
465
  })
466
- fastify.addHook('preHandler', function (request, reply, next) {
466
+ fastify.addHook('preHandler', function (request, reply, done) {
467
467
  t.strictEqual(this.foo, 42)
468
- next()
468
+ done()
469
469
  })
470
- fastify.addHook('onSend', function (request, reply, payload, next) {
470
+ fastify.addHook('onSend', function (request, reply, payload, done) {
471
471
  t.strictEqual(this.foo, 42)
472
- next()
472
+ done()
473
473
  })
474
- fastify.addHook('onResponse', function (request, reply, next) {
474
+ fastify.addHook('onResponse', function (request, reply, done) {
475
475
  t.strictEqual(this.foo, 42)
476
- next()
476
+ done()
477
477
  })
478
478
 
479
479
  fastify.setNotFoundHandler(function (req, reply) {
@@ -481,24 +481,24 @@ test('custom 404 hook and handler context', t => {
481
481
  reply.code(404).send('this was not found')
482
482
  })
483
483
 
484
- fastify.register(function (instance, opts, next) {
484
+ fastify.register(function (instance, opts, done) {
485
485
  instance.decorate('bar', 84)
486
486
 
487
- instance.addHook('onRequest', function (req, res, next) {
487
+ instance.addHook('onRequest', function (req, res, done) {
488
488
  t.strictEqual(this.bar, 84)
489
- next()
489
+ done()
490
490
  })
491
- instance.addHook('preHandler', function (request, reply, next) {
491
+ instance.addHook('preHandler', function (request, reply, done) {
492
492
  t.strictEqual(this.bar, 84)
493
- next()
493
+ done()
494
494
  })
495
- instance.addHook('onSend', function (request, reply, payload, next) {
495
+ instance.addHook('onSend', function (request, reply, payload, done) {
496
496
  t.strictEqual(this.bar, 84)
497
- next()
497
+ done()
498
498
  })
499
- instance.addHook('onResponse', function (request, reply, next) {
499
+ instance.addHook('onResponse', function (request, reply, done) {
500
500
  t.strictEqual(this.bar, 84)
501
- next()
501
+ done()
502
502
  })
503
503
 
504
504
  instance.setNotFoundHandler(function (req, reply) {
@@ -507,7 +507,7 @@ test('custom 404 hook and handler context', t => {
507
507
  reply.code(404).send('encapsulated was not found')
508
508
  })
509
509
 
510
- next()
510
+ done()
511
511
  }, { prefix: '/encapsulated' })
512
512
 
513
513
  fastify.inject('/not-found', (err, res) => {
@@ -530,28 +530,28 @@ test('encapsulated custom 404 without - prefix hook and handler context', t => {
530
530
 
531
531
  fastify.decorate('foo', 42)
532
532
 
533
- fastify.register(function (instance, opts, next) {
533
+ fastify.register(function (instance, opts, done) {
534
534
  instance.decorate('bar', 84)
535
535
 
536
- instance.addHook('onRequest', function (req, res, next) {
536
+ instance.addHook('onRequest', function (req, res, done) {
537
537
  t.strictEqual(this.foo, 42)
538
538
  t.strictEqual(this.bar, 84)
539
- next()
539
+ done()
540
540
  })
541
- instance.addHook('preHandler', function (request, reply, next) {
541
+ instance.addHook('preHandler', function (request, reply, done) {
542
542
  t.strictEqual(this.foo, 42)
543
543
  t.strictEqual(this.bar, 84)
544
- next()
544
+ done()
545
545
  })
546
- instance.addHook('onSend', function (request, reply, payload, next) {
546
+ instance.addHook('onSend', function (request, reply, payload, done) {
547
547
  t.strictEqual(this.foo, 42)
548
548
  t.strictEqual(this.bar, 84)
549
- next()
549
+ done()
550
550
  })
551
- instance.addHook('onResponse', function (request, reply, next) {
551
+ instance.addHook('onResponse', function (request, reply, done) {
552
552
  t.strictEqual(this.foo, 42)
553
553
  t.strictEqual(this.bar, 84)
554
- next()
554
+ done()
555
555
  })
556
556
 
557
557
  instance.setNotFoundHandler(function (request, reply) {
@@ -560,7 +560,7 @@ test('encapsulated custom 404 without - prefix hook and handler context', t => {
560
560
  reply.code(404).send('custom not found')
561
561
  })
562
562
 
563
- next()
563
+ done()
564
564
  })
565
565
 
566
566
  fastify.inject('/not-found', (err, res) => {
@@ -575,24 +575,24 @@ test('run hooks on default 404', t => {
575
575
 
576
576
  const fastify = Fastify()
577
577
 
578
- fastify.addHook('onRequest', function (req, res, next) {
578
+ fastify.addHook('onRequest', function (req, res, done) {
579
579
  t.pass('onRequest called')
580
- next()
580
+ done()
581
581
  })
582
582
 
583
- fastify.addHook('preHandler', function (request, reply, next) {
583
+ fastify.addHook('preHandler', function (request, reply, done) {
584
584
  t.pass('preHandler called')
585
- next()
585
+ done()
586
586
  })
587
587
 
588
- fastify.addHook('onSend', function (request, reply, payload, next) {
588
+ fastify.addHook('onSend', function (request, reply, payload, done) {
589
589
  t.pass('onSend called')
590
- next()
590
+ done()
591
591
  })
592
592
 
593
- fastify.addHook('onResponse', function (request, reply, next) {
593
+ fastify.addHook('onResponse', function (request, reply, done) {
594
594
  t.pass('onResponse called')
595
- next()
595
+ done()
596
596
  })
597
597
 
598
598
  fastify.get('/', function (req, reply) {
@@ -621,28 +621,28 @@ test('run non-encapsulated plugin hooks on default 404', t => {
621
621
 
622
622
  const fastify = Fastify()
623
623
 
624
- fastify.register(fp(function (instance, options, next) {
625
- instance.addHook('onRequest', function (req, res, next) {
624
+ fastify.register(fp(function (instance, options, done) {
625
+ instance.addHook('onRequest', function (req, res, done) {
626
626
  t.pass('onRequest called')
627
- next()
627
+ done()
628
628
  })
629
629
 
630
- instance.addHook('preHandler', function (request, reply, next) {
630
+ instance.addHook('preHandler', function (request, reply, done) {
631
631
  t.pass('preHandler called')
632
- next()
632
+ done()
633
633
  })
634
634
 
635
- instance.addHook('onSend', function (request, reply, payload, next) {
635
+ instance.addHook('onSend', function (request, reply, payload, done) {
636
636
  t.pass('onSend called')
637
- next()
637
+ done()
638
638
  })
639
639
 
640
- instance.addHook('onResponse', function (request, reply, next) {
640
+ instance.addHook('onResponse', function (request, reply, done) {
641
641
  t.pass('onResponse called')
642
- next()
642
+ done()
643
643
  })
644
644
 
645
- next()
645
+ done()
646
646
  }))
647
647
 
648
648
  fastify.get('/', function (req, reply) {
@@ -664,28 +664,28 @@ test('run non-encapsulated plugin hooks on custom 404', t => {
664
664
 
665
665
  const fastify = Fastify()
666
666
 
667
- const plugin = fp((instance, opts, next) => {
668
- instance.addHook('onRequest', function (req, res, next) {
667
+ const plugin = fp((instance, opts, done) => {
668
+ instance.addHook('onRequest', function (req, res, done) {
669
669
  t.pass('onRequest called')
670
- next()
670
+ done()
671
671
  })
672
672
 
673
- instance.addHook('preHandler', function (request, reply, next) {
673
+ instance.addHook('preHandler', function (request, reply, done) {
674
674
  t.pass('preHandler called')
675
- next()
675
+ done()
676
676
  })
677
677
 
678
- instance.addHook('onSend', function (request, reply, payload, next) {
678
+ instance.addHook('onSend', function (request, reply, payload, done) {
679
679
  t.pass('onSend called')
680
- next()
680
+ done()
681
681
  })
682
682
 
683
- instance.addHook('onResponse', function (request, reply, next) {
683
+ instance.addHook('onResponse', function (request, reply, done) {
684
684
  t.pass('onResponse called')
685
- next()
685
+ done()
686
686
  })
687
687
 
688
- next()
688
+ done()
689
689
  })
690
690
 
691
691
  fastify.register(plugin)
@@ -712,52 +712,52 @@ test('run hook with encapsulated 404', t => {
712
712
 
713
713
  const fastify = Fastify()
714
714
 
715
- fastify.addHook('onRequest', function (req, res, next) {
715
+ fastify.addHook('onRequest', function (req, res, done) {
716
716
  t.pass('onRequest called')
717
- next()
717
+ done()
718
718
  })
719
719
 
720
- fastify.addHook('preHandler', function (request, reply, next) {
720
+ fastify.addHook('preHandler', function (request, reply, done) {
721
721
  t.pass('preHandler called')
722
- next()
722
+ done()
723
723
  })
724
724
 
725
- fastify.addHook('onSend', function (request, reply, payload, next) {
725
+ fastify.addHook('onSend', function (request, reply, payload, done) {
726
726
  t.pass('onSend called')
727
- next()
727
+ done()
728
728
  })
729
729
 
730
- fastify.addHook('onResponse', function (request, reply, next) {
730
+ fastify.addHook('onResponse', function (request, reply, done) {
731
731
  t.pass('onResponse called')
732
- next()
732
+ done()
733
733
  })
734
734
 
735
- fastify.register(function (f, opts, next) {
735
+ fastify.register(function (f, opts, done) {
736
736
  f.setNotFoundHandler(function (req, reply) {
737
737
  reply.code(404).send('this was not found 2')
738
738
  })
739
739
 
740
- f.addHook('onRequest', function (req, res, next) {
740
+ f.addHook('onRequest', function (req, res, done) {
741
741
  t.pass('onRequest 2 called')
742
- next()
742
+ done()
743
743
  })
744
744
 
745
- f.addHook('preHandler', function (request, reply, next) {
745
+ f.addHook('preHandler', function (request, reply, done) {
746
746
  t.pass('preHandler 2 called')
747
- next()
747
+ done()
748
748
  })
749
749
 
750
- f.addHook('onSend', function (request, reply, payload, next) {
750
+ f.addHook('onSend', function (request, reply, payload, done) {
751
751
  t.pass('onSend 2 called')
752
- next()
752
+ done()
753
753
  })
754
754
 
755
- f.addHook('onResponse', function (request, reply, next) {
755
+ f.addHook('onResponse', function (request, reply, done) {
756
756
  t.pass('onResponse 2 called')
757
- next()
757
+ done()
758
758
  })
759
759
 
760
- next()
760
+ done()
761
761
  }, { prefix: '/test' })
762
762
 
763
763
  t.tearDown(fastify.close.bind(fastify))
@@ -786,18 +786,18 @@ test('hooks check 404', t => {
786
786
  reply.send({ hello: 'world' })
787
787
  })
788
788
 
789
- fastify.addHook('onSend', (req, reply, payload, next) => {
789
+ fastify.addHook('onSend', (req, reply, payload, done) => {
790
790
  t.deepEqual(req.query, { foo: 'asd' })
791
791
  t.ok('called', 'onSend')
792
- next()
792
+ done()
793
793
  })
794
- fastify.addHook('onRequest', (req, res, next) => {
794
+ fastify.addHook('onRequest', (req, res, done) => {
795
795
  t.ok('called', 'onRequest')
796
- next()
796
+ done()
797
797
  })
798
- fastify.addHook('onResponse', (request, reply, next) => {
798
+ fastify.addHook('onResponse', (request, reply, done) => {
799
799
  t.ok('called', 'onResponse')
800
- next()
800
+ done()
801
801
  })
802
802
 
803
803
  t.tearDown(fastify.close.bind(fastify))
@@ -966,12 +966,12 @@ test('the default 404 handler can be invoked inside a prefixed plugin', t => {
966
966
 
967
967
  const fastify = Fastify()
968
968
 
969
- fastify.register(function (instance, opts, next) {
969
+ fastify.register(function (instance, opts, done) {
970
970
  instance.get('/path', function (request, reply) {
971
971
  reply.send(httpErrors.NotFound())
972
972
  })
973
973
 
974
- next()
974
+ done()
975
975
  }, { prefix: '/v1' })
976
976
 
977
977
  fastify.inject('/v1/path', (err, res) => {
@@ -994,12 +994,12 @@ test('an inherited custom 404 handler can be invoked inside a prefixed plugin',
994
994
  reply.code(404).send('custom handler')
995
995
  })
996
996
 
997
- fastify.register(function (instance, opts, next) {
997
+ fastify.register(function (instance, opts, done) {
998
998
  instance.get('/path', function (request, reply) {
999
999
  reply.send(httpErrors.NotFound())
1000
1000
  })
1001
1001
 
1002
- next()
1002
+ done()
1003
1003
  }, { prefix: '/v1' })
1004
1004
 
1005
1005
  fastify.inject('/v1/path', (err, res) => {
@@ -1018,23 +1018,23 @@ test('encapsulated custom 404 handler without a prefix is the handler for the en
1018
1018
 
1019
1019
  const fastify = Fastify()
1020
1020
 
1021
- fastify.register(function (instance, opts, next) {
1021
+ fastify.register(function (instance, opts, done) {
1022
1022
  instance.setNotFoundHandler(function (request, reply) {
1023
1023
  reply.code(404).send('custom handler')
1024
1024
  })
1025
1025
 
1026
- next()
1026
+ done()
1027
1027
  })
1028
1028
 
1029
- fastify.register(function (instance, opts, next) {
1030
- instance.register(function (instance2, opts, next) {
1029
+ fastify.register(function (instance, opts, done) {
1030
+ instance.register(function (instance2, opts, done) {
1031
1031
  instance2.setNotFoundHandler(function (request, reply) {
1032
1032
  reply.code(404).send('custom handler 2')
1033
1033
  })
1034
- next()
1034
+ done()
1035
1035
  })
1036
1036
 
1037
- next()
1037
+ done()
1038
1038
  }, { prefix: 'prefixed' })
1039
1039
 
1040
1040
  fastify.inject('/not-found', (err, res) => {
@@ -1079,12 +1079,12 @@ test('404 inside onSend', t => {
1079
1079
  reply.send({ hello: 'world' })
1080
1080
  })
1081
1081
 
1082
- fastify.addHook('onSend', function (request, reply, payload, next) {
1082
+ fastify.addHook('onSend', function (request, reply, payload, done) {
1083
1083
  if (!called) {
1084
1084
  called = true
1085
- next(new errors.NotFound())
1085
+ done(new errors.NotFound())
1086
1086
  } else {
1087
- next()
1087
+ done()
1088
1088
  }
1089
1089
  })
1090
1090
 
@@ -1174,9 +1174,9 @@ test('onSend hooks run when an encapsulated route invokes the notFound handler',
1174
1174
  const fastify = Fastify()
1175
1175
 
1176
1176
  fastify.register((instance, options, done) => {
1177
- instance.addHook('onSend', (request, reply, payload, next) => {
1177
+ instance.addHook('onSend', (request, reply, payload, done) => {
1178
1178
  t.pass('onSend hook called')
1179
- next()
1179
+ done()
1180
1180
  })
1181
1181
 
1182
1182
  instance.get('/', (request, reply) => {
@@ -1287,9 +1287,9 @@ test('preHandler option for setNotFoundHandler', t => {
1287
1287
  t.plan(2)
1288
1288
  const fastify = Fastify()
1289
1289
 
1290
- fastify.addHook('preHandler', (req, reply, next) => {
1290
+ fastify.addHook('preHandler', (req, reply, done) => {
1291
1291
  req.body.check = 'a'
1292
- next()
1292
+ done()
1293
1293
  })
1294
1294
 
1295
1295
  fastify.setNotFoundHandler({
@@ -1445,9 +1445,9 @@ test('preHandler option for setNotFoundHandler', t => {
1445
1445
  t.plan(4)
1446
1446
  const fastify = Fastify()
1447
1447
 
1448
- fastify.addHook('preHandler', (req, reply, next) => {
1448
+ fastify.addHook('preHandler', (req, reply, done) => {
1449
1449
  req.body.check = 'a'
1450
- next()
1450
+ done()
1451
1451
  })
1452
1452
 
1453
1453
  fastify.setNotFoundHandler({
@@ -1702,7 +1702,7 @@ test('Should fail to invoke callNotFound inside a 404 handler', t => {
1702
1702
  })
1703
1703
 
1704
1704
  test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
1705
- t.test('Dyamic route', t => {
1705
+ t.test('Dynamic route', t => {
1706
1706
  t.plan(3)
1707
1707
  const fastify = Fastify()
1708
1708
  fastify.get('/hello/:id', () => t.fail('we should not be here'))
package/test/500s.test.js CHANGED
@@ -67,7 +67,7 @@ test('encapsulated 500', t => {
67
67
  reply.send(new Error('kaboom'))
68
68
  })
69
69
 
70
- fastify.register(function (f, opts, next) {
70
+ fastify.register(function (f, opts, done) {
71
71
  f.get('/', function (req, reply) {
72
72
  reply.send(new Error('kaboom'))
73
73
  })
@@ -81,7 +81,7 @@ test('encapsulated 500', t => {
81
81
  .send('an error happened: ' + err.message)
82
82
  })
83
83
 
84
- next()
84
+ done()
85
85
  }, { prefix: 'test' })
86
86
 
87
87
  fastify.inject({
@@ -125,17 +125,17 @@ test('custom 500 with hooks', t => {
125
125
  .send('an error happened: ' + err.message)
126
126
  })
127
127
 
128
- fastify.addHook('onSend', (req, res, payload, next) => {
128
+ fastify.addHook('onSend', (req, res, payload, done) => {
129
129
  t.ok('called', 'onSend')
130
- next()
130
+ done()
131
131
  })
132
- fastify.addHook('onRequest', (req, res, next) => {
132
+ fastify.addHook('onRequest', (req, res, done) => {
133
133
  t.ok('called', 'onRequest')
134
- next()
134
+ done()
135
135
  })
136
- fastify.addHook('onResponse', (request, reply, next) => {
136
+ fastify.addHook('onResponse', (request, reply, done) => {
137
137
  t.ok('called', 'onResponse')
138
- next()
138
+ done()
139
139
  })
140
140
 
141
141
  fastify.inject({
@@ -378,7 +378,7 @@ test('async await plugin', async t => {
378
378
  }
379
379
  })
380
380
 
381
- test('does not call reply.send() twice if 204 reponse is already sent', t => {
381
+ test('does not call reply.send() twice if 204 response is already sent', t => {
382
382
  t.plan(2)
383
383
 
384
384
  const fastify = Fastify()
@@ -618,6 +618,34 @@ test('customErrorHandler support without throwing', t => {
618
618
  })
619
619
  })
620
620
 
621
+ // See https://github.com/fastify/fastify/issues/2653
622
+ test('customErrorHandler only called if reply not already sent', t => {
623
+ t.plan(3)
624
+
625
+ const fastify = Fastify()
626
+
627
+ fastify.get('/', async (req, reply) => {
628
+ await reply.send('success')
629
+ const error = new Error('ouch')
630
+ error.statusCode = 400
631
+ throw error
632
+ })
633
+
634
+ fastify.setErrorHandler(t.fail.bind(t, 'should not be called'))
635
+
636
+ fastify.inject({
637
+ method: 'GET',
638
+ url: '/'
639
+ }, (err, res) => {
640
+ t.error(err)
641
+ t.strictEqual(res.statusCode, 200)
642
+ t.deepEqual(
643
+ 'success',
644
+ res.payload
645
+ )
646
+ })
647
+ })
648
+
621
649
  test('await self', async t => {
622
650
  const app = Fastify()
623
651
  t.is(await app, app)