Skip to content

Commit 28289ea

Browse files
lundibundicodebytere
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 b657df4 commit 28289ea

File tree

4 files changed

+77
-68
lines changed

4 files changed

+77
-68
lines changed

src/node.cc

Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -741,80 +741,19 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
741741
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
742742
#endif
743743

744-
std::shared_ptr<EnvironmentOptions> default_env_options =
745-
per_process::cli_options->per_isolate->per_env;
746-
{
747-
std::string text;
748-
default_env_options->pending_deprecation =
749-
credentials::SafeGetenv("NODE_PENDING_DEPRECATION", &text) &&
750-
text[0] == '1';
751-
}
752-
753-
// Allow for environment set preserving symlinks.
754-
{
755-
std::string text;
756-
default_env_options->preserve_symlinks =
757-
credentials::SafeGetenv("NODE_PRESERVE_SYMLINKS", &text) &&
758-
text[0] == '1';
759-
}
760-
761-
{
762-
std::string text;
763-
default_env_options->preserve_symlinks_main =
764-
credentials::SafeGetenv("NODE_PRESERVE_SYMLINKS_MAIN", &text) &&
765-
text[0] == '1';
766-
}
767-
768-
if (default_env_options->redirect_warnings.empty()) {
769-
credentials::SafeGetenv("NODE_REDIRECT_WARNINGS",
770-
&default_env_options->redirect_warnings);
771-
}
744+
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
772745

773746
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
774747
std::string node_options;
775748

776749
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
777-
std::vector<std::string> env_argv;
778-
// [0] is expected to be the program name, fill it in from the real argv.
779-
env_argv.push_back(argv->at(0));
780-
781-
bool is_in_string = false;
782-
bool will_start_new_arg = true;
783-
for (std::string::size_type index = 0;
784-
index < node_options.size();
785-
++index) {
786-
char c = node_options.at(index);
787-
788-
// Backslashes escape the following character
789-
if (c == '\\' && is_in_string) {
790-
if (index + 1 == node_options.size()) {
791-
errors->push_back("invalid value for NODE_OPTIONS "
792-
"(invalid escape)\n");
793-
return 9;
794-
} else {
795-
c = node_options.at(++index);
796-
}
797-
} else if (c == ' ' && !is_in_string) {
798-
will_start_new_arg = true;
799-
continue;
800-
} else if (c == '"') {
801-
is_in_string = !is_in_string;
802-
continue;
803-
}
750+
std::vector<std::string> env_argv =
751+
ParseNodeOptionsEnvVar(node_options, errors);
804752

805-
if (will_start_new_arg) {
806-
env_argv.emplace_back(std::string(1, c));
807-
will_start_new_arg = false;
808-
} else {
809-
env_argv.back() += c;
810-
}
811-
}
753+
if (!errors->empty()) return 9;
812754

813-
if (is_in_string) {
814-
errors->push_back("invalid value for NODE_OPTIONS "
815-
"(unterminated string)\n");
816-
return 9;
817-
}
755+
// [0] is expected to be the program name, fill it in from the real argv.
756+
env_argv.insert(env_argv.begin(), argv->at(0));
818757

819758
const int exit_code = ProcessGlobalArgs(&env_argv,
820759
nullptr,

src/node_options-inl.h

Lines changed: 1 addition & 1 deletion
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

Lines changed: 63 additions & 0 deletions
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>
@@ -1032,6 +1033,68 @@ void Initialize(Local<Object> target,
10321033
}
10331034

10341035
} // namespace options_parser
1036+
1037+
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) {
1038+
HandleEnvOptions(env_options, [](const char* name) {
1039+
std::string text;
1040+
return credentials::SafeGetenv(name, &text) ? text : "";
1041+
});
1042+
}
1043+
1044+
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
1045+
std::function<std::string(const char*)> opt_getter) {
1046+
env_options->pending_deprecation =
1047+
opt_getter("NODE_PENDING_DEPRECATION") == "1";
1048+
1049+
env_options->preserve_symlinks = opt_getter("NODE_PRESERVE_SYMLINKS") == "1";
1050+
1051+
env_options->preserve_symlinks_main =
1052+
opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
1053+
1054+
if (env_options->redirect_warnings.empty())
1055+
env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
1056+
}
1057+
1058+
std::vector<std::string> ParseNodeOptionsEnvVar(
1059+
const std::string& node_options, std::vector<std::string>* errors) {
1060+
std::vector<std::string> env_argv;
1061+
1062+
bool is_in_string = false;
1063+
bool will_start_new_arg = true;
1064+
for (std::string::size_type index = 0; index < node_options.size(); ++index) {
1065+
char c = node_options.at(index);
1066+
1067+
// Backslashes escape the following character
1068+
if (c == '\\' && is_in_string) {
1069+
if (index + 1 == node_options.size()) {
1070+
errors->push_back("invalid value for NODE_OPTIONS "
1071+
"(invalid escape)\n");
1072+
return env_argv;
1073+
} else {
1074+
c = node_options.at(++index);
1075+
}
1076+
} else if (c == ' ' && !is_in_string) {
1077+
will_start_new_arg = true;
1078+
continue;
1079+
} else if (c == '"') {
1080+
is_in_string = !is_in_string;
1081+
continue;
1082+
}
1083+
1084+
if (will_start_new_arg) {
1085+
env_argv.emplace_back(std::string(1, c));
1086+
will_start_new_arg = false;
1087+
} else {
1088+
env_argv.back() += c;
1089+
}
1090+
}
1091+
1092+
if (is_in_string) {
1093+
errors->push_back("invalid value for NODE_OPTIONS "
1094+
"(unterminated string)\n");
1095+
}
1096+
return env_argv;
1097+
}
10351098
} // namespace node
10361099

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

src/node_options.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ extern Mutex cli_options_mutex;
456456
extern std::shared_ptr<PerProcessOptions> cli_options;
457457

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

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

0 commit comments

Comments
 (0)