node/lib/internal/options.js
Joyee Cheung a480e52b6e
src: reduce unnecessary serialization of CLI options in C++
In this patch we split the serialization routine into two different
routines: `getCLIOptionsValues()` for only serializing the key-value
pairs and `getCLIOptionsInfo()` for getting additional information such
as help text etc. The former is used a lot more frequently than the
latter, which is only used for generating `--help` and building
`process.allowedNodeEnvironmentFlags`.

`getCLIOptionsValues()` also adds `--no-` entries for boolean options so
there is no need to special case in the JS land.
This patch also refactors the option serialization routines to
use v8::Object constructor that takes key-value pairs in one go
to avoid calling Map::Set or Object::Set repeatedly which can go
up to a patched prototype.

PR-URL: https://github.com/nodejs/node/pull/52451
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2024-05-02 22:57:03 +00:00

70 lines
1.7 KiB
JavaScript

'use strict';
const {
getCLIOptionsValues,
getCLIOptionsInfo,
getEmbedderOptions: getEmbedderOptionsFromBinding,
} = internalBinding('options');
let warnOnAllowUnauthorized = true;
let optionsDict;
let cliInfo;
let embedderOptions;
// getCLIOptionsValues() 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 (!optionsDict) {
optionsDict = getCLIOptionsValues();
}
return optionsDict;
}
function getCLIOptionsInfoFromBinding() {
if (!cliInfo) {
cliInfo = getCLIOptionsInfo();
}
return cliInfo;
}
function getEmbedderOptions() {
if (!embedderOptions) {
embedderOptions = getEmbedderOptionsFromBinding();
}
return embedderOptions;
}
function refreshOptions() {
optionsDict = undefined;
}
function getOptionValue(optionName) {
const optionsDict = getCLIOptionsFromBinding();
return optionsDict[optionName];
}
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 = {
getCLIOptionsInfo: getCLIOptionsInfoFromBinding,
getOptionValue,
getAllowUnauthorized,
getEmbedderOptions,
refreshOptions,
};