Skip to content

Commit be0815e

Browse files
mcollinaYukio0315
andauthored
feat: preserve undici agent by default (#452)
Documentation states destroyAgent defaults to false but the option was undefined when omitted, causing custom ag ents to be destroyed. Explicitly default the option to f alse and cover it with a regression test. Signed-off-by: Yukio Ueda <triangle.pillow@gmail.com> Co-authored-by: Yukio Ueda <triangle.pillow@gmail.com>
1 parent 8c0d65d commit be0815e

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

lib/request.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function buildRequest (opts) {
5353
const isBalanced = Array.isArray(opts.base) && opts.base.length > 1
5454
const undiciOpts = opts.undici || {}
5555
const globalAgent = opts.globalAgent
56-
const destroyAgent = opts.destroyAgent
56+
const destroyAgent = opts.destroyAgent || false
5757
let http2Client
5858
let undiciAgent
5959
let undiciInstance
@@ -104,6 +104,11 @@ function buildRequest (opts) {
104104
}
105105

106106
function close () {
107+
// Always destroy http2 client (internal implementation detail)
108+
if (http2Client) {
109+
http2Client.destroy()
110+
}
111+
107112
if (globalAgent || destroyAgent === false) {
108113
return
109114
}
@@ -114,8 +119,6 @@ function buildRequest (opts) {
114119
} else if (!isHttp2) {
115120
agents['http:'].destroy()
116121
agents['https:'].destroy()
117-
} else if (http2Client) {
118-
http2Client.destroy()
119122
}
120123
}
121124

test/undici-no-destroy.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ test('destroyAgent false', async (t) => {
2727
await instance.ready()
2828
await instance.close()
2929
})
30+
31+
test('destroyAgent default false', async (t) => {
32+
const mockAgent = new undici.Agent()
33+
mockAgent.destroy = () => {
34+
t.fail()
35+
}
36+
const instance = Fastify()
37+
38+
t.after(() => instance.close())
39+
40+
instance.get('/', (_request, reply) => {
41+
reply.from()
42+
})
43+
44+
instance.register(From, {
45+
base: 'http://localhost:4242',
46+
undici: mockAgent
47+
})
48+
49+
await instance.ready()
50+
await instance.close()
51+
})

0 commit comments

Comments
 (0)