Skip to content

Commit 6a3a146

Browse files
committed
feat: implement EventSource
1 parent 0e79a9f commit 6a3a146

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ module.exports = {
341341
Crypto: 'readable',
342342
CryptoKey: 'readable',
343343
DecompressionStream: 'readable',
344+
EventSource: 'readable',
344345
fetch: 'readable',
345346
FormData: 'readable',
346347
navigator: 'readable',

lib/internal/process/pre_execution.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ function setupWarningHandler() {
298298
}
299299
}
300300

301+
// https://html.spec.whatwg.org/multipage/server-sent-events.html
301302
// https://fetch.spec.whatwg.org/
302303
// https://websockets.spec.whatwg.org/
303304
function setupUndici() {
@@ -328,6 +329,12 @@ function setupUndici() {
328329
};
329330
}
330331

332+
if (getOptionValue('--experimental-eventsource')) {
333+
ObjectDefineProperties(globalThis, {
334+
EventSource: lazyInterface('EventSource'),
335+
});
336+
}
337+
331338
if (!getOptionValue('--no-experimental-fetch')) {
332339
// Fetch is meant to return a Promise, but not be async.
333340
function fetch(input, init = undefined) {

src/node_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
376376
&EnvironmentOptions::enable_source_maps,
377377
kAllowedInEnvvar);
378378
AddOption("--experimental-abortcontroller", "", NoOp{}, kAllowedInEnvvar);
379+
AddOption("--experimental-eventsource",
380+
"experimental EventSource API",
381+
&EnvironmentOptions::experimental_eventsource,
382+
kAllowedInEnvvar,
383+
true);
379384
AddOption("--experimental-fetch",
380385
"experimental Fetch API",
381386
&EnvironmentOptions::experimental_fetch,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class EnvironmentOptions : public Options {
107107
bool detect_module = false;
108108
std::string dns_result_order;
109109
bool enable_source_maps = false;
110+
bool experimental_eventsource = false;
110111
bool experimental_fetch = true;
111112
bool experimental_websocket = false;
112113
bool experimental_global_customevent = true;

test/common/globals.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const webIdlExposedWindow = new Set([
124124
'Request',
125125
'Response',
126126
'WebSocket',
127+
'EventSource',
127128
]);
128129

129130
const nodeGlobals = new Set([

test/common/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ if (global.WebSocket) {
373373
knownGlobals.push(WebSocket);
374374
}
375375

376+
if (global.EventSource) {
377+
knownGlobals.push(EventSource);
378+
}
379+
376380
function allowGlobals(...allowlist) {
377381
knownGlobals = knownGlobals.concat(allowlist);
378382
}

test/parallel/test-eventsource.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Flags: --experimental-eventsource
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
7+
assert.strictEqual(typeof EventSource, 'function');

0 commit comments

Comments
 (0)