Skip to content

Commit

Permalink
lib: make internal/options lazy
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and richardlau committed Jul 20, 2021
1 parent a657f25 commit 199fe32
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/internal/options.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
'use strict';

const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options');
const { options, aliases } = getOptions();

let warnOnAllowUnauthorized = true;

let optionsMap;
let aliasesMap;

// getOptions() 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 getOptionsFromBinding() {
if (!optionsMap) {
({ options: optionsMap } = getOptions());
}
return optionsMap;
}

function getAliasesFromBinding() {
if (!aliasesMap) {
({ aliases: aliasesMap } = getOptions());
}
return aliasesMap;
}

function getOptionValue(option) {
return options.get(option)?.value;
return getOptionsFromBinding().get(option)?.value;
}

function getAllowUnauthorized() {
Expand All @@ -24,8 +44,12 @@ function getAllowUnauthorized() {
}

module.exports = {
options,
aliases,
get options() {
return getOptionsFromBinding();
},
get aliases() {
return getAliasesFromBinding();
},
getOptionValue,
getAllowUnauthorized,
shouldNotRegisterESMLoader
Expand Down

0 comments on commit 199fe32

Please sign in to comment.