Skip to content

Commit 4e89a87

Browse files
committed
refactor(NODE-7313): only use dns.resolve for all types
1 parent 9151d48 commit 4e89a87

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

src/cmap/auth/gssapi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export async function performGSSAPICanonicalizeHostName(
166166

167167
try {
168168
// Perform a reverse ptr lookup on the ip address.
169-
const results = await dns.promises.resolvePtr(address);
169+
const results = await dns.promises.resolve(address, 'PTR');
170170
// If the ptr did not error but had no results, return the host.
171171
return results.length > 0 ? results[0] : host;
172172
} catch {
@@ -185,7 +185,7 @@ export async function performGSSAPICanonicalizeHostName(
185185
export async function resolveCname(host: string): Promise<string> {
186186
// Attempt to resolve the host name
187187
try {
188-
const results = await dns.promises.resolveCname(host);
188+
const results = await dns.promises.resolve(host, 'CNAME');
189189
// Get the first resolved host id
190190
return results.length > 0 ? results[0] : host;
191191
} catch {

src/connection_string.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ const LB_REPLICA_SET_ERROR = 'loadBalanced option not supported with a replicaSe
4141
const LB_DIRECT_CONNECTION_ERROR =
4242
'loadBalanced option not supported when directConnection is provided';
4343

44-
function retryDNSTimeoutFor(api: 'resolveSrv'): (a: string) => Promise<dns.SrvRecord[]>;
45-
function retryDNSTimeoutFor(api: 'resolveTxt'): (a: string) => Promise<string[][]>;
44+
function retryDNSTimeoutFor(api: 'SRV'): (a: string) => Promise<dns.SrvRecord[]>;
45+
function retryDNSTimeoutFor(api: 'TXT'): (a: string) => Promise<string[][]>;
4646
function retryDNSTimeoutFor(
47-
api: 'resolveSrv' | 'resolveTxt'
47+
api: 'SRV' | 'TXT'
4848
): (a: string) => Promise<dns.SrvRecord[] | string[][]> {
4949
return async function dnsReqRetryTimeout(lookupAddress: string) {
5050
try {
51-
return await dns.promises[api](lookupAddress);
51+
return (await dns.promises.resolve(lookupAddress, api)) as dns.SrvRecord[] | string[][];
5252
} catch (firstDNSError) {
5353
if (firstDNSError.code === dns.TIMEOUT) {
54-
return await dns.promises[api](lookupAddress);
54+
return (await dns.promises.resolve(lookupAddress, api)) as dns.SrvRecord[] | string[][];
5555
} else {
5656
throw firstDNSError;
5757
}
5858
}
5959
};
6060
}
6161

62-
const resolveSrv = retryDNSTimeoutFor('resolveSrv');
63-
const resolveTxt = retryDNSTimeoutFor('resolveTxt');
62+
const resolveSrv = retryDNSTimeoutFor('SRV');
63+
const resolveTxt = retryDNSTimeoutFor('TXT');
6464

6565
/**
6666
* Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal

src/sdam/srv_polling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
116116
let srvRecords;
117117

118118
try {
119-
srvRecords = await dns.promises.resolveSrv(this.srvAddress);
119+
srvRecords = await dns.promises.resolve(this.srvAddress, 'SRV');
120120
} catch {
121121
this.failure();
122122
return;

test/integration/initial-dns-seedlist-discovery/dns_seedlist.test.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ describe('DNS timeout errors', () => {
3131
await client.close();
3232
});
3333

34-
const restoreDNS =
35-
api =>
36-
async (...args) => {
37-
sinon.restore();
38-
return await dns.promises[api](...args);
39-
};
34+
const restoreDNS = api => async args => {
35+
sinon.restore();
36+
return await dns.promises.resolve(args, api);
37+
};
4038

4139
describe('when SRV record look up times out', () => {
4240
beforeEach(() => {
4341
stub = sinon
44-
.stub(dns.promises, 'resolveSrv')
42+
.stub(dns.promises, 'resolve')
4543
.onFirstCall()
4644
.rejects(new DNSTimeoutError())
4745
.onSecondCall()
48-
.callsFake(restoreDNS('resolveSrv'));
46+
.callsFake(restoreDNS('SRV'));
4947
});
5048

5149
afterEach(async function () {
@@ -61,11 +59,11 @@ describe('DNS timeout errors', () => {
6159
describe('when TXT record look up times out', () => {
6260
beforeEach(() => {
6361
stub = sinon
64-
.stub(dns.promises, 'resolveTxt')
62+
.stub(dns.promises, 'resolve')
6563
.onFirstCall()
6664
.rejects(new DNSTimeoutError())
6765
.onSecondCall()
68-
.callsFake(restoreDNS('resolveTxt'));
66+
.callsFake(restoreDNS('TXT'));
6967
});
7068

7169
afterEach(async function () {
@@ -81,7 +79,7 @@ describe('DNS timeout errors', () => {
8179
describe('when SRV record look up times out twice', () => {
8280
beforeEach(() => {
8381
stub = sinon
84-
.stub(dns.promises, 'resolveSrv')
82+
.stub(dns.promises, 'resolve')
8583
.onFirstCall()
8684
.rejects(new DNSTimeoutError())
8785
.onSecondCall()
@@ -102,7 +100,7 @@ describe('DNS timeout errors', () => {
102100
describe('when TXT record look up times out twice', () => {
103101
beforeEach(() => {
104102
stub = sinon
105-
.stub(dns.promises, 'resolveTxt')
103+
.stub(dns.promises, 'resolve')
106104
.onFirstCall()
107105
.rejects(new DNSTimeoutError())
108106
.onSecondCall()
@@ -123,11 +121,11 @@ describe('DNS timeout errors', () => {
123121
describe('when SRV record look up throws a non-timeout error', () => {
124122
beforeEach(() => {
125123
stub = sinon
126-
.stub(dns.promises, 'resolveSrv')
124+
.stub(dns.promises, 'resolve')
127125
.onFirstCall()
128126
.rejects(new DNSSomethingError())
129127
.onSecondCall()
130-
.callsFake(restoreDNS('resolveSrv'));
128+
.callsFake(restoreDNS('SRV'));
131129
});
132130

133131
afterEach(async function () {
@@ -144,11 +142,11 @@ describe('DNS timeout errors', () => {
144142
describe('when TXT record look up throws a non-timeout error', () => {
145143
beforeEach(() => {
146144
stub = sinon
147-
.stub(dns.promises, 'resolveTxt')
145+
.stub(dns.promises, 'resolve')
148146
.onFirstCall()
149147
.rejects(new DNSSomethingError())
150148
.onSecondCall()
151-
.callsFake(restoreDNS('resolveTxt'));
149+
.callsFake(restoreDNS('TXT'));
152150
});
153151

154152
afterEach(async function () {

test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
2323
beforeEach(async function () {
2424
// this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation
2525

26-
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
26+
sinon.stub(dns.promises, 'resolve').callsFake(async () => {
2727
return [
2828
{
2929
name: 'resolved.mongo.localhost',

0 commit comments

Comments
 (0)