From 96447b86c1169d7d499529d7fc82d228eeddb24b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 10 Jun 2021 23:23:31 +0800 Subject: [PATCH 1/2] 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. --- lib/internal/options.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/internal/options.js b/lib/internal/options.js index a1d4bd984c4881..f359f7b14520d8 100644 --- a/lib/internal/options.js +++ b/lib/internal/options.js @@ -1,12 +1,28 @@ 'use strict'; const { getOptions, shouldNotRegisterESMLoader } = internalBinding('options'); -const { options, aliases } = getOptions(); let warnOnAllowUnauthorized = true; +let optionsMap; +let aliasesMap; + +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 +40,12 @@ function getAllowUnauthorized() { } module.exports = { - options, - aliases, + get options() { + return getOptionsFromBinding(); + }, + get aliases() { + return getAliasesFromBinding(); + }, getOptionValue, getAllowUnauthorized, shouldNotRegisterESMLoader From 7a6a085c4136759efcf8f9979714359efe04204d Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 10 Jun 2021 23:33:09 +0800 Subject: [PATCH 2/2] fixup! lib: make internal/options lazy --- lib/internal/options.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/internal/options.js b/lib/internal/options.js index f359f7b14520d8..1c97aaee97742d 100644 --- a/lib/internal/options.js +++ b/lib/internal/options.js @@ -7,6 +7,10 @@ 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());