lib: add EventSource Client

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Aras Abbasi authored and GitHub committed 2024-05-13 05:32:08 +00:00
1 parent ac5f8f0516
commit 1d7d094a98
12 files changed
+50 -1

No files matched your search

+1
View File
@@ -350,6 +350,7 @@ module.exports = {
Crypto: 'readable',
CryptoKey: 'readable',
DecompressionStream: 'readable',
EventSource: 'readable',
fetch: 'readable',
FormData: 'readable',
navigator: 'readable',
+10
View File
@@ -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
@@ -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`
@@ -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
+3
View File
@@ -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.
.
+2
View File
@@ -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
@@ -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
+8
View File
@@ -101,6 +101,7 @@ function prepareExecution(options) {
setupNavigator();
setupWarningHandler();
setupWebsocket();
setupEventsource();
setupCodeCoverage();
setupDebugEnv();
// Process initial diagnostic reporting configuration, if present.
@@ -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() {
+5
View File
@@ -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",
+1
View File
@@ -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;
+1
View File
@@ -126,6 +126,7 @@ const webIdlExposedWindow = new Set([
'Request',
'Response',
'WebSocket',
'EventSource',
]);
const nodeGlobals = new Set([
+4
View File
@@ -333,6 +333,10 @@ if (global.structuredClone) {
knownGlobals.push(global.structuredClone);
}
if (global.EventSource) {
knownGlobals.push(EventSource);
}
if (global.fetch) {
knownGlobals.push(fetch);
}
@@ -0,0 +1,6 @@
'use strict';
require('../common');
const assert = require('assert');
assert.strictEqual(typeof EventSource, 'undefined');
+7
View File
@@ -0,0 +1,7 @@
// Flags: --experimental-eventsource
'use strict';
require('../common');
const assert = require('assert');
assert.strictEqual(typeof EventSource, 'function');