diff --git a/.eslintrc.js b/.eslintrc.js index a154d00794ab4d..ac04fcc02da203 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -341,6 +341,7 @@ module.exports = { Crypto: 'readable', CryptoKey: 'readable', DecompressionStream: 'readable', + EventSource: 'readable', fetch: 'readable', FormData: 'readable', navigator: 'readable', diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 98533b7828d3ff..26797b2f606bfd 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -298,6 +298,7 @@ function setupWarningHandler() { } } +// https://html.spec.whatwg.org/multipage/server-sent-events.html // https://fetch.spec.whatwg.org/ // https://websockets.spec.whatwg.org/ function setupUndici() { @@ -328,6 +329,12 @@ function setupUndici() { }; } + if (getOptionValue('--experimental-eventsource')) { + ObjectDefineProperties(globalThis, { + EventSource: lazyInterface('EventSource'), + }); + } + if (!getOptionValue('--no-experimental-fetch')) { // Fetch is meant to return a Promise, but not be async. function fetch(input, init = undefined) { diff --git a/src/node_options.cc b/src/node_options.cc index 7b5152172c5ce7..9fe322e7598433 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -376,6 +376,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { &EnvironmentOptions::enable_source_maps, kAllowedInEnvvar); AddOption("--experimental-abortcontroller", "", NoOp{}, kAllowedInEnvvar); + AddOption("--experimental-eventsource", + "experimental EventSource API", + &EnvironmentOptions::experimental_eventsource, + kAllowedInEnvvar, + true); AddOption("--experimental-fetch", "experimental Fetch API", &EnvironmentOptions::experimental_fetch, diff --git a/src/node_options.h b/src/node_options.h index 915151b7dc2904..1c20daffc5f77a 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -107,6 +107,7 @@ class EnvironmentOptions : public Options { bool detect_module = false; std::string dns_result_order; bool enable_source_maps = false; + bool experimental_eventsource = false; bool experimental_fetch = true; bool experimental_websocket = false; bool experimental_global_customevent = true; diff --git a/test/common/globals.js b/test/common/globals.js index 8b9b6b34f6abaf..cb7c1629007ecf 100644 --- a/test/common/globals.js +++ b/test/common/globals.js @@ -124,6 +124,7 @@ const webIdlExposedWindow = new Set([ 'Request', 'Response', 'WebSocket', + 'EventSource', ]); const nodeGlobals = new Set([ diff --git a/test/common/index.js b/test/common/index.js index 2ac981608b4e92..fccd616b18edc6 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -373,6 +373,10 @@ if (global.WebSocket) { knownGlobals.push(WebSocket); } +if (global.EventSource) { + knownGlobals.push(EventSource); +} + function allowGlobals(...allowlist) { knownGlobals = knownGlobals.concat(allowlist); } diff --git a/test/parallel/test-eventsource.js b/test/parallel/test-eventsource.js new file mode 100644 index 00000000000000..787195ca19d971 --- /dev/null +++ b/test/parallel/test-eventsource.js @@ -0,0 +1,7 @@ +// Flags: --experimental-eventsource +'use strict'; + +require('../common'); +const assert = require('assert'); + +assert.strictEqual(typeof EventSource, 'function');