diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2021-06-10 18:23:31 +0300 |
---|---|---|
committer | Danielle Adams <adamzdanielle@gmail.com> | 2021-06-22 01:30:41 +0300 |
commit | f17dde81f3f497d4fc37533c1a41757be0af433a (patch) | |
tree | 4e6b1ee7c9296ec8d5b0e7d3a64f49427fee014b /lib | |
parent | 5f51729014e10ee304747815a17b8980efdf909a (diff) |
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: https://github.com/nodejs/node/pull/38993
Refs: https://github.com/nodejs/node/issues/35711
Refs: https://github.com/nodejs/node/pull/38905
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/options.js | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/lib/internal/options.js b/lib/internal/options.js index a1d4bd984c4..1c97aaee977 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 |