fix: fetch-dependent interfaces in Web Workers (#42579)

This commit is contained in:
Shelley Vohr 2024-06-20 17:01:50 +02:00 committed by GitHub
parent edb939ae80
commit 37608933ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 101 additions and 13 deletions

View file

@ -66,9 +66,25 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
std::shared_ptr<node::Environment> env =
node_bindings_->CreateEnvironment(worker_context, nullptr);
// We need to use the Blink implementation of fetch in web workers
// 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.
v8::Local<v8::Object> global = worker_context->Global();
v8::Local<v8::String> fetch_string = gin::StringToV8(env->isolate(), "fetch");
v8::MaybeLocal<v8::Value> fetch = global->Get(worker_context, fetch_string);
std::vector<std::string> keys = {"fetch", "Response", "FormData", "Request",
"Headers"};
for (const auto& key : keys) {
v8::MaybeLocal<v8::Value> value =
global->Get(worker_context, gin::StringToV8(isolate, key.c_str()));
if (!value.IsEmpty()) {
std::string blink_key = "blink" + key;
global
->Set(worker_context, gin::StringToV8(isolate, blink_key.c_str()),
value.ToLocalChecked())
.Check();
}
}
// Add Electron extended APIs.
electron_bindings_->BindTo(env->isolate(), env->process_object());
@ -76,16 +92,6 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
// Load everything.
node_bindings_->LoadEnvironment(env.get());
// We need to use the Blink implementation of fetch in WebWorker 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.
if (!fetch.IsEmpty()) {
worker_context->Global()
->Set(worker_context, fetch_string, fetch.ToLocalChecked())
.Check();
}
// Make uv loop being wrapped by window context.
node_bindings_->set_uv_env(env.get());