Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export WebSocketStream, add docs and types #3645

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions docs/docs/api/WebSocket.md
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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:
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions test/wpt/runner/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions types/websocket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebSocketCloseInfo>
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
}
Loading