lib/uri_parser.js
function parseSrvConnectionString(uri, options, callback) {
  const result = URL.parse(uri, true);

  if (result.hostname.split('.').length < 3) {
    return callback(new MongoParseError('URI does not have hostname, domain name and tld'));
  }

  result.domainLength = result.hostname.split('.').length;
  if (result.pathname && result.pathname.match(',')) {
    return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames'));
  }

  if (result.port) {
    return callback(new MongoParseError(`Ports not accepted with '${PROTOCOL_MONGODB_SRV}' URIs`));
  }

  // Resolve the SRV record and use the result as the list of hosts to connect to.
  const lookupAddress = result.host;
  dns.resolveSrv(`_mongodb._tcp.${lookupAddress}`, (err, addresses) => {
    if (err) return callback(err);

    if (addresses.length === 0) {
      return callback(new MongoParseError('No addresses found at host'));
    }

    for (let i = 0; i < addresses.length; i++) {
      if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
        return callback(
          new MongoParseError('Server record does not share hostname with parent URI')
        );
      }
    }

    // Convert the original URL to a non-SRV URL.
    result.protocol = 'mongodb';
    result.host = addresses.map(address => `${address.name}:${address.port}`).join(',');

    // Default to SSL true if it's not specified.
    if (
      !('ssl' in options) &&
      (!result.search || !('ssl' in result.query) || result.query.ssl === null)
    ) {
      result.query.ssl = true;
    }

    // Resolve TXT record and add options from there if they exist.
    dns.resolveTxt(lookupAddress, (err, record) => {
      if (err) {
        if (err.code !== 'ENODATA') {
          return callback(err);
        }
        record = null;
      }

      if (record) {
        if (record.length > 1) {
          return callback(new MongoParseError('Multiple text records not allowed'));
        }

        record = qs.parse(record[0].join(''));
        if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
          return callback(
            new MongoParseError('Text record must only set `authSource` or `replicaSet`')
          );
        }

        Object.assign(result.query, record);
      }

      // Set completed options back into the URL object.
      result.search = qs.stringify(result.query);

      const finalString = URL.format(result);
      parseConnectionString(finalString, options, callback);
    });
  });
}
function parseSrvConnectionString(uri, options, callback) {
  const result = URL.parse(uri, true);

  if (result.hostname.split('.').length < 3) {
    return callback(new MongoParseError('URI does not have hostname, domain name and tld'));
  }

  result.domainLength = result.hostname.split('.').length;
  if (result.pathname && result.pathname.match(',')) {
    return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames'));
  }

  if (result.port) {
    return callback(new MongoParseError(`Ports not accepted with '${PROTOCOL_MONGODB_SRV}' URIs`));
  }

  // Resolve the SRV record and use the result as the list of hosts to connect to.
  const lookupAddress = result.host;
  dns.resolveSrv(`_mongodb._tcp.${lookupAddress}`, (err, addresses) => {
    if (err) return callback(err);

    if (addresses.length === 0) {
      return callback(new MongoParseError('No addresses found at host'));
    }

    for (let i = 0; i < addresses.length; i++) {
      if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
        return callback(
          new MongoParseError('Server record does not share hostname with parent URI')
        );
      }
    }

    // Convert the original URL to a non-SRV URL.
    result.protocol = 'mongodb';
    result.host = addresses.map(address => `${address.name}:${address.port}`).join(',');

    // Default to SSL true if it's not specified.
    if (
      !('ssl' in options) &&
      (!result.search || !('ssl' in result.query) || result.query.ssl === null)
    ) {
      result.query.ssl = true;
    }

    // Resolve TXT record and add options from there if they exist.
    dns.resolveTxt(lookupAddress, (err, record) => {
      if (err) {
        if (err.code !== 'ENODATA') {
          return callback(err);
        }
        record = null;
      }

      if (record) {
        if (record.length > 1) {
          return callback(new MongoParseError('Multiple text records not allowed'));
        }

        record = qs.parse(record[0].join(''));
        if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
          return callback(
            new MongoParseError('Text record must only set `authSource` or `replicaSet`')
          );
        }

        Object.assign(result.query, record);
      }

      // Set completed options back into the URL object.
      result.search = qs.stringify(result.query);

      const finalString = URL.format(result);
      parseConnectionString(finalString, options, callback);
    });
  });
}
function parseSrvConnectionString ( uri , options , callback ) { const result = URL . parse ( uri , true ) ; if ( result . hostname . split ( '.' ) . length < 3 ) { return callback ( new MongoParseError ( 'URI does not have hostname, domain name and tld' ) ) ; } result . domainLength = result . hostname . split ( '.' ) . length ; if ( result . pathname && result . pathname . match ( ',' ) ) { return callback ( new MongoParseError ( 'Invalid URI, cannot contain multiple hostnames' ) ) ; } if ( result . port ) { return callback ( new MongoParseError ( ` ${ PROTOCOL_MONGODB_SRV } ` ) ) ; } // Resolve the SRV record and use the result as the list of hosts to connect to. const lookupAddress = result . host ; dns . resolveSrv ( ` ${ lookupAddress } ` , ( err , addresses ) => { if ( err ) return callback ( err ) ; if ( addresses . length === 0 ) { return callback ( new MongoParseError ( 'No addresses found at host' ) ) ; } for ( let i = 0 ; i < addresses . length ; i ++ ) { if ( ! matchesParentDomain ( addresses [ i ] . name , result . hostname , result . domainLength ) ) { return callback ( new MongoParseError ( 'Server record does not share hostname with parent URI' ) ) ; } } // Convert the original URL to a non-SRV URL. result . protocol = 'mongodb' ; result . host = addresses . map ( address => ` ${ address . name } ${ address . port } ` ) . join ( ',' ) ; // Default to SSL true if it's not specified. if ( ! ( 'ssl' in options ) && ( ! result . search || ! ( 'ssl' in result . query ) || result . query . ssl === null ) ) { result . query . ssl = true ; } // Resolve TXT record and add options from there if they exist. dns . resolveTxt ( lookupAddress , ( err , record ) => { if ( err ) { if ( err . code !== 'ENODATA' ) { return callback ( err ) ; } record = null ; } if ( record ) { if ( record . length > 1 ) { return callback ( new MongoParseError ( 'Multiple text records not allowed' ) ) ; } record = qs . parse ( record [ 0 ] . join ( '' ) ) ; if ( Object . keys ( record ) . some ( key => key !== 'authSource' && key !== 'replicaSet' ) ) { return callback ( new MongoParseError ( 'Text record must only set `authSource` or `replicaSet`' ) ) ; } Object . assign ( result . query , record ) ; } // Set completed options back into the URL object. result . search = qs . stringify ( result . query ) ; const finalString = URL . format ( result ) ; parseConnectionString ( finalString , options , callback ) ; } ) ; } ) ; }
Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal
connection string.

@param {string} uri The connection string to parse
@param {object} options Optional user provided connection string options
@param {function} callback
Lookup a mongodb + srv connection string combine the parts and reparse it as a normal connection string .