Skip to content

Commit

Permalink
feat: implement EventSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Jan 26, 2024
1 parent 0e79a9f commit 6a3a146
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 0 deletions.
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
7 changes: 7 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/common/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,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 @@ -373,6 +373,10 @@ if (global.WebSocket) {
knownGlobals.push(WebSocket);
}

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

function allowGlobals(...allowlist) {
knownGlobals = knownGlobals.concat(allowlist);
}
Expand Down
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 6a3a146

Please sign in to comment.