Skip to content

Commit

Permalink
lib: add EventSource Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Apr 14, 2024
1 parent f8e325e commit 6a77c9c
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,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 @@ -808,6 +808,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 @@ -2603,6 +2611,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 @@ -3114,6 +3123,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
[ECMAScript module]: esm.md#modules-ecmascript-modules
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
[File System Permissions]: permissions.md#file-system-permissions
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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
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',
]);

// 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
5 changes: 5 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,14 @@ function setupWarningHandler() {
}
}

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

if (getOptionValue('--no-experimental-fetch')) {
delete globalThis.fetch;
delete globalThis.FormData;
Expand Down
5 changes: 5 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,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 @@ -109,6 +109,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 @@ -336,6 +336,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 6a77c9c

Please sign in to comment.