All files / lib/internal options.js

100% Statements 78/78
100% Branches 15/15
100% Functions 8/8
100% Lines 78/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 7923x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 394649x 394649x 7023x 7023x 394649x 394649x 23x 31x 31x 31x 31x 31x 31x 23x 12980x 12980x 6490x 6490x 12980x 12980x 23x 5263x 5263x 5263x 5263x 23x 394618x 394618x 394618x 32458x 32458x 32458x 394618x 394618x 23x 1482x 1482x 1482x 1482x 4x 4x 4x 4x 4x 4x 4x 1482x 1482x 23x 23x 23x 31x 23x 23x 31x 23x 23x 23x 23x 23x 23x  
'use strict';
 
const {
  getCLIOptions,
  getEmbedderOptions: getEmbedderOptionsFromBinding,
} = internalBinding('options');
 
let warnOnAllowUnauthorized = true;
 
let optionsMap;
let aliasesMap;
let embedderOptions;
 
// getCLIOptions() would serialize the option values from C++ land.
// It would error if the values are queried before bootstrap is
// complete so that we don't accidentally include runtime-dependent
// states into a runtime-independent snapshot.
function getCLIOptionsFromBinding() {
  if (!optionsMap) {
    ({ options: optionsMap } = getCLIOptions());
  }
  return optionsMap;
}
 
function getAliasesFromBinding() {
  if (!aliasesMap) {
    ({ aliases: aliasesMap } = getCLIOptions());
  }
  return aliasesMap;
}
 
function getEmbedderOptions() {
  if (!embedderOptions) {
    embedderOptions = getEmbedderOptionsFromBinding();
  }
  return embedderOptions;
}
 
function refreshOptions() {
  optionsMap = undefined;
  aliasesMap = undefined;
}
 
function getOptionValue(optionName) {
  const options = getCLIOptionsFromBinding();
  if (optionName.startsWith('--no-')) {
    const option = options.get('--' + optionName.slice(5));
    return option && !option.value;
  }
  return options.get(optionName)?.value;
}
 
function getAllowUnauthorized() {
  const allowUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0';
 
  if (allowUnauthorized && warnOnAllowUnauthorized) {
    warnOnAllowUnauthorized = false;
    process.emitWarning(
      'Setting the NODE_TLS_REJECT_UNAUTHORIZED ' +
      'environment variable to \'0\' makes TLS connections ' +
      'and HTTPS requests insecure by disabling ' +
      'certificate verification.');
  }
  return allowUnauthorized;
}
 
module.exports = {
  get options() {
    return getCLIOptionsFromBinding();
  },
  get aliases() {
    return getAliasesFromBinding();
  },
  getOptionValue,
  getAllowUnauthorized,
  getEmbedderOptions,
  refreshOptions
};