Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Apr 6, 2024

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Apr 5, 2024

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
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
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
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
1 check passed
@kettanaito kettanaito deleted the feat/ws-client-forward branch April 6, 2024 11:30
@kettanaito
Copy link
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