Closed kettanaito closed 7 months ago
import { io } from '@mswjs/socket.io-parser'
interceptor.on('connection', (args) => {
const client = io(args.client)
const server = io(args.server)
// MessageEvent data is decoded.
client.on('message', (event) => {
console.log(event.data) // "hello"
})
// Custom events are supported
// (decoded from the generic MessageEvent).
client.on('custom-event', (data) => {})
// Outgoing messages are encoded.
client.send('hello')
// Custom events are encoded and transformed
// into the generic MessageEvent.
client.emit('custom-event',new Blob(['hello']))
// Incoming original server events are decoded.
server.on('server-event', (data) => {})
// Sent messages to the server are encoded.
// Also sent in 2 packets as Socket.IO does it.
server.send(new Blob(['hello']))
})
The API itself is complete. One thing remaining is proper test coverage, specifically addressing the bug in the ws
module (the Event
instance check) or migrating to a different server package to use for testing.
This has been released in v0.26.0!
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.
README.md
for the feature documentation.Changes
globalThis.WebSocket
class.client
and theserver
APIs to represent the incoming client connection and the original server connection respectively in the connection listener of the interceptor.WebSocketClassClient
,WebSocketClassServer
,WebSocketClassTransport
.Todo
ws
module we use for testing doesn't pass theinstanceof
check on dispatched events in Node, causing some tests to fail (https://github.com/websockets/ws/issues/1818).test/modules/WebSocket/WebSocketClass/exchange/websocket.server.connect.test.ts
Event
..send()
and.close()
and its input).onopen
and etc)origin
on theMessageEvent
from the socket. Check if the interceptor should do that too (inspect the original WebSocket event).error
event correctly. It's emitted in the following scenarios:> 1000 <= 1015
(https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1)WebSocketClient#emit
. It's non-standard.Support custom encoder/decoder for (a) JSON transfer; (b) to support custom event format (like SocketIO).client.addEventListener()
.Third-party support