msw 2.3.0-ws.rc-6 → 2.3.1

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 (208) hide show
  1. package/README.md +3 -9
  2. package/cli/init.js +0 -0
  3. package/config/scripts/postinstall.js +0 -0
  4. package/lib/browser/index.d.mts +6 -7
  5. package/lib/browser/index.d.ts +6 -7
  6. package/lib/browser/index.js +156 -83
  7. package/lib/browser/index.js.map +1 -1
  8. package/lib/browser/index.mjs +156 -83
  9. package/lib/browser/index.mjs.map +1 -1
  10. package/lib/core/{GraphQLHandler-DGyzNtRQ.d.ts → GraphQLHandler-COiPfZ8k.d.ts} +1 -1
  11. package/lib/core/{GraphQLHandler-CzcXr7At.d.mts → GraphQLHandler-Dq_WRbKe.d.mts} +1 -1
  12. package/lib/core/{HttpResponse-BOzDwzB6.d.ts → HttpResponse-B07UKAkU.d.ts} +2 -2
  13. package/lib/core/{HttpResponse-CSyJ1cbO.d.mts → HttpResponse-C7niBMwb.d.mts} +2 -2
  14. package/lib/core/HttpResponse.d.mts +1 -1
  15. package/lib/core/HttpResponse.d.ts +1 -1
  16. package/lib/core/SetupApi.d.mts +12 -15
  17. package/lib/core/SetupApi.d.ts +12 -15
  18. package/lib/core/SetupApi.js +1 -3
  19. package/lib/core/SetupApi.js.map +1 -1
  20. package/lib/core/SetupApi.mjs +1 -3
  21. package/lib/core/SetupApi.mjs.map +1 -1
  22. package/lib/core/getResponse.d.mts +1 -1
  23. package/lib/core/getResponse.d.ts +1 -1
  24. package/lib/core/graphql.d.mts +2 -2
  25. package/lib/core/graphql.d.ts +2 -2
  26. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  27. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  28. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  29. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  30. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  31. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  32. package/lib/core/http.d.mts +1 -1
  33. package/lib/core/http.d.ts +1 -1
  34. package/lib/core/index.d.mts +2 -5
  35. package/lib/core/index.d.ts +2 -5
  36. package/lib/core/index.js +1 -5
  37. package/lib/core/index.js.map +1 -1
  38. package/lib/core/index.mjs +1 -7
  39. package/lib/core/index.mjs.map +1 -1
  40. package/lib/core/passthrough.d.mts +1 -1
  41. package/lib/core/passthrough.d.ts +1 -1
  42. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  43. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  44. package/lib/core/utils/executeHandlers.d.mts +1 -1
  45. package/lib/core/utils/executeHandlers.d.ts +1 -1
  46. package/lib/core/utils/executeHandlers.js +0 -4
  47. package/lib/core/utils/executeHandlers.js.map +1 -1
  48. package/lib/core/utils/executeHandlers.mjs +0 -6
  49. package/lib/core/utils/executeHandlers.mjs.map +1 -1
  50. package/lib/core/utils/handleRequest.d.mts +2 -2
  51. package/lib/core/utils/handleRequest.d.ts +2 -2
  52. package/lib/core/utils/handleRequest.js.map +1 -1
  53. package/lib/core/utils/handleRequest.mjs.map +1 -1
  54. package/lib/core/utils/internal/Disposable.d.mts +2 -2
  55. package/lib/core/utils/internal/Disposable.d.ts +2 -2
  56. package/lib/core/utils/internal/Disposable.js +5 -2
  57. package/lib/core/utils/internal/Disposable.js.map +1 -1
  58. package/lib/core/utils/internal/Disposable.mjs +5 -2
  59. package/lib/core/utils/internal/Disposable.mjs.map +1 -1
  60. package/lib/core/utils/internal/devUtils.d.mts +10 -1
  61. package/lib/core/utils/internal/devUtils.d.ts +10 -1
  62. package/lib/core/utils/internal/devUtils.js +7 -0
  63. package/lib/core/utils/internal/devUtils.js.map +1 -1
  64. package/lib/core/utils/internal/devUtils.mjs +7 -0
  65. package/lib/core/utils/internal/devUtils.mjs.map +1 -1
  66. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  67. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  68. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  69. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  70. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  71. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  72. package/lib/core/utils/logging/getTimestamp.d.mts +1 -4
  73. package/lib/core/utils/logging/getTimestamp.d.ts +1 -4
  74. package/lib/core/utils/logging/getTimestamp.js +2 -6
  75. package/lib/core/utils/logging/getTimestamp.js.map +1 -1
  76. package/lib/core/utils/logging/getTimestamp.mjs +2 -6
  77. package/lib/core/utils/logging/getTimestamp.mjs.map +1 -1
  78. package/lib/core/utils/matching/matchRequestUrl.d.mts +1 -2
  79. package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -2
  80. package/lib/core/utils/matching/matchRequestUrl.js +0 -4
  81. package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
  82. package/lib/core/utils/matching/matchRequestUrl.mjs +0 -4
  83. package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
  84. package/lib/core/utils/matching/normalizePath.d.mts +1 -0
  85. package/lib/core/utils/matching/normalizePath.d.ts +1 -0
  86. package/lib/core/utils/matching/normalizePath.js.map +1 -1
  87. package/lib/core/utils/matching/normalizePath.mjs.map +1 -1
  88. package/lib/core/utils/request/onUnhandledRequest.js +3 -3
  89. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
  90. package/lib/core/utils/request/onUnhandledRequest.mjs +4 -4
  91. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
  92. package/lib/core/utils/url/cleanUrl.d.mts +2 -1
  93. package/lib/core/utils/url/cleanUrl.d.ts +2 -1
  94. package/lib/core/utils/url/cleanUrl.js +3 -0
  95. package/lib/core/utils/url/cleanUrl.js.map +1 -1
  96. package/lib/core/utils/url/cleanUrl.mjs +3 -0
  97. package/lib/core/utils/url/cleanUrl.mjs.map +1 -1
  98. package/lib/iife/index.js +180 -1103
  99. package/lib/iife/index.js.map +1 -1
  100. package/lib/mockServiceWorker.js +1 -1
  101. package/lib/native/index.d.mts +5 -6
  102. package/lib/native/index.d.ts +5 -6
  103. package/lib/native/index.js +5 -13
  104. package/lib/native/index.js.map +1 -1
  105. package/lib/native/index.mjs +6 -14
  106. package/lib/native/index.mjs.map +1 -1
  107. package/lib/node/index.d.mts +7 -8
  108. package/lib/node/index.d.ts +7 -8
  109. package/lib/node/index.js +5 -13
  110. package/lib/node/index.js.map +1 -1
  111. package/lib/node/index.mjs +6 -14
  112. package/lib/node/index.mjs.map +1 -1
  113. package/package.json +36 -29
  114. package/src/browser/setupWorker/glossary.ts +10 -10
  115. package/src/browser/setupWorker/setupWorker.ts +3 -34
  116. package/src/browser/setupWorker/stop/createStop.ts +0 -4
  117. package/src/core/SetupApi.ts +20 -28
  118. package/src/core/index.ts +1 -8
  119. package/src/core/utils/executeHandlers.ts +2 -6
  120. package/src/core/utils/handleRequest.ts +2 -1
  121. package/src/core/utils/internal/Disposable.ts +6 -3
  122. package/src/core/utils/internal/devUtils.test.ts +21 -0
  123. package/src/core/utils/internal/devUtils.ts +13 -0
  124. package/src/core/utils/logging/getTimestamp.test.ts +6 -20
  125. package/src/core/utils/logging/getTimestamp.ts +6 -11
  126. package/src/core/utils/matching/matchRequestUrl.test.ts +8 -41
  127. package/src/core/utils/matching/matchRequestUrl.ts +0 -4
  128. package/src/core/utils/matching/normalizePath.test.ts +7 -1
  129. package/src/core/utils/matching/normalizePath.ts +1 -0
  130. package/src/core/utils/request/onUnhandledRequest.test.ts +30 -4
  131. package/src/core/utils/request/onUnhandledRequest.ts +4 -4
  132. package/src/core/utils/url/cleanUrl.test.ts +8 -3
  133. package/src/core/utils/url/cleanUrl.ts +9 -1
  134. package/src/core/utils/url/getAbsoluteUrl.node.test.ts +3 -3
  135. package/src/core/utils/url/getAbsoluteUrl.test.ts +5 -5
  136. package/src/core/utils/url/isAbsoluteUrl.test.ts +7 -7
  137. package/src/node/SetupServerApi.ts +7 -8
  138. package/src/node/SetupServerCommonApi.ts +8 -15
  139. package/src/node/glossary.ts +7 -5
  140. package/src/node/setupServer.ts +1 -2
  141. package/lib/core/handlers/WebSocketHandler.d.mts +0 -32
  142. package/lib/core/handlers/WebSocketHandler.d.ts +0 -32
  143. package/lib/core/handlers/WebSocketHandler.js +0 -62
  144. package/lib/core/handlers/WebSocketHandler.js.map +0 -1
  145. package/lib/core/handlers/WebSocketHandler.mjs +0 -44
  146. package/lib/core/handlers/WebSocketHandler.mjs.map +0 -1
  147. package/lib/core/ws/WebSocketClientManager.d.mts +0 -58
  148. package/lib/core/ws/WebSocketClientManager.d.ts +0 -58
  149. package/lib/core/ws/WebSocketClientManager.js +0 -162
  150. package/lib/core/ws/WebSocketClientManager.js.map +0 -1
  151. package/lib/core/ws/WebSocketClientManager.mjs +0 -142
  152. package/lib/core/ws/WebSocketClientManager.mjs.map +0 -1
  153. package/lib/core/ws/handleWebSocketEvent.d.mts +0 -16
  154. package/lib/core/ws/handleWebSocketEvent.d.ts +0 -16
  155. package/lib/core/ws/handleWebSocketEvent.js +0 -56
  156. package/lib/core/ws/handleWebSocketEvent.js.map +0 -1
  157. package/lib/core/ws/handleWebSocketEvent.mjs +0 -36
  158. package/lib/core/ws/handleWebSocketEvent.mjs.map +0 -1
  159. package/lib/core/ws/utils/attachWebSocketLogger.d.mts +0 -34
  160. package/lib/core/ws/utils/attachWebSocketLogger.d.ts +0 -34
  161. package/lib/core/ws/utils/attachWebSocketLogger.js +0 -217
  162. package/lib/core/ws/utils/attachWebSocketLogger.js.map +0 -1
  163. package/lib/core/ws/utils/attachWebSocketLogger.mjs +0 -197
  164. package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +0 -1
  165. package/lib/core/ws/utils/getMessageLength.d.mts +0 -11
  166. package/lib/core/ws/utils/getMessageLength.d.ts +0 -11
  167. package/lib/core/ws/utils/getMessageLength.js +0 -33
  168. package/lib/core/ws/utils/getMessageLength.js.map +0 -1
  169. package/lib/core/ws/utils/getMessageLength.mjs +0 -13
  170. package/lib/core/ws/utils/getMessageLength.mjs.map +0 -1
  171. package/lib/core/ws/utils/getPublicData.d.mts +0 -5
  172. package/lib/core/ws/utils/getPublicData.d.ts +0 -5
  173. package/lib/core/ws/utils/getPublicData.js +0 -36
  174. package/lib/core/ws/utils/getPublicData.js.map +0 -1
  175. package/lib/core/ws/utils/getPublicData.mjs +0 -16
  176. package/lib/core/ws/utils/getPublicData.mjs.map +0 -1
  177. package/lib/core/ws/utils/truncateMessage.d.mts +0 -3
  178. package/lib/core/ws/utils/truncateMessage.d.ts +0 -3
  179. package/lib/core/ws/utils/truncateMessage.js +0 -31
  180. package/lib/core/ws/utils/truncateMessage.js.map +0 -1
  181. package/lib/core/ws/utils/truncateMessage.mjs +0 -11
  182. package/lib/core/ws/utils/truncateMessage.mjs.map +0 -1
  183. package/lib/core/ws/webSocketInterceptor.d.mts +0 -5
  184. package/lib/core/ws/webSocketInterceptor.d.ts +0 -5
  185. package/lib/core/ws/webSocketInterceptor.js +0 -26
  186. package/lib/core/ws/webSocketInterceptor.js.map +0 -1
  187. package/lib/core/ws/webSocketInterceptor.mjs +0 -6
  188. package/lib/core/ws/webSocketInterceptor.mjs.map +0 -1
  189. package/lib/core/ws.d.mts +0 -49
  190. package/lib/core/ws.d.ts +0 -49
  191. package/lib/core/ws.js +0 -65
  192. package/lib/core/ws.js.map +0 -1
  193. package/lib/core/ws.mjs +0 -48
  194. package/lib/core/ws.mjs.map +0 -1
  195. package/src/core/handlers/WebSocketHandler.ts +0 -71
  196. package/src/core/ws/WebSocketClientManager.test.ts +0 -157
  197. package/src/core/ws/WebSocketClientManager.ts +0 -234
  198. package/src/core/ws/handleWebSocketEvent.ts +0 -56
  199. package/src/core/ws/utils/attachWebSocketLogger.ts +0 -269
  200. package/src/core/ws/utils/getMessageLength.test.ts +0 -16
  201. package/src/core/ws/utils/getMessageLength.ts +0 -19
  202. package/src/core/ws/utils/getPublicData.test.ts +0 -38
  203. package/src/core/ws/utils/getPublicData.ts +0 -17
  204. package/src/core/ws/utils/truncateMessage.test.ts +0 -12
  205. package/src/core/ws/utils/truncateMessage.ts +0 -9
  206. package/src/core/ws/webSocketInterceptor.ts +0 -3
  207. package/src/core/ws.test.ts +0 -23
  208. package/src/core/ws.ts +0 -124
@@ -1,58 +0,0 @@
1
- import { WebSocketData, WebSocketClientConnectionProtocol, WebSocketClientConnection } from '@mswjs/interceptors/WebSocket';
2
- import { Path } from '../utils/matching/matchRequestUrl.js';
3
-
4
- declare const MSW_WEBSOCKET_CLIENTS_KEY = "msw:ws:clients";
5
- type WebSocketBroadcastChannelMessage = {
6
- type: 'extraneous:send';
7
- payload: {
8
- clientId: string;
9
- data: WebSocketData;
10
- };
11
- } | {
12
- type: 'extraneous:close';
13
- payload: {
14
- clientId: string;
15
- code?: number;
16
- reason?: string;
17
- };
18
- };
19
- /**
20
- * A manager responsible for accumulating WebSocket client
21
- * connections across different browser runtimes.
22
- */
23
- declare class WebSocketClientManager {
24
- private channel;
25
- private url;
26
- private inMemoryClients;
27
- constructor(channel: BroadcastChannel, url: Path);
28
- /**
29
- * All active WebSocket client connections.
30
- */
31
- get clients(): Set<WebSocketClientConnectionProtocol>;
32
- private getSerializedClients;
33
- private addClient;
34
- /**
35
- * Adds the given `WebSocket` client connection to the set
36
- * of all connections. The given connection is always the complete
37
- * connection object because `addConnection()` is called only
38
- * for the opened connections in the same runtime.
39
- */
40
- addConnection(client: WebSocketClientConnection): void;
41
- }
42
- /**
43
- * A wrapper class to operate with WebSocket client connections
44
- * from other runtimes. This class maintains 1-1 public API
45
- * compatibility to the `WebSocketClientConnection` but relies
46
- * on the given `BroadcastChannel` to communicate instructions
47
- * with the client connections from other runtimes.
48
- */
49
- declare class WebSocketRemoteClientConnection implements WebSocketClientConnectionProtocol {
50
- readonly id: string;
51
- readonly url: URL;
52
- private channel;
53
- constructor(id: string, url: URL, channel: BroadcastChannel);
54
- send(data: WebSocketData): void;
55
- close(code?: number | undefined, reason?: string | undefined): void;
56
- }
57
-
58
- export { MSW_WEBSOCKET_CLIENTS_KEY, type WebSocketBroadcastChannelMessage, WebSocketClientManager, WebSocketRemoteClientConnection };
@@ -1,162 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var WebSocketClientManager_exports = {};
20
- __export(WebSocketClientManager_exports, {
21
- MSW_WEBSOCKET_CLIENTS_KEY: () => MSW_WEBSOCKET_CLIENTS_KEY,
22
- WebSocketClientManager: () => WebSocketClientManager,
23
- WebSocketRemoteClientConnection: () => WebSocketRemoteClientConnection
24
- });
25
- module.exports = __toCommonJS(WebSocketClientManager_exports);
26
- var import_outvariant = require("outvariant");
27
- var import_matchRequestUrl = require("../utils/matching/matchRequestUrl.js");
28
- const MSW_WEBSOCKET_CLIENTS_KEY = "msw:ws:clients";
29
- class WebSocketClientManager {
30
- constructor(channel, url) {
31
- this.channel = channel;
32
- this.url = url;
33
- this.inMemoryClients = /* @__PURE__ */ new Set();
34
- if (typeof localStorage !== "undefined") {
35
- localStorage.removeItem = new Proxy(localStorage.removeItem, {
36
- apply: (target, thisArg, args) => {
37
- const [key] = args;
38
- if (key === MSW_WEBSOCKET_CLIENTS_KEY) {
39
- this.inMemoryClients.clear();
40
- }
41
- return Reflect.apply(target, thisArg, args);
42
- }
43
- });
44
- }
45
- }
46
- inMemoryClients;
47
- /**
48
- * All active WebSocket client connections.
49
- */
50
- get clients() {
51
- if (typeof localStorage !== "undefined") {
52
- const inMemoryClients = Array.from(this.inMemoryClients);
53
- console.log("get clients()", inMemoryClients, this.getSerializedClients());
54
- return new Set(
55
- inMemoryClients.concat(
56
- this.getSerializedClients().filter((serializedClient) => {
57
- if (inMemoryClients.every(
58
- (client) => client.id !== serializedClient.clientId
59
- )) {
60
- return serializedClient;
61
- }
62
- }).map((serializedClient) => {
63
- return new WebSocketRemoteClientConnection(
64
- serializedClient.clientId,
65
- new URL(serializedClient.url),
66
- this.channel
67
- );
68
- })
69
- )
70
- );
71
- }
72
- return this.inMemoryClients;
73
- }
74
- getSerializedClients() {
75
- (0, import_outvariant.invariant)(
76
- typeof localStorage !== "undefined",
77
- "Failed to call WebSocketClientManager#getSerializedClients() in a non-browser environment. This is likely a bug in MSW. Please, report it on GitHub: https://github.com/mswjs/msw"
78
- );
79
- const clientsJson = localStorage.getItem(MSW_WEBSOCKET_CLIENTS_KEY);
80
- if (!clientsJson) {
81
- return [];
82
- }
83
- const allClients = JSON.parse(clientsJson);
84
- const matchingClients = allClients.filter((client) => {
85
- return (0, import_matchRequestUrl.matchRequestUrl)(new URL(client.url), this.url).matches;
86
- });
87
- return matchingClients;
88
- }
89
- addClient(client) {
90
- this.inMemoryClients.add(client);
91
- if (typeof localStorage !== "undefined") {
92
- const serializedClients = this.getSerializedClients();
93
- const nextSerializedClients = serializedClients.concat({
94
- clientId: client.id,
95
- url: client.url.href
96
- });
97
- localStorage.setItem(
98
- MSW_WEBSOCKET_CLIENTS_KEY,
99
- JSON.stringify(nextSerializedClients)
100
- );
101
- }
102
- }
103
- /**
104
- * Adds the given `WebSocket` client connection to the set
105
- * of all connections. The given connection is always the complete
106
- * connection object because `addConnection()` is called only
107
- * for the opened connections in the same runtime.
108
- */
109
- addConnection(client) {
110
- this.addClient(client);
111
- const handleExtraneousMessage = (message) => {
112
- const { type, payload } = message.data;
113
- if (typeof payload === "object" && "clientId" in payload && payload.clientId !== client.id) {
114
- return;
115
- }
116
- switch (type) {
117
- case "extraneous:send": {
118
- client.send(payload.data);
119
- break;
120
- }
121
- case "extraneous:close": {
122
- client.close(payload.code, payload.reason);
123
- break;
124
- }
125
- }
126
- };
127
- const abortController = new AbortController();
128
- this.channel.addEventListener("message", handleExtraneousMessage, {
129
- signal: abortController.signal
130
- });
131
- client.addEventListener("close", () => abortController.abort(), {
132
- once: true
133
- });
134
- }
135
- }
136
- class WebSocketRemoteClientConnection {
137
- constructor(id, url, channel) {
138
- this.id = id;
139
- this.url = url;
140
- this.channel = channel;
141
- }
142
- send(data) {
143
- this.channel.postMessage({
144
- type: "extraneous:send",
145
- payload: {
146
- clientId: this.id,
147
- data
148
- }
149
- });
150
- }
151
- close(code, reason) {
152
- this.channel.postMessage({
153
- type: "extraneous:close",
154
- payload: {
155
- clientId: this.id,
156
- code,
157
- reason
158
- }
159
- });
160
- }
161
- }
162
- //# sourceMappingURL=WebSocketClientManager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/core/ws/WebSocketClientManager.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport type {\n WebSocketData,\n WebSocketClientConnection,\n WebSocketClientConnectionProtocol,\n} from '@mswjs/interceptors/WebSocket'\nimport { matchRequestUrl, type Path } from '../utils/matching/matchRequestUrl'\n\nexport const MSW_WEBSOCKET_CLIENTS_KEY = 'msw:ws:clients'\n\nexport type WebSocketBroadcastChannelMessage =\n | {\n type: 'extraneous:send'\n payload: {\n clientId: string\n data: WebSocketData\n }\n }\n | {\n type: 'extraneous:close'\n payload: {\n clientId: string\n code?: number\n reason?: string\n }\n }\n\ntype SerializedClient = {\n clientId: string\n url: string\n}\n\n/**\n * A manager responsible for accumulating WebSocket client\n * connections across different browser runtimes.\n */\nexport class WebSocketClientManager {\n private inMemoryClients: Set<WebSocketClientConnectionProtocol>\n\n constructor(\n private channel: BroadcastChannel,\n private url: Path,\n ) {\n this.inMemoryClients = new Set()\n\n if (typeof localStorage !== 'undefined') {\n // When the worker clears the local storage key in \"worker.stop()\",\n // also clear the in-memory clients map.\n localStorage.removeItem = new Proxy(localStorage.removeItem, {\n apply: (target, thisArg, args) => {\n const [key] = args\n\n if (key === MSW_WEBSOCKET_CLIENTS_KEY) {\n this.inMemoryClients.clear()\n }\n\n return Reflect.apply(target, thisArg, args)\n },\n })\n }\n }\n\n /**\n * All active WebSocket client connections.\n */\n get clients(): Set<WebSocketClientConnectionProtocol> {\n // In the browser, different runtimes use \"localStorage\"\n // as the shared source of all the clients.\n if (typeof localStorage !== 'undefined') {\n const inMemoryClients = Array.from(this.inMemoryClients)\n\n console.log('get clients()', inMemoryClients, this.getSerializedClients())\n\n return new Set(\n inMemoryClients.concat(\n this.getSerializedClients()\n // Filter out the serialized clients that are already present\n // in this runtime in-memory. This is crucial because a remote client\n // wrapper CANNOT send a message to the client in THIS runtime\n // (the \"message\" event on broadcast channel won't trigger).\n .filter((serializedClient) => {\n if (\n inMemoryClients.every(\n (client) => client.id !== serializedClient.clientId,\n )\n ) {\n return serializedClient\n }\n })\n .map((serializedClient) => {\n return new WebSocketRemoteClientConnection(\n serializedClient.clientId,\n new URL(serializedClient.url),\n this.channel,\n )\n }),\n ),\n )\n }\n\n // In Node.js, the manager acts as a singleton, and all clients\n // are kept in-memory.\n return this.inMemoryClients\n }\n\n private getSerializedClients(): Array<SerializedClient> {\n invariant(\n typeof localStorage !== 'undefined',\n 'Failed to call WebSocketClientManager#getSerializedClients() in a non-browser environment. This is likely a bug in MSW. Please, report it on GitHub: https://github.com/mswjs/msw',\n )\n\n const clientsJson = localStorage.getItem(MSW_WEBSOCKET_CLIENTS_KEY)\n\n if (!clientsJson) {\n return []\n }\n\n const allClients = JSON.parse(clientsJson) as Array<SerializedClient>\n const matchingClients = allClients.filter((client) => {\n return matchRequestUrl(new URL(client.url), this.url).matches\n })\n\n return matchingClients\n }\n\n private addClient(client: WebSocketClientConnection): void {\n this.inMemoryClients.add(client)\n\n if (typeof localStorage !== 'undefined') {\n const serializedClients = this.getSerializedClients()\n\n // Serialize the current client for other runtimes to create\n // a remote wrapper over it. This has no effect on the current runtime.\n const nextSerializedClients = serializedClients.concat({\n clientId: client.id,\n url: client.url.href,\n } as SerializedClient)\n\n localStorage.setItem(\n MSW_WEBSOCKET_CLIENTS_KEY,\n JSON.stringify(nextSerializedClients),\n )\n }\n }\n\n /**\n * Adds the given `WebSocket` client connection to the set\n * of all connections. The given connection is always the complete\n * connection object because `addConnection()` is called only\n * for the opened connections in the same runtime.\n */\n public addConnection(client: WebSocketClientConnection): void {\n this.addClient(client)\n\n // Instruct the current client how to handle events\n // coming from other runtimes (e.g. when calling `.broadcast()`).\n const handleExtraneousMessage = (\n message: MessageEvent<WebSocketBroadcastChannelMessage>,\n ) => {\n const { type, payload } = message.data\n\n // Ignore broadcasted messages for other clients.\n if (\n typeof payload === 'object' &&\n 'clientId' in payload &&\n payload.clientId !== client.id\n ) {\n return\n }\n\n switch (type) {\n case 'extraneous:send': {\n client.send(payload.data)\n break\n }\n\n case 'extraneous:close': {\n client.close(payload.code, payload.reason)\n break\n }\n }\n }\n\n const abortController = new AbortController()\n\n this.channel.addEventListener('message', handleExtraneousMessage, {\n signal: abortController.signal,\n })\n\n // Once closed, this connection cannot be operated on.\n // This must include the extraneous runtimes as well.\n client.addEventListener('close', () => abortController.abort(), {\n once: true,\n })\n }\n}\n\n/**\n * A wrapper class to operate with WebSocket client connections\n * from other runtimes. This class maintains 1-1 public API\n * compatibility to the `WebSocketClientConnection` but relies\n * on the given `BroadcastChannel` to communicate instructions\n * with the client connections from other runtimes.\n */\nexport class WebSocketRemoteClientConnection\n implements WebSocketClientConnectionProtocol\n{\n constructor(\n public readonly id: string,\n public readonly url: URL,\n private channel: BroadcastChannel,\n ) {}\n\n send(data: WebSocketData): void {\n this.channel.postMessage({\n type: 'extraneous:send',\n payload: {\n clientId: this.id,\n data,\n },\n } as WebSocketBroadcastChannelMessage)\n }\n\n close(code?: number | undefined, reason?: string | undefined): void {\n this.channel.postMessage({\n type: 'extraneous:close',\n payload: {\n clientId: this.id,\n code,\n reason,\n },\n } as WebSocketBroadcastChannelMessage)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA0B;AAM1B,6BAA2C;AAEpC,MAAM,4BAA4B;AA4BlC,MAAM,uBAAuB;AAAA,EAGlC,YACU,SACA,KACR;AAFQ;AACA;AAER,SAAK,kBAAkB,oBAAI,IAAI;AAE/B,QAAI,OAAO,iBAAiB,aAAa;AAGvC,mBAAa,aAAa,IAAI,MAAM,aAAa,YAAY;AAAA,QAC3D,OAAO,CAAC,QAAQ,SAAS,SAAS;AAChC,gBAAM,CAAC,GAAG,IAAI;AAEd,cAAI,QAAQ,2BAA2B;AACrC,iBAAK,gBAAgB,MAAM;AAAA,UAC7B;AAEA,iBAAO,QAAQ,MAAM,QAAQ,SAAS,IAAI;AAAA,QAC5C;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAvBQ;AAAA;AAAA;AAAA;AAAA,EA4BR,IAAI,UAAkD;AAGpD,QAAI,OAAO,iBAAiB,aAAa;AACvC,YAAM,kBAAkB,MAAM,KAAK,KAAK,eAAe;AAEvD,cAAQ,IAAI,iBAAiB,iBAAiB,KAAK,qBAAqB,CAAC;AAEzE,aAAO,IAAI;AAAA,QACT,gBAAgB;AAAA,UACd,KAAK,qBAAqB,EAKvB,OAAO,CAAC,qBAAqB;AAC5B,gBACE,gBAAgB;AAAA,cACd,CAAC,WAAW,OAAO,OAAO,iBAAiB;AAAA,YAC7C,GACA;AACA,qBAAO;AAAA,YACT;AAAA,UACF,CAAC,EACA,IAAI,CAAC,qBAAqB;AACzB,mBAAO,IAAI;AAAA,cACT,iBAAiB;AAAA,cACjB,IAAI,IAAI,iBAAiB,GAAG;AAAA,cAC5B,KAAK;AAAA,YACP;AAAA,UACF,CAAC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B;AAE5C,SAAK,QAAQ,iBAAiB,WAAW,yBAAyB;AAAA,MAChE,QAAQ,gBAAgB;AAAA,IAC1B,CAAC;AAID,WAAO,iBAAiB,SAAS,MAAM,gBAAgB,MAAM,GAAG;AAAA,MAC9D,MAAM;AAAA,IACR,CAAC;AAAA,EACH;AACF;AASO,MAAM,gCAEb;AAAA,EACE,YACkB,IACA,KACR,SACR;AAHgB;AACA;AACR;AAAA,EACP;AAAA,EAEH,KAAK,MAA2B;AAC9B,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,KAAK;AAAA,QACf;AAAA,MACF;AAAA,IACF,CAAqC;AAAA,EACvC;AAAA,EAEA,MAAM,MAA2B,QAAmC;AAClE,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,KAAK;AAAA,QACf;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAqC;AAAA,EACvC;AACF;","names":[]}
@@ -1,142 +0,0 @@
1
- import { invariant } from "outvariant";
2
- import { matchRequestUrl } from '../utils/matching/matchRequestUrl.mjs';
3
- const MSW_WEBSOCKET_CLIENTS_KEY = "msw:ws:clients";
4
- class WebSocketClientManager {
5
- constructor(channel, url) {
6
- this.channel = channel;
7
- this.url = url;
8
- this.inMemoryClients = /* @__PURE__ */ new Set();
9
- if (typeof localStorage !== "undefined") {
10
- localStorage.removeItem = new Proxy(localStorage.removeItem, {
11
- apply: (target, thisArg, args) => {
12
- const [key] = args;
13
- if (key === MSW_WEBSOCKET_CLIENTS_KEY) {
14
- this.inMemoryClients.clear();
15
- }
16
- return Reflect.apply(target, thisArg, args);
17
- }
18
- });
19
- }
20
- }
21
- inMemoryClients;
22
- /**
23
- * All active WebSocket client connections.
24
- */
25
- get clients() {
26
- if (typeof localStorage !== "undefined") {
27
- const inMemoryClients = Array.from(this.inMemoryClients);
28
- console.log("get clients()", inMemoryClients, this.getSerializedClients());
29
- return new Set(
30
- inMemoryClients.concat(
31
- this.getSerializedClients().filter((serializedClient) => {
32
- if (inMemoryClients.every(
33
- (client) => client.id !== serializedClient.clientId
34
- )) {
35
- return serializedClient;
36
- }
37
- }).map((serializedClient) => {
38
- return new WebSocketRemoteClientConnection(
39
- serializedClient.clientId,
40
- new URL(serializedClient.url),
41
- this.channel
42
- );
43
- })
44
- )
45
- );
46
- }
47
- return this.inMemoryClients;
48
- }
49
- getSerializedClients() {
50
- invariant(
51
- typeof localStorage !== "undefined",
52
- "Failed to call WebSocketClientManager#getSerializedClients() in a non-browser environment. This is likely a bug in MSW. Please, report it on GitHub: https://github.com/mswjs/msw"
53
- );
54
- const clientsJson = localStorage.getItem(MSW_WEBSOCKET_CLIENTS_KEY);
55
- if (!clientsJson) {
56
- return [];
57
- }
58
- const allClients = JSON.parse(clientsJson);
59
- const matchingClients = allClients.filter((client) => {
60
- return matchRequestUrl(new URL(client.url), this.url).matches;
61
- });
62
- return matchingClients;
63
- }
64
- addClient(client) {
65
- this.inMemoryClients.add(client);
66
- if (typeof localStorage !== "undefined") {
67
- const serializedClients = this.getSerializedClients();
68
- const nextSerializedClients = serializedClients.concat({
69
- clientId: client.id,
70
- url: client.url.href
71
- });
72
- localStorage.setItem(
73
- MSW_WEBSOCKET_CLIENTS_KEY,
74
- JSON.stringify(nextSerializedClients)
75
- );
76
- }
77
- }
78
- /**
79
- * Adds the given `WebSocket` client connection to the set
80
- * of all connections. The given connection is always the complete
81
- * connection object because `addConnection()` is called only
82
- * for the opened connections in the same runtime.
83
- */
84
- addConnection(client) {
85
- this.addClient(client);
86
- const handleExtraneousMessage = (message) => {
87
- const { type, payload } = message.data;
88
- if (typeof payload === "object" && "clientId" in payload && payload.clientId !== client.id) {
89
- return;
90
- }
91
- switch (type) {
92
- case "extraneous:send": {
93
- client.send(payload.data);
94
- break;
95
- }
96
- case "extraneous:close": {
97
- client.close(payload.code, payload.reason);
98
- break;
99
- }
100
- }
101
- };
102
- const abortController = new AbortController();
103
- this.channel.addEventListener("message", handleExtraneousMessage, {
104
- signal: abortController.signal
105
- });
106
- client.addEventListener("close", () => abortController.abort(), {
107
- once: true
108
- });
109
- }
110
- }
111
- class WebSocketRemoteClientConnection {
112
- constructor(id, url, channel) {
113
- this.id = id;
114
- this.url = url;
115
- this.channel = channel;
116
- }
117
- send(data) {
118
- this.channel.postMessage({
119
- type: "extraneous:send",
120
- payload: {
121
- clientId: this.id,
122
- data
123
- }
124
- });
125
- }
126
- close(code, reason) {
127
- this.channel.postMessage({
128
- type: "extraneous:close",
129
- payload: {
130
- clientId: this.id,
131
- code,
132
- reason
133
- }
134
- });
135
- }
136
- }
137
- export {
138
- MSW_WEBSOCKET_CLIENTS_KEY,
139
- WebSocketClientManager,
140
- WebSocketRemoteClientConnection
141
- };
142
- //# sourceMappingURL=WebSocketClientManager.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/core/ws/WebSocketClientManager.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport type {\n WebSocketData,\n WebSocketClientConnection,\n WebSocketClientConnectionProtocol,\n} from '@mswjs/interceptors/WebSocket'\nimport { matchRequestUrl, type Path } from '../utils/matching/matchRequestUrl'\n\nexport const MSW_WEBSOCKET_CLIENTS_KEY = 'msw:ws:clients'\n\nexport type WebSocketBroadcastChannelMessage =\n | {\n type: 'extraneous:send'\n payload: {\n clientId: string\n data: WebSocketData\n }\n }\n | {\n type: 'extraneous:close'\n payload: {\n clientId: string\n code?: number\n reason?: string\n }\n }\n\ntype SerializedClient = {\n clientId: string\n url: string\n}\n\n/**\n * A manager responsible for accumulating WebSocket client\n * connections across different browser runtimes.\n */\nexport class WebSocketClientManager {\n private inMemoryClients: Set<WebSocketClientConnectionProtocol>\n\n constructor(\n private channel: BroadcastChannel,\n private url: Path,\n ) {\n this.inMemoryClients = new Set()\n\n if (typeof localStorage !== 'undefined') {\n // When the worker clears the local storage key in \"worker.stop()\",\n // also clear the in-memory clients map.\n localStorage.removeItem = new Proxy(localStorage.removeItem, {\n apply: (target, thisArg, args) => {\n const [key] = args\n\n if (key === MSW_WEBSOCKET_CLIENTS_KEY) {\n this.inMemoryClients.clear()\n }\n\n return Reflect.apply(target, thisArg, args)\n },\n })\n }\n }\n\n /**\n * All active WebSocket client connections.\n */\n get clients(): Set<WebSocketClientConnectionProtocol> {\n // In the browser, different runtimes use \"localStorage\"\n // as the shared source of all the clients.\n if (typeof localStorage !== 'undefined') {\n const inMemoryClients = Array.from(this.inMemoryClients)\n\n console.log('get clients()', inMemoryClients, this.getSerializedClients())\n\n return new Set(\n inMemoryClients.concat(\n this.getSerializedClients()\n // Filter out the serialized clients that are already present\n // in this runtime in-memory. This is crucial because a remote client\n // wrapper CANNOT send a message to the client in THIS runtime\n // (the \"message\" event on broadcast channel won't trigger).\n .filter((serializedClient) => {\n if (\n inMemoryClients.every(\n (client) => client.id !== serializedClient.clientId,\n )\n ) {\n return serializedClient\n }\n })\n .map((serializedClient) => {\n return new WebSocketRemoteClientConnection(\n serializedClient.clientId,\n new URL(serializedClient.url),\n this.channel,\n )\n }),\n ),\n )\n }\n\n // In Node.js, the manager acts as a singleton, and all clients\n // are kept in-memory.\n return this.inMemoryClients\n }\n\n private getSerializedClients(): Array<SerializedClient> {\n invariant(\n typeof localStorage !== 'undefined',\n 'Failed to call WebSocketClientManager#getSerializedClients() in a non-browser environment. This is likely a bug in MSW. Please, report it on GitHub: https://github.com/mswjs/msw',\n )\n\n const clientsJson = localStorage.getItem(MSW_WEBSOCKET_CLIENTS_KEY)\n\n if (!clientsJson) {\n return []\n }\n\n const allClients = JSON.parse(clientsJson) as Array<SerializedClient>\n const matchingClients = allClients.filter((client) => {\n return matchRequestUrl(new URL(client.url), this.url).matches\n })\n\n return matchingClients\n }\n\n private addClient(client: WebSocketClientConnection): void {\n this.inMemoryClients.add(client)\n\n if (typeof localStorage !== 'undefined') {\n const serializedClients = this.getSerializedClients()\n\n // Serialize the current client for other runtimes to create\n // a remote wrapper over it. This has no effect on the current runtime.\n const nextSerializedClients = serializedClients.concat({\n clientId: client.id,\n url: client.url.href,\n } as SerializedClient)\n\n localStorage.setItem(\n MSW_WEBSOCKET_CLIENTS_KEY,\n JSON.stringify(nextSerializedClients),\n )\n }\n }\n\n /**\n * Adds the given `WebSocket` client connection to the set\n * of all connections. The given connection is always the complete\n * connection object because `addConnection()` is called only\n * for the opened connections in the same runtime.\n */\n public addConnection(client: WebSocketClientConnection): void {\n this.addClient(client)\n\n // Instruct the current client how to handle events\n // coming from other runtimes (e.g. when calling `.broadcast()`).\n const handleExtraneousMessage = (\n message: MessageEvent<WebSocketBroadcastChannelMessage>,\n ) => {\n const { type, payload } = message.data\n\n // Ignore broadcasted messages for other clients.\n if (\n typeof payload === 'object' &&\n 'clientId' in payload &&\n payload.clientId !== client.id\n ) {\n return\n }\n\n switch (type) {\n case 'extraneous:send': {\n client.send(payload.data)\n break\n }\n\n case 'extraneous:close': {\n client.close(payload.code, payload.reason)\n break\n }\n }\n }\n\n const abortController = new AbortController()\n\n this.channel.addEventListener('message', handleExtraneousMessage, {\n signal: abortController.signal,\n })\n\n // Once closed, this connection cannot be operated on.\n // This must include the extraneous runtimes as well.\n client.addEventListener('close', () => abortController.abort(), {\n once: true,\n })\n }\n}\n\n/**\n * A wrapper class to operate with WebSocket client connections\n * from other runtimes. This class maintains 1-1 public API\n * compatibility to the `WebSocketClientConnection` but relies\n * on the given `BroadcastChannel` to communicate instructions\n * with the client connections from other runtimes.\n */\nexport class WebSocketRemoteClientConnection\n implements WebSocketClientConnectionProtocol\n{\n constructor(\n public readonly id: string,\n public readonly url: URL,\n private channel: BroadcastChannel,\n ) {}\n\n send(data: WebSocketData): void {\n this.channel.postMessage({\n type: 'extraneous:send',\n payload: {\n clientId: this.id,\n data,\n },\n } as WebSocketBroadcastChannelMessage)\n }\n\n close(code?: number | undefined, reason?: string | undefined): void {\n this.channel.postMessage({\n type: 'extraneous:close',\n payload: {\n clientId: this.id,\n code,\n reason,\n },\n } as WebSocketBroadcastChannelM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CAAC;AAAA,EACH;AACF;AASO,MAAM,gCAEb;AAAA,EACE,YACkB,IACA,KACR,SACR;AAHgB;AACA;AACR;AAAA,EACP;AAAA,EAEH,KAAK,MAA2B;AAC9B,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,KAAK;AAAA,QACf;AAAA,MACF;AAAA,IACF,CAAqC;AAAA,EACvC;AAAA,EAEA,MAAM,MAA2B,QAAmC;AAClE,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,KAAK;AAAA,QACf;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAqC;AAAA,EACvC;AACF;","names":[]}
@@ -1,16 +0,0 @@
1
- import { WebSocketConnectionData } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket';
2
- import { R as RequestHandler } from '../HttpResponse-CSyJ1cbO.mjs';
3
- import { WebSocketHandler } from '../handlers/WebSocketHandler.mjs';
4
- import '../typeUtils.mjs';
5
- import 'strict-event-emitter';
6
- import '@mswjs/interceptors/WebSocket';
7
- import '../utils/matching/matchRequestUrl.mjs';
8
-
9
- interface HandleWebSocketEventOptions {
10
- getHandlers: () => Array<RequestHandler | WebSocketHandler>;
11
- onMockedConnection: (connection: WebSocketConnectionData) => void;
12
- onPassthroughConnection: (onnection: WebSocketConnectionData) => void;
13
- }
14
- declare function handleWebSocketEvent(options: HandleWebSocketEventOptions): void;
15
-
16
- export { handleWebSocketEvent };
@@ -1,16 +0,0 @@
1
- import { WebSocketConnectionData } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket';
2
- import { R as RequestHandler } from '../HttpResponse-BOzDwzB6.js';
3
- import { WebSocketHandler } from '../handlers/WebSocketHandler.js';
4
- import '../typeUtils.js';
5
- import 'strict-event-emitter';
6
- import '@mswjs/interceptors/WebSocket';
7
- import '../utils/matching/matchRequestUrl.js';
8
-
9
- interface HandleWebSocketEventOptions {
10
- getHandlers: () => Array<RequestHandler | WebSocketHandler>;
11
- onMockedConnection: (connection: WebSocketConnectionData) => void;
12
- onPassthroughConnection: (onnection: WebSocketConnectionData) => void;
13
- }
14
- declare function handleWebSocketEvent(options: HandleWebSocketEventOptions): void;
15
-
16
- export { handleWebSocketEvent };
@@ -1,56 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var handleWebSocketEvent_exports = {};
20
- __export(handleWebSocketEvent_exports, {
21
- handleWebSocketEvent: () => handleWebSocketEvent
22
- });
23
- module.exports = __toCommonJS(handleWebSocketEvent_exports);
24
- var import_WebSocketHandler = require("../handlers/WebSocketHandler.js");
25
- var import_webSocketInterceptor = require("./webSocketInterceptor.js");
26
- function handleWebSocketEvent(options) {
27
- import_webSocketInterceptor.webSocketInterceptor.on("connection", (connection) => {
28
- const handlers = options.getHandlers();
29
- const connectionEvent = new MessageEvent("connection", {
30
- data: connection
31
- });
32
- const matchingHandlers = handlers.filter(
33
- (handler) => {
34
- if (handler instanceof import_WebSocketHandler.WebSocketHandler) {
35
- return handler.predicate({
36
- event: connectionEvent,
37
- parsedResult: handler.parse({
38
- event: connectionEvent
39
- })
40
- });
41
- }
42
- return false;
43
- }
44
- );
45
- if (matchingHandlers.length > 0) {
46
- options?.onMockedConnection(connection);
47
- for (const handler of matchingHandlers) {
48
- handler[import_WebSocketHandler.kDispatchEvent](connectionEvent);
49
- }
50
- } else {
51
- options?.onPassthroughConnection(connection);
52
- connection.server.connect();
53
- }
54
- });
55
- }
56
- //# sourceMappingURL=handleWebSocketEvent.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/core/ws/handleWebSocketEvent.ts"],"sourcesContent":["import type { WebSocketConnectionData } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket'\nimport { RequestHandler } from '../handlers/RequestHandler'\nimport { WebSocketHandler, kDispatchEvent } from '../handlers/WebSocketHandler'\nimport { webSocketInterceptor } from './webSocketInterceptor'\n\ninterface HandleWebSocketEventOptions {\n getHandlers: () => Array<RequestHandler | WebSocketHandler>\n onMockedConnection: (connection: WebSocketConnectionData) => void\n onPassthroughConnection: (onnection: WebSocketConnectionData) => void\n}\n\nexport function handleWebSocketEvent(options: HandleWebSocketEventOptions) {\n webSocketInterceptor.on('connection', (connection) => {\n const handlers = options.getHandlers()\n\n const connectionEvent = new MessageEvent('connection', {\n data: connection,\n })\n\n // First, filter only those WebSocket handlers that\n // match the \"ws.link()\" endpoint predicate. Don't dispatch\n // anything yet so the logger can be attached to the connection\n // before it potentially sends events.\n const matchingHandlers = handlers.filter<WebSocketHandler>(\n (handler): handler is WebSocketHandler => {\n if (handler instanceof WebSocketHandler) {\n return handler.predicate({\n event: connectionEvent,\n parsedResult: handler.parse({\n event: connectionEvent,\n }),\n })\n }\n\n return false\n },\n )\n\n if (matchingHandlers.length > 0) {\n options?.onMockedConnection(connection)\n\n // Iterate over the handlers and forward the connection\n // event to WebSocket event handlers. This is equivalent\n // to dispatching that event onto multiple listeners.\n for (const handler of matchingHandlers) {\n handler[kDispatchEvent](connectionEvent)\n }\n } else {\n options?.onPassthroughConnection(connection)\n\n // If none of the \"ws\" handlers matched,\n // establish the WebSocket connection as-is.\n connection.server.connect()\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,8BAAiD;AACjD,kCAAqC;AAQ9B,SAAS,qBAAqB,SAAsC;AACzE,mDAAqB,GAAG,cAAc,CAAC,eAAe;AACpD,UAAM,WAAW,QAAQ,YAAY;AAErC,UAAM,kBAAkB,IAAI,aAAa,cAAc;AAAA,MACrD,MAAM;AAAA,IACR,CAAC;AAMD,UAAM,mBAAmB,SAAS;AAAA,MAChC,CAAC,YAAyC;AACxC,YAAI,mBAAmB,0CAAkB;AACvC,iBAAO,QAAQ,UAAU;AAAA,YACvB,OAAO;AAAA,YACP,cAAc,QAAQ,MAAM;AAAA,cAC1B,OAAO;AAAA,YACT,CAAC;AAAA,UACH,CAAC;AAAA,QACH;AAEA,eAAO;AAAA,MACT;AAAA,IACF;AAEA,QAAI,iBAAiB,SAAS,GAAG;AAC/B,eAAS,mBAAmB,UAAU;AAKtC,iBAAW,WAAW,kBAAkB;AACtC,gBAAQ,sCAAc,EAAE,eAAe;AAAA,MACzC;AAAA,IACF,OAAO;AACL,eAAS,wBAAwB,UAAU;AAI3C,iBAAW,OAAO,QAAQ;AAAA,IAC5B;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1,36 +0,0 @@
1
- import { WebSocketHandler, kDispatchEvent } from '../handlers/WebSocketHandler.mjs';
2
- import { webSocketInterceptor } from './webSocketInterceptor.mjs';
3
- function handleWebSocketEvent(options) {
4
- webSocketInterceptor.on("connection", (connection) => {
5
- const handlers = options.getHandlers();
6
- const connectionEvent = new MessageEvent("connection", {
7
- data: connection
8
- });
9
- const matchingHandlers = handlers.filter(
10
- (handler) => {
11
- if (handler instanceof WebSocketHandler) {
12
- return handler.predicate({
13
- event: connectionEvent,
14
- parsedResult: handler.parse({
15
- event: connectionEvent
16
- })
17
- });
18
- }
19
- return false;
20
- }
21
- );
22
- if (matchingHandlers.length > 0) {
23
- options?.onMockedConnection(connection);
24
- for (const handler of matchingHandlers) {
25
- handler[kDispatchEvent](connectionEvent);
26
- }
27
- } else {
28
- options?.onPassthroughConnection(connection);
29
- connection.server.connect();
30
- }
31
- });
32
- }
33
- export {
34
- handleWebSocketEvent
35
- };
36
- //# sourceMappingURL=handleWebSocketEvent.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/core/ws/handleWebSocketEvent.ts"],"sourcesContent":["import type { WebSocketConnectionData } from '@mswjs/interceptors/lib/browser/interceptors/WebSocket'\nimport { RequestHandler } from '../handlers/RequestHandler'\nimport { WebSocketHandler, kDispatchEvent } from '../handlers/WebSocketHandler'\nimport { webSocketInterceptor } from './webSocketInterceptor'\n\ninterface HandleWebSocketEventOptions {\n getHandlers: () => Array<RequestHandler | WebSocketHandler>\n onMockedConnection: (connection: WebSocketConnectionData) => void\n onPassthroughConnection: (onnection: WebSocketConnectionData) => void\n}\n\nexport function handleWebSocketEvent(options: HandleWebSocketEventOptions) {\n webSocketInterceptor.on('connection', (connection) => {\n const handlers = options.getHandlers()\n\n const connectionEvent = new MessageEvent('connection', {\n data: connection,\n })\n\n // First, filter only those WebSocket handlers that\n // match the \"ws.link()\" endpoint predicate. Don't dispatch\n // anything yet so the logger can be attached to the connection\n // before it potentially sends events.\n const matchingHandlers = handlers.filter<WebSocketHandler>(\n (handler): handler is WebSocketHandler => {\n if (handler instanceof WebSocketHandler) {\n return handler.predicate({\n event: connectionEvent,\n parsedResult: handler.parse({\n event: connectionEvent,\n }),\n })\n }\n\n return false\n },\n )\n\n if (matchingHandlers.length > 0) {\n options?.onMockedConnection(connection)\n\n // Iterate over the handlers and forward the connection\n // event to WebSocket event handlers. This is equivalent\n // to dispatching that event onto multiple listeners.\n for (const handler of matchingHandlers) {\n handler[kDispatchEvent](connectionEvent)\n }\n } else {\n options?.onPassthroughConnection(connection)\n\n // If none of the \"ws\" handlers matched,\n // establish the WebSocket connection as-is.\n connection.server.connect()\n }\n })\n}\n"],"mappings":"AAEA,SAAS,kBAAkB,sBAAsB;AACjD,SAAS,4BAA4B;AAQ9B,SAAS,qBAAqB,SAAsC;AACzE,uBAAqB,GAAG,cAAc,CAAC,eAAe;AACpD,UAAM,WAAW,QAAQ,YAAY;AAErC,UAAM,kBAAkB,IAAI,aAAa,cAAc;AAAA,MACrD,MAAM;AAAA,IACR,CAAC;AAMD,UAAM,mBAAmB,SAAS;AAAA,MAChC,CAAC,YAAyC;AACxC,YAAI,mBAAmB,kBAAkB;AACvC,iBAAO,QAAQ,UAAU;AAAA,YACvB,OAAO;AAAA,YACP,cAAc,QAAQ,MAAM;AAAA,cAC1B,OAAO;AAAA,YACT,CAAC;AAAA,UACH,CAAC;AAAA,QACH;AAEA,eAAO;AAAA,MACT;AAAA,IACF;AAEA,QAAI,iBAAiB,SAAS,GAAG;AAC/B,eAAS,mBAAmB,UAAU;AAKtC,iBAAW,WAAW,kBAAkB;AACtC,gBAAQ,cAAc,EAAE,eAAe;AAAA,MACzC;AAAA,IACF,OAAO;AACL,eAAS,wBAAwB,UAAU;AAI3C,iBAAW,OAAO,QAAQ;AAAA,IAC5B;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1,34 +0,0 @@
1
- import { WebSocketConnectionData, WebSocketClientConnection, WebSocketData } from '@mswjs/interceptors/WebSocket';
2
-
3
- declare function attachWebSocketLogger(connection: WebSocketConnectionData): void;
4
- /**
5
- * Prints the WebSocket connection.
6
- * This is meant to be logged by every WebSocket handler
7
- * that intercepted this connection. This helps you see
8
- * what handlers observe this connection.
9
- */
10
- declare function logConnectionOpen(client: WebSocketClientConnection): void;
11
- /**
12
- * Prints the outgoing client message.
13
- */
14
- declare function logOutgoingClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
15
- /**
16
- * Prints the outgoing client message initiated
17
- * by `server.send()` in the event handler.
18
- */
19
- declare function logOutgoingMockedClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
20
- /**
21
- * Prings the message received by the WebSocket client.
22
- * This is fired when the "message" event is dispatched
23
- * on the actual WebSocket client instance, and translates to
24
- * the client receiving a message from the server.
25
- */
26
- declare function logIncomingClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
27
- /**
28
- * Prints the outgoing client message initiated
29
- * by `client.send()` in the event handler.
30
- */
31
- declare function logIncomingMockedClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
32
- declare function logIncomingServerMessage(event: MessageEvent<WebSocketData>): Promise<void>;
33
-
34
- export { attachWebSocketLogger, logConnectionOpen, logIncomingClientMessage, logIncomingMockedClientMessage, logIncomingServerMessage, logOutgoingClientMessage, logOutgoingMockedClientMessage };
@@ -1,34 +0,0 @@
1
- import { WebSocketConnectionData, WebSocketClientConnection, WebSocketData } from '@mswjs/interceptors/WebSocket';
2
-
3
- declare function attachWebSocketLogger(connection: WebSocketConnectionData): void;
4
- /**
5
- * Prints the WebSocket connection.
6
- * This is meant to be logged by every WebSocket handler
7
- * that intercepted this connection. This helps you see
8
- * what handlers observe this connection.
9
- */
10
- declare function logConnectionOpen(client: WebSocketClientConnection): void;
11
- /**
12
- * Prints the outgoing client message.
13
- */
14
- declare function logOutgoingClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
15
- /**
16
- * Prints the outgoing client message initiated
17
- * by `server.send()` in the event handler.
18
- */
19
- declare function logOutgoingMockedClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
20
- /**
21
- * Prings the message received by the WebSocket client.
22
- * This is fired when the "message" event is dispatched
23
- * on the actual WebSocket client instance, and translates to
24
- * the client receiving a message from the server.
25
- */
26
- declare function logIncomingClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
27
- /**
28
- * Prints the outgoing client message initiated
29
- * by `client.send()` in the event handler.
30
- */
31
- declare function logIncomingMockedClientMessage(event: MessageEvent<WebSocketData>): Promise<void>;
32
- declare function logIncomingServerMessage(event: MessageEvent<WebSocketData>): Promise<void>;
33
-
34
- export { attachWebSocketLogger, logConnectionOpen, logIncomingClientMessage, logIncomingMockedClientMessage, logIncomingServerMessage, logOutgoingClientMessage, logOutgoingMockedClientMessage };