Skip to content

Commit ee56aec

Browse files
Uzlopaktargos
authored andcommitted
lib: add EventSource Client
PR-URL: #51575 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 96f72ae commit ee56aec

12 files changed

+50
-1
lines changed

.eslintrc.js

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

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

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

856864
<!-- YAML
@@ -2706,6 +2714,7 @@ one is included in the list below.
27062714
* `--experimental-abortcontroller`
27072715
* `--experimental-default-type`
27082716
* `--experimental-detect-module`
2717+
* `--experimental-eventsource`
27092718
* `--experimental-import-meta-resolve`
27102719
* `--experimental-json-modules`
27112720
* `--experimental-loader`
@@ -3216,6 +3225,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
32163225
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
32173226
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
32183227
[ECMAScript module]: esm.md#modules-ecmascript-modules
3228+
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
32193229
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
32203230
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
32213231
[File System Permissions]: permissions.md#file-system-permissions

doc/node.1

+3
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-fetch
189192
Disable experimental support for the Fetch API.
190193
.

lib/.eslintrc.yaml

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

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

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function prepareExecution(options) {
107107
setupUndici();
108108
setupWebCrypto();
109109
setupCustomEvent();
110+
setupEventsource();
110111
setupCodeCoverage();
111112
setupDebugEnv();
112113
// Process initial diagnostic reporting configuration, if present.
@@ -321,6 +322,13 @@ function setupUndici() {
321322
}
322323
}
323324

325+
// https://html.spec.whatwg.org/multipage/server-sent-events.html
326+
function setupEventsource() {
327+
if (!getOptionValue('--experimental-eventsource')) {
328+
delete globalThis.EventSource;
329+
}
330+
}
331+
324332
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
325333
// removed.
326334
function setupNavigator() {

src/node_options.cc

+5
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",
402407
"experimental Fetch API",
403408
&EnvironmentOptions::experimental_fetch,

src/node_options.h

+1
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_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
@@ -325,6 +325,10 @@ if (global.structuredClone) {
325325
knownGlobals.push(global.structuredClone);
326326
}
327327

328+
if (global.EventSource) {
329+
knownGlobals.push(EventSource);
330+
}
331+
328332
if (global.fetch) {
329333
knownGlobals.push(fetch);
330334
}
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)