From c5c685e09afc2778491a3b7ad553e31d30d81d87 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Thu, 22 Jan 2026 20:50:33 +0700 Subject: [PATCH] refactor(NODE-7313): only use dns.resolve for all types --- src/cmap/auth/gssapi.ts | 4 +-- src/connection_string.ts | 14 ++++----- src/sdam/srv_polling.ts | 2 +- .../dns_seedlist.test.ts | 30 +++++++++---------- ...itial_dns_seedlist_discovery.prose.test.ts | 2 +- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/cmap/auth/gssapi.ts b/src/cmap/auth/gssapi.ts index d18cb6b360e..e4216351696 100644 --- a/src/cmap/auth/gssapi.ts +++ b/src/cmap/auth/gssapi.ts @@ -166,7 +166,7 @@ export async function performGSSAPICanonicalizeHostName( try { // Perform a reverse ptr lookup on the ip address. - const results = await dns.promises.resolvePtr(address); + const results = await dns.promises.resolve(address, 'PTR'); // If the ptr did not error but had no results, return the host. return results.length > 0 ? results[0] : host; } catch { @@ -185,7 +185,7 @@ export async function performGSSAPICanonicalizeHostName( export async function resolveCname(host: string): Promise { // Attempt to resolve the host name try { - const results = await dns.promises.resolveCname(host); + const results = await dns.promises.resolve(host, 'CNAME'); // Get the first resolved host id return results.length > 0 ? results[0] : host; } catch { diff --git a/src/connection_string.ts b/src/connection_string.ts index df6dfc607a0..ebe728f1b63 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -41,17 +41,17 @@ const LB_REPLICA_SET_ERROR = 'loadBalanced option not supported with a replicaSe const LB_DIRECT_CONNECTION_ERROR = 'loadBalanced option not supported when directConnection is provided'; -function retryDNSTimeoutFor(api: 'resolveSrv'): (a: string) => Promise; -function retryDNSTimeoutFor(api: 'resolveTxt'): (a: string) => Promise; +function retryDNSTimeoutFor(api: 'SRV'): (a: string) => Promise; +function retryDNSTimeoutFor(api: 'TXT'): (a: string) => Promise; function retryDNSTimeoutFor( - api: 'resolveSrv' | 'resolveTxt' + api: 'SRV' | 'TXT' ): (a: string) => Promise { return async function dnsReqRetryTimeout(lookupAddress: string) { try { - return await dns.promises[api](lookupAddress); + return (await dns.promises.resolve(lookupAddress, api)) as dns.SrvRecord[] | string[][]; } catch (firstDNSError) { if (firstDNSError.code === dns.TIMEOUT) { - return await dns.promises[api](lookupAddress); + return (await dns.promises.resolve(lookupAddress, api)) as dns.SrvRecord[] | string[][]; } else { throw firstDNSError; } @@ -59,8 +59,8 @@ function retryDNSTimeoutFor( }; } -const resolveSrv = retryDNSTimeoutFor('resolveSrv'); -const resolveTxt = retryDNSTimeoutFor('resolveTxt'); +const resolveSrv = retryDNSTimeoutFor('SRV'); +const resolveTxt = retryDNSTimeoutFor('TXT'); /** * Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal diff --git a/src/sdam/srv_polling.ts b/src/sdam/srv_polling.ts index 583c82d1398..cfc4779cf25 100644 --- a/src/sdam/srv_polling.ts +++ b/src/sdam/srv_polling.ts @@ -116,7 +116,7 @@ export class SrvPoller extends TypedEventEmitter { let srvRecords; try { - srvRecords = await dns.promises.resolveSrv(this.srvAddress); + srvRecords = await dns.promises.resolve(this.srvAddress, 'SRV'); } catch { this.failure(); return; diff --git a/test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts b/test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts index 0618c376c01..423df16a1c5 100644 --- a/test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts +++ b/test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts @@ -31,21 +31,19 @@ describe('DNS timeout errors', () => { await client.close(); }); - const restoreDNS = - api => - async (...args) => { - sinon.restore(); - return await dns.promises[api](...args); - }; + const restoreDNS = api => async args => { + sinon.restore(); + return await dns.promises.resolve(args, api); + }; describe('when SRV record look up times out', () => { beforeEach(() => { stub = sinon - .stub(dns.promises, 'resolveSrv') + .stub(dns.promises, 'resolve') .onFirstCall() .rejects(new DNSTimeoutError()) .onSecondCall() - .callsFake(restoreDNS('resolveSrv')); + .callsFake(restoreDNS('SRV')); }); afterEach(async function () { @@ -61,11 +59,11 @@ describe('DNS timeout errors', () => { describe('when TXT record look up times out', () => { beforeEach(() => { stub = sinon - .stub(dns.promises, 'resolveTxt') + .stub(dns.promises, 'resolve') .onFirstCall() .rejects(new DNSTimeoutError()) .onSecondCall() - .callsFake(restoreDNS('resolveTxt')); + .callsFake(restoreDNS('TXT')); }); afterEach(async function () { @@ -81,7 +79,7 @@ describe('DNS timeout errors', () => { describe('when SRV record look up times out twice', () => { beforeEach(() => { stub = sinon - .stub(dns.promises, 'resolveSrv') + .stub(dns.promises, 'resolve') .onFirstCall() .rejects(new DNSTimeoutError()) .onSecondCall() @@ -102,7 +100,7 @@ describe('DNS timeout errors', () => { describe('when TXT record look up times out twice', () => { beforeEach(() => { stub = sinon - .stub(dns.promises, 'resolveTxt') + .stub(dns.promises, 'resolve') .onFirstCall() .rejects(new DNSTimeoutError()) .onSecondCall() @@ -123,11 +121,11 @@ describe('DNS timeout errors', () => { describe('when SRV record look up throws a non-timeout error', () => { beforeEach(() => { stub = sinon - .stub(dns.promises, 'resolveSrv') + .stub(dns.promises, 'resolve') .onFirstCall() .rejects(new DNSSomethingError()) .onSecondCall() - .callsFake(restoreDNS('resolveSrv')); + .callsFake(restoreDNS('SRV')); }); afterEach(async function () { @@ -144,11 +142,11 @@ describe('DNS timeout errors', () => { describe('when TXT record look up throws a non-timeout error', () => { beforeEach(() => { stub = sinon - .stub(dns.promises, 'resolveTxt') + .stub(dns.promises, 'resolve') .onFirstCall() .rejects(new DNSSomethingError()) .onSecondCall() - .callsFake(restoreDNS('resolveTxt')); + .callsFake(restoreDNS('TXT')); }); afterEach(async function () { diff --git a/test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts b/test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts index c36e675ae58..992b43d9ca4 100644 --- a/test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts +++ b/test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts @@ -23,7 +23,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => { beforeEach(async function () { // this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation - sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { + sinon.stub(dns.promises, 'resolve').callsFake(async () => { return [ { name: 'resolved.mongo.localhost',