Skip to content

Commit 1e58b58

Browse files
feat: Allow disabling autoSelectFamily in an Agent (#4070)
* Allow disabling autoSelectFamily in an Agent Currently the options are only passed if they enable autoSelectFamily. However, if autoSelectFamily is the default, we want to be able to disable it too. * Fix the autoSelectFamily error in client too. - Also add tests. - Fix types so that partial options can be passed to buildConnector without violating the TypeScript. For example, TcpNet options require 'port' but the options we pass should not. --------- Co-authored-by: Carlos Fuentes <[email protected]>
1 parent 2767d0e commit 1e58b58

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

lib/dispatcher/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class Client extends DispatcherBase {
207207
allowH2,
208208
socketPath,
209209
timeout: connectTimeout,
210-
...(autoSelectFamily ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
210+
...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
211211
...connect
212212
})
213213
}

lib/dispatcher/pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Pool extends PoolBase {
5858
allowH2,
5959
socketPath,
6060
timeout: connectTimeout,
61-
...(autoSelectFamily ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
61+
...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
6262
...connect
6363
})
6464
}

test/node-test/autoselectfamily.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ test('with autoSelectFamily disabled the request fails when using request', { sk
172172
await p.completed
173173
})
174174

175+
test('with autoSelectFamily disabled via Agent.Options["autoSelectFamily"] the request fails when using request', { skip }, async (t) => {
176+
const p = tspl(t, { plan: 1 })
177+
178+
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
179+
const server = createServer((req, res) => {
180+
res.end('hello')
181+
})
182+
183+
t.after(() => {
184+
server.close()
185+
dnsServer.close()
186+
})
187+
188+
server.listen(0, '127.0.0.1', () => {
189+
const agent = new Agent({ autoSelectFamily: false, connect: { lookup } })
190+
191+
request(`http://example.org:${server.address().port}`, {
192+
method: 'GET',
193+
dispatcher: agent
194+
}, (err, { statusCode, body }) => {
195+
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
196+
})
197+
})
198+
})
199+
200+
await p.completed
201+
})
202+
175203
test('with autoSelectFamily disabled the request fails when using a client', { skip }, async (t) => {
176204
const p = tspl(t, { plan: 1 })
177205

@@ -200,3 +228,32 @@ test('with autoSelectFamily disabled the request fails when using a client', { s
200228

201229
await p.completed
202230
})
231+
232+
test('with autoSelectFamily disabled via Client.Options["autoSelectFamily"] the request fails when using a client', { skip }, async (t) => {
233+
const p = tspl(t, { plan: 1 })
234+
235+
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
236+
const server = createServer((req, res) => {
237+
res.end('hello')
238+
})
239+
240+
t.after(() => {
241+
server.close()
242+
dnsServer.close()
243+
})
244+
245+
server.listen(0, '127.0.0.1', () => {
246+
const client = new Client(`http://example.org:${server.address().port}`, { autoSelectFamily: false, connect: { lookup } })
247+
t.after(client.destroy.bind(client))
248+
249+
client.request({
250+
path: '/',
251+
method: 'GET'
252+
}, (err, { statusCode, body }) => {
253+
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
254+
})
255+
})
256+
})
257+
258+
await p.completed
259+
})

types/client.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export declare namespace Client {
7070
/** TODO */
7171
maxRedirections?: number;
7272
/** TODO */
73-
connect?: buildConnector.BuildOptions | buildConnector.connector;
73+
connect?: Partial<buildConnector.BuildOptions> | buildConnector.connector;
7474
/** TODO */
7575
maxRequestsPerClient?: number;
7676
/** TODO */

0 commit comments

Comments
 (0)