Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add EventSource #51575

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,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 @@ -853,6 +853,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 @@ -2648,6 +2656,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 @@ -3153,6 +3162,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[CommonJS module]: modules.md
[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
[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 @@ -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-websocket
Disable experimental support for the WebSocket 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 @@ -87,8 +87,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 @@ -101,6 +101,7 @@ function prepareExecution(options) {
setupNavigator();
setupWarningHandler();
setupWebsocket();
setupEventsource();
setupCodeCoverage();
setupDebugEnv();
// Process initial diagnostic reporting configuration, if present.
Expand Down Expand Up @@ -306,6 +307,13 @@ function setupWebsocket() {
}
}

// 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", "", NoOp{}, kAllowedInEnvvar);
AddOption("--experimental-websocket",
"experimental WebSocket API",
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_navigator = 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 @@ -126,6 +126,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 @@ -333,6 +333,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');