fastify 3.27.4 → 4.0.0-alpha.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.
- package/.taprc +3 -0
- package/README.md +7 -7
- package/build/build-error-serializer.js +27 -0
- package/build/build-validation.js +47 -35
- package/docs/Guides/Database.md +320 -0
- package/docs/Guides/Ecosystem.md +9 -0
- package/docs/Guides/Getting-Started.md +7 -7
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Serverless.md +3 -3
- package/docs/Guides/Testing.md +2 -2
- package/docs/Migration-Guide-V4.md +12 -0
- package/docs/Reference/ContentTypeParser.md +4 -0
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Encapsulation.md +2 -2
- package/docs/Reference/Errors.md +51 -6
- package/docs/Reference/HTTP2.md +3 -3
- package/docs/Reference/Hooks.md +4 -7
- package/docs/Reference/LTS.md +5 -4
- package/docs/Reference/Plugins.md +3 -3
- package/docs/Reference/Reply.md +73 -22
- package/docs/Reference/Request.md +1 -3
- package/docs/Reference/Routes.md +22 -15
- package/docs/Reference/Server.md +69 -119
- package/docs/Reference/TypeScript.md +20 -22
- package/docs/Reference/Validation-and-Serialization.md +30 -55
- package/docs/Type-Providers.md +257 -0
- package/examples/asyncawait.js +1 -1
- package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
- package/examples/benchmark/hooks-benchmark.js +1 -1
- package/examples/benchmark/simple.js +1 -1
- package/examples/hooks.js +2 -2
- package/examples/http2.js +1 -1
- package/examples/https.js +1 -1
- package/examples/parser.js +13 -3
- package/examples/route-prefix.js +1 -1
- package/examples/shared-schema.js +1 -1
- package/examples/simple-stream.js +18 -0
- package/examples/simple.js +1 -1
- package/examples/simple.mjs +1 -1
- package/examples/typescript-server.ts +1 -1
- package/examples/use-plugin.js +1 -1
- package/fastify.d.ts +34 -22
- package/fastify.js +40 -36
- package/lib/configValidator.js +902 -1023
- package/lib/contentTypeParser.js +6 -16
- package/lib/context.js +36 -10
- package/lib/decorate.js +3 -1
- package/lib/error-handler.js +158 -0
- package/lib/error-serializer.js +257 -0
- package/lib/errors.js +51 -9
- package/lib/fourOhFour.js +31 -20
- package/lib/handleRequest.js +10 -13
- package/lib/hooks.js +14 -9
- package/lib/pluginOverride.js +0 -3
- package/lib/pluginUtils.js +3 -2
- package/lib/reply.js +121 -175
- package/lib/request.js +13 -10
- package/lib/route.js +131 -138
- package/lib/schema-controller.js +2 -2
- package/lib/schemas.js +27 -1
- package/lib/server.js +242 -116
- package/lib/symbols.js +5 -3
- package/lib/validation.js +11 -9
- package/lib/warnings.js +4 -12
- package/lib/wrapThenable.js +4 -11
- package/package.json +37 -39
- package/test/404s.test.js +258 -125
- package/test/500s.test.js +3 -3
- package/test/als.test.js +1 -1
- package/test/async-await.test.js +20 -76
- package/test/bodyLimit.test.js +1 -1
- package/test/build-certificate.js +6 -7
- package/test/case-insensitive.test.js +4 -4
- package/test/close-pipelining.test.js +2 -2
- package/test/close.test.js +11 -11
- package/test/content-parser.test.js +32 -0
- package/test/context-config.test.js +52 -0
- package/test/custom-http-server.test.js +14 -7
- package/test/custom-parser-async.test.js +1 -66
- package/test/custom-parser.test.js +92 -159
- package/test/custom-querystring-parser.test.js +3 -3
- package/test/decorator.test.js +11 -13
- package/test/delete.test.js +6 -6
- package/test/encapsulated-error-handler.test.js +50 -0
- package/test/esm/index.test.js +0 -14
- package/test/fastify-instance.test.js +4 -4
- package/test/fluent-schema.test.js +4 -4
- package/test/genReqId.test.js +1 -1
- package/test/get.test.js +4 -4
- package/test/handler-context.test.js +2 -2
- package/test/head.test.js +1 -1
- package/test/helper.js +19 -4
- package/test/hooks-async.test.js +15 -48
- package/test/hooks.on-ready.test.js +10 -5
- package/test/hooks.test.js +78 -119
- package/test/http2/closing.test.js +10 -16
- package/test/http2/constraint.test.js +1 -1
- package/test/http2/head.test.js +1 -1
- package/test/http2/plain.test.js +1 -1
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/http2/secure.test.js +1 -1
- package/test/http2/unknown-http-method.test.js +4 -10
- package/test/https/custom-https-server.test.js +12 -6
- package/test/https/https.test.js +1 -1
- package/test/input-validation.js +3 -3
- package/test/internals/handleRequest.test.js +6 -43
- package/test/internals/initialConfig.test.js +41 -12
- package/test/internals/logger.test.js +2 -2
- package/test/internals/reply.test.js +317 -48
- package/test/internals/request.test.js +13 -7
- package/test/internals/server.test.js +88 -0
- package/test/listen.deprecated.test.js +202 -0
- package/test/listen.test.js +140 -145
- package/test/logger.test.js +82 -42
- package/test/maxRequestsPerSocket.test.js +8 -6
- package/test/middleware.test.js +2 -25
- package/test/nullable-validation.test.js +53 -16
- package/test/output-validation.test.js +1 -1
- package/test/plugin.test.js +47 -21
- package/test/pretty-print.test.js +22 -10
- package/test/promises.test.js +1 -1
- package/test/proto-poisoning.test.js +6 -6
- package/test/register.test.js +3 -3
- package/test/reply-error.test.js +126 -15
- package/test/reply-trailers.test.js +270 -0
- package/test/request-error.test.js +3 -6
- package/test/route-hooks.test.js +18 -18
- package/test/route-prefix.test.js +2 -1
- package/test/route.test.js +206 -22
- package/test/router-options.test.js +2 -2
- package/test/schema-examples.test.js +11 -5
- package/test/schema-feature.test.js +25 -20
- package/test/schema-serialization.test.js +9 -9
- package/test/schema-special-usage.test.js +5 -153
- package/test/schema-validation.test.js +9 -9
- package/test/skip-reply-send.test.js +2 -2
- package/test/stream.test.js +82 -23
- package/test/throw.test.js +8 -5
- package/test/trust-proxy.test.js +6 -6
- package/test/type-provider.test.js +20 -0
- package/test/types/fastify.test-d.ts +10 -18
- package/test/types/hooks.test-d.ts +61 -5
- package/test/types/import.js +2 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +68 -17
- package/test/types/logger.test-d.ts +44 -15
- package/test/types/reply.test-d.ts +2 -1
- package/test/types/request.test-d.ts +71 -1
- package/test/types/route.test-d.ts +8 -2
- package/test/types/schema.test-d.ts +2 -39
- package/test/types/type-provider.test-d.ts +424 -0
- package/test/url-rewriting.test.js +3 -3
- package/test/validation-error-handling.test.js +8 -8
- package/test/versioned-routes.test.js +30 -18
- package/test/wrapThenable.test.js +7 -6
- package/types/content-type-parser.d.ts +17 -8
- package/types/hooks.d.ts +182 -85
- package/types/instance.d.ts +286 -118
- package/types/logger.d.ts +18 -104
- package/types/plugin.d.ts +10 -4
- package/types/reply.d.ts +18 -12
- package/types/request.d.ts +13 -8
- package/types/route.d.ts +62 -34
- package/types/schema.d.ts +1 -1
- package/types/type-provider.d.ts +99 -0
- package/types/utils.d.ts +1 -1
- package/lib/schema-compilers.js +0 -12
- package/test/emit-warning.test.js +0 -166
package/test/hooks.test.js
CHANGED
|
@@ -9,14 +9,22 @@ const fp = require('fastify-plugin')
|
|
|
9
9
|
const fs = require('fs')
|
|
10
10
|
const split = require('split2')
|
|
11
11
|
const symbols = require('../lib/symbols.js')
|
|
12
|
-
const warning = require('../lib/warnings')
|
|
13
12
|
const payload = { hello: 'world' }
|
|
14
13
|
|
|
15
14
|
process.removeAllListeners('warning')
|
|
16
15
|
|
|
16
|
+
function getUrl (app) {
|
|
17
|
+
const { address, port } = app.server.address()
|
|
18
|
+
if (address === '::1') {
|
|
19
|
+
return `http://[${address}]:${port}`
|
|
20
|
+
} else {
|
|
21
|
+
return `http://${address}:${port}`
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
17
25
|
test('hooks', t => {
|
|
18
26
|
t.plan(43)
|
|
19
|
-
const fastify = Fastify()
|
|
27
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
20
28
|
|
|
21
29
|
try {
|
|
22
30
|
fastify.addHook('preHandler', function (request, reply, done) {
|
|
@@ -46,7 +54,7 @@ test('hooks', t => {
|
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
try {
|
|
49
|
-
fastify.addHook('preParsing', function (request, reply, done) {
|
|
57
|
+
fastify.addHook('preParsing', function (request, reply, payload, done) {
|
|
50
58
|
request.preParsing = true
|
|
51
59
|
t.equal(request.test, 'the request is coming')
|
|
52
60
|
t.equal(reply.test, 'the reply has come')
|
|
@@ -130,7 +138,7 @@ test('hooks', t => {
|
|
|
130
138
|
reply.code(200).send(payload)
|
|
131
139
|
})
|
|
132
140
|
|
|
133
|
-
fastify.listen(0, err => {
|
|
141
|
+
fastify.listen({ port: 0 }, err => {
|
|
134
142
|
t.error(err)
|
|
135
143
|
fastify.server.unref()
|
|
136
144
|
|
|
@@ -253,7 +261,7 @@ test('onRequest hook should support encapsulation / 3', t => {
|
|
|
253
261
|
done()
|
|
254
262
|
})
|
|
255
263
|
|
|
256
|
-
fastify.listen(0, err => {
|
|
264
|
+
fastify.listen({ port: 0 }, err => {
|
|
257
265
|
t.error(err)
|
|
258
266
|
fastify.server.unref()
|
|
259
267
|
|
|
@@ -314,7 +322,7 @@ test('preHandler hook should support encapsulation / 5', t => {
|
|
|
314
322
|
done()
|
|
315
323
|
})
|
|
316
324
|
|
|
317
|
-
fastify.listen(0, err => {
|
|
325
|
+
fastify.listen({ port: 0 }, err => {
|
|
318
326
|
t.error(err)
|
|
319
327
|
fastify.server.unref()
|
|
320
328
|
|
|
@@ -342,7 +350,7 @@ test('preHandler hook should support encapsulation / 5', t => {
|
|
|
342
350
|
|
|
343
351
|
test('onRoute hook should be called / 1', t => {
|
|
344
352
|
t.plan(2)
|
|
345
|
-
const fastify = Fastify()
|
|
353
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
346
354
|
|
|
347
355
|
fastify.register((instance, opts, done) => {
|
|
348
356
|
instance.addHook('onRoute', () => {
|
|
@@ -363,7 +371,7 @@ test('onRoute hook should be called / 2', t => {
|
|
|
363
371
|
t.plan(5)
|
|
364
372
|
let firstHandler = 0
|
|
365
373
|
let secondHandler = 0
|
|
366
|
-
const fastify = Fastify()
|
|
374
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
367
375
|
fastify.addHook('onRoute', (route) => {
|
|
368
376
|
t.pass()
|
|
369
377
|
firstHandler++
|
|
@@ -391,7 +399,7 @@ test('onRoute hook should be called / 2', t => {
|
|
|
391
399
|
|
|
392
400
|
test('onRoute hook should be called / 3', t => {
|
|
393
401
|
t.plan(5)
|
|
394
|
-
const fastify = Fastify()
|
|
402
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
395
403
|
|
|
396
404
|
function handler (req, reply) {
|
|
397
405
|
reply.send()
|
|
@@ -423,7 +431,7 @@ test('onRoute hook should be called / 3', t => {
|
|
|
423
431
|
|
|
424
432
|
test('onRoute hook should be called (encapsulation support) / 4', t => {
|
|
425
433
|
t.plan(4)
|
|
426
|
-
const fastify = Fastify()
|
|
434
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
427
435
|
|
|
428
436
|
fastify.addHook('onRoute', () => {
|
|
429
437
|
t.pass()
|
|
@@ -450,7 +458,7 @@ test('onRoute hook should be called (encapsulation support) / 4', t => {
|
|
|
450
458
|
|
|
451
459
|
test('onRoute hook should be called (encapsulation support) / 5', t => {
|
|
452
460
|
t.plan(2)
|
|
453
|
-
const fastify = Fastify()
|
|
461
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
454
462
|
|
|
455
463
|
fastify.get('/first', function (req, reply) {
|
|
456
464
|
reply.send()
|
|
@@ -477,7 +485,7 @@ test('onRoute hook should be called (encapsulation support) / 5', t => {
|
|
|
477
485
|
|
|
478
486
|
test('onRoute hook should be called (encapsulation support) / 6', t => {
|
|
479
487
|
t.plan(1)
|
|
480
|
-
const fastify = Fastify()
|
|
488
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
481
489
|
|
|
482
490
|
fastify.get('/first', function (req, reply) {
|
|
483
491
|
reply.send()
|
|
@@ -494,7 +502,7 @@ test('onRoute hook should be called (encapsulation support) / 6', t => {
|
|
|
494
502
|
|
|
495
503
|
test('onRoute should keep the context', t => {
|
|
496
504
|
t.plan(4)
|
|
497
|
-
const fastify = Fastify()
|
|
505
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
498
506
|
fastify.register((instance, opts, done) => {
|
|
499
507
|
instance.decorate('test', true)
|
|
500
508
|
instance.addHook('onRoute', onRoute)
|
|
@@ -519,7 +527,7 @@ test('onRoute should keep the context', t => {
|
|
|
519
527
|
|
|
520
528
|
test('onRoute hook should pass correct route', t => {
|
|
521
529
|
t.plan(9)
|
|
522
|
-
const fastify = Fastify()
|
|
530
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
523
531
|
fastify.addHook('onRoute', (route) => {
|
|
524
532
|
t.equal(route.method, 'GET')
|
|
525
533
|
t.equal(route.url, '/')
|
|
@@ -547,7 +555,7 @@ test('onRoute hook should pass correct route', t => {
|
|
|
547
555
|
|
|
548
556
|
test('onRoute hook should pass correct route with custom prefix', t => {
|
|
549
557
|
t.plan(11)
|
|
550
|
-
const fastify = Fastify()
|
|
558
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
551
559
|
fastify.addHook('onRoute', function (route) {
|
|
552
560
|
t.equal(route.method, 'GET')
|
|
553
561
|
t.equal(route.url, '/v1/foo')
|
|
@@ -577,7 +585,7 @@ test('onRoute hook should pass correct route with custom prefix', t => {
|
|
|
577
585
|
|
|
578
586
|
test('onRoute hook should pass correct route with custom options', t => {
|
|
579
587
|
t.plan(6)
|
|
580
|
-
const fastify = Fastify()
|
|
588
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
581
589
|
fastify.register((instance, opts, done) => {
|
|
582
590
|
instance.addHook('onRoute', function (route) {
|
|
583
591
|
t.equal(route.method, 'GET')
|
|
@@ -605,7 +613,7 @@ test('onRoute hook should pass correct route with custom options', t => {
|
|
|
605
613
|
|
|
606
614
|
test('onRoute hook should receive any route option', t => {
|
|
607
615
|
t.plan(5)
|
|
608
|
-
const fastify = Fastify()
|
|
616
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
609
617
|
fastify.register((instance, opts, done) => {
|
|
610
618
|
instance.addHook('onRoute', function (route) {
|
|
611
619
|
t.equal(route.method, 'GET')
|
|
@@ -626,7 +634,7 @@ test('onRoute hook should receive any route option', t => {
|
|
|
626
634
|
|
|
627
635
|
test('onRoute hook should preserve system route configuration', t => {
|
|
628
636
|
t.plan(5)
|
|
629
|
-
const fastify = Fastify()
|
|
637
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
630
638
|
fastify.register((instance, opts, done) => {
|
|
631
639
|
instance.addHook('onRoute', function (route) {
|
|
632
640
|
t.equal(route.method, 'GET')
|
|
@@ -650,7 +658,7 @@ test('onRoute hook should preserve handler function in options of shorthand rout
|
|
|
650
658
|
|
|
651
659
|
const handler = (req, reply) => {}
|
|
652
660
|
|
|
653
|
-
const fastify = Fastify()
|
|
661
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
654
662
|
fastify.register((instance, opts, done) => {
|
|
655
663
|
instance.addHook('onRoute', function (route) {
|
|
656
664
|
t.equal(route.handler, handler)
|
|
@@ -671,7 +679,7 @@ test('onRoute hook should be called once when prefixTrailingSlash', t => {
|
|
|
671
679
|
let onRouteCalled = 0
|
|
672
680
|
let routePatched = 0
|
|
673
681
|
|
|
674
|
-
const fastify = Fastify({ ignoreTrailingSlash: false })
|
|
682
|
+
const fastify = Fastify({ ignoreTrailingSlash: false, exposeHeadRoutes: false })
|
|
675
683
|
|
|
676
684
|
// a plugin that patches route options, similar to fastify-compress
|
|
677
685
|
fastify.register(fp(function myPlugin (instance, opts, next) {
|
|
@@ -710,58 +718,64 @@ test('onRoute hook should be called once when prefixTrailingSlash', t => {
|
|
|
710
718
|
test('onRoute hook should able to change the route url', t => {
|
|
711
719
|
t.plan(5)
|
|
712
720
|
|
|
713
|
-
const fastify = Fastify()
|
|
721
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
714
722
|
|
|
715
723
|
fastify.register((instance, opts, done) => {
|
|
716
724
|
instance.addHook('onRoute', (route) => {
|
|
717
|
-
t.equal(route.url, '/
|
|
725
|
+
t.equal(route.url, '/foo')
|
|
718
726
|
route.url = encodeURI(route.url)
|
|
719
727
|
})
|
|
720
728
|
|
|
721
|
-
instance.get('/
|
|
722
|
-
reply.send('here /
|
|
729
|
+
instance.get('/foo', (request, reply) => {
|
|
730
|
+
reply.send('here /foo')
|
|
723
731
|
})
|
|
724
732
|
|
|
725
733
|
done()
|
|
726
734
|
})
|
|
727
735
|
|
|
728
|
-
fastify.listen(0, err => {
|
|
736
|
+
fastify.listen({ port: 0 }, err => {
|
|
729
737
|
t.error(err)
|
|
730
738
|
fastify.server.unref()
|
|
731
739
|
|
|
732
740
|
sget({
|
|
733
741
|
method: 'GET',
|
|
734
|
-
url:
|
|
742
|
+
url: getUrl(fastify) + encodeURI('/foo')
|
|
735
743
|
}, (err, response, body) => {
|
|
736
744
|
t.error(err)
|
|
737
745
|
t.equal(response.statusCode, 200)
|
|
738
|
-
t.equal(body.toString(), 'here /
|
|
746
|
+
t.equal(body.toString(), 'here /foo')
|
|
739
747
|
})
|
|
740
748
|
})
|
|
741
749
|
})
|
|
742
750
|
|
|
743
|
-
test('onRoute hook that throws should be caught
|
|
751
|
+
test('onRoute hook that throws should be caught', t => {
|
|
744
752
|
t.plan(1)
|
|
745
|
-
const fastify = Fastify()
|
|
753
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
746
754
|
|
|
747
755
|
fastify.register((instance, opts, done) => {
|
|
748
756
|
instance.addHook('onRoute', () => {
|
|
749
757
|
throw new Error('snap')
|
|
750
758
|
})
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
759
|
+
|
|
760
|
+
try {
|
|
761
|
+
instance.get('/', opts, function (req, reply) {
|
|
762
|
+
reply.send()
|
|
763
|
+
})
|
|
764
|
+
|
|
765
|
+
t.fail('onRoute should throw sync if error')
|
|
766
|
+
} catch (error) {
|
|
767
|
+
t.ok(error)
|
|
768
|
+
}
|
|
769
|
+
|
|
754
770
|
done()
|
|
755
771
|
})
|
|
756
772
|
|
|
757
|
-
fastify.ready(
|
|
758
|
-
t.ok(err)
|
|
759
|
-
})
|
|
773
|
+
fastify.ready()
|
|
760
774
|
})
|
|
761
775
|
|
|
762
776
|
test('onRoute hook with many prefix', t => {
|
|
763
777
|
t.plan(3)
|
|
764
|
-
const fastify = Fastify()
|
|
778
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
765
779
|
const handler = (req, reply) => { reply.send({}) }
|
|
766
780
|
|
|
767
781
|
const onRouteChecks = [
|
|
@@ -904,7 +918,7 @@ test('onResponse hook should support encapsulation / 3', t => {
|
|
|
904
918
|
done()
|
|
905
919
|
})
|
|
906
920
|
|
|
907
|
-
fastify.listen(0, err => {
|
|
921
|
+
fastify.listen({ port: 0 }, err => {
|
|
908
922
|
t.error(err)
|
|
909
923
|
fastify.server.unref()
|
|
910
924
|
|
|
@@ -981,7 +995,7 @@ test('onSend hook should support encapsulation / 2', t => {
|
|
|
981
995
|
done()
|
|
982
996
|
})
|
|
983
997
|
|
|
984
|
-
fastify.listen(0, err => {
|
|
998
|
+
fastify.listen({ port: 0 }, err => {
|
|
985
999
|
t.error(err)
|
|
986
1000
|
fastify.server.unref()
|
|
987
1001
|
|
|
@@ -1073,6 +1087,7 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1073
1087
|
})
|
|
1074
1088
|
|
|
1075
1089
|
instance.get('/stream', (request, reply) => {
|
|
1090
|
+
reply.header('content-type', 'application/octet-stream')
|
|
1076
1091
|
reply.send(thePayload)
|
|
1077
1092
|
})
|
|
1078
1093
|
|
|
@@ -1244,7 +1259,7 @@ test('onSend hook throws', t => {
|
|
|
1244
1259
|
reply.send({ hello: 'world' })
|
|
1245
1260
|
})
|
|
1246
1261
|
|
|
1247
|
-
fastify.listen(0, err => {
|
|
1262
|
+
fastify.listen({ port: 0 }, err => {
|
|
1248
1263
|
t.error(err)
|
|
1249
1264
|
fastify.server.unref()
|
|
1250
1265
|
sget({
|
|
@@ -1366,7 +1381,7 @@ test('cannot add hook after binding', t => {
|
|
|
1366
1381
|
reply.send({ hello: 'world' })
|
|
1367
1382
|
})
|
|
1368
1383
|
|
|
1369
|
-
instance.listen(0, err => {
|
|
1384
|
+
instance.listen({ port: 0 }, err => {
|
|
1370
1385
|
t.error(err)
|
|
1371
1386
|
t.teardown(instance.server.close.bind(instance.server))
|
|
1372
1387
|
|
|
@@ -2213,7 +2228,7 @@ test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
|
2213
2228
|
const fastify = Fastify()
|
|
2214
2229
|
|
|
2215
2230
|
fastify.addHook('onRequest', function (request, reply, done) {
|
|
2216
|
-
t.same(request.body,
|
|
2231
|
+
t.same(request.body, undefined)
|
|
2217
2232
|
t.same(request.query, { key: 'value' })
|
|
2218
2233
|
t.same(request.params, { greeting: 'hello' })
|
|
2219
2234
|
t.same(request.headers, {
|
|
@@ -2227,7 +2242,7 @@ test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
|
2227
2242
|
})
|
|
2228
2243
|
|
|
2229
2244
|
fastify.addHook('preParsing', function (request, reply, payload, done) {
|
|
2230
|
-
t.same(request.body,
|
|
2245
|
+
t.same(request.body, undefined)
|
|
2231
2246
|
t.same(request.query, { key: 'value' })
|
|
2232
2247
|
t.same(request.params, { greeting: 'hello' })
|
|
2233
2248
|
t.same(request.headers, {
|
|
@@ -2374,7 +2389,7 @@ test('preValidation hook should support encapsulation / 3', t => {
|
|
|
2374
2389
|
done()
|
|
2375
2390
|
})
|
|
2376
2391
|
|
|
2377
|
-
fastify.listen(0, err => {
|
|
2392
|
+
fastify.listen({ port: 0 }, err => {
|
|
2378
2393
|
t.error(err)
|
|
2379
2394
|
fastify.server.unref()
|
|
2380
2395
|
|
|
@@ -2469,19 +2484,20 @@ test('onError hook with setErrorHandler', t => {
|
|
|
2469
2484
|
|
|
2470
2485
|
const fastify = Fastify()
|
|
2471
2486
|
|
|
2472
|
-
const
|
|
2487
|
+
const external = new Error('ouch')
|
|
2488
|
+
const internal = new Error('kaboom')
|
|
2473
2489
|
|
|
2474
2490
|
fastify.setErrorHandler((_, req, reply) => {
|
|
2475
|
-
reply.send(
|
|
2491
|
+
reply.send(external)
|
|
2476
2492
|
})
|
|
2477
2493
|
|
|
2478
2494
|
fastify.addHook('onError', (request, reply, error, done) => {
|
|
2479
|
-
t.match(error,
|
|
2495
|
+
t.match(error, internal)
|
|
2480
2496
|
done()
|
|
2481
2497
|
})
|
|
2482
2498
|
|
|
2483
2499
|
fastify.get('/', (req, reply) => {
|
|
2484
|
-
reply.send(
|
|
2500
|
+
reply.send(internal)
|
|
2485
2501
|
})
|
|
2486
2502
|
|
|
2487
2503
|
fastify.inject({
|
|
@@ -2497,34 +2513,6 @@ test('onError hook with setErrorHandler', t => {
|
|
|
2497
2513
|
})
|
|
2498
2514
|
})
|
|
2499
2515
|
|
|
2500
|
-
t.test('Hide error', t => {
|
|
2501
|
-
t.plan(2)
|
|
2502
|
-
|
|
2503
|
-
const fastify = Fastify()
|
|
2504
|
-
|
|
2505
|
-
fastify.setErrorHandler((_, req, reply) => {
|
|
2506
|
-
reply.send({ hello: 'world' })
|
|
2507
|
-
})
|
|
2508
|
-
|
|
2509
|
-
fastify.addHook('onError', (request, reply, error, done) => {
|
|
2510
|
-
t.fail('Should not be called')
|
|
2511
|
-
})
|
|
2512
|
-
|
|
2513
|
-
fastify.get('/', (req, reply) => {
|
|
2514
|
-
reply.send(new Error('kaboom'))
|
|
2515
|
-
})
|
|
2516
|
-
|
|
2517
|
-
fastify.inject({
|
|
2518
|
-
method: 'GET',
|
|
2519
|
-
url: '/'
|
|
2520
|
-
}, (err, res) => {
|
|
2521
|
-
t.error(err)
|
|
2522
|
-
t.same(
|
|
2523
|
-
JSON.parse(res.payload),
|
|
2524
|
-
{ hello: 'world' }
|
|
2525
|
-
)
|
|
2526
|
-
})
|
|
2527
|
-
})
|
|
2528
2516
|
t.end()
|
|
2529
2517
|
})
|
|
2530
2518
|
|
|
@@ -2548,7 +2536,7 @@ test('preParsing hook should run before parsing and be able to modify the payloa
|
|
|
2548
2536
|
}
|
|
2549
2537
|
})
|
|
2550
2538
|
|
|
2551
|
-
fastify.listen(0, err => {
|
|
2539
|
+
fastify.listen({ port: 0 }, err => {
|
|
2552
2540
|
t.error(err)
|
|
2553
2541
|
fastify.server.unref()
|
|
2554
2542
|
|
|
@@ -2566,37 +2554,6 @@ test('preParsing hook should run before parsing and be able to modify the payloa
|
|
|
2566
2554
|
})
|
|
2567
2555
|
})
|
|
2568
2556
|
|
|
2569
|
-
test('preParsing hooks can completely ignore the payload - deprecated syntax', t => {
|
|
2570
|
-
t.plan(5)
|
|
2571
|
-
const fastify = Fastify()
|
|
2572
|
-
|
|
2573
|
-
process.on('warning', onWarning)
|
|
2574
|
-
warning.emitted.delete('FSTDEP004')
|
|
2575
|
-
|
|
2576
|
-
function onWarning (warning) {
|
|
2577
|
-
t.equal(warning.name, 'FastifyDeprecation')
|
|
2578
|
-
t.equal(warning.code, 'FSTDEP004')
|
|
2579
|
-
}
|
|
2580
|
-
|
|
2581
|
-
fastify.addHook('preParsing', (req, reply, done) => {
|
|
2582
|
-
done()
|
|
2583
|
-
})
|
|
2584
|
-
|
|
2585
|
-
fastify.post('/', function (request, reply) {
|
|
2586
|
-
reply.send(request.body)
|
|
2587
|
-
})
|
|
2588
|
-
|
|
2589
|
-
fastify.inject({
|
|
2590
|
-
method: 'POST',
|
|
2591
|
-
url: '/',
|
|
2592
|
-
payload: { hello: 'world' }
|
|
2593
|
-
}, (err, res) => {
|
|
2594
|
-
t.error(err)
|
|
2595
|
-
t.equal(res.statusCode, 200)
|
|
2596
|
-
t.same(JSON.parse(res.payload), { hello: 'world' })
|
|
2597
|
-
})
|
|
2598
|
-
})
|
|
2599
|
-
|
|
2600
2557
|
test('preParsing hooks should run in the order in which they are defined', t => {
|
|
2601
2558
|
t.plan(5)
|
|
2602
2559
|
const fastify = Fastify()
|
|
@@ -2622,7 +2579,7 @@ test('preParsing hooks should run in the order in which they are defined', t =>
|
|
|
2622
2579
|
}
|
|
2623
2580
|
})
|
|
2624
2581
|
|
|
2625
|
-
fastify.listen(0, err => {
|
|
2582
|
+
fastify.listen({ port: 0 }, err => {
|
|
2626
2583
|
t.error(err)
|
|
2627
2584
|
fastify.server.unref()
|
|
2628
2585
|
|
|
@@ -2672,7 +2629,7 @@ test('preParsing hooks should support encapsulation', t => {
|
|
|
2672
2629
|
done()
|
|
2673
2630
|
})
|
|
2674
2631
|
|
|
2675
|
-
fastify.listen(0, err => {
|
|
2632
|
+
fastify.listen({ port: 0 }, err => {
|
|
2676
2633
|
t.error(err)
|
|
2677
2634
|
fastify.server.unref()
|
|
2678
2635
|
|
|
@@ -2793,7 +2750,7 @@ test('preParsing hook should support encapsulation / 3', t => {
|
|
|
2793
2750
|
done()
|
|
2794
2751
|
})
|
|
2795
2752
|
|
|
2796
|
-
fastify.listen(0, err => {
|
|
2753
|
+
fastify.listen({ port: 0 }, err => {
|
|
2797
2754
|
t.error(err)
|
|
2798
2755
|
fastify.server.unref()
|
|
2799
2756
|
|
|
@@ -2855,7 +2812,7 @@ test('preSerialization hook should run before serialization and be able to modif
|
|
|
2855
2812
|
}
|
|
2856
2813
|
})
|
|
2857
2814
|
|
|
2858
|
-
fastify.listen(0, err => {
|
|
2815
|
+
fastify.listen({ port: 0 }, err => {
|
|
2859
2816
|
t.error(err)
|
|
2860
2817
|
fastify.server.unref()
|
|
2861
2818
|
|
|
@@ -2906,7 +2863,7 @@ test('preSerialization hook should be able to throw errors which are validated a
|
|
|
2906
2863
|
}
|
|
2907
2864
|
})
|
|
2908
2865
|
|
|
2909
|
-
fastify.listen(0, err => {
|
|
2866
|
+
fastify.listen({ port: 0 }, err => {
|
|
2910
2867
|
t.error(err)
|
|
2911
2868
|
fastify.server.unref()
|
|
2912
2869
|
|
|
@@ -2940,7 +2897,7 @@ test('preSerialization hook which returned error should still run onError hooks'
|
|
|
2940
2897
|
reply.send({ hello: 'world' })
|
|
2941
2898
|
})
|
|
2942
2899
|
|
|
2943
|
-
fastify.listen(0, err => {
|
|
2900
|
+
fastify.listen({ port: 0 }, err => {
|
|
2944
2901
|
t.error(err)
|
|
2945
2902
|
fastify.server.unref()
|
|
2946
2903
|
|
|
@@ -2974,7 +2931,7 @@ test('preSerialization hooks should run in the order in which they are defined',
|
|
|
2974
2931
|
reply.send(payload)
|
|
2975
2932
|
})
|
|
2976
2933
|
|
|
2977
|
-
fastify.listen(0, err => {
|
|
2934
|
+
fastify.listen({ port: 0 }, err => {
|
|
2978
2935
|
t.error(err)
|
|
2979
2936
|
fastify.server.unref()
|
|
2980
2937
|
|
|
@@ -3018,7 +2975,7 @@ test('preSerialization hooks should support encapsulation', t => {
|
|
|
3018
2975
|
done()
|
|
3019
2976
|
})
|
|
3020
2977
|
|
|
3021
|
-
fastify.listen(0, err => {
|
|
2978
|
+
fastify.listen({ port: 0 }, err => {
|
|
3022
2979
|
t.error(err)
|
|
3023
2980
|
fastify.server.unref()
|
|
3024
2981
|
|
|
@@ -3198,13 +3155,14 @@ test('onTimeout should be triggered', t => {
|
|
|
3198
3155
|
})
|
|
3199
3156
|
|
|
3200
3157
|
fastify.get('/', async (req, reply) => {
|
|
3201
|
-
reply.send({ hello: 'world' })
|
|
3158
|
+
await reply.send({ hello: 'world' })
|
|
3202
3159
|
})
|
|
3203
3160
|
|
|
3204
3161
|
fastify.get('/timeout', async (req, reply) => {
|
|
3162
|
+
return reply
|
|
3205
3163
|
})
|
|
3206
3164
|
|
|
3207
|
-
fastify.listen(0, (err, address) => {
|
|
3165
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
3208
3166
|
t.error(err)
|
|
3209
3167
|
t.teardown(() => fastify.close())
|
|
3210
3168
|
|
|
@@ -3236,13 +3194,14 @@ test('onTimeout should be triggered and socket _meta is set', t => {
|
|
|
3236
3194
|
|
|
3237
3195
|
fastify.get('/', async (req, reply) => {
|
|
3238
3196
|
req.raw.socket._meta = {}
|
|
3239
|
-
reply.send({ hello: 'world' })
|
|
3197
|
+
return reply.send({ hello: 'world' })
|
|
3240
3198
|
})
|
|
3241
3199
|
|
|
3242
3200
|
fastify.get('/timeout', async (req, reply) => {
|
|
3201
|
+
return reply
|
|
3243
3202
|
})
|
|
3244
3203
|
|
|
3245
|
-
fastify.listen(0, (err, address) => {
|
|
3204
|
+
fastify.listen({ port: 0 }, (err, address) => {
|
|
3246
3205
|
t.error(err)
|
|
3247
3206
|
t.teardown(() => fastify.close())
|
|
3248
3207
|
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
const t = require('tap')
|
|
4
4
|
const Fastify = require('../..')
|
|
5
5
|
const http2 = require('http2')
|
|
6
|
-
const semver = require('semver')
|
|
7
6
|
const { promisify } = require('util')
|
|
8
7
|
const connect = promisify(http2.connect)
|
|
9
8
|
const { once } = require('events')
|
|
@@ -33,12 +32,11 @@ t.test('http/2 request while fastify closing', t => {
|
|
|
33
32
|
|
|
34
33
|
fastify.get('/', () => Promise.resolve({}))
|
|
35
34
|
|
|
36
|
-
fastify.listen(0, err => {
|
|
35
|
+
fastify.listen({ port: 0 }, err => {
|
|
37
36
|
t.error(err)
|
|
38
37
|
fastify.server.unref()
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
t.test('return 200', { skip: semver.lt(process.versions.node, '10.15.0') }, t => {
|
|
39
|
+
t.test('return 200', t => {
|
|
42
40
|
const url = getUrl(fastify)
|
|
43
41
|
const session = http2.connect(url, function () {
|
|
44
42
|
this.request({
|
|
@@ -81,12 +79,11 @@ t.test('http/2 request while fastify closing - return503OnClosing: false', t =>
|
|
|
81
79
|
|
|
82
80
|
fastify.get('/', () => Promise.resolve({}))
|
|
83
81
|
|
|
84
|
-
fastify.listen(0, err => {
|
|
82
|
+
fastify.listen({ port: 0 }, err => {
|
|
85
83
|
t.error(err)
|
|
86
84
|
fastify.server.unref()
|
|
87
85
|
|
|
88
|
-
|
|
89
|
-
t.test('return 200', { skip: semver.lt(process.versions.node, '10.15.0') }, t => {
|
|
86
|
+
t.test('return 200', t => {
|
|
90
87
|
const url = getUrl(fastify)
|
|
91
88
|
const session = http2.connect(url, function () {
|
|
92
89
|
this.request({
|
|
@@ -115,14 +112,13 @@ t.test('http/2 request while fastify closing - return503OnClosing: false', t =>
|
|
|
115
112
|
})
|
|
116
113
|
})
|
|
117
114
|
|
|
118
|
-
|
|
119
|
-
t.test('http/2 closes successfully with async await', { skip: semver.lt(process.versions.node, '10.15.0') }, async t => {
|
|
115
|
+
t.test('http/2 closes successfully with async await', async t => {
|
|
120
116
|
const fastify = Fastify({
|
|
121
117
|
http2SessionTimeout: 100,
|
|
122
118
|
http2: true
|
|
123
119
|
})
|
|
124
120
|
|
|
125
|
-
await fastify.listen(0)
|
|
121
|
+
await fastify.listen({ port: 0 })
|
|
126
122
|
|
|
127
123
|
const url = getUrl(fastify)
|
|
128
124
|
const session = await connect(url)
|
|
@@ -131,8 +127,7 @@ t.test('http/2 closes successfully with async await', { skip: semver.lt(process.
|
|
|
131
127
|
await fastify.close()
|
|
132
128
|
})
|
|
133
129
|
|
|
134
|
-
|
|
135
|
-
t.test('https/2 closes successfully with async await', { skip: semver.lt(process.versions.node, '10.15.0') }, async t => {
|
|
130
|
+
t.test('https/2 closes successfully with async await', async t => {
|
|
136
131
|
const fastify = Fastify({
|
|
137
132
|
http2SessionTimeout: 100,
|
|
138
133
|
http2: true,
|
|
@@ -142,7 +137,7 @@ t.test('https/2 closes successfully with async await', { skip: semver.lt(process
|
|
|
142
137
|
}
|
|
143
138
|
})
|
|
144
139
|
|
|
145
|
-
await fastify.listen(0)
|
|
140
|
+
await fastify.listen({ port: 0 })
|
|
146
141
|
|
|
147
142
|
const url = getUrl(fastify)
|
|
148
143
|
const session = await connect(url)
|
|
@@ -151,8 +146,7 @@ t.test('https/2 closes successfully with async await', { skip: semver.lt(process
|
|
|
151
146
|
await fastify.close()
|
|
152
147
|
})
|
|
153
148
|
|
|
154
|
-
|
|
155
|
-
t.test('http/2 server side session emits a timeout event', { skip: semver.lt(process.versions.node, '10.15.0') }, async t => {
|
|
149
|
+
t.test('http/2 server side session emits a timeout event', async t => {
|
|
156
150
|
let _resolve
|
|
157
151
|
const p = new Promise((resolve) => { _resolve = resolve })
|
|
158
152
|
|
|
@@ -166,7 +160,7 @@ t.test('http/2 server side session emits a timeout event', { skip: semver.lt(pro
|
|
|
166
160
|
return {}
|
|
167
161
|
})
|
|
168
162
|
|
|
169
|
-
await fastify.listen(0)
|
|
163
|
+
await fastify.listen({ port: 0 })
|
|
170
164
|
|
|
171
165
|
const url = getUrl(fastify)
|
|
172
166
|
const session = await connect(url)
|
package/test/http2/head.test.js
CHANGED
package/test/http2/plain.test.js
CHANGED