Skip to content

Commit 84b8857

Browse files
lundibundiaddaleax
authored andcommitted
src: allow to reuse env options handling
PR-URL: #31711 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 3e9302b commit 84b8857

File tree

4 files changed

+77
-68
lines changed

4 files changed

+77
-68
lines changed

src/node.cc

+6-67
Original file line numberDiff line numberDiff line change
@@ -790,80 +790,19 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
790790
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
791791
#endif
792792

793-
std::shared_ptr<EnvironmentOptions> default_env_options =
794-
per_process::cli_options->per_isolate->per_env;
795-
{
796-
std::string text;
797-
default_env_options->pending_deprecation =
798-
credentials::SafeGetenv("NODE_PENDING_DEPRECATION", &text) &&
799-
text[0] == '1';
800-
}
801-
802-
// Allow for environment set preserving symlinks.
803-
{
804-
std::string text;
805-
default_env_options->preserve_symlinks =
806-
credentials::SafeGetenv("NODE_PRESERVE_SYMLINKS", &text) &&
807-
text[0] == '1';
808-
}
809-
810-
{
811-
std::string text;
812-
default_env_options->preserve_symlinks_main =
813-
credentials::SafeGetenv("NODE_PRESERVE_SYMLINKS_MAIN", &text) &&
814-
text[0] == '1';
815-
}
816-
817-
if (default_env_options->redirect_warnings.empty()) {
818-
credentials::SafeGetenv("NODE_REDIRECT_WARNINGS",
819-
&default_env_options->redirect_warnings);
820-
}
793+
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
821794

822795
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
823796
std::string node_options;
824797

825798
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
826-
std::vector<std::string> env_argv;
827-
// [0] is expected to be the program name, fill it in from the real argv.
828-
env_argv.push_back(argv->at(0));
829-
830-
bool is_in_string = false;
831-
bool will_start_new_arg = true;
832-
for (std::string::size_type index = 0;
833-
index < node_options.size();
834-
++index) {
835-
char c = node_options.at(index);
836-
837-
// Backslashes escape the following character
838-
if (c == '\\' && is_in_string) {
839-
if (index + 1 == node_options.size()) {
840-
errors->push_back("invalid value for NODE_OPTIONS "
841-
"(invalid escape)\n");
842-
return 9;
843-
} else {
844-
c = node_options.at(++index);
845-
}
846-
} else if (c == ' ' && !is_in_string) {
847-
will_start_new_arg = true;
848-
continue;
849-
} else if (c == '"') {
850-
is_in_string = !is_in_string;
851-
continue;
852-
}
799+
std::vector<std::string> env_argv =
800+
ParseNodeOptionsEnvVar(node_options, errors);
853801

854-
if (will_start_new_arg) {
855-
env_argv.emplace_back(std::string(1, c));
856-
will_start_new_arg = false;
857-
} else {
858-
env_argv.back() += c;
859-
}
860-
}
802+
if (!errors->empty()) return 9;
861803

862-
if (is_in_string) {
863-
errors->push_back("invalid value for NODE_OPTIONS "
864-
"(unterminated string)\n");
865-
return 9;
866-
}
804+
// [0] is expected to be the program name, fill it in from the real argv.
805+
env_argv.insert(env_argv.begin(), argv->at(0));
867806

868807
const int exit_code = ProcessGlobalArgs(&env_argv,
869808
nullptr,

src/node_options-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include <cstdlib>
67
#include "node_options.h"
78
#include "util.h"
8-
#include <cstdlib>
99

1010
namespace node {
1111

src/node_options.cc

+63
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "env-inl.h"
55
#include "node_binding.h"
6+
#include "node_internals.h"
67

78
#include <errno.h>
89
#include <sstream>
@@ -1009,6 +1010,68 @@ void Initialize(Local<Object> target,
10091010
}
10101011

10111012
} // namespace options_parser
1013+
1014+
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) {
1015+
HandleEnvOptions(env_options, [](const char* name) {
1016+
std::string text;
1017+
return credentials::SafeGetenv(name, &text) ? text : "";
1018+
});
1019+
}
1020+
1021+
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
1022+
std::function<std::string(const char*)> opt_getter) {
1023+
env_options->pending_deprecation =
1024+
opt_getter("NODE_PENDING_DEPRECATION") == "1";
1025+
1026+
env_options->preserve_symlinks = opt_getter("NODE_PRESERVE_SYMLINKS") == "1";
1027+
1028+
env_options->preserve_symlinks_main =
1029+
opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
1030+
1031+
if (env_options->redirect_warnings.empty())
1032+
env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
1033+
}
1034+
1035+
std::vector<std::string> ParseNodeOptionsEnvVar(
1036+
const std::string& node_options, std::vector<std::string>* errors) {
1037+
std::vector<std::string> env_argv;
1038+
1039+
bool is_in_string = false;
1040+
bool will_start_new_arg = true;
1041+
for (std::string::size_type index = 0; index < node_options.size(); ++index) {
1042+
char c = node_options.at(index);
1043+
1044+
// Backslashes escape the following character
1045+
if (c == '\\' && is_in_string) {
1046+
if (index + 1 == node_options.size()) {
1047+
errors->push_back("invalid value for NODE_OPTIONS "
1048+
"(invalid escape)\n");
1049+
return env_argv;
1050+
} else {
1051+
c = node_options.at(++index);
1052+
}
1053+
} else if (c == ' ' && !is_in_string) {
1054+
will_start_new_arg = true;
1055+
continue;
1056+
} else if (c == '"') {
1057+
is_in_string = !is_in_string;
1058+
continue;
1059+
}
1060+
1061+
if (will_start_new_arg) {
1062+
env_argv.emplace_back(std::string(1, c));
1063+
will_start_new_arg = false;
1064+
} else {
1065+
env_argv.back() += c;
1066+
}
1067+
}
1068+
1069+
if (is_in_string) {
1070+
errors->push_back("invalid value for NODE_OPTIONS "
1071+
"(unterminated string)\n");
1072+
}
1073+
return env_argv;
1074+
}
10121075
} // namespace node
10131076

10141077
NODE_MODULE_CONTEXT_AWARE_INTERNAL(options, node::options_parser::Initialize)

src/node_options.h

+7
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ extern Mutex cli_options_mutex;
457457
extern std::shared_ptr<PerProcessOptions> cli_options;
458458

459459
} // namespace per_process
460+
461+
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options);
462+
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
463+
std::function<std::string(const char*)> opt_getter);
464+
465+
std::vector<std::string> ParseNodeOptionsEnvVar(
466+
const std::string& node_options, std::vector<std::string>* errors);
460467
} // namespace node
461468

462469
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)