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 <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2024-06-12 05:49:40 -05:00 committed by GitHub
parent b21d7884e8
commit 95e2105283
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 11 deletions

View file

@ -3,8 +3,11 @@ import { wrapFsWithAsar } from './asar-fs-wrapper';
wrapFsWithAsar(require('fs')); wrapFsWithAsar(require('fs'));
// See ElectronRendererClient::DidCreateScriptContext. // See ElectronRendererClient::DidCreateScriptContext.
if ((globalThis as any).blinkFetch) { if ((globalThis as any).blinkfetch) {
globalThis.fetch = (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. // Hook child_process.fork.

View file

@ -110,20 +110,26 @@ void ElectronRendererClient::DidCreateScriptContext(
base::BindRepeating(&ElectronRendererClient::UndeferLoad, base::BindRepeating(&ElectronRendererClient::UndeferLoad,
base::Unretained(this), render_frame)); base::Unretained(this), render_frame));
v8::Local<v8::Object> global = renderer_context->Global();
v8::MaybeLocal<v8::Value> fetch =
global->Get(renderer_context, gin::StringToV8(env->isolate(), "fetch"));
// We need to use the Blink implementation of fetch in the renderer process // We need to use the Blink implementation of fetch in the renderer process
// Node.js deletes the global fetch function when their fetch implementation // 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 disabled, so we need to save and re-add it after the Node.js environment
// is loaded. See corresponding change in node/init.ts. // is loaded. See corresponding change in node/init.ts.
if (!fetch.IsEmpty()) { v8::Isolate* isolate = env->isolate();
v8::Local<v8::Object> global = renderer_context->Global();
std::vector<std::string> keys = {"fetch", "Response", "FormData", "Request",
"Headers"};
for (const auto& key : keys) {
v8::MaybeLocal<v8::Value> value =
global->Get(renderer_context, gin::StringToV8(isolate, key.c_str()));
if (!value.IsEmpty()) {
std::string blink_key = "blink" + key;
global global
->Set(renderer_context, gin::StringToV8(env->isolate(), "blinkFetch"), ->Set(renderer_context, gin::StringToV8(isolate, blink_key.c_str()),
fetch.ToLocalChecked()) value.ToLocalChecked())
.Check(); .Check();
} }
}
// If we have disabled the site instance overrides we should prevent loading // If we have disabled the site instance overrides we should prevent loading
// any non-context aware native module. // any non-context aware native module.