lib/uri_parser.js
function applyAuthExpectations(parsed) {
  if (parsed.options == null) {
    return;
  }

  const options = parsed.options;
  const authSource = options.authsource || options.authSource;
  if (authSource != null) {
    parsed.auth = Object.assign({}, parsed.auth, { db: authSource });
  }

  const authMechanism = options.authmechanism || options.authMechanism;
  if (authMechanism != null) {
    if (
      USERNAME_REQUIRED_MECHANISMS.has(authMechanism) &&
      (!parsed.auth || parsed.auth.username == null)
    ) {
      throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``);
    }

    if (authMechanism === 'GSSAPI') {
      if (authSource != null && authSource !== '$external') {
        throw new MongoParseError(
          `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
        );
      }

      parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
    }

    if (authMechanism === 'MONGODB-X509') {
      if (parsed.auth && parsed.auth.password != null) {
        throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``);
      }

      if (authSource != null && authSource !== '$external') {
        throw new MongoParseError(
          `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
        );
      }

      parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
    }

    if (authMechanism === 'PLAIN') {
      if (parsed.auth && parsed.auth.db == null) {
        parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
      }
    }
  }

  // default to `admin` if nothing else was resolved
  if (parsed.auth && parsed.auth.db == null) {
    parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' });
  }

  return parsed;
}
function applyAuthExpectations(parsed) {
  if (parsed.options == null) {
    return;
  }

  const options = parsed.options;
  const authSource = options.authsource || options.authSource;
  if (authSource != null) {
    parsed.auth = Object.assign({}, parsed.auth, { db: authSource });
  }

  const authMechanism = options.authmechanism || options.authMechanism;
  if (authMechanism != null) {
    if (
      USERNAME_REQUIRED_MECHANISMS.has(authMechanism) &&
      (!parsed.auth || parsed.auth.username == null)
    ) {
      throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``);
    }

    if (authMechanism === 'GSSAPI') {
      if (authSource != null && authSource !== '$external') {
        throw new MongoParseError(
          `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
        );
      }

      parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
    }

    if (authMechanism === 'MONGODB-X509') {
      if (parsed.auth && parsed.auth.password != null) {
        throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``);
      }

      if (authSource != null && authSource !== '$external') {
        throw new MongoParseError(
          `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
        );
      }

      parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
    }

    if (authMechanism === 'PLAIN') {
      if (parsed.auth && parsed.auth.db == null) {
        parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
      }
    }
  }

  // default to `admin` if nothing else was resolved
  if (parsed.auth && parsed.auth.db == null) {
    parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' });
  }

  return parsed;
}
function applyAuthExpectations ( parsed ) { if ( parsed . options == null ) { return ; } const options = parsed . options ; const authSource = options . authsource || options . authSource ; if ( authSource != null ) { parsed . auth = Object . assign ( { } , parsed . auth , { db : authSource } ) ; } const authMechanism = options . authmechanism || options . authMechanism ; if ( authMechanism != null ) { if ( USERNAME_REQUIRED_MECHANISMS . has ( authMechanism ) && ( ! parsed . auth || parsed . auth . username == null ) ) { throw new MongoParseError ( ` \` ${ authMechanism } \` ` ) ; } if ( authMechanism === 'GSSAPI' ) { if ( authSource != null && authSource !== '$external' ) { throw new MongoParseError ( ` \` ${ authSource } \` \` ${ authMechanism } \` ` ) ; } parsed . auth = Object . assign ( { } , parsed . auth , { db : '$external' } ) ; } if ( authMechanism === 'MONGODB-X509' ) { if ( parsed . auth && parsed . auth . password != null ) { throw new MongoParseError ( ` \` ${ authMechanism } \` ` ) ; } if ( authSource != null && authSource !== '$external' ) { throw new MongoParseError ( ` \` ${ authSource } \` \` ${ authMechanism } \` ` ) ; } parsed . auth = Object . assign ( { } , parsed . auth , { db : '$external' } ) ; } if ( authMechanism === 'PLAIN' ) { if ( parsed . auth && parsed . auth . db == null ) { parsed . auth = Object . assign ( { } , parsed . auth , { db : '$external' } ) ; } } } // default to `admin` if nothing else was resolved if ( parsed . auth && parsed . auth . db == null ) { parsed . auth = Object . assign ( { } , parsed . auth , { db : 'admin' } ) ; } return parsed ; }
Modifies the parsed connection string object taking into account expectations we
have for authentication-related options.

@param {object} parsed The parsed connection string result
@return The parsed connection string result possibly modified for auth expectations
Modifies the parsed connection string object taking into account expectations we have for authentication - related options .