fastify 5.0.0 → 5.2.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.
- package/.borp.yaml +3 -0
- package/.vscode/settings.json +22 -0
- package/README.md +12 -7
- package/docs/Guides/Database.md +15 -15
- package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
- package/docs/Guides/Ecosystem.md +14 -15
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +11 -11
- package/docs/Guides/Migration-Guide-V5.md +133 -9
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Guides/Recommendations.md +9 -9
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +58 -57
- package/docs/Guides/Write-Plugin.md +2 -2
- package/docs/Guides/Write-Type-Provider.md +3 -3
- package/docs/Reference/ContentTypeParser.md +4 -4
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Errors.md +3 -3
- package/docs/Reference/Hooks.md +7 -7
- package/docs/Reference/LTS.md +8 -0
- package/docs/Reference/Logging.md +5 -4
- package/docs/Reference/Reply.md +55 -58
- package/docs/Reference/Request.md +49 -42
- package/docs/Reference/Routes.md +16 -13
- package/docs/Reference/Server.md +32 -28
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +5 -5
- package/examples/typescript-server.ts +1 -1
- package/fastify.d.ts +14 -5
- package/fastify.js +8 -6
- package/lib/contentTypeParser.js +9 -7
- package/lib/context.js +1 -2
- package/lib/error-handler.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/fourOhFour.js +1 -1
- package/lib/hooks.js +4 -1
- package/lib/{logger.js → logger-factory.js} +70 -122
- package/lib/logger-pino.js +68 -0
- package/lib/pluginOverride.js +1 -1
- package/lib/pluginUtils.js +2 -2
- package/lib/reply.js +4 -5
- package/lib/request.js +16 -9
- package/lib/route.js +23 -22
- package/lib/validation.js +2 -2
- package/package.json +13 -15
- package/test/404s.test.js +675 -629
- package/test/500s.test.js +72 -63
- package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
- package/test/als.test.js +48 -45
- package/test/async-await.test.js +148 -134
- package/test/async-dispose.test.js +4 -5
- package/test/async_hooks.test.js +30 -28
- package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
- package/test/buffer.test.js +9 -10
- package/test/build/error-serializer.test.js +3 -4
- package/test/build/version.test.js +2 -3
- package/test/build-certificate.js +1 -1
- package/test/bundler/README.md +5 -5
- package/test/bundler/esbuild/bundler-test.js +10 -9
- package/test/bundler/webpack/bundler-test.js +10 -9
- package/test/case-insensitive.test.js +31 -28
- package/test/chainable.test.js +4 -5
- package/test/check.test.js +8 -10
- package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
- package/test/client-timeout.test.js +5 -5
- package/test/close-pipelining.test.js +6 -8
- package/test/conditional-pino.test.js +47 -0
- package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
- package/test/constrained-routes.test.js +243 -236
- package/test/content-length.test.js +53 -68
- package/test/content-parser.test.js +186 -158
- package/test/content-type.test.js +8 -9
- package/test/context-config.test.js +44 -54
- package/test/custom-http-server.test.js +16 -20
- package/test/custom-parser.5.test.js +32 -32
- package/test/diagnostics-channel/404.test.js +15 -15
- package/test/diagnostics-channel/async-delay-request.test.js +25 -25
- package/test/diagnostics-channel/async-request.test.js +24 -24
- package/test/diagnostics-channel/error-before-handler.test.js +4 -5
- package/test/diagnostics-channel/error-request.test.js +19 -19
- package/test/diagnostics-channel/error-status.test.js +8 -8
- package/test/diagnostics-channel/init.test.js +6 -7
- package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
- package/test/diagnostics-channel/sync-request.test.js +19 -19
- package/test/encapsulated-child-logger-factory.test.js +8 -8
- package/test/encapsulated-error-handler.test.js +20 -20
- package/test/esm/errorCodes.test.mjs +5 -5
- package/test/esm/esm.test.mjs +3 -3
- package/test/esm/named-exports.mjs +3 -3
- package/test/esm/other.mjs +2 -2
- package/test/fastify-instance.test.js +33 -34
- package/test/{findRoute.test.js → find-route.test.js} +11 -10
- package/test/fluent-schema.test.js +33 -36
- package/test/handler-context.test.js +11 -11
- package/test/has-route.test.js +12 -15
- package/test/header-overflow.test.js +13 -12
- package/test/hooks.on-ready.test.js +2 -2
- package/test/hooks.test.js +25 -25
- package/test/http-methods/copy.test.js +22 -24
- package/test/http-methods/custom-http-methods.test.js +24 -21
- package/test/http-methods/get.test.js +97 -84
- package/test/http-methods/head.test.js +63 -57
- package/test/http-methods/lock.test.js +21 -20
- package/test/http-methods/mkcalendar.test.js +31 -27
- package/test/http-methods/mkcol.test.js +10 -10
- package/test/http-methods/move.test.js +11 -11
- package/test/http-methods/propfind.test.js +32 -27
- package/test/http-methods/proppatch.test.js +21 -19
- package/test/http-methods/report.test.js +32 -27
- package/test/http-methods/search.test.js +52 -47
- package/test/http-methods/trace.test.js +3 -4
- package/test/http-methods/unlock.test.js +10 -10
- package/test/http2/closing.test.js +50 -58
- package/test/http2/constraint.test.js +47 -50
- package/test/http2/head.test.js +18 -19
- package/test/http2/missing-http2-module.test.js +4 -5
- package/test/http2/plain.test.js +31 -31
- package/test/http2/secure-with-fallback.test.js +61 -61
- package/test/http2/secure.test.js +28 -31
- package/test/http2/unknown-http-method.test.js +13 -14
- package/test/https/custom-https-server.test.js +6 -7
- package/test/https/https.test.js +78 -78
- package/test/imports.test.js +5 -6
- package/test/internals/all.test.js +8 -11
- package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
- package/test/internals/context.test.js +9 -11
- package/test/internals/decorator.test.js +20 -21
- package/test/internals/errors.test.js +427 -427
- package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
- package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
- package/test/internals/hooks.test.js +31 -35
- package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
- package/test/internals/logger.test.js +28 -28
- package/test/internals/plugin.test.js +17 -18
- package/test/internals/reply-serialize.test.js +106 -106
- package/test/internals/reply.test.js +620 -585
- package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
- package/test/internals/request-validate.test.js +218 -221
- package/test/internals/request.test.js +225 -107
- package/test/internals/server.test.js +15 -12
- package/test/internals/validation.test.js +35 -36
- package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
- package/test/listen.5.test.js +9 -9
- package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
- package/test/middleware.test.js +4 -5
- package/test/noop-set.test.js +5 -5
- package/test/post-empty-body.test.js +18 -11
- package/test/pretty-print.test.js +59 -49
- package/test/proto-poisoning.test.js +42 -37
- package/test/reply-code.test.js +34 -32
- package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
- package/test/request-error.test.js +122 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +31 -25
- package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
- package/test/route.1.test.js +79 -72
- package/test/route.2.test.js +17 -16
- package/test/route.3.test.js +32 -27
- package/test/route.4.test.js +21 -25
- package/test/route.5.test.js +45 -64
- package/test/route.6.test.js +70 -89
- package/test/route.7.test.js +61 -65
- package/test/route.8.test.js +80 -18
- package/test/router-options.test.js +80 -77
- package/test/same-shape.test.js +5 -5
- package/test/schema-examples.test.js +72 -38
- package/test/serialize-response.test.js +9 -10
- package/test/server.test.js +75 -78
- package/test/set-error-handler.test.js +2 -3
- package/test/stream-serializers.test.js +10 -7
- package/test/sync-routes.test.js +18 -18
- package/test/test-reporter.mjs +68 -0
- package/test/trust-proxy.test.js +51 -45
- package/test/type-provider.test.js +8 -6
- package/test/types/content-type-parser.test-d.ts +1 -1
- package/test/types/fastify.test-d.ts +16 -4
- package/test/types/hooks.test-d.ts +2 -1
- package/test/types/instance.test-d.ts +13 -13
- package/test/types/logger.test-d.ts +2 -2
- package/test/types/plugin.test-d.ts +17 -9
- package/test/types/register.test-d.ts +22 -6
- package/test/types/reply.test-d.ts +1 -1
- package/test/types/route.test-d.ts +34 -4
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +1 -1
- package/test/url-rewriting.test.js +35 -38
- package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
- package/test/validation-error-handling.test.js +259 -285
- package/test/versioned-routes.test.js +126 -113
- package/test/web-api.test.js +48 -37
- package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
- package/types/hooks.d.ts +2 -1
- package/types/instance.d.ts +9 -2
- package/types/register.d.ts +12 -3
- package/types/reply.d.ts +1 -1
- package/types/request.d.ts +2 -6
- package/types/serverFactory.d.ts +3 -3
- package/types/utils.d.ts +13 -5
- package/test/types/import.js +0 -2
package/test/trust-proxy.test.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const { test, before } = t
|
|
3
|
+
const { test, before } = require('node:test')
|
|
5
4
|
const sget = require('simple-get').concat
|
|
6
5
|
const fastify = require('..')
|
|
7
6
|
const helper = require('./helper')
|
|
8
7
|
|
|
9
|
-
const
|
|
8
|
+
const noop = () => {}
|
|
9
|
+
|
|
10
|
+
const sgetForwardedRequest = (app, forHeader, path, protoHeader, testCaseDone) => {
|
|
10
11
|
const headers = {
|
|
11
12
|
'X-Forwarded-For': forHeader,
|
|
12
13
|
'X-Forwarded-Host': 'example.com'
|
|
@@ -18,30 +19,30 @@ const sgetForwardedRequest = (app, forHeader, path, protoHeader) => {
|
|
|
18
19
|
method: 'GET',
|
|
19
20
|
headers,
|
|
20
21
|
url: 'http://localhost:' + app.server.address().port + path
|
|
21
|
-
},
|
|
22
|
+
}, testCaseDone || noop)
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
const testRequestValues = (t, req, options) => {
|
|
25
26
|
if (options.ip) {
|
|
26
|
-
t.ok(req.ip, 'ip is defined')
|
|
27
|
-
t.
|
|
27
|
+
t.assert.ok(req.ip, 'ip is defined')
|
|
28
|
+
t.assert.strictEqual(req.ip, options.ip, 'gets ip from x-forwarded-for')
|
|
28
29
|
}
|
|
29
30
|
if (options.host) {
|
|
30
|
-
t.ok(req.host, 'host is defined')
|
|
31
|
-
t.
|
|
32
|
-
t.ok(req.hostname)
|
|
33
|
-
t.
|
|
31
|
+
t.assert.ok(req.host, 'host is defined')
|
|
32
|
+
t.assert.strictEqual(req.host, options.host, 'gets host from x-forwarded-host')
|
|
33
|
+
t.assert.ok(req.hostname)
|
|
34
|
+
t.assert.strictEqual(req.hostname, options.host, 'gets hostname from x-forwarded-host')
|
|
34
35
|
}
|
|
35
36
|
if (options.ips) {
|
|
36
|
-
t.
|
|
37
|
+
t.assert.deepStrictEqual(req.ips, options.ips, 'gets ips from x-forwarded-for')
|
|
37
38
|
}
|
|
38
39
|
if (options.protocol) {
|
|
39
|
-
t.ok(req.protocol, 'protocol is defined')
|
|
40
|
-
t.
|
|
40
|
+
t.assert.ok(req.protocol, 'protocol is defined')
|
|
41
|
+
t.assert.strictEqual(req.protocol, options.protocol, 'gets protocol from x-forwarded-proto')
|
|
41
42
|
}
|
|
42
43
|
if (options.port) {
|
|
43
|
-
t.ok(req.port, 'port is defined')
|
|
44
|
-
t.
|
|
44
|
+
t.assert.ok(req.port, 'port is defined')
|
|
45
|
+
t.assert.strictEqual(req.port, options.port, 'port is taken from x-forwarded-for or host')
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
|
|
@@ -50,11 +51,13 @@ before(async function () {
|
|
|
50
51
|
[localhost] = await helper.getLoopbackHost()
|
|
51
52
|
})
|
|
52
53
|
|
|
53
|
-
test('trust proxy, not add properties to node req', (t) => {
|
|
54
|
+
test('trust proxy, not add properties to node req', (t, done) => {
|
|
54
55
|
t.plan(14)
|
|
55
56
|
const app = fastify({
|
|
56
57
|
trustProxy: true
|
|
57
58
|
})
|
|
59
|
+
t.after(() => app.close())
|
|
60
|
+
|
|
58
61
|
app.get('/trustproxy', function (req, reply) {
|
|
59
62
|
testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })
|
|
60
63
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
@@ -65,17 +68,23 @@ test('trust proxy, not add properties to node req', (t) => {
|
|
|
65
68
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
66
69
|
})
|
|
67
70
|
|
|
68
|
-
t.teardown(app.close.bind(app))
|
|
69
|
-
|
|
70
71
|
app.listen({ port: 0 }, (err) => {
|
|
71
72
|
app.server.unref()
|
|
72
|
-
t.
|
|
73
|
-
|
|
74
|
-
sgetForwardedRequest(app, '
|
|
73
|
+
t.assert.ifError(err)
|
|
74
|
+
|
|
75
|
+
sgetForwardedRequest(app, '1.1.1.1', '/trustproxy', undefined, completed)
|
|
76
|
+
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxychain', undefined, completed)
|
|
77
|
+
|
|
78
|
+
let pending = 2
|
|
79
|
+
function completed () {
|
|
80
|
+
if (--pending === 0) {
|
|
81
|
+
done()
|
|
82
|
+
}
|
|
83
|
+
}
|
|
75
84
|
})
|
|
76
85
|
})
|
|
77
86
|
|
|
78
|
-
test('trust proxy chain', (t) => {
|
|
87
|
+
test('trust proxy chain', (t, done) => {
|
|
79
88
|
t.plan(9)
|
|
80
89
|
const app = fastify({
|
|
81
90
|
trustProxy: [localhost, '192.168.1.1']
|
|
@@ -86,16 +95,15 @@ test('trust proxy chain', (t) => {
|
|
|
86
95
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
87
96
|
})
|
|
88
97
|
|
|
89
|
-
t.teardown(app.close.bind(app))
|
|
90
|
-
|
|
91
98
|
app.listen({ port: 0 }, (err) => {
|
|
92
99
|
app.server.unref()
|
|
93
|
-
t.
|
|
94
|
-
|
|
100
|
+
t.assert.ifError(err)
|
|
101
|
+
t.after(() => app.close())
|
|
102
|
+
sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined, done)
|
|
95
103
|
})
|
|
96
104
|
})
|
|
97
105
|
|
|
98
|
-
test('trust proxy function', (t) => {
|
|
106
|
+
test('trust proxy function', (t, done) => {
|
|
99
107
|
t.plan(9)
|
|
100
108
|
const app = fastify({
|
|
101
109
|
trustProxy: (address) => address === localhost
|
|
@@ -105,16 +113,15 @@ test('trust proxy function', (t) => {
|
|
|
105
113
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
106
114
|
})
|
|
107
115
|
|
|
108
|
-
t.teardown(app.close.bind(app))
|
|
109
|
-
|
|
110
116
|
app.listen({ port: 0 }, (err) => {
|
|
111
117
|
app.server.unref()
|
|
112
|
-
t.
|
|
113
|
-
|
|
118
|
+
t.assert.ifError(err)
|
|
119
|
+
t.after(() => app.close())
|
|
120
|
+
sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc', undefined, done)
|
|
114
121
|
})
|
|
115
122
|
})
|
|
116
123
|
|
|
117
|
-
test('trust proxy number', (t) => {
|
|
124
|
+
test('trust proxy number', (t, done) => {
|
|
118
125
|
t.plan(10)
|
|
119
126
|
const app = fastify({
|
|
120
127
|
trustProxy: 1
|
|
@@ -124,16 +131,15 @@ test('trust proxy number', (t) => {
|
|
|
124
131
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
125
132
|
})
|
|
126
133
|
|
|
127
|
-
t.teardown(app.close.bind(app))
|
|
128
|
-
|
|
129
134
|
app.listen({ port: 0 }, (err) => {
|
|
130
135
|
app.server.unref()
|
|
131
|
-
t.
|
|
132
|
-
|
|
136
|
+
t.assert.ifError(err)
|
|
137
|
+
t.after(() => app.close())
|
|
138
|
+
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined, done)
|
|
133
139
|
})
|
|
134
140
|
})
|
|
135
141
|
|
|
136
|
-
test('trust proxy IP addresses', (t) => {
|
|
142
|
+
test('trust proxy IP addresses', (t, done) => {
|
|
137
143
|
t.plan(10)
|
|
138
144
|
const app = fastify({
|
|
139
145
|
trustProxy: `${localhost}, 2.2.2.2`
|
|
@@ -143,16 +149,15 @@ test('trust proxy IP addresses', (t) => {
|
|
|
143
149
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
144
150
|
})
|
|
145
151
|
|
|
146
|
-
t.teardown(app.close.bind(app))
|
|
147
|
-
|
|
148
152
|
app.listen({ port: 0 }, (err) => {
|
|
149
153
|
app.server.unref()
|
|
150
|
-
t.
|
|
151
|
-
|
|
154
|
+
t.assert.ifError(err)
|
|
155
|
+
t.after(() => app.close())
|
|
156
|
+
sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined, done)
|
|
152
157
|
})
|
|
153
158
|
})
|
|
154
159
|
|
|
155
|
-
test('trust proxy protocol', (t) => {
|
|
160
|
+
test('trust proxy protocol', (t, done) => {
|
|
156
161
|
t.plan(31)
|
|
157
162
|
const app = fastify({
|
|
158
163
|
trustProxy: true
|
|
@@ -170,13 +175,14 @@ test('trust proxy protocol', (t) => {
|
|
|
170
175
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
171
176
|
})
|
|
172
177
|
|
|
173
|
-
t.
|
|
178
|
+
t.after(() => app.close())
|
|
174
179
|
|
|
175
180
|
app.listen({ port: 0 }, (err) => {
|
|
176
181
|
app.server.unref()
|
|
177
|
-
t.
|
|
182
|
+
t.assert.ifError(err)
|
|
178
183
|
sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocol', 'lorem')
|
|
179
184
|
sgetForwardedRequest(app, '1.1.1.1', '/trustproxynoprotocol')
|
|
180
|
-
sgetForwardedRequest
|
|
185
|
+
// Allow for sgetForwardedRequest requests above to finish
|
|
186
|
+
setTimeout(() => sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor', done))
|
|
181
187
|
})
|
|
182
188
|
})
|
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('..')
|
|
5
5
|
|
|
6
|
-
test('Should export withTypeProvider function', t => {
|
|
6
|
+
test('Should export withTypeProvider function', (t, done) => {
|
|
7
7
|
t.plan(1)
|
|
8
8
|
try {
|
|
9
9
|
Fastify().withTypeProvider()
|
|
10
|
-
t.pass
|
|
10
|
+
t.assert.ok('pass')
|
|
11
|
+
done()
|
|
11
12
|
} catch (e) {
|
|
12
|
-
t.fail()
|
|
13
|
+
t.assert.fail(e)
|
|
13
14
|
}
|
|
14
15
|
})
|
|
15
16
|
|
|
16
|
-
test('Should return same instance', t => {
|
|
17
|
+
test('Should return same instance', (t, done) => {
|
|
17
18
|
t.plan(1)
|
|
18
19
|
const fastify = Fastify()
|
|
19
|
-
t.
|
|
20
|
+
t.assert.strictEqual(fastify, fastify.withTypeProvider())
|
|
21
|
+
done()
|
|
20
22
|
})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fastify, { FastifyBodyParser } from '../../fastify'
|
|
2
2
|
import { expectError, expectType } from 'tsd'
|
|
3
|
-
import { IncomingMessage } from 'http'
|
|
3
|
+
import { IncomingMessage } from 'node:http'
|
|
4
4
|
import { FastifyRequest } from '../../types/request'
|
|
5
5
|
|
|
6
6
|
expectType<void>(fastify().addContentTypeParser('contentType', function (request, payload, done) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ErrorObject as AjvErrorObject } from 'ajv'
|
|
2
|
-
import * as http from 'http'
|
|
3
|
-
import * as http2 from 'http2'
|
|
4
|
-
import * as https from 'https'
|
|
5
|
-
import { Socket } from 'net'
|
|
2
|
+
import * as http from 'node:http'
|
|
3
|
+
import * as http2 from 'node:http2'
|
|
4
|
+
import * as https from 'node:https'
|
|
5
|
+
import { Socket } from 'node:net'
|
|
6
6
|
import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'
|
|
7
7
|
import fastify, {
|
|
8
8
|
ConnectionError,
|
|
@@ -21,6 +21,7 @@ import fastify, {
|
|
|
21
21
|
RouteGenericInterface,
|
|
22
22
|
SafePromiseLike
|
|
23
23
|
} from '../../fastify'
|
|
24
|
+
import { Bindings, ChildLoggerOptions } from '../../types/logger'
|
|
24
25
|
|
|
25
26
|
// FastifyInstance
|
|
26
27
|
// http server
|
|
@@ -219,6 +220,17 @@ expectAssignable<FastifyInstance>(fastify({
|
|
|
219
220
|
}
|
|
220
221
|
}))
|
|
221
222
|
|
|
223
|
+
expectAssignable<FastifyInstance>(fastify({
|
|
224
|
+
childLoggerFactory: function (this: FastifyInstance, logger: FastifyBaseLogger, bindings: Bindings, opts: ChildLoggerOptions, req: RawRequestDefaultExpression) {
|
|
225
|
+
expectType<FastifyBaseLogger>(logger)
|
|
226
|
+
expectType<Bindings>(bindings)
|
|
227
|
+
expectType<ChildLoggerOptions>(opts)
|
|
228
|
+
expectType<RawRequestDefaultExpression>(req)
|
|
229
|
+
expectAssignable<FastifyInstance>(this)
|
|
230
|
+
return logger.child(bindings, opts)
|
|
231
|
+
}
|
|
232
|
+
}))
|
|
233
|
+
|
|
222
234
|
// Thenable
|
|
223
235
|
expectAssignable<PromiseLike<FastifyInstance>>(fastify({ return503OnClosing: true }))
|
|
224
236
|
fastify().then(fastifyInstance => expectAssignable<FastifyInstance>(fastifyInstance))
|
|
@@ -131,7 +131,8 @@ server.addHook('onRoute', function (opts) {
|
|
|
131
131
|
expectType<RouteOptions & { routePath: string; path: string; prefix: string }>(opts)
|
|
132
132
|
})
|
|
133
133
|
|
|
134
|
-
server.addHook('onRegister', (instance, opts)
|
|
134
|
+
server.addHook('onRegister', function (instance, opts) {
|
|
135
|
+
expectType<FastifyInstance>(this)
|
|
135
136
|
expectType<FastifyInstance>(instance)
|
|
136
137
|
expectType<RegisterOptions & FastifyPluginOptions>(opts)
|
|
137
138
|
})
|
|
@@ -13,7 +13,7 @@ import { HookHandlerDoneFunction } from '../../types/hooks'
|
|
|
13
13
|
import { FastifyReply } from '../../types/reply'
|
|
14
14
|
import { FastifyRequest } from '../../types/request'
|
|
15
15
|
import { FastifySchemaControllerOptions, FastifySchemaCompiler, FastifySerializerCompiler } from '../../types/schema'
|
|
16
|
-
import { AddressInfo } from 'net'
|
|
16
|
+
import { AddressInfo } from 'node:net'
|
|
17
17
|
import { Bindings, ChildLoggerOptions } from '../../types/logger'
|
|
18
18
|
|
|
19
19
|
const server = fastify()
|
|
@@ -193,6 +193,8 @@ function invalidSchemaErrorFormatter (err: Error) {
|
|
|
193
193
|
}
|
|
194
194
|
expectError(server.setSchemaErrorFormatter(invalidSchemaErrorFormatter))
|
|
195
195
|
|
|
196
|
+
expectType<FastifyInstance>(server.addHttpMethod('SEARCH', { hasBody: true }))
|
|
197
|
+
|
|
196
198
|
// test listen opts objects
|
|
197
199
|
expectAssignable<PromiseLike<string>>(server.listen())
|
|
198
200
|
expectAssignable<PromiseLike<string>>(server.listen({ port: 3000 }))
|
|
@@ -370,12 +372,11 @@ expectError(server.decorate<string>('test', true))
|
|
|
370
372
|
expectError(server.decorate<(myNumber: number) => number>('test', function (myNumber: number): string {
|
|
371
373
|
return ''
|
|
372
374
|
}))
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
// }))
|
|
375
|
+
expectError(server.decorate<string>('test', {
|
|
376
|
+
getter () {
|
|
377
|
+
return true
|
|
378
|
+
}
|
|
379
|
+
}))
|
|
379
380
|
expectError(server.decorate<string>('test', {
|
|
380
381
|
setter (x) {}
|
|
381
382
|
}))
|
|
@@ -419,12 +420,11 @@ server.decorate('typedTestProperty')
|
|
|
419
420
|
server.decorate('typedTestProperty', null, ['foo'])
|
|
420
421
|
expectError(server.decorate('typedTestProperty', null))
|
|
421
422
|
expectError(server.decorate('typedTestProperty', 'foo'))
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
// }))
|
|
423
|
+
expectError(server.decorate('typedTestProperty', {
|
|
424
|
+
getter () {
|
|
425
|
+
return 'foo'
|
|
426
|
+
}
|
|
427
|
+
}))
|
|
428
428
|
server.decorate('typedTestMethod', function (x) {
|
|
429
429
|
expectType<string>(x)
|
|
430
430
|
expectType<FastifyInstance>(this)
|
|
@@ -7,8 +7,8 @@ import fastify, {
|
|
|
7
7
|
FastifyReply,
|
|
8
8
|
FastifyBaseLogger
|
|
9
9
|
} from '../../fastify'
|
|
10
|
-
import { Server, IncomingMessage, ServerResponse } from 'http'
|
|
11
|
-
import * as fs from 'fs'
|
|
10
|
+
import { Server, IncomingMessage, ServerResponse } from 'node:http'
|
|
11
|
+
import * as fs from 'node:fs'
|
|
12
12
|
import P from 'pino'
|
|
13
13
|
import { ResSerializerReply } from '../../types/logger'
|
|
14
14
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fastify, { FastifyInstance, FastifyPluginOptions, SafePromiseLike } from '../../fastify'
|
|
2
|
-
import * as http from 'http'
|
|
3
|
-
import * as https from 'https'
|
|
2
|
+
import * as http from 'node:http'
|
|
3
|
+
import * as https from 'node:https'
|
|
4
4
|
import { expectType, expectError, expectAssignable } from 'tsd'
|
|
5
5
|
import { FastifyPluginCallback, FastifyPluginAsync } from '../../types/plugin'
|
|
6
6
|
import { FastifyError } from '@fastify/error'
|
|
@@ -10,8 +10,16 @@ interface TestOptions extends FastifyPluginOptions {
|
|
|
10
10
|
option1: string;
|
|
11
11
|
option2: boolean;
|
|
12
12
|
}
|
|
13
|
-
const
|
|
14
|
-
|
|
13
|
+
const testOptions: TestOptions = {
|
|
14
|
+
option1: 'a',
|
|
15
|
+
option2: false,
|
|
16
|
+
}
|
|
17
|
+
const testPluginOpts: FastifyPluginCallback<TestOptions> = function (instance, opts, done) {
|
|
18
|
+
expectType<TestOptions>(opts)
|
|
19
|
+
}
|
|
20
|
+
const testPluginOptsAsync: FastifyPluginAsync<TestOptions> = async function (instance, opts) {
|
|
21
|
+
expectType<TestOptions>(opts)
|
|
22
|
+
}
|
|
15
23
|
|
|
16
24
|
const testPluginOptsWithType = (instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
|
|
17
25
|
const testPluginOptsWithTypeAsync = async (instance: FastifyInstance, opts: FastifyPluginOptions) => { }
|
|
@@ -39,7 +47,7 @@ expectAssignable<FastifyInstance>(fastify().register(function (instance, opts):
|
|
|
39
47
|
expectAssignable<FastifyInstance>(fastify().register(async function (instance, opts) { }, () => { }))
|
|
40
48
|
expectAssignable<FastifyInstance>(fastify().register(async function (instance, opts) { }, { logLevel: 'info', prefix: 'foobar' }))
|
|
41
49
|
|
|
42
|
-
expectError(fastify().register(function (instance, opts, done) { }, { logLevel: '' })) // must use a valid logLevel
|
|
50
|
+
expectError(fastify().register(function (instance, opts, done) { }, { ...testOptions, logLevel: '' })) // must use a valid logLevel
|
|
43
51
|
|
|
44
52
|
const httpsServer = fastify({ https: {} })
|
|
45
53
|
expectError<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> & Promise<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>>(httpsServer)
|
|
@@ -48,7 +56,7 @@ expectType<FastifyInstance<https.Server, http.IncomingMessage, http.ServerRespon
|
|
|
48
56
|
|
|
49
57
|
// Chainable
|
|
50
58
|
httpsServer
|
|
51
|
-
.register(testPluginOpts)
|
|
59
|
+
.register(testPluginOpts, testOptions)
|
|
52
60
|
.after((_error) => { })
|
|
53
61
|
.ready((_error) => { })
|
|
54
62
|
.close(() => { })
|
|
@@ -57,7 +65,7 @@ httpsServer
|
|
|
57
65
|
expectAssignable<PromiseLike<undefined>>(httpsServer.after())
|
|
58
66
|
expectAssignable<PromiseLike<undefined>>(httpsServer.close())
|
|
59
67
|
expectAssignable<PromiseLike<undefined>>(httpsServer.ready())
|
|
60
|
-
expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOpts))
|
|
68
|
+
expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOpts, testOptions))
|
|
61
69
|
expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWithType))
|
|
62
70
|
expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWithTypeAsync))
|
|
63
71
|
expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWithType, { prefix: '/test' }))
|
|
@@ -66,6 +74,6 @@ expectAssignable<PromiseLike<undefined>>(httpsServer.register(testPluginOptsWith
|
|
|
66
74
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
67
75
|
async function testAsync (): Promise<void> {
|
|
68
76
|
await httpsServer
|
|
69
|
-
.register(testPluginOpts)
|
|
70
|
-
.register(testPluginOpts)
|
|
77
|
+
.register(testPluginOpts, testOptions)
|
|
78
|
+
.register(testPluginOpts, testOptions)
|
|
71
79
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
2
|
-
import { IncomingMessage, Server, ServerResponse } from 'http'
|
|
3
|
-
import { Http2Server, Http2ServerRequest, Http2ServerResponse } from 'http2'
|
|
2
|
+
import { IncomingMessage, Server, ServerResponse } from 'node:http'
|
|
3
|
+
import { Http2Server, Http2ServerRequest, Http2ServerResponse } from 'node:http2'
|
|
4
4
|
import fastify, { FastifyInstance, FastifyError, FastifyLoggerInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, RawServerDefault } from '../../fastify'
|
|
5
5
|
|
|
6
6
|
const testPluginCallback: FastifyPluginCallback = function (instance, opts, done) { }
|
|
@@ -48,14 +48,22 @@ const testPluginWithHttp2: FastifyPluginCallback<TestOptions, Http2Server> = fun
|
|
|
48
48
|
const testPluginWithHttp2Async: FastifyPluginAsync<TestOptions, Http2Server> = async function (instance, opts) { }
|
|
49
49
|
const testPluginWithHttp2WithType = (instance: ServerWithHttp2, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
|
|
50
50
|
const testPluginWithHttp2WithTypeAsync = async (instance: ServerWithHttp2, opts: FastifyPluginOptions) => { }
|
|
51
|
+
const testOptions: TestOptions = {
|
|
52
|
+
option1: 'a',
|
|
53
|
+
option2: false,
|
|
54
|
+
}
|
|
51
55
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginCallback))
|
|
52
56
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginAsync))
|
|
53
57
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOpts))
|
|
54
58
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOptsAsync))
|
|
55
59
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOptsWithType))
|
|
56
60
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginOptsWithTypeAsync))
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
// @ts-expect-error
|
|
62
|
+
serverWithHttp2.register(testPluginWithHttp2)
|
|
63
|
+
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2, testOptions))
|
|
64
|
+
// @ts-expect-error
|
|
65
|
+
serverWithHttp2.register(testPluginWithHttp2Async)
|
|
66
|
+
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2Async, testOptions))
|
|
59
67
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2WithType))
|
|
60
68
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register(testPluginWithHttp2WithTypeAsync))
|
|
61
69
|
expectAssignable<ServerWithHttp2>(serverWithHttp2.register((instance) => {
|
|
@@ -85,8 +93,12 @@ expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPlu
|
|
|
85
93
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsAsync))
|
|
86
94
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsWithType))
|
|
87
95
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsWithTypeAsync))
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
// @ts-expect-error
|
|
97
|
+
serverWithTypeProvider.register(testPluginWithTypeProvider)
|
|
98
|
+
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProvider, testOptions))
|
|
99
|
+
// @ts-expect-error
|
|
100
|
+
serverWithTypeProvider.register(testPluginWithTypeProviderAsync)
|
|
101
|
+
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderAsync, testOptions))
|
|
90
102
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderWithType))
|
|
91
103
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderWithTypeAsync))
|
|
92
104
|
expectAssignable<ServerWithTypeProvider>(serverWithTypeProvider.register((instance) => {
|
|
@@ -128,8 +140,12 @@ expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogge
|
|
|
128
140
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsAsync))
|
|
129
141
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsWithType))
|
|
130
142
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginOptsWithTypeAsync))
|
|
143
|
+
// @ts-expect-error
|
|
131
144
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger))
|
|
145
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger, testOptions))
|
|
146
|
+
// @ts-expect-error
|
|
132
147
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync))
|
|
148
|
+
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync, testOptions))
|
|
133
149
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithType))
|
|
134
150
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithTypeAsync))
|
|
135
151
|
expectAssignable<ServerWithTypeProviderAndLogger>(serverWithTypeProviderAndLogger.register((instance) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Buffer } from 'buffer'
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
2
|
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
3
3
|
import fastify, { FastifyContextConfig, FastifyReply, FastifyRequest, FastifySchema, FastifyTypeProviderDefault, RawRequestDefaultExpression, RouteHandler, RouteHandlerMethod } from '../../fastify'
|
|
4
4
|
import { FastifyInstance } from '../../types/instance'
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyError } from '@fastify/error'
|
|
2
|
-
import * as http from 'http'
|
|
2
|
+
import * as http from 'node:http'
|
|
3
3
|
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
4
4
|
import fastify, { FastifyInstance, FastifyReply, FastifyRequest, RouteHandlerMethod } from '../../fastify'
|
|
5
5
|
import { RequestPayload } from '../../types/hooks'
|
|
@@ -398,9 +398,21 @@ type LowerCaseHTTPMethods = 'delete' | 'get' | 'head' | 'patch' | 'post' | 'put'
|
|
|
398
398
|
})
|
|
399
399
|
})
|
|
400
400
|
|
|
401
|
-
|
|
401
|
+
expectType<FastifyInstance>(fastify().route({
|
|
402
|
+
url: '/',
|
|
403
|
+
method: 'CONNECT', // not a valid method but could be implemented by the user
|
|
404
|
+
handler: routeHandler
|
|
405
|
+
}))
|
|
406
|
+
|
|
407
|
+
expectType<FastifyInstance>(fastify().route({
|
|
402
408
|
url: '/',
|
|
403
|
-
method: '
|
|
409
|
+
method: 'OPTIONS',
|
|
410
|
+
handler: routeHandler
|
|
411
|
+
}))
|
|
412
|
+
|
|
413
|
+
expectType<FastifyInstance>(fastify().route({
|
|
414
|
+
url: '/',
|
|
415
|
+
method: 'OPTION', // OPTION is a typo for OPTIONS
|
|
404
416
|
handler: routeHandler
|
|
405
417
|
}))
|
|
406
418
|
|
|
@@ -410,7 +422,7 @@ expectType<FastifyInstance>(fastify().route({
|
|
|
410
422
|
handler: routeHandler
|
|
411
423
|
}))
|
|
412
424
|
|
|
413
|
-
|
|
425
|
+
expectType<FastifyInstance>(fastify().route({
|
|
414
426
|
url: '/',
|
|
415
427
|
method: ['GET', 'POST', 'OPTION'], // OPTION is a typo for OPTIONS
|
|
416
428
|
handler: routeHandler
|
|
@@ -506,3 +518,21 @@ expectType<FastifyInstance>(fastify().route({
|
|
|
506
518
|
method: ['put', 'patch'],
|
|
507
519
|
handler: routeHandlerWithReturnValue
|
|
508
520
|
}))
|
|
521
|
+
|
|
522
|
+
expectType<FastifyInstance>(fastify().route({
|
|
523
|
+
url: '/',
|
|
524
|
+
method: 'GET',
|
|
525
|
+
handler: (req) => {
|
|
526
|
+
expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.method)
|
|
527
|
+
expectAssignable<string | Array<string>>(req.routeOptions.method)
|
|
528
|
+
}
|
|
529
|
+
}))
|
|
530
|
+
|
|
531
|
+
expectType<FastifyInstance>(fastify().route({
|
|
532
|
+
url: '/',
|
|
533
|
+
method: ['HEAD', 'GET'],
|
|
534
|
+
handler: (req) => {
|
|
535
|
+
expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.method)
|
|
536
|
+
expectAssignable<string | Array<string>>(req.routeOptions.method)
|
|
537
|
+
}
|
|
538
|
+
}))
|
|
@@ -8,7 +8,7 @@ import fastify, {
|
|
|
8
8
|
SafePromiseLike
|
|
9
9
|
} from '../../fastify'
|
|
10
10
|
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
11
|
-
import { IncomingHttpHeaders } from 'http'
|
|
11
|
+
import { IncomingHttpHeaders } from 'node:http'
|
|
12
12
|
import { Type, TSchema, Static } from '@sinclair/typebox'
|
|
13
13
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts'
|
|
14
14
|
|