HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-172-31-4-197 6.8.0-1036-aws #38~22.04.1-Ubuntu SMP Fri Aug 22 15:44:33 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/api-parametros/node_modules/mongoose/lib/helpers/processConnectionOptions.js
'use strict';

const clone = require('./clone');
const MongooseError = require('../error/index');

function processConnectionOptions(uri, options) {
  const opts = options ? options : {};
  const readPreference = opts.readPreference
    ? opts.readPreference
    : getUriReadPreference(uri);

  const clonedOpts = clone(opts);
  const resolvedOpts = (readPreference && readPreference !== 'primary' && readPreference !== 'primaryPreferred')
    ? resolveOptsConflicts(readPreference, clonedOpts)
    : clonedOpts;

  return resolvedOpts;
}

function resolveOptsConflicts(pref, opts) {
  // don't silently override user-provided indexing options
  if (setsIndexOptions(opts) && setsSecondaryRead(pref)) {
    throwReadPreferenceError();
  }

  // if user has not explicitly set any auto-indexing options,
  // we can silently default them all to false
  else {
    return defaultIndexOptsToFalse(opts);
  }
}

function setsIndexOptions(opts) {
  const configIdx = opts.config && opts.config.autoIndex;
  const { autoCreate, autoIndex } = opts;
  return !!(configIdx || autoCreate || autoIndex);
}

function setsSecondaryRead(prefString) {
  return !!(prefString === 'secondary' || prefString === 'secondaryPreferred');
}

function getUriReadPreference(connectionString) {
  const exp = /(?:&|\?)readPreference=(\w+)(?:&|$)/;
  const match = exp.exec(connectionString);
  return match ? match[1] : null;
}

function defaultIndexOptsToFalse(opts) {
  opts.config = { autoIndex: false };
  opts.autoCreate = false;
  opts.autoIndex = false;
  return opts;
}

function throwReadPreferenceError() {
  throw new MongooseError(
    'MongoDB prohibits index creation on connections that read from ' +
            'non-primary replicas.  Connections that set "readPreference" to "secondary" or ' +
            '"secondaryPreferred" may not opt-in to the following connection options: ' +
            'autoCreate, autoIndex'
  );
}

module.exports = processConnectionOptions;