Skip to content

Commit

Permalink
lib: add EventSource Client
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Uzlopak authored and targos committed Jun 1, 2024
1 parent 96f72ae commit ee56aec
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ module.exports = {
Crypto: 'readable',
CryptoKey: 'readable',
DecompressionStream: 'readable',
EventSource: 'readable',
fetch: 'readable',
FormData: 'readable',
navigator: 'readable',
Expand Down
10 changes: 10 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,14 @@ CommonJS. This includes the following:
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
`exports`, `__dirname`, `__filename`).

### `--experimental-eventsource`

<!-- YAML
added: REPLACEME
-->

Enable exposition of [EventSource Web API][] on the global scope.

### `--experimental-import-meta-resolve`

<!-- YAML
Expand Down Expand Up @@ -2706,6 +2714,7 @@ one is included in the list below.
* `--experimental-abortcontroller`
* `--experimental-default-type`
* `--experimental-detect-module`
* `--experimental-eventsource`
* `--experimental-import-meta-resolve`
* `--experimental-json-modules`
* `--experimental-loader`
Expand Down Expand Up @@ -3216,6 +3225,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
[ECMAScript module]: esm.md#modules-ecmascript-modules
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
[File System Permissions]: permissions.md#file-system-permissions
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ Use this flag to enable ShadowRealm support.
.It Fl -experimental-test-coverage
Enable code coverage in the test runner.
.
.It Fl -experimental-eventsource
Enable experimental support for the EventSource Web API.
.
.It Fl -no-experimental-fetch
Disable experimental support for the Fetch API.
.
Expand Down
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ rules:
message: Use `const { Crypto } = require('internal/crypto/webcrypto');` instead of the global.
- name: CryptoKey
message: Use `const { CryptoKey } = require('internal/crypto/webcrypto');` instead of the global.
- name: EventSource
message: Use `const { EventSource } = require('internal/deps/undici/undici');` instead of the global.
- name: fetch
message: Use `const { fetch } = require('internal/deps/undici/undici');` instead of the global.
- name: global
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
'FormData', 'Headers', 'Request', 'Response', 'MessageEvent',
]);

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

// The WebAssembly Web API which relies on Response.
// https:// webassembly.github.io/spec/web-api/#streaming-modules
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function prepareExecution(options) {
setupUndici();
setupWebCrypto();
setupCustomEvent();
setupEventsource();
setupCodeCoverage();
setupDebugEnv();
// Process initial diagnostic reporting configuration, if present.
Expand Down Expand Up @@ -321,6 +322,13 @@ function setupUndici() {
}
}

// https://html.spec.whatwg.org/multipage/server-sent-events.html
function setupEventsource() {
if (!getOptionValue('--experimental-eventsource')) {
delete globalThis.EventSource;
}
}

// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
// removed.
function setupNavigator() {
Expand Down
5 changes: 5 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::enable_source_maps,
kAllowedInEnvvar);
AddOption("--experimental-abortcontroller", "", NoOp{}, kAllowedInEnvvar);
AddOption("--experimental-eventsource",
"experimental EventSource API",
&EnvironmentOptions::experimental_eventsource,
kAllowedInEnvvar,
false);
AddOption("--experimental-fetch",
"experimental Fetch API",
&EnvironmentOptions::experimental_fetch,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class EnvironmentOptions : public Options {
bool require_module = false;
std::string dns_result_order;
bool enable_source_maps = false;
bool experimental_eventsource = false;
bool experimental_fetch = true;
bool experimental_websocket = true;
bool experimental_global_customevent = true;
Expand Down
1 change: 1 addition & 0 deletions test/common/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const webIdlExposedWindow = new Set([
'Request',
'Response',
'WebSocket',
'EventSource',
]);

const nodeGlobals = new Set([
Expand Down
4 changes: 4 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ if (global.structuredClone) {
knownGlobals.push(global.structuredClone);
}

if (global.EventSource) {
knownGlobals.push(EventSource);
}

if (global.fetch) {
knownGlobals.push(fetch);
}
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-eventsource-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

require('../common');
const assert = require('assert');

assert.strictEqual(typeof EventSource, 'undefined');
7 changes: 7 additions & 0 deletions test/parallel/test-eventsource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Flags: --experimental-eventsource
'use strict';

require('../common');
const assert = require('assert');

assert.strictEqual(typeof EventSource, 'function');

0 comments on commit ee56aec

Please sign in to comment.