Skip to content

Commit 199fe32

Browse files
joyeecheungrichardlau
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 <[email protected]> Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent a657f25 commit 199fe32

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
letwarnOnAllowUnauthorized=true;
76

7+
letoptionsMap;
8+
letaliasesMap;
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+
functiongetOptionsFromBinding(){
15+
if(!optionsMap){
16+
({options: optionsMap}=getOptions());
17+
}
18+
returnoptionsMap;
19+
}
20+
21+
functiongetAliasesFromBinding(){
22+
if(!aliasesMap){
23+
({aliases: aliasesMap}=getOptions());
24+
}
25+
returnaliasesMap;
26+
}
27+
828
functiongetOptionValue(option){
9-
returnoptions.get(option)?.value;
29+
returngetOptionsFromBinding().get(option)?.value;
1030
}
1131

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

2646
module.exports={
27-
options,
28-
aliases,
47+
getoptions(){
48+
returngetOptionsFromBinding();
49+
},
50+
getaliases(){
51+
returngetAliasesFromBinding();
52+
},
2953
getOptionValue,
3054
getAllowUnauthorized,
3155
shouldNotRegisterESMLoader

0 commit comments

Comments
(0)