From 19d3511f41593995966c9f397e722acdce0669fa Mon Sep 17 00:00:00 2001 From: Anemy Date: Mon, 14 Dec 2020 11:54:37 -0500 Subject: [PATCH 1/2] fix: set direct connection true on single host no specified replica set connections --- lib/connect.js | 11 +++++++++++ lib/model.js | 3 ++- package-lock.json | 8 ++++---- package.json | 2 +- test/connect.test.js | 1 + test/parse-uri-components.test.js | 33 +++++++++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/connect.js b/lib/connect.js index 1c6ca007..4a5cdc8b 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -215,6 +215,17 @@ const getTasks = (model, setupListeners) => { validOptions.useNewUrlParser = true; validOptions.useUnifiedTopology = true; + if ( + model.directConnection === undefined && + model.hosts.length === 1 && + (model.replicaSet === undefined || model.replicaSet === '') + ) { + // Previous to the node driver 3.6.3, directConnection was automatically + // set to true under these conditions. In 3.6.3 this defaulting was removed + // and now we add it. + // https://github.com/mongodb/node-mongodb-native/commit/f8fd310a11a91db82f1c0ddc57482b8edabc231b + validOptions.directConnection = true; + } const mongoClient = new MongoClient(model.driverUrlWithSsh, validOptions); diff --git a/lib/model.js b/lib/model.js index 6a2885b7..18560872 100644 --- a/lib/model.js +++ b/lib/model.js @@ -169,7 +169,8 @@ const CONNECTION_STRING_OPTIONS = { 'pythonLegacy' ], default: undefined - } + }, + directConnection: { type: 'boolean', default: undefined } }; assign(props, CONNECTION_STRING_OPTIONS); diff --git a/package-lock.json b/package-lock.json index 38101219..ee63e1ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4382,11 +4382,11 @@ "dev": true }, "mongodb": { - "version": "3.6.1", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.1.tgz", - "integrity": "sha512-uH76Zzr5wPptnjEKJRQnwTsomtFOU/kQEU8a9hKHr2M7y9qVk7Q4Pkv0EQVp88742z9+RwvsdTw6dRjDZCNu1g==", + "version": "3.6.3", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.3.tgz", + "integrity": "sha512-rOZuR0QkodZiM+UbQE5kDsJykBqWi0CL4Ec2i1nrGrUI3KO11r6Fbxskqmq3JK2NH7aW4dcccBuUujAP0ERl5w==", "requires": { - "bl": "^2.2.0", + "bl": "^2.2.1", "bson": "^1.1.4", "denque": "^1.4.1", "require_optional": "^1.0.1", diff --git a/package.json b/package.json index 5c8d9aa0..ad8be4f6 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "async": "^3.1.0", "debug": "^4.1.1", "lodash": "^4.17.15", - "mongodb": "^3.6.1", + "mongodb": "^3.6.3", "raf": "^3.4.1", "ssh2": "^0.8.7", "storage-mixin": "^3.3.4" diff --git a/test/connect.test.js b/test/connect.test.js index b48ee1b0..fdf68233 100644 --- a/test/connect.test.js +++ b/test/connect.test.js @@ -37,6 +37,7 @@ describe('connection model connector', () => { assert.deepStrictEqual(options, { connectWithNoPrimary: true, + directConnection: true, readPreference: 'primary', useNewUrlParser: true, useUnifiedTopology: true diff --git a/test/parse-uri-components.test.js b/test/parse-uri-components.test.js index 914e5bec..825bdd33 100644 --- a/test/parse-uri-components.test.js +++ b/test/parse-uri-components.test.js @@ -548,6 +548,39 @@ describe('connection model partser should parse URI components such as', () => { } ); }); + + it('defaults directConnection undefined', (done) => { + Connection.from( + 'mongodb://localhost:27017', + (error, result) => { + expect(error).to.not.exist; + expect(result.directConnection).to.be.equal(undefined); + done(); + } + ); + }); + + it('saves directConnection true', (done) => { + Connection.from( + 'mongodb://localhost:27017/?directConnection=true', + (error, result) => { + expect(error).to.not.exist; + expect(result.directConnection).to.be.equal(true); + done(); + } + ); + }); + + it('saves directConnection false', (done) => { + Connection.from( + 'mongodb://localhost:27017/?directConnection=false', + (error, result) => { + expect(error).to.not.exist; + expect(result.directConnection).to.be.equal(false); + done(); + } + ); + }); }); describe('miscellaneous configuration', () => { From d5fff0c6bdc085e43fcbc40e57d129049e56d77e Mon Sep 17 00:00:00 2001 From: Anemy Date: Mon, 14 Dec 2020 11:57:18 -0500 Subject: [PATCH 2/2] improve code comment --- lib/connect.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/connect.js b/lib/connect.js index 4a5cdc8b..6fbba7b5 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -220,9 +220,9 @@ const getTasks = (model, setupListeners) => { model.hosts.length === 1 && (model.replicaSet === undefined || model.replicaSet === '') ) { - // Previous to the node driver 3.6.3, directConnection was automatically - // set to true under these conditions. In 3.6.3 this defaulting was removed - // and now we add it. + // Previous to the node driver 3.6.3, directConnection was + // set to true under these conditions. In 3.6.3 this defaulting + // behavior was removed and now we add it. COMPASS-4534 // https://github.com/mongodb/node-mongodb-native/commit/f8fd310a11a91db82f1c0ddc57482b8edabc231b validOptions.directConnection = true; }