mswjs / interceptors

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

fix(WebSocket): emit "connection" event on the next tick #518

Closed kettanaito closed 4 months ago

kettanaito commented 4 months ago

Right now, the connection event is emitted to the interceptors on the same tick as the WebSocket instance is created. It makes it impossible to observe the changes to that instance while connecting.

For example, the client may set a different .binaryType on the socket after it's constructed (i.e. starts connecting):

const socket = new WebSocket(url)
socket.binaryType = 'arraybuffer'

For the interceptor to know about that change, we have to emit the connection event on the next tick, when any modifications to the socket are complete (including the event listeners).

This will allow us to properly inherit .binaryType on the real socket when calling server.connect(). This also gives us a behavior similar to production code, where the open event will always be emitted on the next tick. The connection interceptor event must not be emitted before the open event.

kettanaito commented 4 months ago

Released: v0.26.8 🎉

This has been released in v0.26.8!

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.