From 85272b00a4e395344aebcf9ec1b5e0d0c61f4e05 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:44:00 -0500 Subject: [PATCH] fix: EventSource undefined in Renderer/Worker (#44497) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- lib/node/init.ts | 2 +- lib/worker/init.ts | 2 +- shell/renderer/electron_renderer_client.cc | 4 +-- shell/renderer/web_worker_observer.cc | 4 +-- spec/chromium-spec.ts | 37 ++++++++++++++++++++++ spec/node-spec.ts | 9 ++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/node/init.ts b/lib/node/init.ts index 8857eabb6bb0..341666db09e1 100644 --- a/lib/node/init.ts +++ b/lib/node/init.ts @@ -6,7 +6,7 @@ wrapFsWithAsar(require('fs')); // See ElectronRendererClient::DidCreateScriptContext. if ((globalThis as any).blinkfetch) { - const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers']; + const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers', 'EventSource']; for (const key of keys) { (globalThis as any)[key] = (globalThis as any)[`blink${key}`]; } diff --git a/lib/worker/init.ts b/lib/worker/init.ts index d23a4f942bb4..ccbd3596fca1 100644 --- a/lib/worker/init.ts +++ b/lib/worker/init.ts @@ -19,7 +19,7 @@ global.require = makeRequireFunction(global.module); // See WebWorkerObserver::WorkerScriptReadyForEvaluation. if ((globalThis as any).blinkfetch) { - const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers']; + const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers', 'EventSource']; for (const key of keys) { (globalThis as any)[key] = (globalThis as any)[`blink${key}`]; } diff --git a/shell/renderer/electron_renderer_client.cc b/shell/renderer/electron_renderer_client.cc index 959a3f69b9ff..408170414751 100644 --- a/shell/renderer/electron_renderer_client.cc +++ b/shell/renderer/electron_renderer_client.cc @@ -117,8 +117,8 @@ void ElectronRendererClient::DidCreateScriptContext( v8::Isolate* isolate = env->isolate(); v8::Local global = renderer_context->Global(); - std::vector keys = {"fetch", "Response", "FormData", "Request", - "Headers"}; + std::vector keys = {"fetch", "Response", "FormData", + "Request", "Headers", "EventSource"}; for (const auto& key : keys) { v8::MaybeLocal value = global->Get(renderer_context, gin::StringToV8(isolate, key)); diff --git a/shell/renderer/web_worker_observer.cc b/shell/renderer/web_worker_observer.cc index eafa8ba00a1c..4bd70413dce1 100644 --- a/shell/renderer/web_worker_observer.cc +++ b/shell/renderer/web_worker_observer.cc @@ -72,8 +72,8 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation( // is loaded. See corresponding change in node/init.ts. v8::Local global = worker_context->Global(); - std::vector keys = {"fetch", "Response", "FormData", "Request", - "Headers"}; + std::vector keys = {"fetch", "Response", "FormData", + "Request", "Headers", "EventSource"}; for (const auto& key : keys) { v8::MaybeLocal value = global->Get(worker_context, gin::StringToV8(isolate, key.c_str())); diff --git a/spec/chromium-spec.ts b/spec/chromium-spec.ts index f6302a4f5774..b428b270b547 100644 --- a/spec/chromium-spec.ts +++ b/spec/chromium-spec.ts @@ -1030,6 +1030,43 @@ describe('chromium features', () => { expect(code).to.equal(0); }); + itremote('Worker with nodeIntegrationInWorker has access to EventSource', () => { + const es = new EventSource('https://example.com'); + expect(es).to.have.property('url').that.is.a('string'); + expect(es).to.have.property('readyState').that.is.a('number'); + expect(es).to.have.property('withCredentials').that.is.a('boolean'); + }); + + itremote('Worker with nodeIntegrationInWorker has access to fetch-dependent interfaces', async (fixtures: string) => { + const file = require('node:path').join(fixtures, 'hello.txt'); + expect(() => { + fetch('file://' + file); + }).to.not.throw(); + + expect(() => { + const formData = new FormData(); + formData.append('username', 'Groucho'); + }).not.to.throw(); + + expect(() => { + const request = new Request('https://example.com', { + method: 'POST', + body: JSON.stringify({ foo: 'bar' }) + }); + expect(request.method).to.equal('POST'); + }).not.to.throw(); + + expect(() => { + const response = new Response('Hello, world!'); + expect(response.status).to.equal(200); + }).not.to.throw(); + + expect(() => { + const headers = new Headers(); + headers.append('Content-Type', 'text/xml'); + }).not.to.throw(); + }, [path.join(__dirname, 'fixtures')]); + it('Worker can work', async () => { const w = new BrowserWindow({ show: false }); await w.loadURL(`file://${fixturesPath}/pages/blank.html`); diff --git a/spec/node-spec.ts b/spec/node-spec.ts index d3f68030a2ab..0d22f3fbfd71 100644 --- a/spec/node-spec.ts +++ b/spec/node-spec.ts @@ -162,6 +162,15 @@ describe('node feature', () => { }); }); + describe('EventSource', () => { + itremote('works correctly when nodeIntegration is enabled in the renderer', () => { + const es = new EventSource('https://example.com'); + expect(es).to.have.property('url').that.is.a('string'); + expect(es).to.have.property('readyState').that.is.a('number'); + expect(es).to.have.property('withCredentials').that.is.a('boolean'); + }); + }); + describe('fetch', () => { itremote('works correctly when nodeIntegration is enabled in the renderer', async (fixtures: string) => { const file = require('node:path').join(fixtures, 'hello.txt');