node/lib/internal/options.js
Aviv Keller bb7d7487f5
lib: replace string prototype usage with alternatives
PR-URL: https://github.com/nodejs/node/pull/52440
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-04-11 21:46:46 +00:00

90 lines
2.1 KiB
JavaScript

'use strict';
const {
getCLIOptions,
getEmbedderOptions: getEmbedderOptionsFromBinding,
} = internalBinding('options');
const {
StringPrototypeSlice,
} = primordials;
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.length > 5 &&
optionName[0] === '-' &&
optionName[1] === '-' &&
optionName[2] === 'n' &&
optionName[3] === 'o' &&
optionName[4] === '-'
) {
const option = options.get('--' + StringPrototypeSlice(optionName, 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,
};