Skip to content

Commit 47c3155

Browse files
committed
Fix dnsLookup option type to accept Node.js dns.lookup
Fixes #2426
1 parent 6633001 commit 47c3155

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

source/core/options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import http, {
1212
type ClientRequest,
1313
} from 'node:http';
1414
import type {Readable} from 'node:stream';
15-
import type {Socket} from 'node:net';
15+
import type {Socket, LookupFunction} from 'node:net';
1616
import is, {assert} from '@sindresorhus/is';
1717
import lowercaseKeys from 'lowercase-keys';
1818
import CacheableLookup from 'cacheable-lookup';
@@ -1990,11 +1990,11 @@ export default class Options {
19901990
throw new Error('The `searchParameters` option does not exist. Use `searchParams` instead.');
19911991
}
19921992

1993-
get dnsLookup(): CacheableLookup['lookup'] | undefined {
1993+
get dnsLookup(): LookupFunction | undefined {
19941994
return this._internals.dnsLookup;
19951995
}
19961996

1997-
set dnsLookup(value: CacheableLookup['lookup'] | undefined) {
1997+
set dnsLookup(value: LookupFunction | undefined) {
19981998
assertAny('dnsLookup', [is.function, is.undefined], value);
19991999

20002000
this._internals.dnsLookup = value;

test/http.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ test('ClientRequest can throw before promise resolves', async t => {
377377
t.true(['EINVAL', 'EHOSTUNREACH', 'ETIMEDOUT', 'ERR_INVALID_IP_ADDRESS'].includes(error!.code));
378378
});
379379

380+
test('dnsLookup option accepts Node.js dns.lookup', withServer, async (t, server, got) => {
381+
const dns = await import('node:dns');
382+
383+
server.get('/', (_request, response) => {
384+
response.end('ok');
385+
});
386+
387+
// This should work without type casting (regression test for #2426)
388+
const {body} = await got('', {
389+
dnsLookup: dns.lookup,
390+
});
391+
392+
t.is(body, 'ok');
393+
});
394+
380395
test('status code 200 has response ok is true', withServer, async (t, server, got) => {
381396
server.get('/', (_request, response) => {
382397
response.statusCode = 200;

test/timeout.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,20 +638,15 @@ test.serial('doesn\'t throw on early lookup', withServerAndFakeTimers, async (t,
638638
lookup: 1,
639639
},
640640
retry: {limit: 0},
641-
// @ts-expect-error Testing custom dnsLookup implementation
642641
dnsLookup(_hostname, options, callback) {
643642
if (typeof options === 'function') {
644643
callback = options;
645644
// Call with default (non-all) signature
646-
// @ts-expect-error Complex DNS lookup callback overloading
647645
callback(null, '127.0.0.1', 4);
648-
// @ts-expect-error Complex DNS lookup callback overloading - options.all check
649646
} else if (options.all) {
650647
// When options.all is true, callback expects an array of address objects
651-
// @ts-expect-error Complex DNS lookup callback overloading
652648
callback(null, [{address: '127.0.0.1', family: 4}]);
653649
} else {
654-
// @ts-expect-error Complex DNS lookup callback overloading
655650
callback(null, '127.0.0.1', 4);
656651
}
657652
},

0 commit comments

Comments
 (0)