Skip to content

feat(WebSocket): forward client messages to the server by default#545

Merged
kettanaito merged 5 commits into
mainfrom
feat/ws-client-forward
Apr 6, 2024
Merged

feat(WebSocket): forward client messages to the server by default#545
kettanaito merged 5 commits into
mainfrom
feat/ws-client-forward

Conversation

@kettanaito

@kettanaito kettanaito commented Apr 5, 2024

Copy link
Copy Markdown
Member

Changes

Outgoing client messages are now forwarded to the actual server by default after server.connect() is called. To prevent this, call event.preventDefault() on the respective client message.

Roadmap

  • Add the event.preventDefault() test for the client-to-server forwarding prevention.
  • Update README to reflect this change.
  • Update in-progress MSW WebSocket docs.

* This is called when the WebSocket client sends data.
*/
abstract onOutgoing: WebSocketTransportOnOutgoingCallback
export type WebSocketTransportEventMap = {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request also includes an internal refactoring of the WebSocketTransport:

  • No longer an abstract class but an interface. Implement it to your heart's content (which, in our case, is EventTarget).
  • Transport is event-based now since multiple agents can listen to the same events (incoming/outgoing).

/**
* Emit the "close" event on the "client" connection
* whenever the underlying transport is closed.
* @note "client.close()" does NOT dispatch the "close"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not clear before. Added a better explanation.

// to the actual server unless the outgoing message event
// has been prevented. The "outgoing" transport event it
// dispatched by the "client" connection.
this.transport.addEventListener('outgoing', (event) => {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feature

The actual implementation for the client-to-server automatic forwarding. The client connection results in the "outgoing" transport event. We listen to it in the server connection, and decide when to forward it to the server.

@kettanaito kettanaito merged commit 7d4d490 into main Apr 6, 2024
@kettanaito kettanaito deleted the feat/ws-client-forward branch April 6, 2024 11:30
@kettanaito

Copy link
Copy Markdown
Member Author

Released: v0.27.0 🎉

This has been released in v0.27.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant