mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
565 stars 127 forks source link

fix(WebSocket): prevent dispatching "open" event if the connection was closed #654

Closed kettanaito closed 1 month ago

kettanaito commented 1 month ago

This change prevents the WebSocket client from dispatching the open event if the connection has been rejected (closed/errored) in the connection event listener.

Note that this keeps the oddity of the client.socket.readtyState being CONNECTING in the connection event listener. This isn't how an actual server connection event behaves, but this is necessary for the interceptor to reject the connection from within before it's ever opened (think of MSW rejecting the WebSocket connection if it's unhandled).

Previous approach

During play testing, I've discovered that client.socket.readyState value was 0 (CONNECTING) in the connection event listener's scope. I believe that's incorrect. The readyState of the intercepted client MUST be 1 (OPEN) in the connection event listener.

The connection event is dispatched when the connection has been successful. In the interceptor's context, any WebSocket connection is considered successful if it has the connection listener.

This is also the behavior when using the actual WebSocket server:

wss.on('connection', (ws) => {
  ws.readyState // 1
})

[!WARNING] If the connection event is dispatched when readyState is OPEN, then MSW cannot reject that connection (onUnhandledRequest). The open even is always emitted, and then the close/error event.

This isn't the right approach. Rejecting a connection is crucial.

kettanaito commented 1 month ago

Released: v0.36.3 🎉

This has been released in v0.36.3!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.