mswjs / interceptors

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

feat: add WebSocket class interceptor #501

Closed kettanaito closed 7 months ago

kettanaito commented 8 months ago

Changes

Todo

Third-party support

kettanaito commented 8 months ago

Socket.IO support

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']))
})
kettanaito commented 8 months ago

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.

kettanaito commented 7 months ago

Released: v0.26.0 šŸŽ‰

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.