Skip to content

Commit

Permalink
lib: add fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Oct 19, 2022
1 parent 9f5fbe6 commit f6cca89
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
86 changes: 46 additions & 40 deletions shell/common/node_bindings.cc
Expand Up @@ -208,46 +208,6 @@ GetAllowedDebugOptions() {
return {};
}

// Initialize Node.js cli options to pass to Node.js
// See https://nodejs.org/api/cli.html#cli_options
void SetNodeCliFlags() {
const std::unordered_set<base::StringPiece, base::StringPieceHash> allowed =
GetAllowedDebugOptions();

const auto argv = base::CommandLine::ForCurrentProcess()->argv();
std::vector<std::string> args;

// TODO(codebytere): We need to set the first entry in args to the
// process name owing to src/node_options-inl.h#L286-L290 but this is
// redundant and so should be refactored upstream.
args.reserve(argv.size() + 1);
args.emplace_back("electron");

for (const auto& arg : argv) {
#if BUILDFLAG(IS_WIN)
const auto& option = base::WideToUTF8(arg);
#else
const auto& option = arg;
#endif
const auto stripped = base::StringPiece(option).substr(0, option.find('='));

// Only allow in no-op (--) option or DebugOptions
if (allowed.count(stripped) != 0 || stripped == "--")
args.push_back(option);
}

std::vector<std::string> errors;
const int exit_code = ProcessGlobalArgs(&args, nullptr, &errors,
node::kDisallowedInEnvironment);

const std::string err_str = "Error parsing Node.js cli flags ";
if (exit_code != 0) {
LOG(ERROR) << err_str;
} else if (!errors.empty()) {
LOG(ERROR) << err_str << base::JoinString(errors, " ");
}
}

// Initialize NODE_OPTIONS to pass to Node.js
// See https://nodejs.org/api/cli.html#cli_node_options_options
void SetNodeOptions(base::Environment* env) {
Expand Down Expand Up @@ -368,6 +328,52 @@ bool NodeBindings::IsInitialized() {
return g_is_initialized;
}

// Initialize Node.js cli options to pass to Node.js
// See https://nodejs.org/api/cli.html#cli_options
void NodeBindings::SetNodeCliFlags() {
const std::unordered_set<base::StringPiece, base::StringPieceHash> allowed =
GetAllowedDebugOptions();

const auto argv = base::CommandLine::ForCurrentProcess()->argv();
std::vector<std::string> args;

// TODO(codebytere): We need to set the first entry in args to the
// process name owing to src/node_options-inl.h#L286-L290 but this is
// redundant and so should be refactored upstream.
args.reserve(argv.size() + 1);
args.emplace_back("electron");

for (const auto& arg : argv) {
#if BUILDFLAG(IS_WIN)
const auto& option = base::WideToUTF8(arg);
#else
const auto& option = arg;
#endif
const auto stripped = base::StringPiece(option).substr(0, option.find('='));

// Only allow in no-op (--) option or DebugOptions
if (allowed.count(stripped) != 0 || stripped == "--")
args.push_back(option);
}

// We need to disable Node.js' fetch implementation to prevent
// conflict with Blink's.
if (browser_env_ != BrowserEnvironment::kBrowser) {
args.push_back("--no-experimental-fetch");
}

std::vector<std::string> errors;
const int exit_code = ProcessGlobalArgs(&args, nullptr, &errors,
node::kDisallowedInEnvironment);

const std::string err_str = "Error parsing Node.js cli flags ";
if (exit_code != 0) {
LOG(ERROR) << err_str;
} else if (!errors.empty()) {
LOG(ERROR) << err_str << base::JoinString(errors, " ");
}
}

void NodeBindings::Initialize() {
TRACE_EVENT0("electron", "NodeBindings::Initialize");
// Open node's error reporting system for browser process.
Expand Down
2 changes: 2 additions & 0 deletions shell/common/node_bindings.h
Expand Up @@ -85,6 +85,8 @@ class NodeBindings {
// Setup V8, libuv.
void Initialize();

void SetNodeCliFlags();

// Create the environment and load node.js.
node::Environment* CreateEnvironment(v8::Handle<v8::Context> context,
node::MultiIsolatePlatform* platform);
Expand Down

0 comments on commit f6cca89

Please sign in to comment.