Skip to content

Commit 02b36cb

Browse files
Uzlopaktargos
authored andcommitted
lib: add EventSource Client
PR-URL: #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 eb2402d commit 02b36cb

12 files changed

+51
-0
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ module.exports = {
348348
Crypto: 'readable',
349349
CryptoKey: 'readable',
350350
DecompressionStream: 'readable',
351+
EventSource: 'readable',
351352
fetch: 'readable',
352353
FormData: 'readable',
353354
ReadableStream: 'readable',

doc/api/cli.md

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

901+
### `--experimental-eventsource`
902+
903+
<!-- YAML
904+
added: REPLACEME
905+
-->
906+
907+
Enable exposition of [EventSource Web API][] on the global scope.
908+
901909
### `--experimental-import-meta-resolve`
902910

903911
<!-- YAML
@@ -2697,6 +2705,7 @@ one is included in the list below.
26972705
* `--experimental-abortcontroller`
26982706
* `--experimental-default-type`
26992707
* `--experimental-detect-module`
2708+
* `--experimental-eventsource`
27002709
* `--experimental-import-meta-resolve`
27012710
* `--experimental-json-modules`
27022711
* `--experimental-loader`
@@ -3172,6 +3181,7 @@ done
31723181
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
31733182
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
31743183
[ECMAScript module]: esm.md#modules-ecmascript-modules
3184+
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
31753185
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
31763186
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
31773187
[File System Permissions]: permissions.md#file-system-permissions

doc/node.1

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ Use this flag to enable ShadowRealm support.
191191
.It Fl -experimental-test-coverage
192192
Enable code coverage in the test runner.
193193
.
194+
.It Fl -experimental-eventsource
195+
Enable experimental support for the EventSource Web API.
196+
.
194197
.It Fl -experimental-websocket
195198
Enable experimental support for the WebSocket API.
196199
.

lib/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ rules:
152152
message: Use `const { Crypto } = require('internal/crypto/webcrypto');` instead of the global.
153153
- name: CryptoKey
154154
message: Use `const { CryptoKey } = require('internal/crypto/webcrypto');` instead of the global.
155+
- name: EventSource
156+
message: Use `const { EventSource } = require('internal/deps/undici/undici');` instead of the global.
155157
- name: fetch
156158
message: Use `const { fetch } = require('internal/deps/undici/undici');` instead of the global.
157159
- name: global

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

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +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/
85+
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['EventSource']);
86+
8487
// The WebAssembly Web API which relies on Response.
8588
// https:// webassembly.github.io/spec/web-api/#streaming-modules
8689
internalBinding('wasm_web_api').setImplementation((streamState, source) => {

lib/internal/process/pre_execution.js

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function prepareExecution(options) {
109109
setupUndici();
110110
setupWebCrypto();
111111
setupCustomEvent();
112+
setupEventsource();
112113
setupCodeCoverage();
113114
setupDebugEnv();
114115
// Process initial diagnostic reporting configuration, if present.
@@ -328,6 +329,13 @@ function setupUndici() {
328329
}
329330
}
330331

332+
// https://html.spec.whatwg.org/multipage/server-sent-events.html
333+
function setupEventsource() {
334+
if (!getOptionValue('--experimental-eventsource')) {
335+
delete globalThis.EventSource;
336+
}
337+
}
338+
331339
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
332340
// removed.
333341
function setupWebCrypto() {

src/node_options.cc

+5
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
406406
&EnvironmentOptions::enable_source_maps,
407407
kAllowedInEnvvar);
408408
AddOption("--experimental-abortcontroller", "", NoOp{}, kAllowedInEnvvar);
409+
AddOption("--experimental-eventsource",
410+
"experimental EventSource API",
411+
&EnvironmentOptions::experimental_eventsource,
412+
kAllowedInEnvvar,
413+
false);
409414
AddOption("--experimental-fetch",
410415
"experimental Fetch API",
411416
&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 = false;
120121
bool experimental_global_customevent = true;

test/common/globals.js

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

+4
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ if (global.structuredClone) {
330330
knownGlobals.push(global.structuredClone);
331331
}
332332

333+
if (global.EventSource) {
334+
knownGlobals.push(EventSource);
335+
}
336+
333337
if (global.fetch) {
334338
knownGlobals.push(fetch);
335339
}
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)