fix(WebSocketFrame): allow for broader connection types#2710
Conversation
📝 WalkthroughWalkthroughThe changes introduce generics to Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
@christoph-fricke, hi 👋 What do you think about this approach? Do you see any potential issues with this? |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/core/handlers/WebSocketHandler.ts (1)
25-33: RenameWebSocketConnectionFoobefore it becomes API baggage.
Foolooks like a placeholder. Since this type is exported and now reused across the frame/logger surface, a descriptive name likeWebSocketConnectionLikeorBaseWebSocketConnectionwill age much better.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/core/handlers/WebSocketHandler.ts` around lines 25 - 33, The exported interface name WebSocketConnectionFoo is a placeholder and should be renamed to a descriptive stable name used across the surface; rename WebSocketConnectionFoo to something like WebSocketConnectionLike or BaseWebSocketConnection and update all references (including the extending type WebSocketHandlerConnection) so the export and internal uses compile and convey intent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/core/handlers/WebSocketHandler.ts`:
- Around line 25-33: The exported interface name WebSocketConnectionFoo is a
placeholder and should be renamed to a descriptive stable name used across the
surface; rename WebSocketConnectionFoo to something like WebSocketConnectionLike
or BaseWebSocketConnection and update all references (including the extending
type WebSocketHandlerConnection) so the export and internal uses compile and
convey intent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: ca97741a-f57b-40c5-bbf0-a5eb2f79e472
📒 Files selected for processing (4)
src/core/experimental/frames/websocket-frame.tssrc/core/experimental/sources/interceptor-source.tssrc/core/handlers/WebSocketHandler.tssrc/core/ws/utils/attachWebSocketLogger.ts
christoph-fricke
left a comment
There was a problem hiding this comment.
@kettanaito I packed it locally and tested it with @msw/playwright. With this, the existing protocol implementations work and the tests pass. Just had to add a wonky import to node_modules/../../WebSocketHandler to get to the WebSocketConnectionFoo type.
Not too sure what I think about the implementation. It feels a bit "bolted on", like trying to squeeze the protocol interfaces in afterwards. But that might be just me knowing the history...
Will it cause big observable differences between sources with WebSocketConnectionFoo and sources with WebSocketConnectionData?
Left an additional comment in #2707 that might make this PR obsolete.
|
The dichotomy comes from the frames not guaranteeing to be created by the WebSocketInterceptor anymore. We have that assumption right now and it's rather strong (nothing wrong with that since The union with |
|
|
||
| export interface WebSocketNetworkFrameOptions { | ||
| connection: WebSocketConnectionData | ||
| export interface WebSocketNetworkFrameOptions< |
There was a problem hiding this comment.
As far as I understand, the ConnectionProtocols interfaces are the intended abstraction for decoupling MSW internals from WebSocket specifics, right? They are supposed to enable custom frames that extend can extend WebSocketNetworkFrame.
The previous @msw/playwright implementation already proofed that it is possible to implement ConnectionProtocols with something that is not a WebSocketInterceptor. For other alternative sources, I am toying with the idea of a HttpServerSource and WebSocketServerSource, i.e. sources that create an actual server. I have some use cases for that... I don't see blockers creating ConnectionProtocols there either.
The biggest change I would make: I think ConnectionProtocols as the abstraction target is better "highlighted" and "communicated" when the WebSocketNetworkFrame does not accept an generic connection. It feels like overhead and is just one more think to understand. To avoid more confusion, it might help if these interfaces do not need imports from @mswjs/interceptors, but that might be unfeasible. In the end, most people will likely just use existing sources and not implement their own sources and frames... Adjusted frame options:
interface WebSocketNetworkFrameOptions {
connection: WebSocketConnectionFoo // <-- Type must be package exported
}
// I actually prefer it flattened (avoids having to find a name for "Foo"):
interface WebSocketNetworkFrameOptions {
client: WebSocketClientConnectionProtocol
server: WebSocketServerConnectionProtocol
info: WebSocketConnectionData['info']
}I quickly verify that removing the generic works. It only requires a small adjustment to the InterceptorWebSocketNetworkFrame:
class InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {
#client: WebSocketConnectionData['client']
constructor(args: { connection: WebSocketConnectionData }) {
super({ connection: args.connection })
this.#client = args.connection.client
}
public errorWith(reason?: unknown): void {
// ... collapsed code
this.#client.socket.dispatchEvent(errorEvent)
}
public passthrough() {
this.data.connection.server.connect()
}
}
WebSocketNetworkFramecannot be initialized withWebSocket(Client|Server)ConnectionProtocolimplementations #2707I'm a bit tentative about this approach.
WebSocketConnectionFoo.