Skip to content

Commit

Permalink
fix(WebSocket): do not call public "server.send()" in message forward…
Browse files Browse the repository at this point in the history
…ing (#650)
  • Loading branch information
kettanaito authored Oct 1, 2024
1 parent 01b96ba commit befd772
Showing 1 changed file with 11 additions and 1 deletion.
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

0 comments on commit befd772

Please sign in to comment.