Skip to content

Commit 5d8fd40

Browse files
authored
feat(devtools-connect): make dns lookup configurable COMPASS-9793 (#602)
1 parent ee947bf commit 5d8fd40

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

packages/devtools-connect/.depcheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ ignores:
88
# this is explicitly called as a dependency to make sure that is bootstrapped
99
# before mongodb-runner, since it is required via mongodb
1010
- '@mongodb-js/saslprep'
11+
- 'ts-node' # used by mocha
1112
ignore-patterns:
1213
- 'dist'

packages/devtools-connect/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"depcheck": "depcheck",
3737
"check": "npm run typecheck && npm run lint && npm run depcheck",
3838
"check-ci": "npm run check",
39-
"testonly": "nyc mocha --colors -r ts-node/register src/**/*.spec.ts",
39+
"testonly": "nyc mocha --colors --register ts-node/register src/**/*.spec.ts",
4040
"test": "npm run lint && npm run compile && npm run testonly",
4141
"test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
4242
"test-watch": "npm run test -- --watch",

packages/devtools-connect/src/connect.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,30 @@ describe('devtools connect', function () {
412412
expect(result.client).to.equal(mClient);
413413
});
414414

415+
it('allows lookup to be configured', async function () {
416+
const uri = 'localhost:27017';
417+
const mClient = stubConstructor(FakeMongoClient);
418+
const mClientType = sinon.stub().returns(mClient);
419+
mClient.connect.onFirstCall().resolves(mClient);
420+
const lookupSpy = sinon.spy();
421+
await connectMongoClient(
422+
uri,
423+
{ ...defaultOpts, useSystemCA: false, lookup: lookupSpy },
424+
bus,
425+
mClientType as any,
426+
);
427+
428+
// get options passed to mongo client
429+
const finalClientOptions = mClientType.getCalls()[0].args[1];
430+
expect(finalClientOptions.lookup).to.not.equal(lookupSpy); // lookup is always wrapped
431+
finalClientOptions.lookup('localhost', 27017, () => {}); // invoke it the way it would be by net/tls
432+
expect(lookupSpy.getCalls()).to.have.lengthOf(1); // verify our input lookup was called instead of the dns package
433+
expect(lookupSpy.getCalls()[0].args[1]).to.have.property(
434+
'verbatim',
435+
false,
436+
); // but we still always set verbatim to false
437+
});
438+
415439
describe('retryable TLS errors', function () {
416440
it('retries TLS errors without system CA integration enabled -- MongoClient error', async function () {
417441
const uri = 'localhost:27017';

packages/devtools-connect/src/connect.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,15 @@ async function connectMongoClientImpl({
588588
ca ? { ca } : {},
589589
);
590590

591+
const customLookup = mongoClientOptions.lookup;
591592
// Adopt dns result order changes with Node v18 that affected the VSCode extension VSCODE-458.
592593
// Refs https://github.com/microsoft/vscode/issues/189805
593594
mongoClientOptions.lookup = (hostname, options, callback) => {
594-
return dns.lookup(hostname, { verbatim: false, ...options }, callback);
595+
return (customLookup ?? dns.lookup)(
596+
hostname,
597+
{ verbatim: false, ...options },
598+
callback,
599+
);
595600
};
596601

597602
delete (mongoClientOptions as any).useSystemCA; // can be removed once no product uses this anymore

0 commit comments

Comments
 (0)