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/500s.test.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const Fastify = require('..')
|
|
6
5
|
const symbols = require('../lib/symbols.js')
|
|
7
6
|
|
|
8
|
-
test('default 500', t => {
|
|
7
|
+
test('default 500', (t, done) => {
|
|
9
8
|
t.plan(4)
|
|
10
9
|
|
|
11
10
|
const fastify = Fastify()
|
|
12
|
-
t.
|
|
11
|
+
t.after(() => fastify.close())
|
|
13
12
|
|
|
14
13
|
fastify.get('/', function (req, reply) {
|
|
15
14
|
reply.send(new Error('kaboom'))
|
|
@@ -19,30 +18,31 @@ test('default 500', t => {
|
|
|
19
18
|
method: 'GET',
|
|
20
19
|
url: '/'
|
|
21
20
|
}, (err, res) => {
|
|
22
|
-
t.
|
|
23
|
-
t.
|
|
24
|
-
t.
|
|
25
|
-
t.
|
|
21
|
+
t.assert.ifError(err)
|
|
22
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
23
|
+
t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
|
|
24
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
26
25
|
error: 'Internal Server Error',
|
|
27
26
|
message: 'kaboom',
|
|
28
27
|
statusCode: 500
|
|
29
28
|
})
|
|
29
|
+
done()
|
|
30
30
|
})
|
|
31
31
|
})
|
|
32
32
|
|
|
33
|
-
test('custom 500', t => {
|
|
33
|
+
test('custom 500', (t, done) => {
|
|
34
34
|
t.plan(6)
|
|
35
35
|
|
|
36
36
|
const fastify = Fastify()
|
|
37
|
-
t.
|
|
37
|
+
t.after(() => fastify.close())
|
|
38
38
|
|
|
39
39
|
fastify.get('/', function (req, reply) {
|
|
40
40
|
reply.send(new Error('kaboom'))
|
|
41
41
|
})
|
|
42
42
|
|
|
43
43
|
fastify.setErrorHandler(function (err, request, reply) {
|
|
44
|
-
t.
|
|
45
|
-
t.
|
|
44
|
+
t.assert.ok(typeof request === 'object')
|
|
45
|
+
t.assert.ok(request instanceof fastify[symbols.kRequest].parent)
|
|
46
46
|
reply
|
|
47
47
|
.code(500)
|
|
48
48
|
.type('text/plain')
|
|
@@ -53,18 +53,19 @@ test('custom 500', t => {
|
|
|
53
53
|
method: 'GET',
|
|
54
54
|
url: '/'
|
|
55
55
|
}, (err, res) => {
|
|
56
|
-
t.
|
|
57
|
-
t.
|
|
58
|
-
t.
|
|
59
|
-
t.
|
|
56
|
+
t.assert.ifError(err)
|
|
57
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
58
|
+
t.assert.strictEqual(res.headers['content-type'], 'text/plain')
|
|
59
|
+
t.assert.deepStrictEqual(res.payload.toString(), 'an error happened: kaboom')
|
|
60
|
+
done()
|
|
60
61
|
})
|
|
61
62
|
})
|
|
62
63
|
|
|
63
|
-
test('encapsulated 500', t => {
|
|
64
|
-
t.plan(
|
|
64
|
+
test('encapsulated 500', async t => {
|
|
65
|
+
t.plan(8)
|
|
65
66
|
|
|
66
67
|
const fastify = Fastify()
|
|
67
|
-
t.
|
|
68
|
+
t.after(() => fastify.close())
|
|
68
69
|
|
|
69
70
|
fastify.get('/', function (req, reply) {
|
|
70
71
|
reply.send(new Error('kaboom'))
|
|
@@ -76,8 +77,8 @@ test('encapsulated 500', t => {
|
|
|
76
77
|
})
|
|
77
78
|
|
|
78
79
|
f.setErrorHandler(function (err, request, reply) {
|
|
79
|
-
t.
|
|
80
|
-
t.
|
|
80
|
+
t.assert.ok(typeof request === 'object')
|
|
81
|
+
t.assert.ok(request instanceof fastify[symbols.kRequest].parent)
|
|
81
82
|
reply
|
|
82
83
|
.code(500)
|
|
83
84
|
.type('text/plain')
|
|
@@ -87,36 +88,38 @@ test('encapsulated 500', t => {
|
|
|
87
88
|
done()
|
|
88
89
|
}, { prefix: 'test' })
|
|
89
90
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
t.equal(res.statusCode, 500)
|
|
96
|
-
t.equal(res.headers['content-type'], 'text/plain')
|
|
97
|
-
t.same(res.payload.toString(), 'an error happened: kaboom')
|
|
98
|
-
})
|
|
91
|
+
{
|
|
92
|
+
const response = await fastify.inject({
|
|
93
|
+
method: 'GET',
|
|
94
|
+
url: '/test'
|
|
95
|
+
})
|
|
99
96
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
97
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
98
|
+
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
99
|
+
t.assert.deepStrictEqual(response.payload.toString(), 'an error happened: kaboom')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
const response = await fastify.inject({
|
|
104
|
+
method: 'GET',
|
|
105
|
+
url: '/'
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
109
|
+
t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
|
|
110
|
+
t.assert.deepStrictEqual(JSON.parse(response.payload), {
|
|
108
111
|
error: 'Internal Server Error',
|
|
109
112
|
message: 'kaboom',
|
|
110
113
|
statusCode: 500
|
|
111
114
|
})
|
|
112
|
-
}
|
|
115
|
+
}
|
|
113
116
|
})
|
|
114
117
|
|
|
115
|
-
test('custom 500 with hooks', t => {
|
|
118
|
+
test('custom 500 with hooks', (t, done) => {
|
|
116
119
|
t.plan(7)
|
|
117
120
|
|
|
118
121
|
const fastify = Fastify()
|
|
119
|
-
t.
|
|
122
|
+
t.after(() => fastify.close())
|
|
120
123
|
|
|
121
124
|
fastify.get('/', function (req, reply) {
|
|
122
125
|
reply.send(new Error('kaboom'))
|
|
@@ -130,15 +133,15 @@ test('custom 500 with hooks', t => {
|
|
|
130
133
|
})
|
|
131
134
|
|
|
132
135
|
fastify.addHook('onSend', (req, res, payload, done) => {
|
|
133
|
-
t.ok('called', 'onSend')
|
|
136
|
+
t.assert.ok('called', 'onSend')
|
|
134
137
|
done()
|
|
135
138
|
})
|
|
136
139
|
fastify.addHook('onRequest', (req, res, done) => {
|
|
137
|
-
t.ok('called', 'onRequest')
|
|
140
|
+
t.assert.ok('called', 'onRequest')
|
|
138
141
|
done()
|
|
139
142
|
})
|
|
140
143
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
141
|
-
t.ok('called', 'onResponse')
|
|
144
|
+
t.assert.ok('called', 'onResponse')
|
|
142
145
|
done()
|
|
143
146
|
})
|
|
144
147
|
|
|
@@ -146,54 +149,59 @@ test('custom 500 with hooks', t => {
|
|
|
146
149
|
method: 'GET',
|
|
147
150
|
url: '/'
|
|
148
151
|
}, (err, res) => {
|
|
149
|
-
t.
|
|
150
|
-
t.
|
|
151
|
-
t.
|
|
152
|
-
t.
|
|
152
|
+
t.assert.ifError(err)
|
|
153
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
154
|
+
t.assert.strictEqual(res.headers['content-type'], 'text/plain')
|
|
155
|
+
t.assert.deepStrictEqual(res.payload.toString(), 'an error happened: kaboom')
|
|
156
|
+
done()
|
|
153
157
|
})
|
|
154
158
|
})
|
|
155
159
|
|
|
156
|
-
test('cannot set errorHandler after binding', t => {
|
|
160
|
+
test('cannot set errorHandler after binding', (t, done) => {
|
|
157
161
|
t.plan(2)
|
|
158
162
|
|
|
159
163
|
const fastify = Fastify()
|
|
160
|
-
t.
|
|
164
|
+
t.after(() => fastify.close())
|
|
161
165
|
|
|
162
166
|
fastify.listen({ port: 0 }, err => {
|
|
163
|
-
t.
|
|
167
|
+
t.assert.ifError(err)
|
|
164
168
|
|
|
165
169
|
try {
|
|
166
170
|
fastify.setErrorHandler(() => { })
|
|
167
|
-
t.fail()
|
|
171
|
+
t.assert.fail()
|
|
168
172
|
} catch (e) {
|
|
169
|
-
t.
|
|
173
|
+
t.assert.ok(true)
|
|
174
|
+
} finally {
|
|
175
|
+
done()
|
|
170
176
|
}
|
|
171
177
|
})
|
|
172
178
|
})
|
|
173
179
|
|
|
174
|
-
test('cannot set childLoggerFactory after binding', t => {
|
|
180
|
+
test('cannot set childLoggerFactory after binding', (t, done) => {
|
|
175
181
|
t.plan(2)
|
|
176
182
|
|
|
177
183
|
const fastify = Fastify()
|
|
178
|
-
t.
|
|
184
|
+
t.after(() => fastify.close())
|
|
179
185
|
|
|
180
186
|
fastify.listen({ port: 0 }, err => {
|
|
181
|
-
t.
|
|
187
|
+
t.assert.ifError(err)
|
|
182
188
|
|
|
183
189
|
try {
|
|
184
190
|
fastify.setChildLoggerFactory(() => { })
|
|
185
|
-
t.fail()
|
|
191
|
+
t.assert.fail()
|
|
186
192
|
} catch (e) {
|
|
187
|
-
t.
|
|
193
|
+
t.assert.ok(true)
|
|
194
|
+
} finally {
|
|
195
|
+
done()
|
|
188
196
|
}
|
|
189
197
|
})
|
|
190
198
|
})
|
|
191
199
|
|
|
192
|
-
test('catch synchronous errors', t => {
|
|
200
|
+
test('catch synchronous errors', (t, done) => {
|
|
193
201
|
t.plan(3)
|
|
194
202
|
|
|
195
203
|
const fastify = Fastify()
|
|
196
|
-
t.
|
|
204
|
+
t.after(() => fastify.close())
|
|
197
205
|
|
|
198
206
|
fastify.setErrorHandler((_, req, reply) => {
|
|
199
207
|
throw new Error('kaboom2')
|
|
@@ -211,12 +219,13 @@ test('catch synchronous errors', t => {
|
|
|
211
219
|
},
|
|
212
220
|
payload: JSON.stringify({ hello: 'world' }).substring(0, 5)
|
|
213
221
|
}, (err, res) => {
|
|
214
|
-
t.
|
|
215
|
-
t.
|
|
216
|
-
t.
|
|
222
|
+
t.assert.ifError(err)
|
|
223
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
224
|
+
t.assert.deepStrictEqual(res.json(), {
|
|
217
225
|
error: 'Internal Server Error',
|
|
218
226
|
message: 'kaboom2',
|
|
219
227
|
statusCode: 500
|
|
220
228
|
})
|
|
229
|
+
done()
|
|
221
230
|
})
|
|
222
231
|
})
|
|
@@ -1,117 +1,121 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const Fastify = require('..')
|
|
6
5
|
const sget = require('simple-get').concat
|
|
7
6
|
|
|
8
|
-
test('allow unsafe regex', t => {
|
|
7
|
+
test('allow unsafe regex', (t, done) => {
|
|
9
8
|
t.plan(4)
|
|
10
9
|
|
|
11
10
|
const fastify = Fastify({
|
|
12
11
|
allowUnsafeRegex: false
|
|
13
12
|
})
|
|
14
|
-
t.
|
|
13
|
+
t.after(() => fastify.close())
|
|
15
14
|
|
|
16
15
|
fastify.get('/:foo(^[0-9]*$)', (req, reply) => {
|
|
17
16
|
reply.send({ foo: req.params.foo })
|
|
18
17
|
})
|
|
19
18
|
|
|
20
19
|
fastify.listen({ port: 0 }, err => {
|
|
21
|
-
t.
|
|
20
|
+
t.assert.ifError(err)
|
|
22
21
|
|
|
23
22
|
sget({
|
|
24
23
|
method: 'GET',
|
|
25
24
|
url: 'http://localhost:' + fastify.server.address().port + '/1234'
|
|
26
25
|
}, (err, response, body) => {
|
|
27
|
-
t.
|
|
28
|
-
t.
|
|
29
|
-
t.
|
|
26
|
+
t.assert.ifError(err)
|
|
27
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
28
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
30
29
|
foo: '1234'
|
|
31
30
|
})
|
|
31
|
+
done()
|
|
32
32
|
})
|
|
33
33
|
})
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
test('allow unsafe regex not match', t => {
|
|
36
|
+
test('allow unsafe regex not match', (t, done) => {
|
|
37
37
|
t.plan(3)
|
|
38
38
|
|
|
39
39
|
const fastify = Fastify({
|
|
40
40
|
allowUnsafeRegex: false
|
|
41
41
|
})
|
|
42
|
-
t.
|
|
42
|
+
t.after(() => fastify.close())
|
|
43
43
|
|
|
44
44
|
fastify.get('/:foo(^[0-9]*$)', (req, reply) => {
|
|
45
45
|
reply.send({ foo: req.params.foo })
|
|
46
46
|
})
|
|
47
47
|
|
|
48
48
|
fastify.listen({ port: 0 }, err => {
|
|
49
|
-
t.
|
|
49
|
+
t.assert.ifError(err)
|
|
50
50
|
|
|
51
51
|
sget({
|
|
52
52
|
method: 'GET',
|
|
53
53
|
url: 'http://localhost:' + fastify.server.address().port + '/a1234'
|
|
54
54
|
}, (err, response, body) => {
|
|
55
|
-
t.
|
|
56
|
-
t.
|
|
55
|
+
t.assert.ifError(err)
|
|
56
|
+
t.assert.strictEqual(response.statusCode, 404)
|
|
57
|
+
done()
|
|
57
58
|
})
|
|
58
59
|
})
|
|
59
60
|
})
|
|
60
61
|
|
|
61
|
-
test('allow unsafe regex not safe', t => {
|
|
62
|
+
test('allow unsafe regex not safe', (t, done) => {
|
|
62
63
|
t.plan(1)
|
|
63
64
|
|
|
64
65
|
const fastify = Fastify({
|
|
65
66
|
allowUnsafeRegex: false
|
|
66
67
|
})
|
|
67
|
-
t.
|
|
68
|
+
t.after(() => fastify.close())
|
|
68
69
|
|
|
69
|
-
t.throws(() => {
|
|
70
|
+
t.assert.throws(() => {
|
|
70
71
|
fastify.get('/:foo(^([0-9]+){4}$)', (req, reply) => {
|
|
71
72
|
reply.send({ foo: req.params.foo })
|
|
72
73
|
})
|
|
73
74
|
})
|
|
75
|
+
done()
|
|
74
76
|
})
|
|
75
77
|
|
|
76
|
-
test('allow unsafe regex not safe by default', t => {
|
|
78
|
+
test('allow unsafe regex not safe by default', (t, done) => {
|
|
77
79
|
t.plan(1)
|
|
78
80
|
|
|
79
81
|
const fastify = Fastify()
|
|
80
|
-
t.
|
|
82
|
+
t.after(() => fastify.close())
|
|
81
83
|
|
|
82
|
-
t.throws(() => {
|
|
84
|
+
t.assert.throws(() => {
|
|
83
85
|
fastify.get('/:foo(^([0-9]+){4}$)', (req, reply) => {
|
|
84
86
|
reply.send({ foo: req.params.foo })
|
|
85
87
|
})
|
|
86
88
|
})
|
|
89
|
+
done()
|
|
87
90
|
})
|
|
88
91
|
|
|
89
|
-
test('allow unsafe regex allow unsafe', t => {
|
|
92
|
+
test('allow unsafe regex allow unsafe', (t, done) => {
|
|
90
93
|
t.plan(5)
|
|
91
94
|
|
|
92
95
|
const fastify = Fastify({
|
|
93
96
|
allowUnsafeRegex: true
|
|
94
97
|
})
|
|
95
|
-
t.
|
|
98
|
+
t.after(() => fastify.close())
|
|
96
99
|
|
|
97
|
-
t.doesNotThrow(() => {
|
|
100
|
+
t.assert.doesNotThrow(() => {
|
|
98
101
|
fastify.get('/:foo(^([0-9]+){4}$)', (req, reply) => {
|
|
99
102
|
reply.send({ foo: req.params.foo })
|
|
100
103
|
})
|
|
101
104
|
})
|
|
102
105
|
|
|
103
106
|
fastify.listen({ port: 0 }, err => {
|
|
104
|
-
t.
|
|
107
|
+
t.assert.ifError(err)
|
|
105
108
|
|
|
106
109
|
sget({
|
|
107
110
|
method: 'GET',
|
|
108
111
|
url: 'http://localhost:' + fastify.server.address().port + '/1234'
|
|
109
112
|
}, (err, response, body) => {
|
|
110
|
-
t.
|
|
111
|
-
t.
|
|
112
|
-
t.
|
|
113
|
+
t.assert.ifError(err)
|
|
114
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
115
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
113
116
|
foo: '1234'
|
|
114
117
|
})
|
|
118
|
+
done()
|
|
115
119
|
})
|
|
116
120
|
})
|
|
117
121
|
})
|
package/test/als.test.js
CHANGED
|
@@ -1,50 +1,40 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { AsyncLocalStorage } = require('node:async_hooks')
|
|
4
|
-
const
|
|
4
|
+
const { test } = require('node:test')
|
|
5
5
|
const Fastify = require('..')
|
|
6
6
|
const sget = require('simple-get').concat
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
t.
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
test('Async Local Storage test', (t, done) => {
|
|
9
|
+
t.plan(13)
|
|
10
|
+
if (!AsyncLocalStorage) {
|
|
11
|
+
t.skip('AsyncLocalStorage not available, skipping test')
|
|
12
|
+
process.exit(0)
|
|
13
|
+
}
|
|
12
14
|
|
|
13
|
-
const storage = new AsyncLocalStorage()
|
|
14
|
-
const app = Fastify({ logger: false })
|
|
15
|
+
const storage = new AsyncLocalStorage()
|
|
16
|
+
const app = Fastify({ logger: false })
|
|
15
17
|
|
|
16
|
-
let counter = 0
|
|
17
|
-
app.addHook('onRequest', (req, reply, next) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
app.get('/', function (request, reply) {
|
|
23
|
-
t.ok(storage.getStore())
|
|
24
|
-
const id = storage.getStore().id
|
|
25
|
-
reply.send({ id })
|
|
26
|
-
})
|
|
18
|
+
let counter = 0
|
|
19
|
+
app.addHook('onRequest', (req, reply, next) => {
|
|
20
|
+
const id = counter++
|
|
21
|
+
storage.run({ id }, next)
|
|
22
|
+
})
|
|
27
23
|
|
|
28
|
-
app.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
})
|
|
24
|
+
app.get('/', function (request, reply) {
|
|
25
|
+
t.assert.ok(storage.getStore())
|
|
26
|
+
const id = storage.getStore().id
|
|
27
|
+
reply.send({ id })
|
|
28
|
+
})
|
|
33
29
|
|
|
34
|
-
app.
|
|
35
|
-
|
|
30
|
+
app.post('/', function (request, reply) {
|
|
31
|
+
t.assert.ok(storage.getStore())
|
|
32
|
+
const id = storage.getStore().id
|
|
33
|
+
reply.send({ id })
|
|
34
|
+
})
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
url: 'http://localhost:' + app.server.address().port,
|
|
40
|
-
body: {
|
|
41
|
-
hello: 'world'
|
|
42
|
-
},
|
|
43
|
-
json: true
|
|
44
|
-
}, (err, response, body) => {
|
|
45
|
-
t.error(err)
|
|
46
|
-
t.equal(response.statusCode, 200)
|
|
47
|
-
t.same(body, { id: 0 })
|
|
36
|
+
app.listen({ port: 0 }, function (err, address) {
|
|
37
|
+
t.assert.ifError(err)
|
|
48
38
|
|
|
49
39
|
sget({
|
|
50
40
|
method: 'POST',
|
|
@@ -54,20 +44,33 @@ app.listen({ port: 0 }, function (err, address) {
|
|
|
54
44
|
},
|
|
55
45
|
json: true
|
|
56
46
|
}, (err, response, body) => {
|
|
57
|
-
t.
|
|
58
|
-
t.
|
|
59
|
-
t.
|
|
47
|
+
t.assert.ifError(err)
|
|
48
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
49
|
+
t.assert.deepStrictEqual(body, { id: 0 })
|
|
60
50
|
|
|
61
51
|
sget({
|
|
62
|
-
method: '
|
|
52
|
+
method: 'POST',
|
|
63
53
|
url: 'http://localhost:' + app.server.address().port,
|
|
54
|
+
body: {
|
|
55
|
+
hello: 'world'
|
|
56
|
+
},
|
|
64
57
|
json: true
|
|
65
58
|
}, (err, response, body) => {
|
|
66
|
-
t.
|
|
67
|
-
t.
|
|
68
|
-
t.
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
t.assert.ifError(err)
|
|
60
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
61
|
+
t.assert.deepStrictEqual(body, { id: 1 })
|
|
62
|
+
|
|
63
|
+
sget({
|
|
64
|
+
method: 'GET',
|
|
65
|
+
url: 'http://localhost:' + app.server.address().port,
|
|
66
|
+
json: true
|
|
67
|
+
}, (err, response, body) => {
|
|
68
|
+
t.assert.ifError(err)
|
|
69
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
70
|
+
t.assert.deepStrictEqual(body, { id: 2 })
|
|
71
|
+
app.close()
|
|
72
|
+
done()
|
|
73
|
+
})
|
|
71
74
|
})
|
|
72
75
|
})
|
|
73
76
|
})
|