Skip to content

Commit 6704fa0

Browse files
authored
fix: update @mswjs/interceptors to support WebSocket server protocol (#2528)
1 parent dce459e commit 6704fa0

File tree

7 files changed

+53
-17
lines changed

7 files changed

+53
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
"@bundled-es-modules/statuses": "^1.0.1",
242242
"@bundled-es-modules/tough-cookie": "^0.1.6",
243243
"@inquirer/confirm": "^5.0.0",
244-
"@mswjs/interceptors": "^0.38.7",
244+
"@mswjs/interceptors": "^0.39.1",
245245
"@open-draft/deferred-promise": "^2.2.0",
246246
"@open-draft/until": "^2.1.0",
247247
"@types/cookie": "^0.6.0",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/handlers/WebSocketHandler.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Emitter } from 'strict-event-emitter'
22
import { createRequestId } from '@mswjs/interceptors'
3-
import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
3+
import type {
4+
WebSocketClientConnectionProtocol,
5+
WebSocketConnectionData,
6+
WebSocketServerConnectionProtocol,
7+
} from '@mswjs/interceptors/WebSocket'
48
import {
59
type Match,
610
type Path,
@@ -18,7 +22,10 @@ export type WebSocketHandlerEventMap = {
1822
connection: [args: WebSocketHandlerConnection]
1923
}
2024

21-
export interface WebSocketHandlerConnection extends WebSocketConnectionData {
25+
export interface WebSocketHandlerConnection {
26+
client: WebSocketClientConnectionProtocol
27+
server: WebSocketServerConnectionProtocol
28+
info: WebSocketConnectionData['info']
2229
params: PathParams
2330
}
2431

@@ -67,7 +74,9 @@ export class WebSocketHandler {
6774
return args.parsedResult.match.matches
6875
}
6976

70-
public async run(connection: WebSocketConnectionData): Promise<boolean> {
77+
public async run(
78+
connection: Omit<WebSocketHandlerConnection, 'params'>,
79+
): Promise<boolean> {
7180
const parsedResult = this.parse({
7281
url: connection.client.url,
7382
})

src/core/ws/WebSocketClientManager.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {
22
WebSocketData,
3-
WebSocketClientConnection,
43
WebSocketClientConnectionProtocol,
4+
WebSocketClientEventMap,
55
} from '@mswjs/interceptors/WebSocket'
66
import { WebSocketClientStore } from './WebSocketClientStore'
77
import { WebSocketMemoryClientStore } from './WebSocketMemoryClientStore'
@@ -105,7 +105,9 @@ export class WebSocketClientManager {
105105
this.channel.postMessage({ type: 'db:update' })
106106
}
107107

108-
private async addClient(client: WebSocketClientConnection): Promise<void> {
108+
private async addClient(
109+
client: WebSocketClientConnectionProtocol,
110+
): Promise<void> {
109111
await this.store.add(client)
110112
// Sync the in-memory clients in this runtime with the
111113
// updated database. This pulls in all the stored clients.
@@ -119,7 +121,9 @@ export class WebSocketClientManager {
119121
* connection object because `addConnection()` is called only
120122
* for the opened connections in the same runtime.
121123
*/
122-
public async addConnection(client: WebSocketClientConnection): Promise<void> {
124+
public async addConnection(
125+
client: WebSocketClientConnectionProtocol,
126+
): Promise<void> {
123127
// Store this client in the map of clients created in this runtime.
124128
// This way, the manager can distinguish between this runtime clients
125129
// and extraneous runtime clients when synchronizing clients storage.
@@ -208,4 +212,30 @@ export class WebSocketRemoteClientConnection
208212
},
209213
} as WebSocketBroadcastChannelMessage)
210214
}
215+
216+
addEventListener<EventType extends keyof WebSocketClientEventMap>(
217+
_type: EventType,
218+
_listener: (
219+
this: WebSocket,
220+
event: WebSocketClientEventMap[EventType],
221+
) => void,
222+
_options?: AddEventListenerOptions | boolean,
223+
): void {
224+
throw new Error(
225+
'WebSocketRemoteClientConnection.addEventListener is not supported',
226+
)
227+
}
228+
229+
removeEventListener<EventType extends keyof WebSocketClientEventMap>(
230+
_event: EventType,
231+
_listener: (
232+
this: WebSocket,
233+
event: WebSocketClientEventMap[EventType],
234+
) => void,
235+
_options?: EventListenerOptions | boolean,
236+
): void {
237+
throw new Error(
238+
'WebSocketRemoteClientConnection.removeEventListener is not supported',
239+
)
240+
}
211241
}

test/browser/rest-api/response/body/body-stream.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('responds with a mocked ReadableStream response', async ({
2525

2626
chunks.push({
2727
text: decoder.decode(value),
28-
timestamp: Date.now(),
28+
timestamp: performance.now(),
2929
})
3030
}
3131
})

test/node/rest-api/response/body-stream.node.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test('supports delays when enqueuing chunks', async () => {
8181
response.on('data', (data) => {
8282
chunks.push({
8383
buffer: Buffer.from(data),
84-
timestamp: Date.now(),
84+
timestamp: performance.now(),
8585
})
8686
})
8787

test/typings/ws.test-d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ it('exposes root-level "client" APIs', () => {
5353

5454
link.addEventListener('connection', ({ client }) => {
5555
expectTypeOf(client.id).toBeString()
56-
expectTypeOf(client.socket).toEqualTypeOf<WebSocket>()
5756
expectTypeOf(client.url).toEqualTypeOf<URL>()
5857

5958
expectTypeOf(client.addEventListener).toBeFunction()
@@ -103,8 +102,6 @@ it('exposes root-level "server" APIs', () => {
103102
const link = ws.link('ws://localhost')
104103

105104
link.addEventListener('connection', ({ server }) => {
106-
expectTypeOf(server.socket).toEqualTypeOf<WebSocket>()
107-
108105
expectTypeOf(server.connect).toEqualTypeOf<() => void>()
109106
expectTypeOf(server.addEventListener).toBeFunction()
110107
expectTypeOf(server.send).toBeFunction()

0 commit comments

Comments
 (0)