diff --git a/lib/internal/options.js b/lib/internal/options.js index a1d4bd984c4881..1c97aaee97742d 100644 --- a/lib/internal/options.js +++ b/lib/internal/options.js @@ -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() { @@ -24,8 +44,12 @@ function getAllowUnauthorized() { } module.exports = { - options, - aliases, + get options() { + return getOptionsFromBinding(); + }, + get aliases() { + return getAliasesFromBinding(); + }, getOptionValue, getAllowUnauthorized, shouldNotRegisterESMLoader