fix: fetch-dependent interfaces in Web Workers (#42596)
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:
parent
91de7d0f13
commit
f6966f6e35
6 changed files with 101 additions and 13 deletions
|
@ -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());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue