@fastify/reply-from 12.0.0 → 12.0.2
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/.github/dependabot.yml +1 -1
- package/.github/workflows/ci.yml +0 -1
- package/README.md +16 -16
- package/eslint.config.js +6 -0
- package/examples/example.js +2 -2
- package/index.js +6 -8
- package/lib/request.js +4 -4
- package/package.json +37 -10
- package/test/async-route-handler.test.js +1 -1
- package/test/base-get.test.js +1 -1
- package/test/base-path.test.js +2 -2
- package/test/base-querystring.test.js +1 -1
- package/test/core-with-path-in-base.test.js +1 -1
- package/test/custom-undici-instance.test.js +2 -2
- package/test/disable-request-logging.test.js +3 -3
- package/test/fastify-multipart-incompatibility.test.js +2 -2
- package/test/fix-GHSA-v2v2-hph8-q5xp.test.js +52 -36
- package/test/full-delete-http2.test.js +2 -2
- package/test/full-get-test.test.js +1 -1
- package/test/full-https-get.test.js +1 -1
- package/test/full-post-extended-content-type.test.js +1 -1
- package/test/full-post-http2.test.js +1 -1
- package/test/full-post-stream-core.test.js +3 -3
- package/test/full-post-stream.test.js +3 -3
- package/test/full-post.test.js +2 -2
- package/test/full-querystring-rewrite-option-complex.test.js +1 -1
- package/test/full-querystring-rewrite-option-function-request.test.js +3 -3
- package/test/full-querystring-rewrite-option-function.test.js +1 -1
- package/test/full-querystring-rewrite-option.test.js +1 -1
- package/test/full-querystring-rewrite-string.test.js +1 -1
- package/test/full-querystring-rewrite.test.js +1 -1
- package/test/full-querystring.test.js +1 -1
- package/test/full-rewrite-body-content-type.test.js +10 -7
- package/test/full-rewrite-body-http.test.js +2 -2
- package/test/full-rewrite-body-to-empty-string.test.js +2 -2
- package/test/full-rewrite-body-to-null.test.js +2 -2
- package/test/full-rewrite-body.test.js +2 -2
- package/test/get-upstream-http.test.js +3 -3
- package/test/get-upstream-undici.test.js +1 -1
- package/test/get-with-body.test.js +1 -1
- package/test/head-with-body.test.js +1 -7
- package/test/host-header.test.js +4 -4
- package/test/http-agents.test.js +1 -1
- package/test/http-global-agent.test.js +1 -1
- package/test/http-http2.test.js +2 -2
- package/test/http-invalid-target.test.js +1 -1
- package/test/http-retry.test.js +3 -3
- package/test/http-timeout.test.js +4 -4
- package/test/http2-http2.test.js +2 -2
- package/test/http2-https.test.js +2 -2
- package/test/http2-invalid-target.test.js +1 -1
- package/test/http2-target-crash.test.js +2 -2
- package/test/http2-target-multi-crash.test.js +1 -1
- package/test/http2-timeout-disabled.test.js +2 -2
- package/test/http2-timeout.test.js +4 -4
- package/test/https-agents.test.js +1 -1
- package/test/https-global-agent.test.js +1 -1
- package/test/method.test.js +3 -3
- package/test/modifyCoreObjects-false.test.js +1 -1
- package/test/no-body-opts-with-get.test.js +2 -2
- package/test/no-body-opts-with-head.test.js +2 -2
- package/test/no-stream-body-option.test.js +2 -2
- package/test/on-error.test.js +2 -2
- package/test/on-invalid-upstream-response.test.js +1 -1
- package/test/padded-body.test.js +2 -2
- package/test/post-formbody.test.js +1 -1
- package/test/post-plain-text.test.js +1 -1
- package/test/post-with-custom-encoded-contenttype.test.js +2 -2
- package/test/post-with-octet-stream.test.js +5 -5
- package/test/retry-on-503.test.js +4 -4
- package/test/retry-with-a-custom-handler.test.js +7 -7
- package/test/rewrite-headers-type.test.js +2 -2
- package/test/rewrite-headers.test.js +3 -3
- package/test/rewrite-request-headers-type.test.js +2 -2
- package/test/rewrite-request-headers.test.js +2 -2
- package/test/transform-body.test.js +3 -3
- package/test/undici-agent.test.js +2 -2
- package/test/undici-body.test.js +3 -3
- package/test/undici-chaining.test.js +3 -3
- package/test/undici-connect-timeout.test.js +2 -2
- package/test/undici-custom-dispatcher.test.js +2 -2
- package/test/undici-global-agent.test.js +2 -2
- package/test/undici-no-destroy.test.js +1 -1
- package/test/undici-options.test.js +2 -2
- package/test/undici-proxy-agent.test.js +47 -28
- package/test/undici-retry.test.js +3 -3
- package/test/undici-timeout-body-partial.test.js +1 -1
- package/test/undici-timeout-body.test.js +1 -1
- package/test/undici-timeout.test.js +2 -2
- package/test/undici-with-path-in-base.test.js +1 -1
- package/test/undici.test.js +1 -1
- package/test/unexpected-error.test.js +2 -2
- package/test/unix-http-undici-from.test.js +3 -3
- package/test/unix-http-undici.test.js +1 -1
- package/test/unix-http.test.js +1 -1
- package/test/unix-https-undici.test.js +1 -1
- package/test/unix-https.test.js +1 -1
- package/types/index.d.ts +10 -11
- package/types/index.test-d.ts +138 -108
|
@@ -11,12 +11,12 @@ const instance = Fastify()
|
|
|
11
11
|
t.plan(6)
|
|
12
12
|
t.teardown(instance.close.bind(instance))
|
|
13
13
|
|
|
14
|
-
const target = http.createServer((
|
|
14
|
+
const target = http.createServer((_req, res) => {
|
|
15
15
|
t.fail('this should never get called')
|
|
16
16
|
res.end('hello world')
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
instance.get('/', (
|
|
19
|
+
instance.get('/', (_request, reply) => {
|
|
20
20
|
try {
|
|
21
21
|
reply.from(null, { body: 'this is the new body' })
|
|
22
22
|
} catch (e) {
|
|
@@ -11,12 +11,12 @@ const instance = Fastify()
|
|
|
11
11
|
t.plan(7)
|
|
12
12
|
t.teardown(instance.close.bind(instance))
|
|
13
13
|
|
|
14
|
-
const target = http.createServer((
|
|
14
|
+
const target = http.createServer((_req, res) => {
|
|
15
15
|
t.fail('this should never get called')
|
|
16
16
|
res.end('hello world')
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
instance.head('/', (
|
|
19
|
+
instance.head('/', (_request, reply) => {
|
|
20
20
|
try {
|
|
21
21
|
reply.from(null, { body: 'this is the new body' })
|
|
22
22
|
} catch (e) {
|
|
@@ -13,12 +13,12 @@ instance.register(From)
|
|
|
13
13
|
t.plan(5)
|
|
14
14
|
t.teardown(instance.close.bind(instance))
|
|
15
15
|
|
|
16
|
-
const target = http.createServer((
|
|
16
|
+
const target = http.createServer((_req, res) => {
|
|
17
17
|
t.fail('the target server should never be called')
|
|
18
18
|
res.end()
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
instance.post('/', (
|
|
21
|
+
instance.post('/', (_request, reply) => {
|
|
22
22
|
const body = new Readable({
|
|
23
23
|
read: function () {
|
|
24
24
|
t.fail('the read function should never be called')
|
package/test/on-error.test.js
CHANGED
|
@@ -12,7 +12,7 @@ t.test('on-error', async (t) => {
|
|
|
12
12
|
const target = Fastify()
|
|
13
13
|
t.teardown(target.close.bind(target))
|
|
14
14
|
|
|
15
|
-
target.get('/', (
|
|
15
|
+
target.get('/', (_request, reply) => {
|
|
16
16
|
t.pass('request arrives')
|
|
17
17
|
|
|
18
18
|
clock.setTimeout(() => {
|
|
@@ -28,7 +28,7 @@ t.test('on-error', async (t) => {
|
|
|
28
28
|
|
|
29
29
|
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
|
|
30
30
|
|
|
31
|
-
instance.get('/', (
|
|
31
|
+
instance.get('/', (_request, reply) => {
|
|
32
32
|
reply.from(`http://localhost:${target.server.address().port}/`,
|
|
33
33
|
{
|
|
34
34
|
onError: (reply, { error }) => {
|
|
@@ -21,7 +21,7 @@ const target = http.createServer((req, res) => {
|
|
|
21
21
|
|
|
22
22
|
instance.get('/', (_, reply) => {
|
|
23
23
|
reply.from(`http://localhost:${target.address().port}`, {
|
|
24
|
-
onResponse: (_,
|
|
24
|
+
onResponse: (_, _reply, res) => {
|
|
25
25
|
t.equal(res.statusCode, 888)
|
|
26
26
|
}
|
|
27
27
|
})
|
package/test/padded-body.test.js
CHANGED
|
@@ -36,7 +36,7 @@ const target = http.createServer((req, res) => {
|
|
|
36
36
|
})
|
|
37
37
|
})
|
|
38
38
|
|
|
39
|
-
instance.post('/', (
|
|
39
|
+
instance.post('/', (_request, reply) => {
|
|
40
40
|
reply.from(`http://localhost:${target.address().port}`)
|
|
41
41
|
})
|
|
42
42
|
|
|
@@ -55,7 +55,7 @@ instance.listen({ port: 0 }, (err) => {
|
|
|
55
55
|
'content-type': 'application/json'
|
|
56
56
|
},
|
|
57
57
|
body: bodyString
|
|
58
|
-
}, (err,
|
|
58
|
+
}, (err, _res, data) => {
|
|
59
59
|
t.error(err)
|
|
60
60
|
const parsed = JSON.parse(data)
|
|
61
61
|
t.same(parsed, { something: 'else' })
|
|
@@ -15,7 +15,7 @@ instance.register(From, {
|
|
|
15
15
|
instance.addContentTypeParser(
|
|
16
16
|
'application/x-www-form-urlencoded',
|
|
17
17
|
{ parseAs: 'buffer', bodyLimit: 1000 },
|
|
18
|
-
(
|
|
18
|
+
(_req, body, done) => done(null, parse(body.toString()))
|
|
19
19
|
)
|
|
20
20
|
|
|
21
21
|
t.plan(9)
|
|
@@ -39,7 +39,7 @@ const target = http.createServer((req, res) => {
|
|
|
39
39
|
})
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
-
instance.post('/', (
|
|
42
|
+
instance.post('/', (_request, reply) => {
|
|
43
43
|
reply.from(`http://localhost:${target.address().port}`)
|
|
44
44
|
})
|
|
45
45
|
|
|
@@ -16,7 +16,7 @@ test('with explicitly set content-type application/octet-stream', t => {
|
|
|
16
16
|
instance.addContentTypeParser(
|
|
17
17
|
'application/octet-stream',
|
|
18
18
|
{ parseAs: 'buffer', bodyLimit: 1000 },
|
|
19
|
-
(
|
|
19
|
+
(_req, body, done) => done(null, parse(body.toString()))
|
|
20
20
|
)
|
|
21
21
|
|
|
22
22
|
t.plan(9)
|
|
@@ -40,7 +40,7 @@ test('with explicitly set content-type application/octet-stream', t => {
|
|
|
40
40
|
})
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
instance.post('/', (
|
|
43
|
+
instance.post('/', (_request, reply) => {
|
|
44
44
|
reply.from(`http://localhost:${target.address().port}`)
|
|
45
45
|
})
|
|
46
46
|
|
|
@@ -75,13 +75,13 @@ test('with implicit content-type application/octet-stream', t => {
|
|
|
75
75
|
instance.addContentTypeParser(
|
|
76
76
|
'application/octet-stream',
|
|
77
77
|
{ parseAs: 'buffer', bodyLimit: 1000 },
|
|
78
|
-
(
|
|
78
|
+
(_req, body, done) => done(null, parse(body.toString()))
|
|
79
79
|
)
|
|
80
80
|
|
|
81
81
|
instance.addContentTypeParser(
|
|
82
82
|
'*',
|
|
83
83
|
{ parseAs: 'buffer', bodyLimit: 1000 },
|
|
84
|
-
(
|
|
84
|
+
(_req, body, done) => done(null, parse(body.toString()))
|
|
85
85
|
)
|
|
86
86
|
|
|
87
87
|
t.plan(9)
|
|
@@ -105,7 +105,7 @@ test('with implicit content-type application/octet-stream', t => {
|
|
|
105
105
|
})
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
-
instance.post('/', (
|
|
108
|
+
instance.post('/', (_request, reply) => {
|
|
109
109
|
reply.from(`http://localhost:${target.address().port}`)
|
|
110
110
|
})
|
|
111
111
|
|
|
@@ -8,7 +8,7 @@ const got = require('got')
|
|
|
8
8
|
|
|
9
9
|
function createTargetServer (withRetryAfterHeader, stopAfter = 1) {
|
|
10
10
|
let requestCount = 0
|
|
11
|
-
return http.createServer((
|
|
11
|
+
return http.createServer((_req, res) => {
|
|
12
12
|
if (requestCount++ < stopAfter) {
|
|
13
13
|
res.statusCode = 503
|
|
14
14
|
res.setHeader('Content-Type', 'text/plain')
|
|
@@ -35,7 +35,7 @@ test('Should retry on 503 HTTP error', async function (t) {
|
|
|
35
35
|
base: `http://localhost:${target.address().port}`
|
|
36
36
|
})
|
|
37
37
|
|
|
38
|
-
instance.get('/', (
|
|
38
|
+
instance.get('/', (_request, reply) => {
|
|
39
39
|
reply.from()
|
|
40
40
|
})
|
|
41
41
|
|
|
@@ -60,7 +60,7 @@ test('Should retry on 503 HTTP error with Retry-After response header', async fu
|
|
|
60
60
|
base: `http://localhost:${target.address().port}`
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
instance.get('/', (
|
|
63
|
+
instance.get('/', (_request, reply) => {
|
|
64
64
|
reply.from()
|
|
65
65
|
})
|
|
66
66
|
|
|
@@ -85,7 +85,7 @@ test('Should abort if server is always returning 503', async function (t) {
|
|
|
85
85
|
base: `http://localhost:${target.address().port}`
|
|
86
86
|
})
|
|
87
87
|
|
|
88
|
-
instance.get('/', (
|
|
88
|
+
instance.get('/', (_request, reply) => {
|
|
89
89
|
reply.from()
|
|
90
90
|
})
|
|
91
91
|
|
|
@@ -33,7 +33,7 @@ async function setupServer (t, fromOptions = {}, statusCodeToFailOn = 500, stopA
|
|
|
33
33
|
base: `http://localhost:${target.address().port}`
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
instance.get('/', (
|
|
36
|
+
instance.get('/', (_request, reply) => {
|
|
37
37
|
reply.from(`http://localhost:${target.address().port}`, fromOptions)
|
|
38
38
|
})
|
|
39
39
|
|
|
@@ -59,7 +59,7 @@ test('a 500 status code with no custom handler should fail', async (t) => {
|
|
|
59
59
|
})
|
|
60
60
|
|
|
61
61
|
test("a server 500's with a custom handler and should revive", async (t) => {
|
|
62
|
-
const customRetryLogic = ({ req, res,
|
|
62
|
+
const customRetryLogic = ({ req, res, getDefaultDelay }) => {
|
|
63
63
|
const defaultDelay = getDefaultDelay()
|
|
64
64
|
if (defaultDelay) return defaultDelay
|
|
65
65
|
|
|
@@ -80,7 +80,7 @@ test("a server 500's with a custom handler and should revive", async (t) => {
|
|
|
80
80
|
|
|
81
81
|
test('custom retry does not invoke the default delay causing a 501', async (t) => {
|
|
82
82
|
// the key here is our retryDelay doesn't register the deefault handler and as a result it doesn't work
|
|
83
|
-
const customRetryLogic = ({ req, res
|
|
83
|
+
const customRetryLogic = ({ req, res }) => {
|
|
84
84
|
if (res && res.statusCode === 500 && req.method === 'GET') {
|
|
85
85
|
return 0
|
|
86
86
|
}
|
|
@@ -100,7 +100,7 @@ test('custom retry does not invoke the default delay causing a 501', async (t) =
|
|
|
100
100
|
})
|
|
101
101
|
|
|
102
102
|
test('custom retry delay functions can invoke the default delay', async (t) => {
|
|
103
|
-
const customRetryLogic = ({ req, res,
|
|
103
|
+
const customRetryLogic = ({ req, res, getDefaultDelay }) => {
|
|
104
104
|
// registering the default retry logic for non 500 errors if it occurs
|
|
105
105
|
const defaultDelay = getDefaultDelay()
|
|
106
106
|
if (defaultDelay) return defaultDelay
|
|
@@ -122,7 +122,7 @@ test('custom retry delay functions can invoke the default delay', async (t) => {
|
|
|
122
122
|
})
|
|
123
123
|
|
|
124
124
|
test('custom retry delay function inspects the err paramater', async (t) => {
|
|
125
|
-
const customRetryLogic = ({
|
|
125
|
+
const customRetryLogic = ({ err }) => {
|
|
126
126
|
if (err && (err.code === 'UND_ERR_SOCKET' || err.code === 'ECONNRESET')) {
|
|
127
127
|
return 0.1
|
|
128
128
|
}
|
|
@@ -141,7 +141,7 @@ test('custom retry delay function inspects the err paramater', async (t) => {
|
|
|
141
141
|
test('we can exceed our retryCount and introspect attempts independently', async (t) => {
|
|
142
142
|
const attemptCounter = []
|
|
143
143
|
|
|
144
|
-
const customRetryLogic = ({
|
|
144
|
+
const customRetryLogic = ({ err, attempt }) => {
|
|
145
145
|
attemptCounter.push(attempt)
|
|
146
146
|
|
|
147
147
|
if (err && (err.code === 'UND_ERR_SOCKET' || err.code === 'ECONNRESET')) {
|
|
@@ -163,7 +163,7 @@ test('we can exceed our retryCount and introspect attempts independently', async
|
|
|
163
163
|
|
|
164
164
|
test('we handle our retries based on the retryCount', async (t) => {
|
|
165
165
|
const attemptCounter = []
|
|
166
|
-
const customRetryLogic = ({ req, res,
|
|
166
|
+
const customRetryLogic = ({ req, res, attempt, retriesCount }) => {
|
|
167
167
|
if (retriesCount < attempt) {
|
|
168
168
|
return null
|
|
169
169
|
}
|
|
@@ -21,7 +21,7 @@ const target = http.createServer((req, res) => {
|
|
|
21
21
|
|
|
22
22
|
instance.get('/', (request, reply) => {
|
|
23
23
|
reply.from(`http://localhost:${target.address().port}`, {
|
|
24
|
-
rewriteHeaders: (
|
|
24
|
+
rewriteHeaders: (_headers, req) => {
|
|
25
25
|
t.pass('rewriteHeaders called with correct request parameter')
|
|
26
26
|
t.equal(req, request)
|
|
27
27
|
return {}
|
|
@@ -37,7 +37,7 @@ instance.listen({ port: 0 }, (err) => {
|
|
|
37
37
|
target.listen({ port: 0 }, (err) => {
|
|
38
38
|
t.error(err)
|
|
39
39
|
|
|
40
|
-
get(`http://localhost:${instance.server.address().port}`, (err, res
|
|
40
|
+
get(`http://localhost:${instance.server.address().port}`, (err, res) => {
|
|
41
41
|
t.error(err)
|
|
42
42
|
t.equal(res.statusCode, 205)
|
|
43
43
|
})
|
|
@@ -21,9 +21,9 @@ const target = http.createServer((req, res) => {
|
|
|
21
21
|
res.end('hello world')
|
|
22
22
|
})
|
|
23
23
|
|
|
24
|
-
instance.get('/', (
|
|
24
|
+
instance.get('/', (_request, reply) => {
|
|
25
25
|
reply.from(`http://localhost:${target.address().port}`, {
|
|
26
|
-
rewriteHeaders: (headers
|
|
26
|
+
rewriteHeaders: (headers) => {
|
|
27
27
|
t.pass('rewriteHeaders called')
|
|
28
28
|
return {
|
|
29
29
|
'content-type': headers['content-type'],
|
|
@@ -41,7 +41,7 @@ instance.listen({ port: 0 }, (err) => {
|
|
|
41
41
|
target.listen({ port: 0 }, (err) => {
|
|
42
42
|
t.error(err)
|
|
43
43
|
|
|
44
|
-
get(`http://localhost:${instance.server.address().port}`, (err, res
|
|
44
|
+
get(`http://localhost:${instance.server.address().port}`, (err, res) => {
|
|
45
45
|
t.error(err)
|
|
46
46
|
t.equal(res.headers['content-type'], 'text/plain')
|
|
47
47
|
t.equal(res.headers['x-another-header'], 'so headers!')
|
|
@@ -21,7 +21,7 @@ const target = http.createServer((req, res) => {
|
|
|
21
21
|
|
|
22
22
|
instance.get('/', (request, reply) => {
|
|
23
23
|
reply.from(`http://localhost:${target.address().port}`, {
|
|
24
|
-
rewriteRequestHeaders: (originalReq
|
|
24
|
+
rewriteRequestHeaders: (originalReq) => {
|
|
25
25
|
t.pass('rewriteRequestHeaders called with correct request parameter')
|
|
26
26
|
t.equal(originalReq, request)
|
|
27
27
|
return {}
|
|
@@ -37,7 +37,7 @@ instance.listen({ port: 0 }, (err) => {
|
|
|
37
37
|
target.listen({ port: 0 }, (err) => {
|
|
38
38
|
t.error(err)
|
|
39
39
|
|
|
40
|
-
get(`http://localhost:${instance.server.address().port}`, (err, res
|
|
40
|
+
get(`http://localhost:${instance.server.address().port}`, (err, res) => {
|
|
41
41
|
t.error(err)
|
|
42
42
|
t.equal(res.statusCode, 205)
|
|
43
43
|
})
|
|
@@ -20,9 +20,9 @@ const target = http.createServer((req, res) => {
|
|
|
20
20
|
res.end(req.headers.host)
|
|
21
21
|
})
|
|
22
22
|
|
|
23
|
-
instance.get('/', (
|
|
23
|
+
instance.get('/', (_request, reply) => {
|
|
24
24
|
reply.from(`http://localhost:${target.address().port}`, {
|
|
25
|
-
rewriteRequestHeaders: (
|
|
25
|
+
rewriteRequestHeaders: (_originalReq, headers) => {
|
|
26
26
|
t.pass('rewriteRequestHeaders called')
|
|
27
27
|
return Object.assign(headers, { host: 'host-override' })
|
|
28
28
|
}
|
|
@@ -22,13 +22,13 @@ const target = http.createServer((req, res) => {
|
|
|
22
22
|
res.end('hello world')
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
-
instance.get('/', (
|
|
25
|
+
instance.get('/', (_request, reply) => {
|
|
26
26
|
reply.from(`http://localhost:${target.address().port}`, {
|
|
27
|
-
onResponse: (
|
|
27
|
+
onResponse: (_request, reply, res) => {
|
|
28
28
|
reply.send(
|
|
29
29
|
res.stream.pipe(
|
|
30
30
|
new Transform({
|
|
31
|
-
transform: function (chunk,
|
|
31
|
+
transform: function (chunk, _enc, cb) {
|
|
32
32
|
this.push(chunk.toString().toUpperCase())
|
|
33
33
|
cb()
|
|
34
34
|
}
|
|
@@ -13,12 +13,12 @@ t.plan(10)
|
|
|
13
13
|
const instance = Fastify()
|
|
14
14
|
t.teardown(instance.close.bind(instance))
|
|
15
15
|
|
|
16
|
-
const target = http.createServer((
|
|
16
|
+
const target = http.createServer((_req, res) => {
|
|
17
17
|
res.statusCode = 200
|
|
18
18
|
res.end('hello world')
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
instance.get('/', (
|
|
21
|
+
instance.get('/', (_request, reply) => {
|
|
22
22
|
reply.from()
|
|
23
23
|
})
|
|
24
24
|
|
package/test/undici-body.test.js
CHANGED
|
@@ -33,7 +33,7 @@ const target = http.createServer((req, res) => {
|
|
|
33
33
|
})
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
instance.post('/', (
|
|
36
|
+
instance.post('/', (_request, reply) => {
|
|
37
37
|
reply.from(`http://localhost:${target.address().port}`)
|
|
38
38
|
})
|
|
39
39
|
|
|
@@ -42,7 +42,7 @@ t.teardown(target.close.bind(target))
|
|
|
42
42
|
target.listen({ port: 0 }, (err) => {
|
|
43
43
|
t.error(err)
|
|
44
44
|
|
|
45
|
-
instance.addContentTypeParser('application/json', function (
|
|
45
|
+
instance.addContentTypeParser('application/json', function (_req, payload, done) {
|
|
46
46
|
done(null, payload)
|
|
47
47
|
})
|
|
48
48
|
|
|
@@ -61,7 +61,7 @@ target.listen({ port: 0 }, (err) => {
|
|
|
61
61
|
'content-type': 'application/json'
|
|
62
62
|
},
|
|
63
63
|
body: bodyString
|
|
64
|
-
}, (err,
|
|
64
|
+
}, (err, _res, data) => {
|
|
65
65
|
t.error(err)
|
|
66
66
|
const parsed = JSON.parse(data)
|
|
67
67
|
t.same(parsed, { something: 'else' })
|
|
@@ -16,7 +16,7 @@ t.teardown(proxy1.close.bind(proxy1))
|
|
|
16
16
|
const proxy2 = Fastify()
|
|
17
17
|
t.teardown(proxy2.close.bind(proxy2))
|
|
18
18
|
|
|
19
|
-
instance.get('/', (
|
|
19
|
+
instance.get('/', (_request, reply) => {
|
|
20
20
|
reply.header('content-disposition', header).send('OK')
|
|
21
21
|
})
|
|
22
22
|
|
|
@@ -25,7 +25,7 @@ proxy1.register(From, {
|
|
|
25
25
|
keepAliveMaxTimeout: 10
|
|
26
26
|
}
|
|
27
27
|
})
|
|
28
|
-
proxy1.get('/', (
|
|
28
|
+
proxy1.get('/', (_request, reply) => {
|
|
29
29
|
return reply.from(`http://localhost:${instance.server.address().port}`)
|
|
30
30
|
})
|
|
31
31
|
|
|
@@ -34,7 +34,7 @@ proxy2.register(From, {
|
|
|
34
34
|
keepAliveMaxTimeout: 10
|
|
35
35
|
}
|
|
36
36
|
})
|
|
37
|
-
proxy2.get('/', (
|
|
37
|
+
proxy2.get('/', (_request, reply) => {
|
|
38
38
|
return reply.from(`http://localhost:${proxy1.server.address().port}`)
|
|
39
39
|
})
|
|
40
40
|
|
|
@@ -13,7 +13,7 @@ t.test('undici connect timeout', async (t) => {
|
|
|
13
13
|
return new net.Socket(options)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
const target = http.createServer((
|
|
16
|
+
const target = http.createServer(() => {
|
|
17
17
|
t.fail('target never called')
|
|
18
18
|
})
|
|
19
19
|
|
|
@@ -31,7 +31,7 @@ t.test('undici connect timeout', async (t) => {
|
|
|
31
31
|
}
|
|
32
32
|
})
|
|
33
33
|
|
|
34
|
-
instance.get('/', (
|
|
34
|
+
instance.get('/', (_request, reply) => {
|
|
35
35
|
reply.from()
|
|
36
36
|
})
|
|
37
37
|
|
|
@@ -28,7 +28,7 @@ test('use a custom instance of \'undici\'', async t => {
|
|
|
28
28
|
keepAliveTimeout: 1
|
|
29
29
|
})
|
|
30
30
|
|
|
31
|
-
target.get('/', (
|
|
31
|
+
target.get('/', (_req, reply) => {
|
|
32
32
|
t.pass('request proxied')
|
|
33
33
|
|
|
34
34
|
reply.headers({
|
|
@@ -53,7 +53,7 @@ test('use a custom instance of \'undici\'', async t => {
|
|
|
53
53
|
undici: new CustomDispatcher('http://localhost:3001')
|
|
54
54
|
})
|
|
55
55
|
|
|
56
|
-
instance.get('/', (
|
|
56
|
+
instance.get('/', (_request, reply) => {
|
|
57
57
|
reply.from('http://myserver.local')
|
|
58
58
|
})
|
|
59
59
|
|
|
@@ -17,12 +17,12 @@ test('undici global agent is used, but not destroyed', async (t) => {
|
|
|
17
17
|
|
|
18
18
|
t.teardown(instance.close.bind(instance))
|
|
19
19
|
|
|
20
|
-
const target = http.createServer((
|
|
20
|
+
const target = http.createServer((_req, res) => {
|
|
21
21
|
res.statusCode = 200
|
|
22
22
|
res.end()
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
-
instance.get('/', (
|
|
25
|
+
instance.get('/', (_request, reply) => {
|
|
26
26
|
reply.from()
|
|
27
27
|
})
|
|
28
28
|
|
|
@@ -13,12 +13,12 @@ const instance = Fastify()
|
|
|
13
13
|
t.plan(6)
|
|
14
14
|
t.teardown(instance.close.bind(instance))
|
|
15
15
|
|
|
16
|
-
const target = http.createServer((
|
|
16
|
+
const target = http.createServer((_req, res) => {
|
|
17
17
|
res.statusCode = 200
|
|
18
18
|
res.end('hello world')
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
instance.get('/', (
|
|
21
|
+
instance.get('/', (_request, reply) => {
|
|
22
22
|
reply.from()
|
|
23
23
|
})
|
|
24
24
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test, after } = require('node:test')
|
|
4
4
|
const { createServer } = require('node:http')
|
|
5
5
|
const Fastify = require('fastify')
|
|
6
|
-
const get = require('simple-get').concat
|
|
7
6
|
const { createProxy } = require('proxy')
|
|
8
|
-
const
|
|
7
|
+
const fastifyProxyFrom = require('..')
|
|
8
|
+
const { isIPv6 } = require('node:net')
|
|
9
9
|
|
|
10
10
|
const configFormat = {
|
|
11
11
|
string: (value) => value,
|
|
@@ -15,54 +15,73 @@ const configFormat = {
|
|
|
15
15
|
|
|
16
16
|
for (const [description, format] of Object.entries(configFormat)) {
|
|
17
17
|
test(`use undici ProxyAgent to connect through proxy - configured via ${description}`, async (t) => {
|
|
18
|
-
t.plan(
|
|
18
|
+
t.plan(3)
|
|
19
|
+
|
|
19
20
|
const target = await buildServer()
|
|
20
21
|
const proxy = await buildProxy()
|
|
21
|
-
t.teardown(target.close.bind(target))
|
|
22
|
-
t.teardown(proxy.close.bind(proxy))
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
after(() => {
|
|
24
|
+
target.close()
|
|
25
|
+
proxy.close()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
let targetAddress = target.address().address
|
|
29
|
+
|
|
30
|
+
if (isIPv6(targetAddress)) {
|
|
31
|
+
targetAddress = `[${targetAddress}]`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let proxyAddress = proxy.address().address
|
|
35
|
+
|
|
36
|
+
if (isIPv6(proxyAddress)) {
|
|
37
|
+
proxyAddress = `[${proxyAddress}]`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const targetUrl = `http://${targetAddress}:${target.address().port}`
|
|
41
|
+
const proxyUrl = `http://${proxyAddress}:${proxy.address().port}`
|
|
26
42
|
|
|
27
43
|
proxy.on('connect', () => {
|
|
28
|
-
t.ok(true, 'should connect to proxy')
|
|
44
|
+
t.assert.ok(true, 'should connect to proxy')
|
|
29
45
|
})
|
|
30
46
|
|
|
31
|
-
target.on('request', (
|
|
47
|
+
target.on('request', (_req, res) => {
|
|
32
48
|
res.setHeader('content-type', 'application/json')
|
|
33
49
|
res.end(JSON.stringify({ hello: 'world' }))
|
|
34
50
|
})
|
|
35
51
|
|
|
36
52
|
const instance = Fastify()
|
|
37
|
-
t.teardown(instance.close.bind(instance))
|
|
38
53
|
|
|
39
|
-
|
|
54
|
+
after(() => {
|
|
55
|
+
instance.close()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
instance.register(fastifyProxyFrom, {
|
|
40
59
|
base: targetUrl,
|
|
41
60
|
undici: {
|
|
42
61
|
proxy: format(proxyUrl)
|
|
43
62
|
}
|
|
44
63
|
})
|
|
45
64
|
|
|
46
|
-
instance.get('/', (
|
|
65
|
+
instance.get('/', (_request, reply) => {
|
|
47
66
|
reply.from()
|
|
48
67
|
})
|
|
49
68
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
69
|
+
await instance.listen({ port: 0 })
|
|
70
|
+
|
|
71
|
+
let instanceAddress = proxy.address().address
|
|
72
|
+
|
|
73
|
+
if (isIPv6(instanceAddress)) {
|
|
74
|
+
if (instanceAddress === '::') {
|
|
75
|
+
instanceAddress = '::1'
|
|
76
|
+
} else {
|
|
77
|
+
instanceAddress = `[${instanceAddress}]`
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const response = await fetch(`http://localhost:${instance.server.address().port}`)
|
|
64
82
|
|
|
65
|
-
|
|
83
|
+
t.assert.strictEqual(response.status, 200)
|
|
84
|
+
t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
|
|
66
85
|
})
|
|
67
86
|
}
|
|
68
87
|
|
|
@@ -29,7 +29,7 @@ test('Will retry', async function (t) {
|
|
|
29
29
|
|
|
30
30
|
instance.register(From, { undici: true })
|
|
31
31
|
|
|
32
|
-
instance.get('/', (
|
|
32
|
+
instance.get('/', (_request, reply) => {
|
|
33
33
|
reply.from(`http://localhost:${target.address().port}/`, {
|
|
34
34
|
retriesCount: 1,
|
|
35
35
|
onError: (reply, { error }) => {
|
|
@@ -56,7 +56,7 @@ test('will not retry', async function (t) {
|
|
|
56
56
|
|
|
57
57
|
instance.register(From, { undici: true })
|
|
58
58
|
|
|
59
|
-
instance.get('/', (
|
|
59
|
+
instance.get('/', (_request, reply) => {
|
|
60
60
|
reply.from(`http://localhost:${target.address().port}/`, {
|
|
61
61
|
retriesCount: 0,
|
|
62
62
|
onError: (reply, { error }) => {
|
|
@@ -87,7 +87,7 @@ test('will not retry unsupported method', async function (t) {
|
|
|
87
87
|
|
|
88
88
|
instance.register(From, { undici: true, retryMethods: ['DELETE'] })
|
|
89
89
|
|
|
90
|
-
instance.get('/', (
|
|
90
|
+
instance.get('/', (_request, reply) => {
|
|
91
91
|
reply.from(`http://localhost:${target.address().port}/`, {
|
|
92
92
|
retriesCount: 1,
|
|
93
93
|
onError: (reply, { error }) => {
|