GeezerVison is showing github.com — lib: add WebSocket client · nodejs/node@a01050d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .eslintrc.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -360,5 +360,6 @@ module.exports = { | 360 | 360 | WritableStream: 'readable', | 361 | 361 | WritableStreamDefaultWriter: 'readable', | 362 | 362 | WritableStreamDefaultController: 'readable', | | 363 | + WebSocket: 'readable', | 363 | 364 | }, | 364 | 365 | }; |
Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -725,6 +725,14 @@ added: v12.3.0 | 725 | 725 |
| 726 | 726 | Enable experimental WebAssembly module support. | 727 | 727 |
| | 728 | +### `--experimental-websocket` | | 729 | + | | 730 | +<!-- YAML | | 731 | +added: REPLACEME | | 732 | +--> | | 733 | + | | 734 | +Enable experimental [`WebSocket`][] support. | | 735 | + | 728 | 736 | ### `--force-context-aware` | 729 | 737 |
| 730 | 738 | <!-- YAML | @@ -2220,6 +2228,7 @@ Node.js options that are allowed are: | 2220 | 2228 | * `--experimental-vm-modules` | 2221 | 2229 | * `--experimental-wasi-unstable-preview1` | 2222 | 2230 | * `--experimental-wasm-modules` | | 2231 | +* `--experimental-websocket` | 2223 | 2232 | * `--force-context-aware` | 2224 | 2233 | * `--force-fips` | 2225 | 2234 | * `--force-node-api-uncaught-exceptions-policy` | @@ -2687,6 +2696,7 @@ done | 2687 | 2696 | [`NODE_OPTIONS`]: #node_optionsoptions | 2688 | 2697 | [`NO_COLOR`]: https://no-color.org | 2689 | 2698 | [`SlowBuffer`]: buffer.md#class-slowbuffer | | 2699 | +[`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket | 2690 | 2700 | [`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328 | 2691 | 2701 | [`dns.lookup()`]: dns.md#dnslookuphostname-options-callback | 2692 | 2702 | [`dns.setDefaultResultOrder()`]: dns.md#dnssetdefaultresultorderorder | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/globals.md +13Lines changed: 13 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -966,6 +966,17 @@ The object that acts as the namespace for all W3C | 966 | 966 | [WebAssembly][webassembly-org] related functionality. See the | 967 | 967 | [Mozilla Developer Network][webassembly-mdn] for usage and compatibility. | 968 | 968 |
| | 969 | +## `WebSocket` | | 970 | + | | 971 | +<!-- YAML | | 972 | +added: REPLACEME | | 973 | +--> | | 974 | + | | 975 | +> Stability: 1 - Experimental. | | 976 | +
| | 977 | +A browser-compatible implementation of [`WebSocket`][]. Enable this API | | 978 | +with the [`--experimental-websocket`][] CLI flag. | | 979 | + | 969 | 980 | ## Class: `WritableStream` | 970 | 981 |
| 971 | 982 | <!-- YAML | @@ -999,6 +1010,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][]. | 999 | 1010 | [CommonJS module]: modules.md | 1000 | 1011 | [ECMAScript module]: esm.md | 1001 | 1012 | [Web Crypto API]: webcrypto.md | | 1013 | +[`--experimental-websocket`]: cli.md#--experimental-websocket | 1002 | 1014 | [`--no-experimental-fetch`]: cli.md#--no-experimental-fetch | 1003 | 1015 | [`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent | 1004 | 1016 | [`--no-experimental-global-webcrypto`]: cli.md#--no-experimental-global-webcrypto | @@ -1033,6 +1045,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][]. | 1033 | 1045 | [`TransformStream`]: webstreams.md#class-transformstream | 1034 | 1046 | [`URLSearchParams`]: url.md#class-urlsearchparams | 1035 | 1047 | [`URL`]: url.md#class-url | | 1048 | +[`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket | 1036 | 1049 | [`WritableStreamDefaultController`]: webstreams.md#class-writablestreamdefaultcontroller | 1037 | 1050 | [`WritableStreamDefaultWriter`]: webstreams.md#class-writablestreamdefaultwriter | 1038 | 1051 | [`WritableStream`]: webstreams.md#class-writablestream | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -178,6 +178,9 @@ Use this flag to enable ShadowRealm support. | 178 | 178 | .It Fl -experimental-test-coverage | 179 | 179 | Enable code coverage in the test runner. | 180 | 180 | . | | 181 | +.It Fl -experimental-websocket | | 182 | +Enable experimental support for the WebSocket API. | | 183 | +. | 181 | 184 | .It Fl -no-experimental-fetch | 182 | 185 | Disable experimental support for the Fetch API. | 183 | 186 | . | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/pre_execution.js +28-20Lines changed: 28 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,7 +78,7 @@ function prepareExecution(options) { | 78 | 78 | setupTraceCategoryState(); | 79 | 79 | setupInspectorHooks(); | 80 | 80 | setupWarningHandler(); | 81 | | - setupFetch(); | | 81 | + setupUndici(); | 82 | 82 | setupWebCrypto(); | 83 | 83 | setupCustomEvent(); | 84 | 84 | setupCodeCoverage(); | @@ -262,9 +262,9 @@ function setupWarningHandler() { | 262 | 262 | } | 263 | 263 |
| 264 | 264 | // https://fetch.spec.whatwg.org/ | 265 | | -function setupFetch() { | 266 | | - if (getEmbedderOptions().noBrowserGlobals || | 267 | | - getOptionValue('--no-experimental-fetch')) { | | 265 | +// https://websockets.spec.whatwg.org/ | | 266 | +function setupUndici() { | | 267 | + if (getEmbedderOptions().noBrowserGlobals) { | 268 | 268 | return; | 269 | 269 | } | 270 | 270 |
| @@ -278,12 +278,6 @@ function setupFetch() { | 278 | 278 | return undici; | 279 | 279 | } | 280 | 280 |
| 281 | | - async function fetch(input, init = undefined) { | 282 | | - return lazyUndici().fetch(input, init); | 283 | | - } | 284 | | - | 285 | | - defineOperation(globalThis, 'fetch', fetch); | 286 | | - | 287 | 281 | function lazyInterface(name) { | 288 | 282 | return { | 289 | 283 | configurable: true, | @@ -297,17 +291,31 @@ function setupFetch() { | 297 | 291 | }; | 298 | 292 | } | 299 | 293 |
| 300 | | - ObjectDefineProperties(globalThis, { | 301 | | - FormData: lazyInterface('FormData'), | 302 | | - Headers: lazyInterface('Headers'), | 303 | | - Request: lazyInterface('Request'), | 304 | | - Response: lazyInterface('Response'), | 305 | | - }); | | 294 | + if (!getOptionValue('--no-experimental-fetch')) { | | 295 | + async function fetch(input, init = undefined) { | | 296 | + return lazyUndici().fetch(input, init); | | 297 | + } | 306 | 298 |
| 307 | | - // The WebAssembly Web API: https://webassembly.github.io/spec/web-api | 308 | | - internalBinding('wasm_web_api').setImplementation((streamState, source) => { | 309 | | - require('internal/wasm_web_api').wasmStreamingCallback(streamState, source); | 310 | | - }); | | 299 | + defineOperation(globalThis, 'fetch', fetch); | | 300 | + | | 301 | + ObjectDefineProperties(globalThis, { | | 302 | + FormData: lazyInterface('FormData'), | | 303 | + Headers: lazyInterface('Headers'), | | 304 | + Request: lazyInterface('Request'), | | 305 | + Response: lazyInterface('Response'), | | 306 | + }); | | 307 | + | | 308 | + // The WebAssembly Web API: https://webassembly.github.io/spec/web-api | | 309 | + internalBinding('wasm_web_api').setImplementation((streamState, source) => { | | 310 | + require('internal/wasm_web_api').wasmStreamingCallback(streamState, source); | | 311 | + }); | | 312 | + } | | 313 | + | | 314 | + if (getOptionValue('--experimental-websocket')) { | | 315 | + ObjectDefineProperties(globalThis, { | | 316 | + WebSocket: lazyInterface('WebSocket'), | | 317 | + }); | | 318 | + } | 311 | 319 | } | 312 | 320 |
| 313 | 321 | // TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -370,6 +370,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 370 | 370 | &EnvironmentOptions::experimental_fetch, | 371 | 371 | kAllowedInEnvvar, | 372 | 372 | true); | | 373 | + AddOption("--experimental-websocket", | | 374 | + "experimental WebSocket API", | | 375 | + &EnvironmentOptions::experimental_websocket, | | 376 | + kAllowedInEnvvar, | | 377 | + true); | 373 | 378 | AddOption("--experimental-global-customevent", | 374 | 379 | "expose experimental CustomEvent on the global scope", | 375 | 380 | &EnvironmentOptions::experimental_global_customevent, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,6 +111,7 @@ class EnvironmentOptions : public Options { | 111 | 111 | std::string dns_result_order; | 112 | 112 | bool enable_source_maps = false; | 113 | 113 | bool experimental_fetch = true; | | 114 | + bool experimental_websocket = false; | 114 | 115 | bool experimental_global_customevent = true; | 115 | 116 | bool experimental_global_web_crypto = true; | 116 | 117 | bool experimental_https_modules = false; | |
Collapse file Copy file name to clipboardExpand all lines: test/common/globals.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -123,6 +123,7 @@ const webIdlExposedWindow = new Set([ | 123 | 123 | 'Headers', | 124 | 124 | 'Request', | 125 | 125 | 'Response', | | 126 | + 'WebSocket', | 126 | 127 | ]); | 127 | 128 |
| 128 | 129 | const nodeGlobals = new Set([ | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -362,6 +362,9 @@ if (global.ReadableStream) { | 362 | 362 | global.DecompressionStream, | 363 | 363 | ); | 364 | 364 | } | | 365 | +if (global.WebSocket) { | | 366 | + knownGlobals.push(WebSocket); | | 367 | +} | 365 | 368 |
| 366 | 369 | function allowGlobals(...allowlist) { | 367 | 370 | knownGlobals = knownGlobals.concat(allowlist); | |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --experimental-websocket | | 2 | +'use strict'; | | 3 | + | | 4 | +require('../common'); | | 5 | +const assert = require('assert'); | | 6 | + | | 7 | +assert.strictEqual(typeof WebSocket, 'function'); |
|