Skip to content

Commit f17dde8

Browse files
joyeecheungdanielleadams
authored andcommitted
lib: make internal/options lazy
This way, internal modules can still require the module and cache the function getOptionValue() early (therefore these code can be included in the snapshots), but the options map won't be serialized from C++ land until the option values are actually queried. PR-URL: #38993 Refs: #35711 Refs: #38905 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 5f51729 commit f17dde8

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

lib/internal/options.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
'use strict';
22

33
const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options');
4-
const { options, aliases } = getOptions();
54

65
let warnOnAllowUnauthorized = true;
76

7+
let optionsMap;
8+
let aliasesMap;
9+
10+
// getOptions() would serialize the option values from C++ land.
11+
// It would error if the values are queried before bootstrap is
12+
// complete so that we don't accidentally include runtime-dependent
13+
// states into a runtime-independent snapshot.
14+
function getOptionsFromBinding() {
15+
if (!optionsMap) {
16+
({ options: optionsMap } = getOptions());
17+
}
18+
return optionsMap;
19+
}
20+
21+
function getAliasesFromBinding() {
22+
if (!aliasesMap) {
23+
({ aliases: aliasesMap } = getOptions());
24+
}
25+
return aliasesMap;
26+
}
27+
828
function getOptionValue(option) {
9-
return options.get(option)?.value;
29+
return getOptionsFromBinding().get(option)?.value;
1030
}
1131

1232
function getAllowUnauthorized() {
@@ -24,8 +44,12 @@ function getAllowUnauthorized() {
2444
}
2545

2646
module.exports = {
27-
options,
28-
aliases,
47+
get options() {
48+
return getOptionsFromBinding();
49+
},
50+
get aliases() {
51+
return getAliasesFromBinding();
52+
},
2953
getOptionValue,
3054
getAllowUnauthorized,
3155
shouldNotRegisterESMLoader

0 commit comments

Comments
 (0)