-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
fix(WebSocket): exports client connection protocol, transport, renames types #509
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d67f96
fix(WebSocket): rename "WebSocketRawData" to "WebSocketData"
kettanaito 1dcec82
fix(WebSocket): rename "ws" to "socket" internally
kettanaito 3f9fd7f
fix(WebSocketServerConnection): accept "createConnection" as last arg…
kettanaito e49a287
feat(WebSocket): export "WebSocketTransport" class
kettanaito c7e108b
chore(WebSocketTransport): explain callbacks
kettanaito 0233b2e
fix: export "WebSocketClientConnectionProtocol" interface
kettanaito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,44 +5,53 @@ | |
* meant to be used over any WebSocket implementation | ||
* (not all of them follow the one from WHATWG). | ||
*/ | ||
import type { WebSocketRawData, WebSocketTransport } from './WebSocketTransport' | ||
import type { WebSocketData, WebSocketTransport } from './WebSocketTransport' | ||
import { WebSocketMessageListener } from './WebSocketOverride' | ||
import { bindEvent } from './utils/bindEvent' | ||
import { CloseEvent } from './utils/events' | ||
import { uuidv4 } from '../../utils/uuid' | ||
|
||
const kEmitter = Symbol('kEmitter') | ||
|
||
export interface WebSocketClientConnectionProtocol { | ||
id: string | ||
url: URL | ||
send(data: WebSocketData): void | ||
close(code?: number, reason?: string): void | ||
} | ||
|
||
/** | ||
* The WebSocket client instance represents an incoming | ||
* client connection. The user can control the connection, | ||
* send and receive events. | ||
*/ | ||
export class WebSocketClientConnection { | ||
export class WebSocketClientConnection | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extending this class itself to implement connection-like objects is inefficient. It requires to provide a bunch of irrelevant properties, like |
||
implements WebSocketClientConnectionProtocol | ||
{ | ||
public readonly id: string | ||
public readonly url: URL | ||
|
||
protected [kEmitter]: EventTarget | ||
private [kEmitter]: EventTarget | ||
|
||
constructor( | ||
protected readonly ws: WebSocket, | ||
protected readonly transport: WebSocketTransport | ||
private readonly socket: WebSocket, | ||
private readonly transport: WebSocketTransport | ||
) { | ||
this.id = uuidv4() | ||
this.url = new URL(ws.url) | ||
this.url = new URL(socket.url) | ||
this[kEmitter] = new EventTarget() | ||
|
||
// Emit outgoing client data ("ws.send()") as "message" | ||
// events on the client connection. | ||
this.transport.onOutgoing = (data) => { | ||
this[kEmitter].dispatchEvent( | ||
bindEvent(this.ws, new MessageEvent('message', { data })) | ||
bindEvent(this.socket, new MessageEvent('message', { data })) | ||
) | ||
} | ||
|
||
this.transport.onClose = (event) => { | ||
this[kEmitter].dispatchEvent( | ||
bindEvent(this.ws, new CloseEvent('close', event)) | ||
bindEvent(this.socket, new CloseEvent('close', event)) | ||
) | ||
} | ||
} | ||
|
@@ -76,7 +85,7 @@ export class WebSocketClientConnection { | |
/** | ||
* Send data to the connected client. | ||
*/ | ||
public send(data: WebSocketRawData): void { | ||
public send(data: WebSocketData): void { | ||
this.transport.send(data) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting this protocol means that other consumers, like MSW, can implement custom client connection-like classes. For example, to support
.clients
in the browser, MSW creates virtual connection objects that useBroadcastChannel
to implement methods likesend()
(signal other clients from other runtimes that they should now receive data).