lib/uri_parser.js
function applyConnectionStringOption(obj, key, value, options) {
  // simple key translation
  if (key === 'journal') {
    key = 'j';
  } else if (key === 'wtimeoutms') {
    key = 'wtimeout';
  }

  // more complicated translation
  if (BOOLEAN_OPTIONS.has(key)) {
    value = value === 'true' || value === true;
  } else if (key === 'appname') {
    value = decodeURIComponent(value);
  } else if (key === 'readconcernlevel') {
    obj['readConcernLevel'] = value;
    key = 'readconcern';
    value = { level: value };
  }

  // simple validation
  if (key === 'compressors') {
    value = Array.isArray(value) ? value : [value];

    if (!value.every(c => c === 'snappy' || c === 'zlib')) {
      throw new MongoParseError(
        'Value for `compressors` must be at least one of: `snappy`, `zlib`'
      );
    }
  }

  if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) {
    throw new MongoParseError(
      'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`'
    );
  }

  if (key === 'readpreference' && !ReadPreference.isValid(value)) {
    throw new MongoParseError(
      'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`'
    );
  }

  if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) {
    throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9');
  }

  // special cases
  if (key === 'compressors' || key === 'zlibcompressionlevel') {
    obj.compression = obj.compression || {};
    obj = obj.compression;
  }

  if (key === 'authmechanismproperties') {
    if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME;
    if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM;
    if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') {
      obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME;
    }
  }

  if (key === 'readpreferencetags' && Array.isArray(value)) {
    value = splitArrayOfMultipleReadPreferenceTags(value);
  }

  // set the actual value
  if (options.caseTranslate && CASE_TRANSLATION[key]) {
    obj[CASE_TRANSLATION[key]] = value;
    return;
  }

  obj[key] = value;
}
function applyConnectionStringOption(obj, key, value, options) {
  // simple key translation
  if (key === 'journal') {
    key = 'j';
  } else if (key === 'wtimeoutms') {
    key = 'wtimeout';
  }

  // more complicated translation
  if (BOOLEAN_OPTIONS.has(key)) {
    value = value === 'true' || value === true;
  } else if (key === 'appname') {
    value = decodeURIComponent(value);
  } else if (key === 'readconcernlevel') {
    obj['readConcernLevel'] = value;
    key = 'readconcern';
    value = { level: value };
  }

  // simple validation
  if (key === 'compressors') {
    value = Array.isArray(value) ? value : [value];

    if (!value.every(c => c === 'snappy' || c === 'zlib')) {
      throw new MongoParseError(
        'Value for `compressors` must be at least one of: `snappy`, `zlib`'
      );
    }
  }

  if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) {
    throw new MongoParseError(
      'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`'
    );
  }

  if (key === 'readpreference' && !ReadPreference.isValid(value)) {
    throw new MongoParseError(
      'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`'
    );
  }

  if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) {
    throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9');
  }

  // special cases
  if (key === 'compressors' || key === 'zlibcompressionlevel') {
    obj.compression = obj.compression || {};
    obj = obj.compression;
  }

  if (key === 'authmechanismproperties') {
    if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME;
    if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM;
    if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') {
      obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME;
    }
  }

  if (key === 'readpreferencetags' && Array.isArray(value)) {
    value = splitArrayOfMultipleReadPreferenceTags(value);
  }

  // set the actual value
  if (options.caseTranslate && CASE_TRANSLATION[key]) {
    obj[CASE_TRANSLATION[key]] = value;
    return;
  }

  obj[key] = value;
}
function applyConnectionStringOption ( obj , key , value , options ) { // simple key translation if ( key === 'journal' ) { key = 'j' ; } else if ( key === 'wtimeoutms' ) { key = 'wtimeout' ; } // more complicated translation if ( BOOLEAN_OPTIONS . has ( key ) ) { value = value === 'true' || value === true ; } else if ( key === 'appname' ) { value = decodeURIComponent ( value ) ; } else if ( key === 'readconcernlevel' ) { obj [ 'readConcernLevel' ] = value ; key = 'readconcern' ; value = { level : value } ; } // simple validation if ( key === 'compressors' ) { value = Array . isArray ( value ) ? value : [ value ] ; if ( ! value . every ( c => c === 'snappy' || c === 'zlib' ) ) { throw new MongoParseError ( 'Value for `compressors` must be at least one of: `snappy`, `zlib`' ) ; } } if ( key === 'authmechanism' && ! AUTH_MECHANISMS . has ( value ) ) { throw new MongoParseError ( 'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`' ) ; } if ( key === 'readpreference' && ! ReadPreference . isValid ( value ) ) { throw new MongoParseError ( 'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`' ) ; } if ( key === 'zlibcompressionlevel' && ( value < - 1 || value > 9 ) ) { throw new MongoParseError ( 'zlibCompressionLevel must be an integer between -1 and 9' ) ; } // special cases if ( key === 'compressors' || key === 'zlibcompressionlevel' ) { obj . compression = obj . compression || { } ; obj = obj . compression ; } if ( key === 'authmechanismproperties' ) { if ( typeof value . SERVICE_NAME === 'string' ) obj . gssapiServiceName = value . SERVICE_NAME ; if ( typeof value . SERVICE_REALM === 'string' ) obj . gssapiServiceRealm = value . SERVICE_REALM ; if ( typeof value . CANONICALIZE_HOST_NAME !== 'undefined' ) { obj . gssapiCanonicalizeHostName = value . CANONICALIZE_HOST_NAME ; } } if ( key === 'readpreferencetags' && Array . isArray ( value ) ) { value = splitArrayOfMultipleReadPreferenceTags ( value ) ; } // set the actual value if ( options . caseTranslate && CASE_TRANSLATION [ key ] ) { obj [ CASE_TRANSLATION [ key ] ] = value ; return ; } obj [ key ] = value ; }
Sets the value for `key`, allowing for any required translation

@param {object} obj The object to set the key on
@param {string} key The key to set the value for
@param {*} value The value to set
@param {object} options The options used for option parsing
Sets the value for key allowing for any required translation