fastify 3.26.0 → 4.0.0-alpha.1
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/README.md +5 -4
- package/build/build-error-serializer.js +27 -0
- package/build/build-validation.js +49 -35
- package/docs/Guides/Ecosystem.md +2 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Migration-Guide-V4.md +12 -0
- package/docs/Reference/ContentTypeParser.md +8 -1
- package/docs/Reference/Errors.md +51 -6
- package/docs/Reference/Hooks.md +4 -7
- package/docs/Reference/LTS.md +5 -4
- package/docs/Reference/Reply.md +23 -22
- package/docs/Reference/Request.md +1 -3
- package/docs/Reference/Routes.md +17 -10
- package/docs/Reference/Server.md +98 -63
- package/docs/Reference/TypeScript.md +11 -13
- package/docs/Reference/Validation-and-Serialization.md +32 -54
- package/docs/Type-Providers.md +257 -0
- package/examples/hooks.js +1 -1
- package/examples/simple-stream.js +18 -0
- package/fastify.d.ts +36 -22
- package/fastify.js +72 -53
- package/lib/configValidator.js +902 -1023
- package/lib/contentTypeParser.js +6 -16
- package/lib/context.js +36 -10
- package/lib/decorate.js +5 -3
- package/lib/error-handler.js +158 -0
- package/lib/error-serializer.js +257 -0
- package/lib/errors.js +49 -10
- package/lib/fourOhFour.js +31 -20
- package/lib/handleRequest.js +10 -13
- package/lib/hooks.js +14 -9
- package/lib/noop-set.js +10 -0
- package/lib/pluginOverride.js +0 -3
- package/lib/pluginUtils.js +3 -2
- package/lib/reply.js +44 -163
- package/lib/request.js +13 -10
- package/lib/route.js +158 -139
- package/lib/schema-controller.js +3 -3
- package/lib/schemas.js +27 -1
- package/lib/server.js +219 -116
- package/lib/symbols.js +6 -4
- package/lib/validation.js +2 -1
- package/lib/warnings.js +2 -12
- package/lib/wrapThenable.js +4 -11
- package/package.json +40 -45
- package/test/404s.test.js +265 -108
- package/test/500s.test.js +2 -2
- package/test/async-await.test.js +15 -71
- package/test/close.test.js +39 -1
- package/test/content-parser.test.js +32 -0
- package/test/context-config.test.js +56 -4
- package/test/custom-http-server.test.js +14 -7
- package/test/custom-parser-async.test.js +0 -65
- package/test/custom-parser.test.js +54 -121
- package/test/decorator.test.js +1 -3
- package/test/delete.test.js +5 -5
- 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/get.test.js +3 -3
- package/test/helper.js +18 -3
- package/test/hooks-async.test.js +14 -47
- package/test/hooks.on-ready.test.js +9 -4
- package/test/hooks.test.js +58 -99
- package/test/http2/closing.test.js +5 -11
- package/test/http2/unknown-http-method.test.js +3 -9
- package/test/https/custom-https-server.test.js +12 -6
- package/test/inject.test.js +1 -1
- package/test/input-validation.js +2 -2
- package/test/internals/all.test.js +2 -2
- package/test/internals/contentTypeParser.test.js +4 -4
- package/test/internals/handleRequest.test.js +9 -46
- package/test/internals/initialConfig.test.js +33 -12
- package/test/internals/logger.test.js +1 -1
- package/test/internals/reply.test.js +245 -3
- package/test/internals/request.test.js +13 -7
- package/test/internals/server.test.js +88 -0
- package/test/listen.test.js +84 -1
- package/test/logger.test.js +98 -58
- package/test/maxRequestsPerSocket.test.js +8 -6
- package/test/middleware.test.js +2 -25
- package/test/noop-set.test.js +19 -0
- package/test/nullable-validation.test.js +51 -14
- package/test/plugin.test.js +31 -5
- package/test/pretty-print.test.js +22 -10
- package/test/reply-error.test.js +123 -12
- package/test/request-error.test.js +2 -5
- package/test/route-hooks.test.js +17 -17
- package/test/route-prefix.test.js +2 -1
- package/test/route.test.js +216 -20
- package/test/router-options.test.js +1 -1
- package/test/schema-examples.test.js +11 -5
- package/test/schema-feature.test.js +24 -19
- package/test/schema-serialization.test.js +50 -9
- package/test/schema-special-usage.test.js +14 -81
- package/test/schema-validation.test.js +9 -9
- package/test/skip-reply-send.test.js +8 -8
- package/test/stream.test.js +23 -12
- package/test/throw.test.js +8 -5
- package/test/trust-proxy.test.js +1 -1
- package/test/type-provider.test.js +20 -0
- package/test/types/fastify.test-d.ts +12 -18
- package/test/types/hooks.test-d.ts +7 -3
- package/test/types/import.js +2 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +61 -15
- package/test/types/logger.test-d.ts +44 -15
- 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 +417 -0
- package/test/validation-error-handling.test.js +9 -9
- package/test/versioned-routes.test.js +29 -17
- package/test/wrapThenable.test.js +7 -6
- package/types/.eslintrc.json +1 -1
- package/types/content-type-parser.d.ts +17 -8
- package/types/hooks.d.ts +107 -60
- package/types/instance.d.ts +137 -105
- package/types/logger.d.ts +18 -104
- package/types/plugin.d.ts +10 -4
- package/types/register.d.ts +1 -1
- package/types/reply.d.ts +16 -11
- package/types/request.d.ts +10 -5
- package/types/route.d.ts +42 -31
- package/types/schema.d.ts +15 -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
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('tap')
|
|
4
|
-
const Joi = require('
|
|
4
|
+
const Joi = require('joi')
|
|
5
5
|
const AJV = require('ajv')
|
|
6
6
|
const S = require('fluent-json-schema')
|
|
7
7
|
const Fastify = require('..')
|
|
8
8
|
const ajvMergePatch = require('ajv-merge-patch')
|
|
9
9
|
const ajvErrors = require('ajv-errors')
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const buildValidatorAJV6 = require('@fastify/ajv-compiler-6')
|
|
12
12
|
|
|
13
|
-
test('
|
|
13
|
+
test('Ajv6 usage instead of the bundle one', t => {
|
|
14
14
|
t.plan(2)
|
|
15
15
|
|
|
16
|
-
t.test('use
|
|
16
|
+
t.test('use old ajv6 option', t => {
|
|
17
17
|
t.plan(2)
|
|
18
18
|
const fastify = Fastify({
|
|
19
19
|
ajv: {
|
|
20
|
-
customOptions: {
|
|
20
|
+
customOptions: { jsonPointers: true }
|
|
21
21
|
},
|
|
22
22
|
schemaController: {
|
|
23
23
|
compilersFactory: {
|
|
24
|
-
buildValidator:
|
|
24
|
+
buildValidator: buildValidatorAJV6()
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
})
|
|
@@ -42,17 +42,17 @@ test('Ajv8 usage instead of the bundle one', t => {
|
|
|
42
42
|
})
|
|
43
43
|
|
|
44
44
|
fastify.ready(err => {
|
|
45
|
-
t.
|
|
46
|
-
t.
|
|
45
|
+
t.error(err)
|
|
46
|
+
t.pass('the removed ajv6 option let the startup pass')
|
|
47
47
|
})
|
|
48
48
|
})
|
|
49
49
|
|
|
50
|
-
t.test('use
|
|
50
|
+
t.test('use old ajv6 option within a response schema', t => {
|
|
51
51
|
t.plan(2)
|
|
52
52
|
const fastify = Fastify({
|
|
53
53
|
schemaController: {
|
|
54
54
|
compilersFactory: {
|
|
55
|
-
buildValidator:
|
|
55
|
+
buildValidator: buildValidatorAJV6()
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
})
|
|
@@ -89,79 +89,11 @@ test('Ajv8 usage instead of the bundle one', t => {
|
|
|
89
89
|
})
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
-
test('Ajv8 usage with plugins', t => {
|
|
93
|
-
t.plan(2)
|
|
94
|
-
|
|
95
|
-
t.test('use new ajv8 option', t => {
|
|
96
|
-
t.plan(3)
|
|
97
|
-
const fastify = Fastify({
|
|
98
|
-
ajv: {
|
|
99
|
-
customOptions: { validateFormats: true },
|
|
100
|
-
plugins: [require('ajv-formats')]
|
|
101
|
-
},
|
|
102
|
-
schemaController: {
|
|
103
|
-
compilersFactory: {
|
|
104
|
-
buildValidator: buildValidatorAJV8()
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
callIt(fastify, (err, res) => {
|
|
110
|
-
t.error(err)
|
|
111
|
-
t.equal(res.statusCode, 400)
|
|
112
|
-
t.equal(res.json().message, 'body must match format "date"')
|
|
113
|
-
})
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
t.test('use new ajv8 option - avoid check', t => {
|
|
117
|
-
t.plan(2)
|
|
118
|
-
const fastify = Fastify({
|
|
119
|
-
ajv: {
|
|
120
|
-
customOptions: { validateFormats: false }
|
|
121
|
-
},
|
|
122
|
-
schemaController: {
|
|
123
|
-
compilersFactory: {
|
|
124
|
-
buildValidator: buildValidatorAJV8()
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
callIt(fastify, (err, res) => {
|
|
130
|
-
t.error(err)
|
|
131
|
-
t.equal(res.statusCode, 200)
|
|
132
|
-
})
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
function callIt (fastify, cb) {
|
|
136
|
-
fastify.post('/', {
|
|
137
|
-
schema: {
|
|
138
|
-
body: {
|
|
139
|
-
type: 'object',
|
|
140
|
-
properties: {
|
|
141
|
-
foo: {
|
|
142
|
-
type: 'string',
|
|
143
|
-
format: 'date'
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
handler (req, reply) { reply.send({ ok: 1 }) }
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
fastify.inject({
|
|
152
|
-
method: 'POST',
|
|
153
|
-
url: '/',
|
|
154
|
-
payload: { foo: '99' }
|
|
155
|
-
}, cb)
|
|
156
|
-
}
|
|
157
|
-
})
|
|
158
|
-
|
|
159
92
|
test('Ajv plugins array parameter', t => {
|
|
160
93
|
t.plan(3)
|
|
161
94
|
const fastify = Fastify({
|
|
162
95
|
ajv: {
|
|
163
96
|
customOptions: {
|
|
164
|
-
jsonPointers: true,
|
|
165
97
|
allErrors: true
|
|
166
98
|
},
|
|
167
99
|
plugins: [
|
|
@@ -509,7 +441,7 @@ test('setSchemaController in a plugin', t => {
|
|
|
509
441
|
ajvInstance.addSchema(baseSchema)
|
|
510
442
|
ajvInstance.addSchema(refSchema)
|
|
511
443
|
|
|
512
|
-
const fastify = Fastify()
|
|
444
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
513
445
|
fastify.register(schemaPlugin)
|
|
514
446
|
fastify.get('/', {
|
|
515
447
|
schema: {
|
|
@@ -760,7 +692,8 @@ test('multiple refs with the same ids', t => {
|
|
|
760
692
|
|
|
761
693
|
fastify.addSchema(baseSchema)
|
|
762
694
|
fastify.addSchema(refSchema)
|
|
763
|
-
|
|
695
|
+
|
|
696
|
+
fastify.head('/', {
|
|
764
697
|
schema: {
|
|
765
698
|
query: refSchema,
|
|
766
699
|
response: {
|
|
@@ -772,7 +705,7 @@ test('multiple refs with the same ids', t => {
|
|
|
772
705
|
}
|
|
773
706
|
})
|
|
774
707
|
|
|
775
|
-
fastify.
|
|
708
|
+
fastify.get('/', {
|
|
776
709
|
schema: {
|
|
777
710
|
query: refSchema,
|
|
778
711
|
response: {
|
|
@@ -99,7 +99,7 @@ test('Basic validation test', t => {
|
|
|
99
99
|
url: '/'
|
|
100
100
|
}, (err, res) => {
|
|
101
101
|
t.error(err)
|
|
102
|
-
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: "body
|
|
102
|
+
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: "body must have required property 'work'" })
|
|
103
103
|
t.equal(res.statusCode, 400)
|
|
104
104
|
})
|
|
105
105
|
})
|
|
@@ -314,7 +314,7 @@ test('Triple $ref with a simple $id', t => {
|
|
|
314
314
|
}, (err, res) => {
|
|
315
315
|
t.error(err)
|
|
316
316
|
t.equal(res.statusCode, 400)
|
|
317
|
-
t.same(res.json().message, "body
|
|
317
|
+
t.same(res.json().message, "body must have required property 'foo'")
|
|
318
318
|
})
|
|
319
319
|
})
|
|
320
320
|
|
|
@@ -348,6 +348,7 @@ test('Extending schema', t => {
|
|
|
348
348
|
allOf: [
|
|
349
349
|
{ $ref: 'address.id#/definitions/address' },
|
|
350
350
|
{
|
|
351
|
+
type: 'object',
|
|
351
352
|
properties: { type: { enum: ['residential', 'business'] } },
|
|
352
353
|
required: ['type']
|
|
353
354
|
}
|
|
@@ -439,7 +440,7 @@ test('Should work with nested ids', t => {
|
|
|
439
440
|
}, (err, res) => {
|
|
440
441
|
t.error(err)
|
|
441
442
|
t.equal(res.statusCode, 400)
|
|
442
|
-
t.equal(res.json().message, 'params
|
|
443
|
+
t.equal(res.json().message, 'params/id must be number')
|
|
443
444
|
})
|
|
444
445
|
})
|
|
445
446
|
|
|
@@ -536,7 +537,7 @@ test('JSON Schema validation keywords', t => {
|
|
|
536
537
|
t.same(res.json(), {
|
|
537
538
|
statusCode: 400,
|
|
538
539
|
error: 'Bad Request',
|
|
539
|
-
message: 'params
|
|
540
|
+
message: 'params/ip must match format "ipv4"'
|
|
540
541
|
})
|
|
541
542
|
})
|
|
542
543
|
})
|
|
@@ -590,7 +591,7 @@ test('Nested id calls', t => {
|
|
|
590
591
|
t.equal(res.statusCode, 400)
|
|
591
592
|
t.same(res.json(), {
|
|
592
593
|
error: 'Bad Request',
|
|
593
|
-
message: 'body
|
|
594
|
+
message: 'body/host/ip must match format "ipv4"',
|
|
594
595
|
statusCode: 400
|
|
595
596
|
})
|
|
596
597
|
})
|
|
@@ -692,7 +693,7 @@ test('Use shared schema and $ref with $id ($ref to $id)', t => {
|
|
|
692
693
|
t.equal(res.statusCode, 400)
|
|
693
694
|
t.same(res.json(), {
|
|
694
695
|
error: 'Bad Request',
|
|
695
|
-
message: "body
|
|
696
|
+
message: "body must have required property 'address'",
|
|
696
697
|
statusCode: 400
|
|
697
698
|
})
|
|
698
699
|
})
|
|
@@ -712,8 +713,7 @@ test('Use items with $ref', t => {
|
|
|
712
713
|
|
|
713
714
|
const body = {
|
|
714
715
|
type: 'array',
|
|
715
|
-
items: { $ref: 'http://example.com/ref-to-external-validator.json#' }
|
|
716
|
-
default: []
|
|
716
|
+
items: { $ref: 'http://example.com/ref-to-external-validator.json#' }
|
|
717
717
|
}
|
|
718
718
|
|
|
719
719
|
fastify.post('/', {
|
|
@@ -809,7 +809,7 @@ test('Use $ref to /definitions', t => {
|
|
|
809
809
|
t.equal(res.statusCode, 400)
|
|
810
810
|
t.same(res.json(), {
|
|
811
811
|
error: 'Bad Request',
|
|
812
|
-
message: 'body
|
|
812
|
+
message: 'body/test/id must be number',
|
|
813
813
|
statusCode: 400
|
|
814
814
|
})
|
|
815
815
|
})
|
|
@@ -23,7 +23,7 @@ test('skip automatic reply.send() with reply.sent = true and a body', (t) => {
|
|
|
23
23
|
const stream = split(JSON.parse)
|
|
24
24
|
const app = Fastify({
|
|
25
25
|
logger: {
|
|
26
|
-
stream
|
|
26
|
+
stream
|
|
27
27
|
}
|
|
28
28
|
})
|
|
29
29
|
|
|
@@ -52,7 +52,7 @@ test('skip automatic reply.send() with reply.sent = true and no body', (t) => {
|
|
|
52
52
|
const stream = split(JSON.parse)
|
|
53
53
|
const app = Fastify({
|
|
54
54
|
logger: {
|
|
55
|
-
stream
|
|
55
|
+
stream
|
|
56
56
|
}
|
|
57
57
|
})
|
|
58
58
|
|
|
@@ -81,7 +81,7 @@ test('skip automatic reply.send() with reply.sent = true and an error', (t) => {
|
|
|
81
81
|
const stream = split(JSON.parse)
|
|
82
82
|
const app = Fastify({
|
|
83
83
|
logger: {
|
|
84
|
-
stream
|
|
84
|
+
stream
|
|
85
85
|
}
|
|
86
86
|
})
|
|
87
87
|
|
|
@@ -125,7 +125,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
125
125
|
const stream = split(JSON.parse)
|
|
126
126
|
const app = Fastify({
|
|
127
127
|
logger: {
|
|
128
|
-
stream
|
|
128
|
+
stream
|
|
129
129
|
}
|
|
130
130
|
})
|
|
131
131
|
|
|
@@ -170,7 +170,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
170
170
|
const stream = split(JSON.parse)
|
|
171
171
|
const app = Fastify({
|
|
172
172
|
logger: {
|
|
173
|
-
stream
|
|
173
|
+
stream
|
|
174
174
|
}
|
|
175
175
|
})
|
|
176
176
|
t.teardown(() => app.close())
|
|
@@ -224,7 +224,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
224
224
|
const stream = split(JSON.parse)
|
|
225
225
|
const app = Fastify({
|
|
226
226
|
logger: {
|
|
227
|
-
stream
|
|
227
|
+
stream
|
|
228
228
|
}
|
|
229
229
|
})
|
|
230
230
|
t.teardown(() => app.close())
|
|
@@ -274,7 +274,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
274
274
|
const stream = split(JSON.parse)
|
|
275
275
|
const app = Fastify({
|
|
276
276
|
logger: {
|
|
277
|
-
stream
|
|
277
|
+
stream
|
|
278
278
|
}
|
|
279
279
|
})
|
|
280
280
|
|
|
@@ -297,7 +297,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
297
297
|
} else {
|
|
298
298
|
app.addHook(hookOrHandler, async (req, reply) => {
|
|
299
299
|
reply.hijack()
|
|
300
|
-
reply.send('hello from reply.send()')
|
|
300
|
+
return reply.send('hello from reply.send()')
|
|
301
301
|
})
|
|
302
302
|
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
|
303
303
|
}
|
package/test/stream.test.js
CHANGED
|
@@ -14,7 +14,8 @@ const JSONStream = require('JSONStream')
|
|
|
14
14
|
const send = require('send')
|
|
15
15
|
const Readable = require('stream').Readable
|
|
16
16
|
const split = require('split2')
|
|
17
|
-
const
|
|
17
|
+
const semver = require('semver')
|
|
18
|
+
const { kDisableRequestLogging } = require('../lib/symbols.js')
|
|
18
19
|
|
|
19
20
|
function getUrl (app) {
|
|
20
21
|
const { address, port } = app.server.address()
|
|
@@ -26,7 +27,7 @@ function getUrl (app) {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
test('should respond with a stream', t => {
|
|
29
|
-
t.plan(
|
|
30
|
+
t.plan(6)
|
|
30
31
|
const fastify = Fastify()
|
|
31
32
|
|
|
32
33
|
fastify.get('/', function (req, reply) {
|
|
@@ -34,18 +35,13 @@ test('should respond with a stream', t => {
|
|
|
34
35
|
reply.code(200).send(stream)
|
|
35
36
|
})
|
|
36
37
|
|
|
37
|
-
fastify.get('/error', function (req, reply) {
|
|
38
|
-
const stream = fs.createReadStream('not-existing-file', 'utf8')
|
|
39
|
-
reply.code(200).send(stream)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
38
|
fastify.listen(0, err => {
|
|
43
39
|
t.error(err)
|
|
44
40
|
fastify.server.unref()
|
|
45
41
|
|
|
46
42
|
sget(`http://localhost:${fastify.server.address().port}`, function (err, response, data) {
|
|
47
43
|
t.error(err)
|
|
48
|
-
t.equal(response.headers['content-type'],
|
|
44
|
+
t.equal(response.headers['content-type'], undefined)
|
|
49
45
|
t.equal(response.statusCode, 200)
|
|
50
46
|
|
|
51
47
|
fs.readFile(__filename, (err, expected) => {
|
|
@@ -53,6 +49,21 @@ test('should respond with a stream', t => {
|
|
|
53
49
|
t.equal(expected.toString(), data.toString())
|
|
54
50
|
})
|
|
55
51
|
})
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('should respond with a stream (error)', t => {
|
|
56
|
+
t.plan(3)
|
|
57
|
+
const fastify = Fastify()
|
|
58
|
+
|
|
59
|
+
fastify.get('/error', function (req, reply) {
|
|
60
|
+
const stream = fs.createReadStream('not-existing-file', 'utf8')
|
|
61
|
+
reply.code(200).send(stream)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
fastify.listen(0, err => {
|
|
65
|
+
t.error(err)
|
|
66
|
+
fastify.server.unref()
|
|
56
67
|
|
|
57
68
|
sget(`http://localhost:${fastify.server.address().port}/error`, function (err, response) {
|
|
58
69
|
t.error(err)
|
|
@@ -582,7 +593,7 @@ test('return a 404 if the stream emits a 404 error', t => {
|
|
|
582
593
|
})
|
|
583
594
|
})
|
|
584
595
|
|
|
585
|
-
test('should support send module 200 and 404', {
|
|
596
|
+
test('should support send module 200 and 404', { skip: semver.gte(process.versions.node, '17.0.0') }, t => {
|
|
586
597
|
t.plan(8)
|
|
587
598
|
const fastify = Fastify()
|
|
588
599
|
|
|
@@ -604,7 +615,7 @@ test('should support send module 200 and 404', { only: true }, t => {
|
|
|
604
615
|
|
|
605
616
|
sget(url, function (err, response, data) {
|
|
606
617
|
t.error(err)
|
|
607
|
-
t.equal(response.headers['content-type'], 'application/
|
|
618
|
+
t.equal(response.headers['content-type'], 'application/javascript; charset=UTF-8')
|
|
608
619
|
t.equal(response.statusCode, 200)
|
|
609
620
|
|
|
610
621
|
fs.readFile(__filename, (err, expected) => {
|
|
@@ -654,7 +665,7 @@ test('should mark reply as sent before pumping the payload stream into response
|
|
|
654
665
|
const handleRequest = proxyquire('../lib/handleRequest', {
|
|
655
666
|
'./wrapThenable': (thenable, reply) => {
|
|
656
667
|
thenable.then(function (payload) {
|
|
657
|
-
t.equal(reply
|
|
668
|
+
t.equal(reply.sent, true)
|
|
658
669
|
})
|
|
659
670
|
}
|
|
660
671
|
})
|
|
@@ -671,7 +682,7 @@ test('should mark reply as sent before pumping the payload stream into response
|
|
|
671
682
|
|
|
672
683
|
fastify.get('/', async function (req, reply) {
|
|
673
684
|
const stream = fs.createReadStream(__filename, 'utf8')
|
|
674
|
-
reply.code(200).send(stream)
|
|
685
|
+
return reply.code(200).send(stream)
|
|
675
686
|
})
|
|
676
687
|
|
|
677
688
|
fastify.inject({
|
package/test/throw.test.js
CHANGED
|
@@ -17,12 +17,15 @@ test('Fastify should throw on wrong options', t => {
|
|
|
17
17
|
test('Fastify should throw on multiple assignment to the same route', t => {
|
|
18
18
|
t.plan(1)
|
|
19
19
|
const fastify = Fastify()
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
fastify.get('/', () => {})
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
try {
|
|
24
|
+
fastify.get('/', () => {})
|
|
25
|
+
t.fail('Should throw on duplicated route declaration')
|
|
26
|
+
} catch (error) {
|
|
27
|
+
t.equal(error.message, "Method 'GET' already declared for route '/'")
|
|
28
|
+
}
|
|
26
29
|
})
|
|
27
30
|
|
|
28
31
|
test('Fastify should throw for an invalid schema, printing the error route - headers', t => {
|
|
@@ -78,7 +81,7 @@ test('Fastify should throw for an invalid shorthand option type', t => {
|
|
|
78
81
|
t.fail()
|
|
79
82
|
} catch (e) {
|
|
80
83
|
t.equal(e.code, 'FST_ERR_INIT_OPTS_INVALID')
|
|
81
|
-
t.match(e.message, /
|
|
84
|
+
t.match(e.message, /must be boolean/)
|
|
82
85
|
t.pass()
|
|
83
86
|
}
|
|
84
87
|
})
|
package/test/trust-proxy.test.js
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const Fastify = require('..')
|
|
5
|
+
|
|
6
|
+
test('Should export withTypeProvider function', t => {
|
|
7
|
+
t.plan(1)
|
|
8
|
+
try {
|
|
9
|
+
Fastify().withTypeProvider()
|
|
10
|
+
t.pass()
|
|
11
|
+
} catch (e) {
|
|
12
|
+
t.fail()
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('Should return same instance', t => {
|
|
17
|
+
t.plan(1)
|
|
18
|
+
const fastify = Fastify()
|
|
19
|
+
t.equal(fastify, fastify.withTypeProvider())
|
|
20
|
+
})
|
|
@@ -7,7 +7,7 @@ import fastify, {
|
|
|
7
7
|
LightMyRequestChain,
|
|
8
8
|
LightMyRequestResponse,
|
|
9
9
|
LightMyRequestCallback,
|
|
10
|
-
InjectOptions
|
|
10
|
+
InjectOptions, FastifyBaseLogger
|
|
11
11
|
} from '../../fastify'
|
|
12
12
|
import * as http from 'http'
|
|
13
13
|
import * as https from 'https'
|
|
@@ -50,13 +50,16 @@ expectAssignable<InjectOptions>({ query: '' })
|
|
|
50
50
|
fastify({ http2: true, https: {} }).inject().then((resp) => {
|
|
51
51
|
expectAssignable<LightMyRequestResponse>(resp)
|
|
52
52
|
})
|
|
53
|
-
const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => {
|
|
53
|
+
const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => {
|
|
54
|
+
if (err) throw err
|
|
55
|
+
}
|
|
54
56
|
fastify({ http2: true, https: {} }).inject({}, lightMyRequestCallback)
|
|
55
57
|
|
|
56
58
|
// server options
|
|
57
59
|
expectAssignable<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>(fastify({ http2: true }))
|
|
58
60
|
expectAssignable<FastifyInstance>(fastify({ ignoreTrailingSlash: true }))
|
|
59
61
|
expectAssignable<FastifyInstance>(fastify({ connectionTimeout: 1000 }))
|
|
62
|
+
expectAssignable<FastifyInstance>(fastify({ forceCloseConnections: true }))
|
|
60
63
|
expectAssignable<FastifyInstance>(fastify({ keepAliveTimeout: 1000 }))
|
|
61
64
|
expectAssignable<FastifyInstance>(fastify({ pluginTimeout: 1000 }))
|
|
62
65
|
expectAssignable<FastifyInstance>(fastify({ bodyLimit: 100 }))
|
|
@@ -69,9 +72,9 @@ expectAssignable<FastifyInstance>(fastify({ serializerOpts: { rounding: 'ceil' }
|
|
|
69
72
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } }))
|
|
70
73
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { schema: { } } }))
|
|
71
74
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { otherProp: { } } }))
|
|
72
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>(fastify({ logger: true }))
|
|
73
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse,
|
|
74
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse,
|
|
75
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
|
|
76
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
|
|
77
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({
|
|
75
78
|
logger: {
|
|
76
79
|
level: 'info',
|
|
77
80
|
genReqId: () => 'request-id',
|
|
@@ -102,6 +105,7 @@ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerR
|
|
|
102
105
|
}
|
|
103
106
|
}))
|
|
104
107
|
const customLogger = {
|
|
108
|
+
level: 'info',
|
|
105
109
|
info: () => { },
|
|
106
110
|
warn: () => { },
|
|
107
111
|
error: () => { },
|
|
@@ -110,7 +114,7 @@ const customLogger = {
|
|
|
110
114
|
debug: () => { },
|
|
111
115
|
child: () => customLogger
|
|
112
116
|
}
|
|
113
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse,
|
|
117
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: customLogger }))
|
|
114
118
|
expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
|
|
115
119
|
expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
|
|
116
120
|
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
|
|
@@ -173,24 +177,13 @@ expectAssignable<FastifyInstance>(fastify({ return503OnClosing: true }))
|
|
|
173
177
|
expectAssignable<FastifyInstance>(fastify({
|
|
174
178
|
ajv: {
|
|
175
179
|
customOptions: {
|
|
176
|
-
|
|
180
|
+
removeAdditional: 'all'
|
|
177
181
|
},
|
|
178
182
|
plugins: [() => { }]
|
|
179
183
|
}
|
|
180
184
|
}))
|
|
181
185
|
expectAssignable<FastifyInstance>(fastify({
|
|
182
186
|
ajv: {
|
|
183
|
-
customOptions: {
|
|
184
|
-
nullable: false
|
|
185
|
-
},
|
|
186
|
-
plugins: [[() => { }, 'keyword']]
|
|
187
|
-
}
|
|
188
|
-
}))
|
|
189
|
-
expectAssignable<FastifyInstance>(fastify({
|
|
190
|
-
ajv: {
|
|
191
|
-
customOptions: {
|
|
192
|
-
nullable: false
|
|
193
|
-
},
|
|
194
187
|
plugins: [[() => { }, ['keyword1', 'keyword2']]]
|
|
195
188
|
}
|
|
196
189
|
}))
|
|
@@ -205,6 +198,7 @@ expectAssignable<FastifyInstance>(fastify({
|
|
|
205
198
|
expectType<Socket>(socket)
|
|
206
199
|
}
|
|
207
200
|
}))
|
|
201
|
+
expectAssignable<FastifyInstance>(fastify({ jsonShorthand: true }))
|
|
208
202
|
|
|
209
203
|
// Thenable
|
|
210
204
|
expectAssignable<PromiseLike<FastifyInstance>>(fastify({ return503OnClosing: true }))
|
|
@@ -7,7 +7,9 @@ import fastify, {
|
|
|
7
7
|
RawReplyDefaultExpression,
|
|
8
8
|
RawRequestDefaultExpression,
|
|
9
9
|
RawServerBase,
|
|
10
|
-
RouteOptions
|
|
10
|
+
RouteOptions,
|
|
11
|
+
RegisterOptions,
|
|
12
|
+
FastifyPluginOptions
|
|
11
13
|
} from '../../fastify'
|
|
12
14
|
import { preHandlerAsyncHookHandler, RequestPayload } from '../../types/hooks'
|
|
13
15
|
|
|
@@ -113,8 +115,9 @@ server.addHook('onRoute', function (opts) {
|
|
|
113
115
|
expectType<RouteOptions & { routePath: string; path: string; prefix: string}>(opts)
|
|
114
116
|
})
|
|
115
117
|
|
|
116
|
-
server.addHook('onRegister', (instance, done) => {
|
|
118
|
+
server.addHook('onRegister', (instance, opts, done) => {
|
|
117
119
|
expectType<FastifyInstance>(instance)
|
|
120
|
+
expectType<RegisterOptions & FastifyPluginOptions>(opts)
|
|
118
121
|
expectAssignable<(err?: FastifyError) => void>(done)
|
|
119
122
|
expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
|
|
120
123
|
expectType<void>(done(new Error()))
|
|
@@ -194,8 +197,9 @@ server.addHook('onError', async function (request, reply, error) {
|
|
|
194
197
|
expectType<FastifyError>(error)
|
|
195
198
|
})
|
|
196
199
|
|
|
197
|
-
server.addHook('onRegister', async (instance) => {
|
|
200
|
+
server.addHook('onRegister', async (instance, opts) => {
|
|
198
201
|
expectType<FastifyInstance>(instance)
|
|
202
|
+
expectType<RegisterOptions & FastifyPluginOptions>(opts)
|
|
199
203
|
})
|
|
200
204
|
|
|
201
205
|
server.addHook('onReady', async function () {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { FastifyLogFn } from '../../fastify'
|