diff options
author | Stephen Gallagher <sgallagh@redhat.com> | 2019-12-07 00:40:25 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2019-12-18 02:17:19 +0300 |
commit | 00cbf5b1b621b8c5f6bdeaeb20653035fcf9bef8 (patch) | |
tree | b4767179d58fc3a7465a1d3136c21cbd2d13d746 /src | |
parent | 30d32492a0ec50eb51363506f38656d8a7673d54 (diff) |
build: auto-load ICU data from --with-icu-default-data-dir
When compiled with `--with-intl=small` and
`--with-icu-default-data-dir=PATH`, Node.js will use PATH as a
fallback location for the ICU data.
We will first perform an access check using fopen(PATH, 'r') to
ensure that the file is readable. If it is, we'll set the
icu_data_directory and proceed. There's a slight overhead for the
fopen() check, but it should be barely measurable.
This will be useful for Linux distribution packagers who want to
be able to ship a minimal node binary in a container image but
also be able to add on the full i18n support where needed. With
this patch, it becomes possible to ship the interpreter as
/usr/bin/node in one package for the distribution and to ship the
data files in another package (without a strict dependency
between the two). This means that users of the distribution will
not need to explicitly direct Node.js to locate the ICU data. It
also means that in environments where full internationalization is
not required, they do not need to carry the extra content (with
the associated storage costs).
Refs: https://github.com/nodejs/node/issues/3460
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
PR-URL: https://github.com/nodejs/node/pull/30825
Reviewed-By: Steven R Loomis <srloomis@us.ibm.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/node.cc | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/node.cc b/src/node.cc index b44f0210842..d8da205b681 100644 --- a/src/node.cc +++ b/src/node.cc @@ -90,6 +90,7 @@ #if defined(NODE_HAVE_I18N_SUPPORT) #include <unicode/uvernum.h> +#include <unicode/utypes.h> #endif @@ -882,6 +883,25 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv, if (per_process::cli_options->icu_data_dir.empty()) credentials::SafeGetenv("NODE_ICU_DATA", &per_process::cli_options->icu_data_dir); + +#ifdef NODE_ICU_DEFAULT_DATA_DIR + // If neither the CLI option nor the environment variable was specified, + // fall back to the configured default + if (per_process::cli_options->icu_data_dir.empty()) { + // Check whether the NODE_ICU_DEFAULT_DATA_DIR contains the right data + // file and can be read. + static const char full_path[] = + NODE_ICU_DEFAULT_DATA_DIR "/" U_ICUDATA_NAME ".dat"; + + FILE* f = fopen(full_path, "rb"); + + if (f != nullptr) { + fclose(f); + per_process::cli_options->icu_data_dir = NODE_ICU_DEFAULT_DATA_DIR; + } + } +#endif // NODE_ICU_DEFAULT_DATA_DIR + // Initialize ICU. // If icu_data_dir is empty here, it will load the 'minimal' data. if (!i18n::InitializeICUDirectory(per_process::cli_options->icu_data_dir)) { |