Skip to content

Commit

Permalink
fix: isAtlas check not working properly
Browse files Browse the repository at this point in the history
Use ServerDescription methods for getting server host and port instead of parsing hostname,
which did not work properly.

Closes #12063
  • Loading branch information
skrtheboss committed Jul 15, 2022
1 parent 115f922 commit 2bc0704
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/helpers/topology/isAtlas.js
Expand Up @@ -2,25 +2,30 @@

const getConstructorName = require('../getConstructorName');

/**
* @typedef { import('mongodb') } TopologyDescription
*/

/**
* Checks if topologyDescription contains servers connected to an atlas instance
*
* @param {TopologyDescription} topologyDescription
* @returns {boolean}
*/
module.exports = function isAtlas(topologyDescription) {
if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
return false;
}

const hostnames = Array.from(topologyDescription.servers.keys());

if (hostnames.length === 0) {
if (topologyDescription.servers.size === 0) {
return false;
}

for (let i = 0, il = hostnames.length; i < il; ++i) {
const url = new URL(hostnames[i]);
if (
url.hostname.endsWith('.mongodb.net') === false ||
url.port !== '27017'
) {
for (const server of topologyDescription.servers.values()) {
if (server.host.endsWith('.mongodb.net') === false || server.port !== 27017) {
return false;
}
}

return true;
};

0 comments on commit 2bc0704

Please sign in to comment.