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
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const proxyquire = require('proxyquire')
|
|
5
|
+
|
|
6
|
+
const Fastify = require('../../fastify')
|
|
7
|
+
const createServer = require('../../lib/server')
|
|
8
|
+
|
|
9
|
+
const handler = (req, res) => {
|
|
10
|
+
res.writeHead(200, { 'Content-Type': 'application/json' })
|
|
11
|
+
res.end(JSON.stringify({ data: 'Hello World!' }))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
test('start listening', async t => {
|
|
15
|
+
const { server, listen } = createServer({}, handler)
|
|
16
|
+
await listen.call(Fastify(), { port: 0, host: 'localhost' })
|
|
17
|
+
server.close()
|
|
18
|
+
t.pass('server started')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('DNS errors does not stop the main server on localhost - promise interface', async t => {
|
|
22
|
+
const createServer = proxyquire('../../lib/server', {
|
|
23
|
+
dns: {
|
|
24
|
+
lookup: (hostname, options, cb) => {
|
|
25
|
+
cb(new Error('DNS error'))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
const { server, listen } = createServer({}, handler)
|
|
30
|
+
await listen.call(Fastify(), { port: 0, host: 'localhost' })
|
|
31
|
+
server.close()
|
|
32
|
+
t.pass('server started')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('DNS errors does not stop the main server on localhost - callback interface', t => {
|
|
36
|
+
t.plan(2)
|
|
37
|
+
const createServer = proxyquire('../../lib/server', {
|
|
38
|
+
dns: {
|
|
39
|
+
lookup: (hostname, options, cb) => {
|
|
40
|
+
cb(new Error('DNS error'))
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
const { server, listen } = createServer({}, handler)
|
|
45
|
+
listen.call(Fastify(), { port: 0, host: 'localhost' }, (err) => {
|
|
46
|
+
t.error(err)
|
|
47
|
+
server.close()
|
|
48
|
+
t.pass('server started')
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('DNS returns empty binding', t => {
|
|
53
|
+
t.plan(2)
|
|
54
|
+
const createServer = proxyquire('../../lib/server', {
|
|
55
|
+
dns: {
|
|
56
|
+
lookup: (hostname, options, cb) => {
|
|
57
|
+
cb(null, [])
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
const { server, listen } = createServer({}, handler)
|
|
62
|
+
listen.call(Fastify(), { port: 0, host: 'localhost' }, (err) => {
|
|
63
|
+
t.error(err)
|
|
64
|
+
server.close()
|
|
65
|
+
t.pass('server started')
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('DNS returns more than two binding', t => {
|
|
70
|
+
t.plan(2)
|
|
71
|
+
const createServer = proxyquire('../../lib/server', {
|
|
72
|
+
dns: {
|
|
73
|
+
lookup: (hostname, options, cb) => {
|
|
74
|
+
cb(null, [
|
|
75
|
+
{ address: '::1', family: 6 },
|
|
76
|
+
{ address: '127.0.0.1', family: 4 },
|
|
77
|
+
{ address: '0.0.0.0', family: 4 }
|
|
78
|
+
])
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
const { server, listen } = createServer({}, handler)
|
|
83
|
+
listen.call(Fastify(), { port: 0, host: 'localhost' }, (err) => {
|
|
84
|
+
t.error(err)
|
|
85
|
+
server.close()
|
|
86
|
+
t.pass('server started')
|
|
87
|
+
})
|
|
88
|
+
})
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Tests for deprecated `.listen` signature. This file should be
|
|
4
|
+
// removed when the deprecation is complete.
|
|
5
|
+
|
|
6
|
+
const { test, before } = require('tap')
|
|
7
|
+
const dns = require('dns').promises
|
|
8
|
+
const Fastify = require('..')
|
|
9
|
+
|
|
10
|
+
let localhost
|
|
11
|
+
let localhostForURL
|
|
12
|
+
|
|
13
|
+
process.removeAllListeners('warning')
|
|
14
|
+
|
|
15
|
+
before(async function (t) {
|
|
16
|
+
const lookup = await dns.lookup('localhost')
|
|
17
|
+
localhost = lookup.address
|
|
18
|
+
if (lookup.family === 6) {
|
|
19
|
+
localhostForURL = `[${lookup.address}]`
|
|
20
|
+
} else {
|
|
21
|
+
localhostForURL = localhost
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('listen accepts a port and a callback', t => {
|
|
26
|
+
t.plan(2)
|
|
27
|
+
const fastify = Fastify()
|
|
28
|
+
t.teardown(fastify.close.bind(fastify))
|
|
29
|
+
fastify.listen(0, (err) => {
|
|
30
|
+
t.equal(fastify.server.address().address, localhost)
|
|
31
|
+
t.error(err)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('listen accepts a port and a callback with (err, address)', t => {
|
|
36
|
+
t.plan(2)
|
|
37
|
+
const fastify = Fastify()
|
|
38
|
+
t.teardown(fastify.close.bind(fastify))
|
|
39
|
+
fastify.listen(0, (err, address) => {
|
|
40
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
41
|
+
t.error(err)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('listen accepts a port, address, and callback', t => {
|
|
46
|
+
t.plan(1)
|
|
47
|
+
const fastify = Fastify()
|
|
48
|
+
t.teardown(fastify.close.bind(fastify))
|
|
49
|
+
fastify.listen(0, localhost, (err) => {
|
|
50
|
+
t.error(err)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('listen accepts options, backlog and a callback', t => {
|
|
55
|
+
t.plan(1)
|
|
56
|
+
const fastify = Fastify()
|
|
57
|
+
t.teardown(fastify.close.bind(fastify))
|
|
58
|
+
fastify.listen({
|
|
59
|
+
port: 0,
|
|
60
|
+
host: 'localhost'
|
|
61
|
+
}, 511, (err) => {
|
|
62
|
+
t.error(err)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('listen accepts options (no port), backlog and a callback', t => {
|
|
67
|
+
t.plan(1)
|
|
68
|
+
const fastify = Fastify()
|
|
69
|
+
t.teardown(fastify.close.bind(fastify))
|
|
70
|
+
fastify.listen({
|
|
71
|
+
host: 'localhost'
|
|
72
|
+
}, 511, (err) => {
|
|
73
|
+
t.error(err)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('listen accepts options (no host), backlog and a callback', t => {
|
|
78
|
+
t.plan(1)
|
|
79
|
+
const fastify = Fastify()
|
|
80
|
+
t.teardown(fastify.close.bind(fastify))
|
|
81
|
+
fastify.listen({
|
|
82
|
+
port: 0
|
|
83
|
+
}, 511, (err) => {
|
|
84
|
+
t.error(err)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('listen accepts options (no port, no host), backlog and a callback', t => {
|
|
89
|
+
t.plan(1)
|
|
90
|
+
const fastify = Fastify()
|
|
91
|
+
t.teardown(fastify.close.bind(fastify))
|
|
92
|
+
fastify.listen({
|
|
93
|
+
ipv6Only: false
|
|
94
|
+
}, 511, (err) => {
|
|
95
|
+
t.error(err)
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('listen accepts a port, address and a callback with (err, address)', t => {
|
|
100
|
+
t.plan(2)
|
|
101
|
+
const fastify = Fastify()
|
|
102
|
+
t.teardown(fastify.close.bind(fastify))
|
|
103
|
+
fastify.listen(0, localhost, (err, address) => {
|
|
104
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
105
|
+
t.error(err)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('listen accepts a port, address, backlog and callback', t => {
|
|
110
|
+
t.plan(1)
|
|
111
|
+
const fastify = Fastify()
|
|
112
|
+
t.teardown(fastify.close.bind(fastify))
|
|
113
|
+
fastify.listen(0, localhost, 511, (err) => {
|
|
114
|
+
t.error(err)
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('listen accepts a port, address, backlog and callback with (err, address)', t => {
|
|
119
|
+
t.plan(2)
|
|
120
|
+
const fastify = Fastify()
|
|
121
|
+
t.teardown(fastify.close.bind(fastify))
|
|
122
|
+
fastify.listen(0, localhost, 511, (err, address) => {
|
|
123
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
124
|
+
t.error(err)
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('listen without callback (port zero)', t => {
|
|
129
|
+
t.plan(1)
|
|
130
|
+
const fastify = Fastify()
|
|
131
|
+
t.teardown(fastify.close.bind(fastify))
|
|
132
|
+
fastify.listen(0)
|
|
133
|
+
.then(() => {
|
|
134
|
+
t.equal(fastify.server.address().address, localhost)
|
|
135
|
+
})
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('listen without callback (port not given)', t => {
|
|
139
|
+
t.plan(1)
|
|
140
|
+
const fastify = Fastify()
|
|
141
|
+
t.teardown(fastify.close.bind(fastify))
|
|
142
|
+
fastify.listen()
|
|
143
|
+
.then(() => {
|
|
144
|
+
t.equal(fastify.server.address().address, localhost)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('listen null without callback with (address)', t => {
|
|
149
|
+
t.plan(1)
|
|
150
|
+
const fastify = Fastify()
|
|
151
|
+
t.teardown(fastify.close.bind(fastify))
|
|
152
|
+
fastify.listen(null)
|
|
153
|
+
.then(address => {
|
|
154
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('listen without port without callback with (address)', t => {
|
|
159
|
+
t.plan(1)
|
|
160
|
+
const fastify = Fastify()
|
|
161
|
+
t.teardown(fastify.close.bind(fastify))
|
|
162
|
+
fastify.listen()
|
|
163
|
+
.then(address => {
|
|
164
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('listen with undefined without callback with (address)', t => {
|
|
169
|
+
t.plan(1)
|
|
170
|
+
const fastify = Fastify()
|
|
171
|
+
t.teardown(fastify.close.bind(fastify))
|
|
172
|
+
fastify.listen(undefined)
|
|
173
|
+
.then(address => {
|
|
174
|
+
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('listen when firstArg is string(pipe) and without backlog', async t => {
|
|
179
|
+
t.plan(1)
|
|
180
|
+
const fastify = Fastify()
|
|
181
|
+
t.teardown(fastify.close.bind(fastify))
|
|
182
|
+
const address = await fastify.listen('\\\\.\\pipe\\testPipe')
|
|
183
|
+
t.equal(address, '\\\\.\\pipe\\testPipe')
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('listen when firstArg is string(pipe) and with backlog', async t => {
|
|
187
|
+
t.plan(1)
|
|
188
|
+
const fastify = Fastify()
|
|
189
|
+
t.teardown(fastify.close.bind(fastify))
|
|
190
|
+
const address = await fastify.listen('\\\\.\\pipe\\testPipe2', 511)
|
|
191
|
+
t.equal(address, '\\\\.\\pipe\\testPipe2')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('listen when firstArg is { path: string(pipe) } and with backlog and callback', t => {
|
|
195
|
+
t.plan(2)
|
|
196
|
+
const fastify = Fastify()
|
|
197
|
+
t.teardown(fastify.close.bind(fastify))
|
|
198
|
+
fastify.listen({ path: '\\\\.\\pipe\\testPipe3' }, 511, (err, address) => {
|
|
199
|
+
t.error(err)
|
|
200
|
+
t.equal(address, '\\\\.\\pipe\\testPipe3')
|
|
201
|
+
})
|
|
202
|
+
})
|