msw 2.5.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/lib/browser/index.d.mts +7 -6
  2. package/lib/browser/index.d.ts +7 -6
  3. package/lib/browser/index.js +29 -1
  4. package/lib/browser/index.js.map +1 -1
  5. package/lib/browser/index.mjs +29 -1
  6. package/lib/browser/index.mjs.map +1 -1
  7. package/lib/core/{GraphQLHandler-ClMB0BOy.d.mts → GraphQLHandler-Doool6Q_.d.mts} +1 -1
  8. package/lib/core/{GraphQLHandler-D6mLMXGZ.d.ts → GraphQLHandler-udzgBRPf.d.ts} +1 -1
  9. package/lib/core/{HttpResponse-vn-Pb4Bi.d.mts → HttpResponse-BLGmJolh.d.mts} +1 -1
  10. package/lib/core/{HttpResponse-DaYkf3ml.d.ts → HttpResponse-Cgbkdkje.d.ts} +1 -1
  11. package/lib/core/HttpResponse.d.mts +1 -1
  12. package/lib/core/HttpResponse.d.ts +1 -1
  13. package/lib/core/SetupApi.d.mts +15 -12
  14. package/lib/core/SetupApi.d.ts +15 -12
  15. package/lib/core/SetupApi.js +3 -1
  16. package/lib/core/SetupApi.js.map +1 -1
  17. package/lib/core/SetupApi.mjs +3 -1
  18. package/lib/core/SetupApi.mjs.map +1 -1
  19. package/lib/core/getResponse.d.mts +1 -1
  20. package/lib/core/getResponse.d.ts +1 -1
  21. package/lib/core/graphql.d.mts +2 -2
  22. package/lib/core/graphql.d.ts +2 -2
  23. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  24. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  25. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  26. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  27. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  28. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  29. package/lib/core/handlers/WebSocketHandler.d.mts +33 -0
  30. package/lib/core/handlers/WebSocketHandler.d.ts +33 -0
  31. package/lib/core/handlers/WebSocketHandler.js +120 -0
  32. package/lib/core/handlers/WebSocketHandler.js.map +1 -0
  33. package/lib/core/handlers/WebSocketHandler.mjs +102 -0
  34. package/lib/core/handlers/WebSocketHandler.mjs.map +1 -0
  35. package/lib/core/http.d.mts +1 -1
  36. package/lib/core/http.d.ts +1 -1
  37. package/lib/core/index.d.mts +5 -2
  38. package/lib/core/index.d.ts +5 -2
  39. package/lib/core/index.js +5 -1
  40. package/lib/core/index.js.map +1 -1
  41. package/lib/core/index.mjs +7 -1
  42. package/lib/core/index.mjs.map +1 -1
  43. package/lib/core/passthrough.d.mts +1 -1
  44. package/lib/core/passthrough.d.ts +1 -1
  45. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  46. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  47. package/lib/core/utils/executeHandlers.d.mts +1 -1
  48. package/lib/core/utils/executeHandlers.d.ts +1 -1
  49. package/lib/core/utils/executeHandlers.js +4 -0
  50. package/lib/core/utils/executeHandlers.js.map +1 -1
  51. package/lib/core/utils/executeHandlers.mjs +6 -0
  52. package/lib/core/utils/executeHandlers.mjs.map +1 -1
  53. package/lib/core/utils/handleRequest.d.mts +2 -2
  54. package/lib/core/utils/handleRequest.d.ts +2 -2
  55. package/lib/core/utils/handleRequest.js.map +1 -1
  56. package/lib/core/utils/handleRequest.mjs.map +1 -1
  57. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  58. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  59. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  60. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  61. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  62. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  63. package/lib/core/utils/logging/getTimestamp.d.mts +4 -1
  64. package/lib/core/utils/logging/getTimestamp.d.ts +4 -1
  65. package/lib/core/utils/logging/getTimestamp.js +6 -2
  66. package/lib/core/utils/logging/getTimestamp.js.map +1 -1
  67. package/lib/core/utils/logging/getTimestamp.mjs +6 -2
  68. package/lib/core/utils/logging/getTimestamp.mjs.map +1 -1
  69. package/lib/core/utils/matching/matchRequestUrl.d.mts +2 -1
  70. package/lib/core/utils/matching/matchRequestUrl.d.ts +2 -1
  71. package/lib/core/utils/matching/matchRequestUrl.js +4 -0
  72. package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
  73. package/lib/core/utils/matching/matchRequestUrl.mjs +4 -0
  74. package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
  75. package/lib/core/ws/WebSocketClientManager.d.mts +63 -0
  76. package/lib/core/ws/WebSocketClientManager.d.ts +63 -0
  77. package/lib/core/ws/WebSocketClientManager.js +149 -0
  78. package/lib/core/ws/WebSocketClientManager.js.map +1 -0
  79. package/lib/core/ws/WebSocketClientManager.mjs +129 -0
  80. package/lib/core/ws/WebSocketClientManager.mjs.map +1 -0
  81. package/lib/core/ws/WebSocketClientStore.d.mts +13 -0
  82. package/lib/core/ws/WebSocketClientStore.d.ts +13 -0
  83. package/lib/core/ws/WebSocketClientStore.js +26 -0
  84. package/lib/core/ws/WebSocketClientStore.js.map +1 -0
  85. package/lib/core/ws/WebSocketClientStore.mjs +6 -0
  86. package/lib/core/ws/WebSocketClientStore.mjs.map +1 -0
  87. package/lib/core/ws/WebSocketIndexedDBClientStore.d.mts +15 -0
  88. package/lib/core/ws/WebSocketIndexedDBClientStore.d.ts +15 -0
  89. package/lib/core/ws/WebSocketIndexedDBClientStore.js +130 -0
  90. package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -0
  91. package/lib/core/ws/WebSocketIndexedDBClientStore.mjs +110 -0
  92. package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -0
  93. package/lib/core/ws/WebSocketMemoryClientStore.d.mts +13 -0
  94. package/lib/core/ws/WebSocketMemoryClientStore.d.ts +13 -0
  95. package/lib/core/ws/WebSocketMemoryClientStore.js +41 -0
  96. package/lib/core/ws/WebSocketMemoryClientStore.js.map +1 -0
  97. package/lib/core/ws/WebSocketMemoryClientStore.mjs +21 -0
  98. package/lib/core/ws/WebSocketMemoryClientStore.mjs.map +1 -0
  99. package/lib/core/ws/handleWebSocketEvent.d.mts +19 -0
  100. package/lib/core/ws/handleWebSocketEvent.d.ts +19 -0
  101. package/lib/core/ws/handleWebSocketEvent.js +73 -0
  102. package/lib/core/ws/handleWebSocketEvent.js.map +1 -0
  103. package/lib/core/ws/handleWebSocketEvent.mjs +55 -0
  104. package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -0
  105. package/lib/core/ws/utils/attachWebSocketLogger.d.mts +12 -0
  106. package/lib/core/ws/utils/attachWebSocketLogger.d.ts +12 -0
  107. package/lib/core/ws/utils/attachWebSocketLogger.js +198 -0
  108. package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -0
  109. package/lib/core/ws/utils/attachWebSocketLogger.mjs +178 -0
  110. package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -0
  111. package/lib/core/ws/utils/getMessageLength.d.mts +11 -0
  112. package/lib/core/ws/utils/getMessageLength.d.ts +11 -0
  113. package/lib/core/ws/utils/getMessageLength.js +33 -0
  114. package/lib/core/ws/utils/getMessageLength.js.map +1 -0
  115. package/lib/core/ws/utils/getMessageLength.mjs +13 -0
  116. package/lib/core/ws/utils/getMessageLength.mjs.map +1 -0
  117. package/lib/core/ws/utils/getPublicData.d.mts +5 -0
  118. package/lib/core/ws/utils/getPublicData.d.ts +5 -0
  119. package/lib/core/ws/utils/getPublicData.js +36 -0
  120. package/lib/core/ws/utils/getPublicData.js.map +1 -0
  121. package/lib/core/ws/utils/getPublicData.mjs +16 -0
  122. package/lib/core/ws/utils/getPublicData.mjs.map +1 -0
  123. package/lib/core/ws/utils/truncateMessage.d.mts +3 -0
  124. package/lib/core/ws/utils/truncateMessage.d.ts +3 -0
  125. package/lib/core/ws/utils/truncateMessage.js +31 -0
  126. package/lib/core/ws/utils/truncateMessage.js.map +1 -0
  127. package/lib/core/ws/utils/truncateMessage.mjs +11 -0
  128. package/lib/core/ws/utils/truncateMessage.mjs.map +1 -0
  129. package/lib/core/ws/webSocketInterceptor.d.mts +5 -0
  130. package/lib/core/ws/webSocketInterceptor.d.ts +5 -0
  131. package/lib/core/ws/webSocketInterceptor.js +26 -0
  132. package/lib/core/ws/webSocketInterceptor.js.map +1 -0
  133. package/lib/core/ws/webSocketInterceptor.mjs +6 -0
  134. package/lib/core/ws/webSocketInterceptor.mjs.map +1 -0
  135. package/lib/core/ws.d.mts +75 -0
  136. package/lib/core/ws.d.ts +75 -0
  137. package/lib/core/ws.js +71 -0
  138. package/lib/core/ws.js.map +1 -0
  139. package/lib/core/ws.mjs +54 -0
  140. package/lib/core/ws.mjs.map +1 -0
  141. package/lib/iife/index.js +1413 -85
  142. package/lib/iife/index.js.map +1 -1
  143. package/lib/mockServiceWorker.js +1 -1
  144. package/lib/native/index.d.mts +6 -5
  145. package/lib/native/index.d.ts +6 -5
  146. package/lib/native/index.js +22 -4
  147. package/lib/native/index.js.map +1 -1
  148. package/lib/native/index.mjs +22 -4
  149. package/lib/native/index.mjs.map +1 -1
  150. package/lib/node/index.d.mts +8 -7
  151. package/lib/node/index.d.ts +8 -7
  152. package/lib/node/index.js +22 -4
  153. package/lib/node/index.js.map +1 -1
  154. package/lib/node/index.mjs +22 -4
  155. package/lib/node/index.mjs.map +1 -1
  156. package/package.json +10 -1
  157. package/src/browser/setupWorker/glossary.ts +10 -10
  158. package/src/browser/setupWorker/setupWorker.ts +32 -3
  159. package/src/browser/setupWorker/start/createRequestListener.ts +7 -1
  160. package/src/browser/setupWorker/start/createStartHandler.ts +5 -0
  161. package/src/browser/setupWorker/stop/createStop.ts +6 -0
  162. package/src/core/SetupApi.ts +28 -20
  163. package/src/core/handlers/WebSocketHandler.ts +142 -0
  164. package/src/core/index.ts +11 -1
  165. package/src/core/utils/executeHandlers.ts +6 -2
  166. package/src/core/utils/handleRequest.ts +1 -1
  167. package/src/core/utils/logging/getTimestamp.test.ts +20 -6
  168. package/src/core/utils/logging/getTimestamp.ts +11 -6
  169. package/src/core/utils/matching/matchRequestUrl.test.ts +44 -0
  170. package/src/core/utils/matching/matchRequestUrl.ts +4 -0
  171. package/src/core/ws/WebSocketClientManager.test.ts +164 -0
  172. package/src/core/ws/WebSocketClientManager.ts +211 -0
  173. package/src/core/ws/WebSocketClientStore.ts +14 -0
  174. package/src/core/ws/WebSocketIndexedDBClientStore.ts +145 -0
  175. package/src/core/ws/WebSocketMemoryClientStore.ts +27 -0
  176. package/src/core/ws/handleWebSocketEvent.ts +82 -0
  177. package/src/core/ws/utils/attachWebSocketLogger.ts +259 -0
  178. package/src/core/ws/utils/getMessageLength.test.ts +16 -0
  179. package/src/core/ws/utils/getMessageLength.ts +19 -0
  180. package/src/core/ws/utils/getPublicData.test.ts +38 -0
  181. package/src/core/ws/utils/getPublicData.ts +17 -0
  182. package/src/core/ws/utils/truncateMessage.test.ts +12 -0
  183. package/src/core/ws/utils/truncateMessage.ts +9 -0
  184. package/src/core/ws/webSocketInterceptor.ts +3 -0
  185. package/src/core/ws.test.ts +23 -0
  186. package/src/core/ws.ts +166 -0
  187. package/src/node/SetupServerApi.ts +8 -7
  188. package/src/node/SetupServerCommonApi.ts +29 -5
  189. package/src/node/glossary.ts +5 -7
  190. package/src/node/setupServer.ts +2 -1
@@ -0,0 +1,164 @@
1
+ // @vitest-environment node-websocket
2
+ import { setMaxListeners } from 'node:events'
3
+ import {
4
+ WebSocketClientConnection,
5
+ WebSocketData,
6
+ WebSocketTransport,
7
+ } from '@mswjs/interceptors/WebSocket'
8
+ import {
9
+ WebSocketClientManager,
10
+ WebSocketBroadcastChannelMessage,
11
+ } from './WebSocketClientManager'
12
+
13
+ const channel = new BroadcastChannel('test:channel')
14
+
15
+ /**
16
+ * @note Increase the number of maximum event listeners
17
+ * because the same channel is shared between different
18
+ * manager instances in different tests.
19
+ */
20
+ setMaxListeners(Number.MAX_SAFE_INTEGER, channel)
21
+
22
+ vi.spyOn(channel, 'postMessage')
23
+
24
+ const socket = new WebSocket('ws://localhost')
25
+
26
+ class TestWebSocketTransport extends EventTarget implements WebSocketTransport {
27
+ send(_data: WebSocketData): void {}
28
+ close(_code?: number | undefined, _reason?: string | undefined): void {}
29
+ }
30
+
31
+ afterEach(() => {
32
+ vi.resetAllMocks()
33
+ })
34
+
35
+ it('adds a client from this runtime to the list of clients', async () => {
36
+ const manager = new WebSocketClientManager(channel)
37
+ const connection = new WebSocketClientConnection(
38
+ socket,
39
+ new TestWebSocketTransport(),
40
+ )
41
+
42
+ await manager.addConnection(connection)
43
+
44
+ // Must add the client to the list of clients.
45
+ expect(Array.from(manager.clients.values())).toEqual([connection])
46
+ })
47
+
48
+ it('adds multiple clients from this runtime to the list of clients', async () => {
49
+ const manager = new WebSocketClientManager(channel)
50
+ const connectionOne = new WebSocketClientConnection(
51
+ socket,
52
+ new TestWebSocketTransport(),
53
+ )
54
+ await manager.addConnection(connectionOne)
55
+
56
+ // Must add the client to the list of clients.
57
+ expect(Array.from(manager.clients.values())).toEqual([connectionOne])
58
+
59
+ const connectionTwo = new WebSocketClientConnection(
60
+ socket,
61
+ new TestWebSocketTransport(),
62
+ )
63
+ await manager.addConnection(connectionTwo)
64
+
65
+ // Must add the new cilent to the list as well.
66
+ expect(Array.from(manager.clients.values())).toEqual([
67
+ connectionOne,
68
+ connectionTwo,
69
+ ])
70
+ })
71
+
72
+ it('replays a "send" event coming from another runtime', async () => {
73
+ const manager = new WebSocketClientManager(channel)
74
+ const connection = new WebSocketClientConnection(
75
+ socket,
76
+ new TestWebSocketTransport(),
77
+ )
78
+ await manager.addConnection(connection)
79
+ vi.spyOn(connection, 'send')
80
+
81
+ // Emulate another runtime signaling this connection to receive data.
82
+ channel.dispatchEvent(
83
+ new MessageEvent<WebSocketBroadcastChannelMessage>('message', {
84
+ data: {
85
+ type: 'extraneous:send',
86
+ payload: {
87
+ clientId: connection.id,
88
+ data: 'hello',
89
+ },
90
+ },
91
+ }),
92
+ )
93
+
94
+ await vi.waitFor(() => {
95
+ // Must execute the requested operation on the connection.
96
+ expect(connection.send).toHaveBeenCalledWith('hello')
97
+ expect(connection.send).toHaveBeenCalledTimes(1)
98
+ })
99
+ })
100
+
101
+ it('replays a "close" event coming from another runtime', async () => {
102
+ const manager = new WebSocketClientManager(channel)
103
+ const connection = new WebSocketClientConnection(
104
+ socket,
105
+ new TestWebSocketTransport(),
106
+ )
107
+ await manager.addConnection(connection)
108
+ vi.spyOn(connection, 'close')
109
+
110
+ // Emulate another runtime signaling this connection to close.
111
+ channel.dispatchEvent(
112
+ new MessageEvent<WebSocketBroadcastChannelMessage>('message', {
113
+ data: {
114
+ type: 'extraneous:close',
115
+ payload: {
116
+ clientId: connection.id,
117
+ code: 1000,
118
+ reason: 'Normal closure',
119
+ },
120
+ },
121
+ }),
122
+ )
123
+
124
+ await vi.waitFor(() => {
125
+ // Must execute the requested operation on the connection.
126
+ expect(connection.close).toHaveBeenCalledWith(1000, 'Normal closure')
127
+ expect(connection.close).toHaveBeenCalledTimes(1)
128
+ })
129
+ })
130
+
131
+ it('removes the extraneous message listener when the connection closes', async () => {
132
+ const manager = new WebSocketClientManager(channel)
133
+ const transport = new TestWebSocketTransport()
134
+ const connection = new WebSocketClientConnection(socket, transport)
135
+ vi.spyOn(connection, 'close').mockImplementationOnce(() => {
136
+ /**
137
+ * @note This is a nasty hack so we don't have to uncouple
138
+ * the connection from transport. Creating a mock transport
139
+ * is difficult because it relies on the `WebSocketOverride` class.
140
+ * All we care here is that closing the connection triggers
141
+ * the transport closure, which it always does.
142
+ */
143
+ transport.dispatchEvent(new Event('close'))
144
+ })
145
+ vi.spyOn(connection, 'send')
146
+
147
+ await manager.addConnection(connection)
148
+ connection.close()
149
+
150
+ // Signals from other runtimes have no effect on the closed connection.
151
+ channel.dispatchEvent(
152
+ new MessageEvent<WebSocketBroadcastChannelMessage>('message', {
153
+ data: {
154
+ type: 'extraneous:send',
155
+ payload: {
156
+ clientId: connection.id,
157
+ data: 'hello',
158
+ },
159
+ },
160
+ }),
161
+ )
162
+
163
+ expect(connection.send).not.toHaveBeenCalled()
164
+ })
@@ -0,0 +1,211 @@
1
+ import type {
2
+ WebSocketData,
3
+ WebSocketClientConnection,
4
+ WebSocketClientConnectionProtocol,
5
+ } from '@mswjs/interceptors/WebSocket'
6
+ import { WebSocketClientStore } from './WebSocketClientStore'
7
+ import { WebSocketMemoryClientStore } from './WebSocketMemoryClientStore'
8
+ import { WebSocketIndexedDBClientStore } from './WebSocketIndexedDBClientStore'
9
+
10
+ export type WebSocketBroadcastChannelMessage =
11
+ | {
12
+ type: 'extraneous:send'
13
+ payload: {
14
+ clientId: string
15
+ data: WebSocketData
16
+ }
17
+ }
18
+ | {
19
+ type: 'extraneous:close'
20
+ payload: {
21
+ clientId: string
22
+ code?: number
23
+ reason?: string
24
+ }
25
+ }
26
+
27
+ /**
28
+ * A manager responsible for accumulating WebSocket client
29
+ * connections across different browser runtimes.
30
+ */
31
+ export class WebSocketClientManager {
32
+ private store: WebSocketClientStore
33
+ private runtimeClients: Map<string, WebSocketClientConnectionProtocol>
34
+ private allClients: Set<WebSocketClientConnectionProtocol>
35
+
36
+ constructor(private channel: BroadcastChannel) {
37
+ // Store the clients in the IndexedDB in the browser,
38
+ // otherwise, store the clients in memory.
39
+ this.store =
40
+ typeof indexedDB !== 'undefined'
41
+ ? new WebSocketIndexedDBClientStore()
42
+ : new WebSocketMemoryClientStore()
43
+
44
+ this.runtimeClients = new Map()
45
+ this.allClients = new Set()
46
+
47
+ this.channel.addEventListener('message', (message) => {
48
+ if (message.data?.type === 'db:update') {
49
+ this.flushDatabaseToMemory()
50
+ }
51
+ })
52
+
53
+ if (typeof window !== 'undefined') {
54
+ window.addEventListener('message', async (message) => {
55
+ if (message.data?.type === 'msw/worker:stop') {
56
+ await this.removeRuntimeClients()
57
+ }
58
+ })
59
+ }
60
+ }
61
+
62
+ private async flushDatabaseToMemory() {
63
+ const storedClients = await this.store.getAll()
64
+
65
+ this.allClients = new Set(
66
+ storedClients.map((client) => {
67
+ const runtimeClient = this.runtimeClients.get(client.id)
68
+
69
+ /**
70
+ * @note For clients originating in this runtime, use their
71
+ * direct references. No need to wrap them in a remote connection.
72
+ */
73
+ if (runtimeClient) {
74
+ return runtimeClient
75
+ }
76
+
77
+ return new WebSocketRemoteClientConnection(
78
+ client.id,
79
+ new URL(client.url),
80
+ this.channel,
81
+ )
82
+ }),
83
+ )
84
+ }
85
+
86
+ private async removeRuntimeClients(): Promise<void> {
87
+ await this.store.deleteMany(Array.from(this.runtimeClients.keys()))
88
+ this.runtimeClients.clear()
89
+ await this.flushDatabaseToMemory()
90
+ this.notifyOthersAboutDatabaseUpdate()
91
+ }
92
+
93
+ /**
94
+ * All active WebSocket client connections.
95
+ */
96
+ get clients(): Set<WebSocketClientConnectionProtocol> {
97
+ return this.allClients
98
+ }
99
+
100
+ /**
101
+ * Notify other runtimes about the database update
102
+ * using the shared `BroadcastChannel` instance.
103
+ */
104
+ private notifyOthersAboutDatabaseUpdate(): void {
105
+ this.channel.postMessage({ type: 'db:update' })
106
+ }
107
+
108
+ private async addClient(client: WebSocketClientConnection): Promise<void> {
109
+ await this.store.add(client)
110
+ // Sync the in-memory clients in this runtime with the
111
+ // updated database. This pulls in all the stored clients.
112
+ await this.flushDatabaseToMemory()
113
+ this.notifyOthersAboutDatabaseUpdate()
114
+ }
115
+
116
+ /**
117
+ * Adds the given `WebSocket` client connection to the set
118
+ * of all connections. The given connection is always the complete
119
+ * connection object because `addConnection()` is called only
120
+ * for the opened connections in the same runtime.
121
+ */
122
+ public async addConnection(client: WebSocketClientConnection): Promise<void> {
123
+ // Store this client in the map of clients created in this runtime.
124
+ // This way, the manager can distinguish between this runtime clients
125
+ // and extraneous runtime clients when synchronizing clients storage.
126
+ this.runtimeClients.set(client.id, client)
127
+
128
+ // Add the new client to the storage.
129
+ await this.addClient(client)
130
+
131
+ // Handle the incoming BroadcastChannel messages from other runtimes
132
+ // that attempt to control this runtime (via a remote connection wrapper).
133
+ // E.g. another runtime calling `client.send()` for the client in this runtime.
134
+ const handleExtraneousMessage = (
135
+ message: MessageEvent<WebSocketBroadcastChannelMessage>,
136
+ ) => {
137
+ const { type, payload } = message.data
138
+
139
+ // Ignore broadcasted messages for other clients.
140
+ if (
141
+ typeof payload === 'object' &&
142
+ 'clientId' in payload &&
143
+ payload.clientId !== client.id
144
+ ) {
145
+ return
146
+ }
147
+
148
+ switch (type) {
149
+ case 'extraneous:send': {
150
+ client.send(payload.data)
151
+ break
152
+ }
153
+
154
+ case 'extraneous:close': {
155
+ client.close(payload.code, payload.reason)
156
+ break
157
+ }
158
+ }
159
+ }
160
+
161
+ const abortController = new AbortController()
162
+
163
+ this.channel.addEventListener('message', handleExtraneousMessage, {
164
+ signal: abortController.signal,
165
+ })
166
+
167
+ // Once closed, this connection cannot be operated on.
168
+ // This must include the extraneous runtimes as well.
169
+ client.addEventListener('close', () => abortController.abort(), {
170
+ once: true,
171
+ })
172
+ }
173
+ }
174
+
175
+ /**
176
+ * A wrapper class to operate with WebSocket client connections
177
+ * from other runtimes. This class maintains 1-1 public API
178
+ * compatibility to the `WebSocketClientConnection` but relies
179
+ * on the given `BroadcastChannel` to communicate instructions
180
+ * with the client connections from other runtimes.
181
+ */
182
+ export class WebSocketRemoteClientConnection
183
+ implements WebSocketClientConnectionProtocol
184
+ {
185
+ constructor(
186
+ public readonly id: string,
187
+ public readonly url: URL,
188
+ private channel: BroadcastChannel,
189
+ ) {}
190
+
191
+ send(data: WebSocketData): void {
192
+ this.channel.postMessage({
193
+ type: 'extraneous:send',
194
+ payload: {
195
+ clientId: this.id,
196
+ data,
197
+ },
198
+ } as WebSocketBroadcastChannelMessage)
199
+ }
200
+
201
+ close(code?: number | undefined, reason?: string | undefined): void {
202
+ this.channel.postMessage({
203
+ type: 'extraneous:close',
204
+ payload: {
205
+ clientId: this.id,
206
+ code,
207
+ reason,
208
+ },
209
+ } as WebSocketBroadcastChannelMessage)
210
+ }
211
+ }
@@ -0,0 +1,14 @@
1
+ import type { WebSocketClientConnectionProtocol } from '@mswjs/interceptors/WebSocket'
2
+
3
+ export interface SerializedWebSocketClient {
4
+ id: string
5
+ url: string
6
+ }
7
+
8
+ export abstract class WebSocketClientStore {
9
+ public abstract add(client: WebSocketClientConnectionProtocol): Promise<void>
10
+
11
+ public abstract getAll(): Promise<Array<SerializedWebSocketClient>>
12
+
13
+ public abstract deleteMany(clientIds: Array<string>): Promise<void>
14
+ }
@@ -0,0 +1,145 @@
1
+ import { DeferredPromise } from '@open-draft/deferred-promise'
2
+ import { WebSocketClientConnectionProtocol } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket'
3
+ import {
4
+ type SerializedWebSocketClient,
5
+ WebSocketClientStore,
6
+ } from './WebSocketClientStore'
7
+
8
+ const DB_NAME = 'msw-websocket-clients'
9
+ const DB_STORE_NAME = 'clients'
10
+
11
+ export class WebSocketIndexedDBClientStore implements WebSocketClientStore {
12
+ private db: Promise<IDBDatabase>
13
+
14
+ constructor() {
15
+ this.db = this.createDatabase()
16
+ }
17
+
18
+ public async add(client: WebSocketClientConnectionProtocol): Promise<void> {
19
+ const promise = new DeferredPromise<void>()
20
+ const store = await this.getStore()
21
+
22
+ /**
23
+ * @note Use `.put()` instead of `.add()` to allow setting clients
24
+ * that already exist in the database. This can happen if a single page
25
+ * has multiple event handlers. Each handler will receive the "connection"
26
+ * event in parallel, and try to set that WebSocket client in the database.
27
+ */
28
+ const request = store.put({
29
+ id: client.id,
30
+ url: client.url.href,
31
+ } satisfies SerializedWebSocketClient)
32
+
33
+ request.onsuccess = () => {
34
+ promise.resolve()
35
+ }
36
+ request.onerror = () => {
37
+ // eslint-disable-next-line no-console
38
+ console.error(request.error)
39
+ promise.reject(
40
+ new Error(
41
+ `Failed to add WebSocket client "${client.id}". There is likely an additional output above.`,
42
+ ),
43
+ )
44
+ }
45
+
46
+ return promise
47
+ }
48
+
49
+ public async getAll(): Promise<Array<SerializedWebSocketClient>> {
50
+ const promise = new DeferredPromise<Array<SerializedWebSocketClient>>()
51
+ const store = await this.getStore()
52
+ const request = store.getAll() as IDBRequest<
53
+ Array<SerializedWebSocketClient>
54
+ >
55
+
56
+ request.onsuccess = () => {
57
+ promise.resolve(request.result)
58
+ }
59
+ request.onerror = () => {
60
+ // eslint-disable-next-line no-console
61
+ console.log(request.error)
62
+ promise.reject(
63
+ new Error(
64
+ `Failed to get all WebSocket clients. There is likely an additional output above.`,
65
+ ),
66
+ )
67
+ }
68
+
69
+ return promise
70
+ }
71
+
72
+ public async deleteMany(clientIds: Array<string>): Promise<void> {
73
+ const promise = new DeferredPromise<void>()
74
+ const store = await this.getStore()
75
+
76
+ for (const clientId of clientIds) {
77
+ store.delete(clientId)
78
+ }
79
+
80
+ store.transaction.oncomplete = () => {
81
+ promise.resolve()
82
+ }
83
+ store.transaction.onerror = () => {
84
+ // eslint-disable-next-line no-console
85
+ console.error(store.transaction.error)
86
+ promise.reject(
87
+ new Error(
88
+ `Failed to delete WebSocket clients [${clientIds.join(', ')}]. There is likely an additional output above.`,
89
+ ),
90
+ )
91
+ }
92
+
93
+ return promise
94
+ }
95
+
96
+ private async createDatabase(): Promise<IDBDatabase> {
97
+ const promise = new DeferredPromise<IDBDatabase>()
98
+ const request = indexedDB.open(DB_NAME, 1)
99
+
100
+ request.onsuccess = ({ currentTarget }) => {
101
+ const db = Reflect.get(currentTarget!, 'result') as IDBDatabase
102
+
103
+ if (db.objectStoreNames.contains(DB_STORE_NAME)) {
104
+ return promise.resolve(db)
105
+ }
106
+ }
107
+
108
+ request.onupgradeneeded = async ({ currentTarget }) => {
109
+ const db = Reflect.get(currentTarget!, 'result') as IDBDatabase
110
+ if (db.objectStoreNames.contains(DB_STORE_NAME)) {
111
+ return
112
+ }
113
+
114
+ const store = db.createObjectStore(DB_STORE_NAME, { keyPath: 'id' })
115
+ store.transaction.oncomplete = () => {
116
+ promise.resolve(db)
117
+ }
118
+ store.transaction.onerror = () => {
119
+ // eslint-disable-next-line no-console
120
+ console.error(store.transaction.error)
121
+ promise.reject(
122
+ new Error(
123
+ 'Failed to create WebSocket client store. There is likely an additional output above.',
124
+ ),
125
+ )
126
+ }
127
+ }
128
+ request.onerror = () => {
129
+ // eslint-disable-next-line no-console
130
+ console.error(request.error)
131
+ promise.reject(
132
+ new Error(
133
+ 'Failed to open an IndexedDB database. There is likely an additional output above.',
134
+ ),
135
+ )
136
+ }
137
+
138
+ return promise
139
+ }
140
+
141
+ private async getStore(): Promise<IDBObjectStore> {
142
+ const db = await this.db
143
+ return db.transaction(DB_STORE_NAME, 'readwrite').objectStore(DB_STORE_NAME)
144
+ }
145
+ }
@@ -0,0 +1,27 @@
1
+ import { WebSocketClientConnectionProtocol } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket'
2
+ import {
3
+ SerializedWebSocketClient,
4
+ WebSocketClientStore,
5
+ } from './WebSocketClientStore'
6
+
7
+ export class WebSocketMemoryClientStore implements WebSocketClientStore {
8
+ private store: Map<string, SerializedWebSocketClient>
9
+
10
+ constructor() {
11
+ this.store = new Map()
12
+ }
13
+
14
+ public async add(client: WebSocketClientConnectionProtocol): Promise<void> {
15
+ this.store.set(client.id, { id: client.id, url: client.url.href })
16
+ }
17
+
18
+ public getAll(): Promise<Array<SerializedWebSocketClient>> {
19
+ return Promise.resolve(Array.from(this.store.values()))
20
+ }
21
+
22
+ public async deleteMany(clientIds: Array<string>): Promise<void> {
23
+ for (const clientId of clientIds) {
24
+ this.store.delete(clientId)
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,82 @@
1
+ import type { WebSocketConnectionData } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket'
2
+ import { RequestHandler } from '../handlers/RequestHandler'
3
+ import { WebSocketHandler, kDispatchEvent } from '../handlers/WebSocketHandler'
4
+ import { webSocketInterceptor } from './webSocketInterceptor'
5
+ import {
6
+ onUnhandledRequest,
7
+ UnhandledRequestStrategy,
8
+ } from '../utils/request/onUnhandledRequest'
9
+
10
+ interface HandleWebSocketEventOptions {
11
+ getUnhandledRequestStrategy: () => UnhandledRequestStrategy
12
+ getHandlers: () => Array<RequestHandler | WebSocketHandler>
13
+ onMockedConnection: (connection: WebSocketConnectionData) => void
14
+ onPassthroughConnection: (onnection: WebSocketConnectionData) => void
15
+ }
16
+
17
+ export function handleWebSocketEvent(options: HandleWebSocketEventOptions) {
18
+ webSocketInterceptor.on('connection', async (connection) => {
19
+ const handlers = options.getHandlers()
20
+
21
+ const connectionEvent = new MessageEvent('connection', {
22
+ data: connection,
23
+ })
24
+
25
+ // First, filter only those WebSocket handlers that
26
+ // match the "ws.link()" endpoint predicate. Don't dispatch
27
+ // anything yet so the logger can be attached to the connection
28
+ // before it potentially sends events.
29
+ const matchingHandlers: Array<WebSocketHandler> = []
30
+
31
+ for (const handler of handlers) {
32
+ if (
33
+ handler instanceof WebSocketHandler &&
34
+ handler.predicate({
35
+ event: connectionEvent,
36
+ parsedResult: handler.parse({
37
+ event: connectionEvent,
38
+ }),
39
+ })
40
+ ) {
41
+ matchingHandlers.push(handler)
42
+ }
43
+ }
44
+
45
+ if (matchingHandlers.length > 0) {
46
+ options?.onMockedConnection(connection)
47
+
48
+ // Iterate over the handlers and forward the connection
49
+ // event to WebSocket event handlers. This is equivalent
50
+ // to dispatching that event onto multiple listeners.
51
+ for (const handler of matchingHandlers) {
52
+ handler[kDispatchEvent](connectionEvent)
53
+ }
54
+ } else {
55
+ // Construct a request representing this WebSocket connection.
56
+ const request = new Request(connection.client.url, {
57
+ headers: {
58
+ upgrade: 'websocket',
59
+ connection: 'upgrade',
60
+ },
61
+ })
62
+ await onUnhandledRequest(
63
+ request,
64
+ options.getUnhandledRequestStrategy(),
65
+ ).catch((error) => {
66
+ const errorEvent = new Event('error')
67
+ Object.defineProperty(errorEvent, 'cause', {
68
+ enumerable: true,
69
+ configurable: false,
70
+ value: error,
71
+ })
72
+ connection.client.socket.dispatchEvent(errorEvent)
73
+ })
74
+
75
+ options?.onPassthroughConnection(connection)
76
+
77
+ // If none of the "ws" handlers matched,
78
+ // establish the WebSocket connection as-is.
79
+ connection.server.connect()
80
+ }
81
+ })
82
+ }