From c975384264dc553de62398be814d0c66fc1fc1fb Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sun, 4 Feb 2024 15:03:39 +0100 Subject: [PATCH] lib: enable WebSocket by default PR-URL: https://github.com/nodejs/node/pull/51594 Reviewed-By: Matteo Collina Reviewed-By: Paolo Insogna Reviewed-By: Robert Nagy Reviewed-By: Matthew Aitken Reviewed-By: Trivikram Kamat --- benchmark/websocket/simple.js | 1 - doc/api/cli.md | 20 +++++++++---------- doc/api/globals.md | 10 +++++++--- doc/node.1 | 6 +++--- .../bootstrap/web/exposed-window-or-worker.js | 3 +++ lib/internal/process/pre_execution.js | 4 ++-- src/node_options.h | 2 +- test/common/index.js | 3 --- test/parallel/test-websocket-disabled.js | 7 +++++++ test/parallel/test-websocket.js | 1 - 10 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 test/parallel/test-websocket-disabled.js diff --git a/benchmark/websocket/simple.js b/benchmark/websocket/simple.js index 1147602a7b1ea7..982f6bc83338e2 100644 --- a/benchmark/websocket/simple.js +++ b/benchmark/websocket/simple.js @@ -3,7 +3,6 @@ const common = require('../common.js'); const crypto = require('crypto'); const http = require('http'); -const { WebSocket } = require('../../deps/undici/undici'); const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; diff --git a/doc/api/cli.md b/doc/api/cli.md index 5ba337276597b9..4043597b4fbafd 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -949,16 +949,6 @@ added: v12.3.0 Enable experimental WebAssembly module support. -### `--experimental-websocket` - - - -Enable experimental [`WebSocket`][] support. - ### `--force-context-aware` + +Use this flag to disable experimental [`WebSocket`][] support. + ### `--no-extra-info-on-fatal-exception` > Stability: 1 - Experimental. -A browser-compatible implementation of [`WebSocket`][]. Enable this API -with the [`--experimental-websocket`][] CLI flag. +A browser-compatible implementation of [`WebSocket`][]. Disable this API +with the [`--no-experimental-websocket`][] CLI flag. ## Class: `WritableStream` @@ -1139,10 +1143,10 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][]. [Navigator API]: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object [RFC 5646]: https://www.rfc-editor.org/rfc/rfc5646.txt [Web Crypto API]: webcrypto.md -[`--experimental-websocket`]: cli.md#--experimental-websocket [`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent [`--no-experimental-global-navigator`]: cli.md#--no-experimental-global-navigator [`--no-experimental-global-webcrypto`]: cli.md#--no-experimental-global-webcrypto +[`--no-experimental-websocket`]: cli.md#--no-experimental-websocket [`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController [`ByteLengthQueuingStrategy`]: webstreams.md#class-bytelengthqueuingstrategy [`CompressionStream`]: webstreams.md#class-compressionstream diff --git a/doc/node.1 b/doc/node.1 index 2966c14e1c3f5e..26a979363f8d5b 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -186,12 +186,12 @@ Use this flag to enable ShadowRealm support. .It Fl -experimental-test-coverage Enable code coverage in the test runner. . -.It Fl -experimental-websocket -Enable experimental support for the WebSocket API. -. .It Fl -no-experimental-fetch Disable experimental support for the Fetch API. . +.It Fl -no-experimental-websocket +Disable experimental support for the WebSocket API. +. .It Fl -no-experimental-global-customevent Disable exposition of the CustomEvent on the global scope. . diff --git a/lib/internal/bootstrap/web/exposed-window-or-worker.js b/lib/internal/bootstrap/web/exposed-window-or-worker.js index fe38603cdab5cc..2459bee85bb8b0 100644 --- a/lib/internal/bootstrap/web/exposed-window-or-worker.js +++ b/lib/internal/bootstrap/web/exposed-window-or-worker.js @@ -92,6 +92,9 @@ exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [ 'FormData', 'Headers', 'Request', 'Response', ]); +// https://websockets.spec.whatwg.org/ +exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['WebSocket']); + // The WebAssembly Web API which relies on Response. // https:// webassembly.github.io/spec/web-api/#streaming-modules internalBinding('wasm_web_api').setImplementation((streamState, source) => { diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index eae3859cc5bc83..b7cf9390c9845c 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -315,8 +315,8 @@ function setupUndici() { delete globalThis.Response; } - if (!getEmbedderOptions().noBrowserGlobals && getOptionValue('--experimental-websocket')) { - exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['WebSocket']); + if (getOptionValue('--no-experimental-websocket')) { + delete globalThis.WebSocket; } } diff --git a/src/node_options.h b/src/node_options.h index 915151b7dc2904..a0b56ebacb436c 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -108,7 +108,7 @@ class EnvironmentOptions : public Options { std::string dns_result_order; bool enable_source_maps = false; bool experimental_fetch = true; - bool experimental_websocket = false; + bool experimental_websocket = true; bool experimental_global_customevent = true; bool experimental_global_navigator = true; bool experimental_global_web_crypto = true; diff --git a/test/common/index.js b/test/common/index.js index 2ac981608b4e92..74e583603fda3b 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -369,9 +369,6 @@ if (global.ReadableStream) { global.DecompressionStream, ); } -if (global.WebSocket) { - knownGlobals.push(WebSocket); -} function allowGlobals(...allowlist) { knownGlobals = knownGlobals.concat(allowlist); diff --git a/test/parallel/test-websocket-disabled.js b/test/parallel/test-websocket-disabled.js new file mode 100644 index 00000000000000..4d72294367718b --- /dev/null +++ b/test/parallel/test-websocket-disabled.js @@ -0,0 +1,7 @@ +// Flags: --no-experimental-websocket +'use strict'; + +require('../common'); +const assert = require('assert'); + +assert.strictEqual(typeof WebSocket, 'undefined'); diff --git a/test/parallel/test-websocket.js b/test/parallel/test-websocket.js index 2a7069ccf3b739..c595ec12bfb66c 100644 --- a/test/parallel/test-websocket.js +++ b/test/parallel/test-websocket.js @@ -1,4 +1,3 @@ -// Flags: --experimental-websocket 'use strict'; require('../common');