Skip to content

Commit 4f502fb

Browse files
Uzlopakbmeck
authored andcommitted
lib: add EventSource Client
PR-URL: nodejs#51575 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent c16e664 commit 4f502fb

File tree

12 files changed

+50
-1
lines changed

12 files changed

+50
-1
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ module.exports = {
350350
Crypto: 'readable',
351351
CryptoKey: 'readable',
352352
DecompressionStream: 'readable',
353+
EventSource: 'readable',
353354
fetch: 'readable',
354355
FormData: 'readable',
355356
navigator: 'readable',

doc/api/cli.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,14 @@ CommonJS. This includes the following:
853853
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
854854
`exports`, `__dirname`, `__filename`).
855855

856+
### `--experimental-eventsource`
857+
858+
<!-- YAML
859+
added: REPLACEME
860+
-->
861+
862+
Enable exposition of [EventSource Web API][] on the global scope.
863+
856864
### `--experimental-import-meta-resolve`
857865

858866
<!-- YAML
@@ -2648,6 +2656,7 @@ one is included in the list below.
26482656
* `--experimental-abortcontroller`
26492657
* `--experimental-default-type`
26502658
* `--experimental-detect-module`
2659+
* `--experimental-eventsource`
26512660
* `--experimental-import-meta-resolve`
26522661
* `--experimental-json-modules`
26532662
* `--experimental-loader`
@@ -3153,6 +3162,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
31533162
[CommonJS module]: modules.md
31543163
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
31553164
[ECMAScript module]: esm.md#modules-ecmascript-modules
3165+
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
31563166
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
31573167
[File System Permissions]: permissions.md#file-system-permissions
31583168
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ Use this flag to enable ShadowRealm support.
185185
.It Fl -experimental-test-coverage
186186
Enable code coverage in the test runner.
187187
.
188+
.It Fl -experimental-eventsource
189+
Enable experimental support for the EventSource Web API.
190+
.
188191
.It Fl -no-experimental-websocket
189192
Disable experimental support for the WebSocket API.
190193
.

lib/.eslintrc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ rules:
154154
message: Use `const { Crypto } = require('internal/crypto/webcrypto');` instead of the global.
155155
- name: CryptoKey
156156
message: Use `const { CryptoKey } = require('internal/crypto/webcrypto');` instead of the global.
157+
- name: EventSource
158+
message: Use `const { EventSource } = require('internal/deps/undici/undici');` instead of the global.
157159
- name: fetch
158160
message: Use `const { fetch } = require('internal/deps/undici/undici');` instead of the global.
159161
- name: global

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
8787
'FormData', 'Headers', 'Request', 'Response', 'MessageEvent',
8888
]);
8989

90+
// https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events.org/
9091
// https://websockets.spec.whatwg.org/
91-
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['WebSocket']);
92+
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['EventSource', 'WebSocket']);
9293

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

lib/internal/process/pre_execution.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function prepareExecution(options) {
101101
setupNavigator();
102102
setupWarningHandler();
103103
setupWebsocket();
104+
setupEventsource();
104105
setupCodeCoverage();
105106
setupDebugEnv();
106107
// Process initial diagnostic reporting configuration, if present.
@@ -306,6 +307,13 @@ function setupWebsocket() {
306307
}
307308
}
308309

310+
// https://html.spec.whatwg.org/multipage/server-sent-events.html
311+
function setupEventsource() {
312+
if (!getOptionValue('--experimental-eventsource')) {
313+
delete globalThis.EventSource;
314+
}
315+
}
316+
309317
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
310318
// removed.
311319
function setupNavigator() {

src/node_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
398398
&EnvironmentOptions::enable_source_maps,
399399
kAllowedInEnvvar);
400400
AddOption("--experimental-abortcontroller", "", NoOp{}, kAllowedInEnvvar);
401+
AddOption("--experimental-eventsource",
402+
"experimental EventSource API",
403+
&EnvironmentOptions::experimental_eventsource,
404+
kAllowedInEnvvar,
405+
false);
401406
AddOption("--experimental-fetch", "", NoOp{}, kAllowedInEnvvar);
402407
AddOption("--experimental-websocket",
403408
"experimental WebSocket API",

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class EnvironmentOptions : public Options {
115115
bool require_module = false;
116116
std::string dns_result_order;
117117
bool enable_source_maps = false;
118+
bool experimental_eventsource = false;
118119
bool experimental_fetch = true;
119120
bool experimental_websocket = true;
120121
bool experimental_global_navigator = true;

test/common/globals.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const webIdlExposedWindow = new Set([
126126
'Request',
127127
'Response',
128128
'WebSocket',
129+
'EventSource',
129130
]);
130131

131132
const nodeGlobals = new Set([

test/common/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ if (global.structuredClone) {
333333
knownGlobals.push(global.structuredClone);
334334
}
335335

336+
if (global.EventSource) {
337+
knownGlobals.push(EventSource);
338+
}
339+
336340
if (global.fetch) {
337341
knownGlobals.push(fetch);
338342
}

0 commit comments

Comments
 (0)