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

fix(WebSocket): do not call public "server.send()" in message forwarding #650

Merged
merged 2 commits into from
Oct 1, 2024
Merged
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
12 changes: 11 additions & 1 deletion src/interceptors/WebSocket/WebSocketServerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

const kEmitter = Symbol('kEmitter')
const kBoundListener = Symbol('kBoundListener')
const kSend = Symbol('kSend')

interface WebSocketServerEventMap {
open: Event
Expand Down Expand Up @@ -63,7 +64,12 @@ export class WebSocketServerConnection {
// so execute the logic on the next tick.
queueMicrotask(() => {
if (!event.defaultPrevented) {
this.send(event.data)
/**
* @note Use the internal send mechanism so consumers can tell
* apart direct user calls to `server.send()` and internal calls.
* E.g. MSW has to ignore this internal call to log out messages correctly.
*/
this[kSend](event.data)
}
})
})
Expand Down Expand Up @@ -202,6 +208,10 @@ export class WebSocketServerConnection {
* server.send(new TextEncoder().encode('hello'))
*/
public send(data: WebSocketData): void {
this[kSend](data)
}

private [kSend](data: WebSocketData): void {
const { realWebSocket } = this

invariant(
Expand Down
Loading