Skip to content

Commit e7db75b

Browse files
committed
lib: define FormData and fetch etc. in the built-in snapshot
nodejs/node#51598
1 parent 2f42278 commit e7db75b

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

shell/renderer/electron_renderer_client.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ void ElectronRendererClient::DidCreateScriptContext(
110110
base::BindRepeating(&ElectronRendererClient::UndeferLoad,
111111
base::Unretained(this), render_frame));
112112

113+
v8::Local<v8::Object> global = renderer_context->Global();
114+
v8::Local<v8::String> fetch_string = gin::StringToV8(env->isolate(), "fetch");
115+
v8::MaybeLocal<v8::Value> fetch = global->Get(renderer_context, fetch_string);
116+
113117
// If we have disabled the site instance overrides we should prevent loading
114118
// any non-context aware native module.
115119
env->options()->force_context_aware = true;
@@ -134,6 +138,16 @@ void ElectronRendererClient::DidCreateScriptContext(
134138
// Give the node loop a run to make sure everything is ready.
135139
node_bindings_->StartPolling();
136140
}
141+
142+
// We need to use the Blink implementation of fetch in the renderer process
143+
// Node.js deletes the global fetch function when their fetch implementation
144+
// is disabled, so we need to save and re-add it after the Node.js environment
145+
// is loaded.
146+
if (!fetch.IsEmpty()) {
147+
renderer_context->Global()
148+
->Set(renderer_context, fetch_string, fetch.ToLocalChecked())
149+
.Check();
150+
}
137151
}
138152

139153
void ElectronRendererClient::WillReleaseScriptContext(

shell/renderer/web_worker_observer.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,26 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
6666
std::shared_ptr<node::Environment> env =
6767
node_bindings_->CreateEnvironment(worker_context, nullptr);
6868

69+
v8::Local<v8::Object> global = worker_context->Global();
70+
v8::Local<v8::String> fetch_string = gin::StringToV8(env->isolate(), "fetch");
71+
v8::MaybeLocal<v8::Value> fetch = global->Get(worker_context, fetch_string);
72+
6973
// Add Electron extended APIs.
7074
electron_bindings_->BindTo(env->isolate(), env->process_object());
7175

7276
// Load everything.
7377
node_bindings_->LoadEnvironment(env.get());
7478

79+
// We need to use the Blink implementation of fetch in WebWorker process
80+
// Node.js deletes the global fetch function when their fetch implementation
81+
// is disabled, so we need to save and re-add it after the Node.js environment
82+
// is loaded.
83+
if (!fetch.IsEmpty()) {
84+
worker_context->Global()
85+
->Set(worker_context, fetch_string, fetch.ToLocalChecked())
86+
.Check();
87+
}
88+
7589
// Make uv loop being wrapped by window context.
7690
node_bindings_->set_uv_env(env.get());
7791

0 commit comments

Comments
 (0)