From 6b4e30cabedacff62d8546de13dfe36d2de18395 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 19 Oct 2022 12:07:52 +0200 Subject: [PATCH] lib: add fetch https://github.com/nodejs/node/pull/41749 --- shell/common/node_bindings.cc | 86 +++++++++++++++++++---------------- shell/common/node_bindings.h | 2 + 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 5cb17e20d0934..2019e9207f6db 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -210,46 +210,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 allowed = - GetAllowedDebugOptions(); - - const auto argv = base::CommandLine::ForCurrentProcess()->argv(); - std::vector 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 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) { @@ -370,6 +330,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 allowed = + GetAllowedDebugOptions(); + + const auto argv = base::CommandLine::ForCurrentProcess()->argv(); + std::vector 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 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. diff --git a/shell/common/node_bindings.h b/shell/common/node_bindings.h index c56e1aecd51ff..efd33fc62cda1 100644 --- a/shell/common/node_bindings.h +++ b/shell/common/node_bindings.h @@ -87,6 +87,8 @@ class NodeBindings { // Setup V8, libuv. void Initialize(); + void SetNodeCliFlags(); + // Create the environment and load node.js. node::Environment* CreateEnvironment(v8::Handle context, node::MultiIsolatePlatform* platform,