From 95e2105283246300c3c42eb95c34658b217f0918 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 05:49:40 -0500 Subject: [PATCH] fix: missing `fetch`-dependent interfaces in Node.js (#42454) fix: missing fetch-dependent interfaces in Node.js Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- lib/node/init.ts | 7 +++++-- shell/renderer/electron_renderer_client.cc | 24 ++++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/node/init.ts b/lib/node/init.ts index e55293386107..12c908a60739 100644 --- a/lib/node/init.ts +++ b/lib/node/init.ts @@ -3,8 +3,11 @@ import { wrapFsWithAsar } from './asar-fs-wrapper'; wrapFsWithAsar(require('fs')); // See ElectronRendererClient::DidCreateScriptContext. -if ((globalThis as any).blinkFetch) { - globalThis.fetch = (globalThis as any).blinkFetch; +if ((globalThis as any).blinkfetch) { + const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers']; + for (const key of keys) { + (globalThis as any)[key] = (globalThis as any)[`blink${key}`]; + } } // Hook child_process.fork. diff --git a/shell/renderer/electron_renderer_client.cc b/shell/renderer/electron_renderer_client.cc index 228ce6839ceb..a8e7dfea4f95 100644 --- a/shell/renderer/electron_renderer_client.cc +++ b/shell/renderer/electron_renderer_client.cc @@ -110,19 +110,25 @@ void ElectronRendererClient::DidCreateScriptContext( base::BindRepeating(&ElectronRendererClient::UndeferLoad, base::Unretained(this), render_frame)); - v8::Local global = renderer_context->Global(); - v8::MaybeLocal fetch = - global->Get(renderer_context, gin::StringToV8(env->isolate(), "fetch")); - // We need to use the Blink implementation of fetch in the renderer process // Node.js deletes the global fetch function when their fetch implementation // is disabled, so we need to save and re-add it after the Node.js environment // is loaded. See corresponding change in node/init.ts. - if (!fetch.IsEmpty()) { - global - ->Set(renderer_context, gin::StringToV8(env->isolate(), "blinkFetch"), - fetch.ToLocalChecked()) - .Check(); + v8::Isolate* isolate = env->isolate(); + v8::Local global = renderer_context->Global(); + + std::vector keys = {"fetch", "Response", "FormData", "Request", + "Headers"}; + for (const auto& key : keys) { + v8::MaybeLocal value = + global->Get(renderer_context, gin::StringToV8(isolate, key.c_str())); + if (!value.IsEmpty()) { + std::string blink_key = "blink" + key; + global + ->Set(renderer_context, gin::StringToV8(isolate, blink_key.c_str()), + value.ToLocalChecked()) + .Check(); + } } // If we have disabled the site instance overrides we should prevent loading