From d99e18adbccd6deafdcc0b7aba67f3569ee5956d Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 26 Sep 2024 01:40:23 -0400 Subject: [PATCH] export WebSocketStream, add docs and types (#3645) --- docs/docs/api/WebSocket.md | 48 +++++++++++++++++++++++++++++++++++--- index.js | 3 +++ test/wpt/runner/worker.mjs | 7 +++--- types/websocket.d.ts | 33 ++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/docs/docs/api/WebSocket.md b/docs/docs/api/WebSocket.md index 9d374f4046c..7b8c24c3ff2 100644 --- a/docs/docs/api/WebSocket.md +++ b/docs/docs/api/WebSocket.md @@ -1,7 +1,5 @@ # Class: WebSocket -> ⚠️ Warning: the WebSocket API is experimental. - Extends: [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the [WebSocket spec](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455). @@ -10,7 +8,7 @@ The WebSocket object provides a way to manage a WebSocket connection to a server Arguments: -* **url** `URL | string` - The url's protocol *must* be `ws` or `wss`. +* **url** `URL | string` * **protocol** `string | string[] | WebSocketInit` (optional) - Subprotocol(s) to request the server use, or a [`Dispatcher`](./Dispatcher.md). ### Example: @@ -36,6 +34,50 @@ import { WebSocket } from 'undici' const ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat']) ``` +# Class: WebSocketStream + +> ⚠️ Warning: the WebSocketStream API has not been finalized and is likely to change. + +See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocketStream) for more information. + +## `new WebSocketStream(url[, protocol])` + +Arguments: + +* **url** `URL | string` +* **options** `WebSocketStreamOptions` (optional) + +### WebSocketStream Example + +```js +const stream = new WebSocketStream('https://echo.websocket.org/') +const { readable, writable } = await stream.opened + +async function read () { + /** @type {ReadableStreamReader} */ + const reader = readable.getReader() + + while (true) { + const { done, value } = await reader.read() + if (done) break + + // do something with value + } +} + +async function write () { + /** @type {WritableStreamDefaultWriter} */ + const writer = writable.getWriter() + writer.write('Hello, world!') + writer.releaseLock() +} + +read() + +setInterval(() => write(), 5000) + +``` + ## Read More - [MDN - WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) diff --git a/index.js b/index.js index 35d3d0428bc..967d07e29e5 100644 --- a/index.js +++ b/index.js @@ -148,6 +148,9 @@ module.exports.CloseEvent = CloseEvent module.exports.ErrorEvent = ErrorEvent module.exports.MessageEvent = MessageEvent +module.exports.WebSocketStream = require('./lib/web/websocket/stream/websocketstream').WebSocketStream +module.exports.WebSocketError = require('./lib/web/websocket/stream/websocketerror').WebSocketError + module.exports.request = makeDispatcher(api.request) module.exports.stream = makeDispatcher(api.stream) module.exports.pipeline = makeDispatcher(api.pipeline) diff --git a/test/wpt/runner/worker.mjs b/test/wpt/runner/worker.mjs index 0bfdebff179..05ff38ae196 100644 --- a/test/wpt/runner/worker.mjs +++ b/test/wpt/runner/worker.mjs @@ -14,15 +14,14 @@ import { CloseEvent, WebSocket, caches, - EventSource + EventSource, + WebSocketStream, + WebSocketError } from '../../../index.js' // TODO(@KhafraDev): export these in index.js import { Cache } from '../../../lib/web/cache/cache.js' import { CacheStorage } from '../../../lib/web/cache/cachestorage.js' import { webcrypto } from 'node:crypto' -// TODO(@KhafraDev): move this import once its added to index -import { WebSocketStream } from '../../../lib/web/websocket/stream/websocketstream.js' -import { WebSocketError } from '../../../lib/web/websocket/stream/websocketerror.js' const { initScripts, meta, test, url, path } = workerData diff --git a/types/websocket.d.ts b/types/websocket.d.ts index 946cbdc2864..563866479f1 100644 --- a/types/websocket.d.ts +++ b/types/websocket.d.ts @@ -148,3 +148,36 @@ interface WebSocketInit { dispatcher?: Dispatcher, headers?: HeadersInit } + +interface WebSocketStreamOptions { + protocols?: string | string[] + signal?: AbortSignal +} + +interface WebSocketCloseInfo { + closeCode: number + reason: string +} + +interface WebSocketStream { + closed: Promise + opened: Promise<{ + extensions: string + protocol: string + readable: ReadableStream + writable: WritableStream + }> + url: string +} + +export declare const WebSocketStream: { + prototype: WebSocketStream + new (url: string | URL, options?: WebSocketStreamOptions): WebSocketStream +} + +interface WebSocketError extends Event, WebSocketCloseInfo {} + +export declare const WebSocketError: { + prototype: WebSocketError + new (type: string, init?: WebSocketCloseInfo): WebSocketError +}