◐ Shell
clean mode source ↗

src: reduce unnecessary serialization of CLI options in C++ · nodejs/node@a6dd864

11

'use strict';

2233

const {

4-

getCLIOptions,

4+

getCLIOptionsValues,

5+

getCLIOptionsInfo,

56

getEmbedderOptions: getEmbedderOptionsFromBinding,

67

} = internalBinding('options');

788-

const {

9-

StringPrototypeSlice,

10-

} = primordials;

11-129

let warnOnAllowUnauthorized = true;

131014-

let optionsMap;

15-

let aliasesMap;

11+

let optionsDict;

12+

let cliInfo;

1613

let embedderOptions;

171418-

// getCLIOptions() would serialize the option values from C++ land.

15+

// getCLIOptionsValues() would serialize the option values from C++ land.

1916

// It would error if the values are queried before bootstrap is

2017

// complete so that we don't accidentally include runtime-dependent

2118

// states into a runtime-independent snapshot.

2219

function getCLIOptionsFromBinding() {

23-

if (!optionsMap) {

24-

({ options: optionsMap } = getCLIOptions());

20+

if (!optionsDict) {

21+

optionsDict = getCLIOptionsValues();

2522

}

26-

return optionsMap;

23+

return optionsDict;

2724

}

282529-

function getAliasesFromBinding() {

30-

if (!aliasesMap) {

31-

({ aliases: aliasesMap } = getCLIOptions());

26+

function getCLIOptionsInfoFromBinding() {

27+

if (!cliInfo) {

28+

cliInfo = getCLIOptionsInfo();

3229

}

33-

return aliasesMap;

30+

return cliInfo;

3431

}

35323633

function getEmbedderOptions() {

@@ -41,24 +38,12 @@ function getEmbedderOptions() {

4138

}

42394340

function refreshOptions() {

44-

optionsMap = undefined;

45-

aliasesMap = undefined;

41+

optionsDict = undefined;

4642

}

47434844

function getOptionValue(optionName) {

49-

const options = getCLIOptionsFromBinding();

50-

if (

51-

optionName.length > 5 &&

52-

optionName[0] === '-' &&

53-

optionName[1] === '-' &&

54-

optionName[2] === 'n' &&

55-

optionName[3] === 'o' &&

56-

optionName[4] === '-'

57-

) {

58-

const option = options.get('--' + StringPrototypeSlice(optionName, 5));

59-

return option && !option.value;

60-

}

61-

return options.get(optionName)?.value;

45+

const optionsDict = getCLIOptionsFromBinding();

46+

return optionsDict[optionName];

6247

}

63486449

function getAllowUnauthorized() {

@@ -76,12 +61,7 @@ function getAllowUnauthorized() {

7661

}

77627863

module.exports = {

79-

get options() {

80-

return getCLIOptionsFromBinding();

81-

},

82-

get aliases() {

83-

return getAliasesFromBinding();

84-

},

64+

getCLIOptionsInfo: getCLIOptionsInfoFromBinding,

8565

getOptionValue,

8666

getAllowUnauthorized,

8767

getEmbedderOptions,