Skip to content

Commit bccbba6

Browse files
committed
lib: implement EventSource
1 parent f8e325e commit bccbba6

11 files changed

+45
-1
lines changed

.eslintrc.js

+1
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',

doc/api/cli.md

+10
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,14 @@ CommonJS. This includes the following:
808808
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
809809
`exports`, `__dirname`, `__filename`).
810810

811+
### `--experimental-eventsource`
812+
813+
<!-- YAML
814+
added: REPLACEME
815+
-->
816+
817+
Enable exposition of [EventSource Web API][] on the global scope.
818+
811819
### `--experimental-import-meta-resolve`
812820

813821
<!-- YAML
@@ -2603,6 +2611,7 @@ one is included in the list below.
26032611
* `--experimental-abortcontroller`
26042612
* `--experimental-default-type`
26052613
* `--experimental-detect-module`
2614+
* `--experimental-eventsource`
26062615
* `--experimental-import-meta-resolve`
26072616
* `--experimental-json-modules`
26082617
* `--experimental-loader`
@@ -3114,6 +3123,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
31143123
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
31153124
[ECMAScript module]: esm.md#modules-ecmascript-modules
31163125
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
3126+
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
31173127
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
31183128
[File System Permissions]: permissions.md#file-system-permissions
31193129
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require

doc/node.1

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ Use this flag to enable ShadowRealm support.
183183
.It Fl -experimental-test-coverage
184184
Enable code coverage in the test runner.
185185
.
186+
.It Fl -experimental-eventsource
187+
Enable experimental support for the EventSource Web API.
188+
.
186189
.It Fl -no-experimental-fetch
187190
Disable experimental support for the Fetch API.
188191
.

lib/internal/bootstrap/web/exposed-window-or-worker.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
8181
'FormData', 'Headers', 'Request', 'Response',
8282
]);
8383

84+
// https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events.org/
8485
// https://websockets.spec.whatwg.org/
85-
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['WebSocket']);
86+
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['EventSource', 'WebSocket']);
8687

8788
// The WebAssembly Web API which relies on Response.
8889
// https:// webassembly.github.io/spec/web-api/#streaming-modules

lib/internal/process/pre_execution.js

+5
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,14 @@ function setupWarningHandler() {
312312
}
313313
}
314314

315+
// https://html.spec.whatwg.org/multipage/server-sent-events.html
315316
// https://fetch.spec.whatwg.org/
316317
// https://websockets.spec.whatwg.org/
317318
function setupUndici() {
319+
if (!getOptionValue('--experimental-eventsource')) {
320+
delete globalThis.EventSource;
321+
}
322+
318323
if (getOptionValue('--no-experimental-fetch')) {
319324
delete globalThis.fetch;
320325
delete globalThis.FormData;

src/node_options.cc

+5
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
390390
&EnvironmentOptions::enable_source_maps,
391391
kAllowedInEnvvar);
392392
AddOption("--experimental-abortcontroller", "", NoOp{}, kAllowedInEnvvar);
393+
AddOption("--experimental-eventsource",
394+
"experimental EventSource API",
395+
&EnvironmentOptions::experimental_eventsource,
396+
kAllowedInEnvvar,
397+
false);
393398
AddOption("--experimental-fetch",
394399
"experimental Fetch API",
395400
&EnvironmentOptions::experimental_fetch,

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class EnvironmentOptions : public Options {
109109
bool require_module = false;
110110
std::string dns_result_order;
111111
bool enable_source_maps = false;
112+
bool experimental_eventsource = false;
112113
bool experimental_fetch = true;
113114
bool experimental_websocket = true;
114115
bool experimental_global_customevent = true;

test/common/globals.js

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const webIdlExposedWindow = new Set([
125125
'Request',
126126
'Response',
127127
'WebSocket',
128+
'EventSource',
128129
]);
129130

130131
const nodeGlobals = new Set([

test/common/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ if (global.structuredClone) {
336336
knownGlobals.push(global.structuredClone);
337337
}
338338

339+
if (global.EventSource) {
340+
knownGlobals.push(EventSource);
341+
}
342+
339343
if (global.fetch) {
340344
knownGlobals.push(fetch);
341345
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
assert.strictEqual(typeof EventSource, 'undefined');

test/parallel/test-eventsource.js

+7
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)