Skip to content

Commit 8c633ad

Browse files
feat(WebSocket): add connection "info" to the "connection" event payload (#577)
Co-authored-by: Artem Zakharchenko <[email protected]>
1 parent efac32f commit 8c633ad

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,11 @@ intereceptor.on('connection', ({ client }) => {
322322

323323
The `connection` event exposes the following arguments:
324324

325-
| Name | Type | Description |
326-
| -------- | --------------------------------------------------------- | ---------------------------------------------------------------- |
327-
| `client` | [`WebSocketClientConnection`](#websocketclientconnection) | An object representing a connected WebSocket client instance. |
328-
| `server` | [`WebSocketServerConnection`](#websocketserverconnection) | An object representing the original WebSocket server connection. |
325+
| Name | Type | Description |
326+
| -------- | --------------------------------------------------------- | ----------------------------------------------------------------------------------- |
327+
| `client` | [`WebSocketClientConnection`](#websocketclientconnection) | An object representing a connected WebSocket client instance. |
328+
| `server` | [`WebSocketServerConnection`](#websocketserverconnection) | An object representing the original WebSocket server connection. |
329+
| `info` | `object` | Additional WebSocket connection information (like the original client `protocols`). |
329330

330331
### `WebSocketClientConnection`
331332

src/interceptors/WebSocket/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ export type WebSocketConnectionData = {
2828
* The original WebSocket server connection.
2929
*/
3030
server: WebSocketServerConnection
31+
32+
/**
33+
* The connection information.
34+
*/
35+
info: {
36+
/**
37+
* The protocols supported by the WebSocket client.
38+
*/
39+
protocols: string | Array<string> | undefined
40+
}
3141
}
3242

3343
/**
@@ -82,6 +92,9 @@ export class WebSocketInterceptor extends Interceptor<WebSocketEventMap> {
8292
transport,
8393
createConnection
8494
),
95+
info: {
96+
protocols,
97+
},
8598
})
8699
})
87100

test/modules/WebSocket/compliance/websocket.connection.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ it('emits the correct "connection" event on the interceptor', async () => {
5757
addEventListener: expect.any(Function),
5858
removeEventListener: expect.any(Function),
5959
}),
60+
info: {
61+
protocols: undefined,
62+
},
6063
})
6164
)
6265
})
@@ -74,3 +77,22 @@ it('does not connect to the actual WebSocket server by default', async () => {
7477
expect(connectionListener).toHaveBeenCalledTimes(1)
7578
expect(realConnectionListener).not.toHaveBeenCalled()
7679
})
80+
81+
it('includes connection information in the "connection" event payload', async () => {
82+
const connectionListener = vi.fn()
83+
interceptor.once('connection', connectionListener)
84+
85+
new WebSocket('wss://example.com', ['protocol1', 'protocol2'])
86+
await waitForNextTick()
87+
88+
expect(connectionListener).toHaveBeenCalledTimes(1)
89+
expect(connectionListener).toHaveBeenNthCalledWith(
90+
1,
91+
expect.objectContaining({
92+
info: {
93+
// Preserves the client protocols as-is.
94+
protocols: ['protocol1', 'protocol2'],
95+
},
96+
})
97+
)
98+
})

0 commit comments

Comments
 (0)