msw 2.3.0-ws.rc-1 → 2.3.0-ws.rc-3

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 (194) hide show
  1. package/README.md +8 -3
  2. package/cli/init.js +1 -1
  3. package/lib/browser/index.d.mts +4 -4
  4. package/lib/browser/index.js +65 -60
  5. package/lib/browser/index.js.map +1 -1
  6. package/lib/browser/index.mjs +65 -60
  7. package/lib/browser/index.mjs.map +1 -1
  8. package/lib/core/{GraphQLHandler-QGQY_9Rc.d.mts → GraphQLHandler-CzcXr7At.d.mts} +3 -3
  9. package/lib/core/{GraphQLHandler-Cbu12sb0.d.ts → GraphQLHandler-DGyzNtRQ.d.ts} +3 -3
  10. package/lib/core/{HttpResponse-BWB1yDNM.d.mts → HttpResponse-BOzDwzB6.d.ts} +3 -3
  11. package/lib/core/{HttpResponse-DeJBWGN5.d.ts → HttpResponse-CSyJ1cbO.d.mts} +3 -3
  12. package/lib/core/HttpResponse.d.mts +1 -1
  13. package/lib/core/HttpResponse.d.ts +1 -1
  14. package/lib/core/HttpResponse.js.map +1 -1
  15. package/lib/core/HttpResponse.mjs.map +1 -1
  16. package/lib/core/SetupApi.d.mts +1 -1
  17. package/lib/core/SetupApi.d.ts +1 -1
  18. package/lib/core/bypass.js +6 -1
  19. package/lib/core/bypass.js.map +1 -1
  20. package/lib/core/bypass.mjs +6 -1
  21. package/lib/core/bypass.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/getResponse.js +2 -2
  25. package/lib/core/getResponse.js.map +1 -1
  26. package/lib/core/getResponse.mjs +2 -2
  27. package/lib/core/getResponse.mjs.map +1 -1
  28. package/lib/core/graphql.d.mts +8 -6
  29. package/lib/core/graphql.d.ts +8 -6
  30. package/lib/core/graphql.js.map +1 -1
  31. package/lib/core/graphql.mjs.map +1 -1
  32. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  33. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  34. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  35. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  36. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  37. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  38. package/lib/core/handlers/HttpHandler.js +1 -1
  39. package/lib/core/handlers/HttpHandler.js.map +1 -1
  40. package/lib/core/handlers/HttpHandler.mjs +1 -1
  41. package/lib/core/handlers/HttpHandler.mjs.map +1 -1
  42. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  43. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  44. package/lib/core/handlers/WebSocketHandler.d.mts +11 -17
  45. package/lib/core/handlers/WebSocketHandler.d.ts +11 -17
  46. package/lib/core/handlers/WebSocketHandler.js +9 -12
  47. package/lib/core/handlers/WebSocketHandler.js.map +1 -1
  48. package/lib/core/handlers/WebSocketHandler.mjs +9 -12
  49. package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
  50. package/lib/core/http.d.mts +1 -1
  51. package/lib/core/http.d.ts +1 -1
  52. package/lib/core/index.d.mts +5 -5
  53. package/lib/core/index.d.ts +5 -5
  54. package/lib/core/index.js +3 -1
  55. package/lib/core/index.js.map +1 -1
  56. package/lib/core/index.mjs +5 -1
  57. package/lib/core/index.mjs.map +1 -1
  58. package/lib/core/passthrough.d.mts +1 -1
  59. package/lib/core/passthrough.d.ts +1 -1
  60. package/lib/core/typeUtils.d.mts +6 -1
  61. package/lib/core/typeUtils.d.ts +6 -1
  62. package/lib/core/typeUtils.js.map +1 -1
  63. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  64. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  65. package/lib/core/utils/HttpResponse/decorators.js +4 -1
  66. package/lib/core/utils/HttpResponse/decorators.js.map +1 -1
  67. package/lib/core/utils/HttpResponse/decorators.mjs +4 -1
  68. package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -1
  69. package/lib/core/utils/executeHandlers.d.mts +1 -1
  70. package/lib/core/utils/executeHandlers.d.ts +1 -1
  71. package/lib/core/utils/handleRequest.d.mts +1 -1
  72. package/lib/core/utils/handleRequest.d.ts +1 -1
  73. package/lib/core/utils/handleRequest.js.map +1 -1
  74. package/lib/core/utils/handleRequest.mjs.map +1 -1
  75. package/lib/core/utils/internal/mergeRight.js +15 -12
  76. package/lib/core/utils/internal/mergeRight.js.map +1 -1
  77. package/lib/core/utils/internal/mergeRight.mjs +15 -12
  78. package/lib/core/utils/internal/mergeRight.mjs.map +1 -1
  79. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  80. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  81. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  82. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  83. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  84. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  85. package/lib/core/utils/logging/getTimestamp.d.mts +4 -1
  86. package/lib/core/utils/logging/getTimestamp.d.ts +4 -1
  87. package/lib/core/utils/logging/getTimestamp.js +6 -2
  88. package/lib/core/utils/logging/getTimestamp.js.map +1 -1
  89. package/lib/core/utils/logging/getTimestamp.mjs +6 -2
  90. package/lib/core/utils/logging/getTimestamp.mjs.map +1 -1
  91. package/lib/core/ws/handleWebSocketEvent.d.mts +16 -0
  92. package/lib/core/ws/handleWebSocketEvent.d.ts +16 -0
  93. package/lib/core/{utils → ws}/handleWebSocketEvent.js +21 -18
  94. package/lib/core/ws/handleWebSocketEvent.js.map +1 -0
  95. package/lib/core/ws/handleWebSocketEvent.mjs +39 -0
  96. package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -0
  97. package/lib/core/ws/utils/attachWebSocketLogger.d.mts +34 -0
  98. package/lib/core/ws/utils/attachWebSocketLogger.d.ts +34 -0
  99. package/lib/core/ws/utils/attachWebSocketLogger.js +211 -0
  100. package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -0
  101. package/lib/core/ws/utils/attachWebSocketLogger.mjs +191 -0
  102. package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -0
  103. package/lib/core/ws/utils/getMessageLength.d.mts +11 -0
  104. package/lib/core/ws/utils/getMessageLength.d.ts +11 -0
  105. package/lib/core/ws/utils/getMessageLength.js +33 -0
  106. package/lib/core/ws/utils/getMessageLength.js.map +1 -0
  107. package/lib/core/ws/utils/getMessageLength.mjs +13 -0
  108. package/lib/core/ws/utils/getMessageLength.mjs.map +1 -0
  109. package/lib/core/ws/utils/getPublicData.d.mts +5 -0
  110. package/lib/core/ws/utils/getPublicData.d.ts +5 -0
  111. package/lib/core/ws/utils/getPublicData.js +36 -0
  112. package/lib/core/ws/utils/getPublicData.js.map +1 -0
  113. package/lib/core/ws/utils/getPublicData.mjs +16 -0
  114. package/lib/core/ws/utils/getPublicData.mjs.map +1 -0
  115. package/lib/core/ws/utils/truncateMessage.d.mts +3 -0
  116. package/lib/core/ws/utils/truncateMessage.d.ts +3 -0
  117. package/lib/core/{utils/internal/randomId.js → ws/utils/truncateMessage.js} +11 -7
  118. package/lib/core/ws/utils/truncateMessage.js.map +1 -0
  119. package/lib/core/ws/utils/truncateMessage.mjs +11 -0
  120. package/lib/core/ws/utils/truncateMessage.mjs.map +1 -0
  121. package/lib/core/{ws/ws.d.mts → ws.d.mts} +19 -14
  122. package/lib/core/{ws/ws.d.ts → ws.d.ts} +19 -14
  123. package/lib/core/{ws/ws.js → ws.js} +4 -23
  124. package/lib/core/ws.js.map +1 -0
  125. package/lib/core/{ws/ws.mjs → ws.mjs} +4 -23
  126. package/lib/core/ws.mjs.map +1 -0
  127. package/lib/iife/index.js +516 -256
  128. package/lib/iife/index.js.map +1 -1
  129. package/lib/mockServiceWorker.js +8 -11
  130. package/lib/native/index.d.mts +4 -4
  131. package/lib/native/index.js +9 -3
  132. package/lib/native/index.js.map +1 -1
  133. package/lib/native/index.mjs +9 -3
  134. package/lib/native/index.mjs.map +1 -1
  135. package/lib/node/index.d.mts +6 -6
  136. package/lib/node/index.d.ts +2 -2
  137. package/lib/node/index.js +9 -3
  138. package/lib/node/index.js.map +1 -1
  139. package/lib/node/index.mjs +9 -3
  140. package/lib/node/index.mjs.map +1 -1
  141. package/package.json +9 -12
  142. package/src/browser/setupWorker/glossary.ts +4 -1
  143. package/src/browser/setupWorker/setupWorker.ts +20 -3
  144. package/src/browser/setupWorker/start/createRequestListener.ts +1 -1
  145. package/src/browser/setupWorker/start/createStartHandler.ts +9 -19
  146. package/src/browser/setupWorker/start/utils/createMessageChannel.ts +1 -1
  147. package/src/browser/utils/checkWorkerIntegrity.ts +34 -0
  148. package/src/core/HttpResponse.ts +3 -2
  149. package/src/core/bypass.test.ts +22 -0
  150. package/src/core/bypass.ts +9 -1
  151. package/src/core/getResponse.ts +2 -2
  152. package/src/core/graphql.ts +6 -3
  153. package/src/core/handlers/GraphQLHandler.test.ts +4 -5
  154. package/src/core/handlers/GraphQLHandler.ts +7 -4
  155. package/src/core/handlers/HttpHandler.test.ts +5 -5
  156. package/src/core/handlers/HttpHandler.ts +1 -1
  157. package/src/core/handlers/WebSocketHandler.ts +21 -39
  158. package/src/core/index.ts +7 -3
  159. package/src/core/typeUtils.ts +16 -10
  160. package/src/core/utils/HttpResponse/decorators.ts +8 -4
  161. package/src/core/utils/handleRequest.test.ts +14 -14
  162. package/src/core/utils/handleRequest.ts +1 -1
  163. package/src/core/utils/internal/mergeRight.ts +16 -13
  164. package/src/core/utils/logging/getTimestamp.test.ts +20 -6
  165. package/src/core/utils/logging/getTimestamp.ts +11 -6
  166. package/src/core/ws/handleWebSocketEvent.ts +59 -0
  167. package/src/core/ws/utils/attachWebSocketLogger.ts +262 -0
  168. package/src/core/ws/utils/getMessageLength.test.ts +16 -0
  169. package/src/core/ws/utils/getMessageLength.ts +19 -0
  170. package/src/core/ws/utils/getPublicData.test.ts +38 -0
  171. package/src/core/ws/utils/getPublicData.ts +17 -0
  172. package/src/core/ws/utils/truncateMessage.test.ts +12 -0
  173. package/src/core/ws/utils/truncateMessage.ts +9 -0
  174. package/src/core/{ws/ws.ts → ws.ts} +49 -35
  175. package/src/mockServiceWorker.js +7 -10
  176. package/src/node/SetupServerApi.ts +4 -4
  177. package/src/node/SetupServerCommonApi.ts +7 -3
  178. package/src/node/glossary.ts +3 -3
  179. package/lib/core/utils/handleWebSocketEvent.d.mts +0 -10
  180. package/lib/core/utils/handleWebSocketEvent.d.ts +0 -10
  181. package/lib/core/utils/handleWebSocketEvent.js.map +0 -1
  182. package/lib/core/utils/handleWebSocketEvent.mjs +0 -40
  183. package/lib/core/utils/handleWebSocketEvent.mjs.map +0 -1
  184. package/lib/core/utils/internal/randomId.d.mts +0 -3
  185. package/lib/core/utils/internal/randomId.d.ts +0 -3
  186. package/lib/core/utils/internal/randomId.js.map +0 -1
  187. package/lib/core/utils/internal/randomId.mjs +0 -7
  188. package/lib/core/utils/internal/randomId.mjs.map +0 -1
  189. package/lib/core/ws/ws.js.map +0 -1
  190. package/lib/core/ws/ws.mjs.map +0 -1
  191. package/src/browser/utils/requestIntegrityCheck.ts +0 -23
  192. package/src/core/utils/handleWebSocketEvent.ts +0 -49
  193. package/src/core/utils/internal/randomId.ts +0 -3
  194. /package/src/core/{ws/ws.test.ts → ws.test.ts} +0 -0
@@ -0,0 +1,11 @@
1
+ const MAX_LENGTH = 24;
2
+ function truncateMessage(message) {
3
+ if (message.length <= MAX_LENGTH) {
4
+ return message;
5
+ }
6
+ return `${message.slice(0, MAX_LENGTH)}\u2026`;
7
+ }
8
+ export {
9
+ truncateMessage
10
+ };
11
+ //# sourceMappingURL=truncateMessage.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/core/ws/utils/truncateMessage.ts"],"sourcesContent":["const MAX_LENGTH = 24\n\nexport function truncateMessage(message: string): string {\n if (message.length <= MAX_LENGTH) {\n return message\n }\n\n return `${message.slice(0, MAX_LENGTH)}…`\n}\n"],"mappings":"AAAA,MAAM,aAAa;AAEZ,SAAS,gBAAgB,SAAyB;AACvD,MAAI,QAAQ,UAAU,YAAY;AAChC,WAAO;AAAA,EACT;AAEA,SAAO,GAAG,QAAQ,MAAM,GAAG,UAAU,CAAC;AACxC;","names":[]}
@@ -1,20 +1,15 @@
1
1
  import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
2
- import { WebSocketHandlerEventMap, WebSocketHandler } from '../handlers/WebSocketHandler.mjs';
3
- import { Path } from '../utils/matching/matchRequestUrl.mjs';
2
+ import { WebSocketHandlerEventMap, WebSocketHandler } from './handlers/WebSocketHandler.mjs';
3
+ import { Path } from './utils/matching/matchRequestUrl.mjs';
4
4
  import 'strict-event-emitter';
5
5
 
6
- /**
7
- * Intercepts outgoing WebSocket connections to the given URL.
8
- *
9
- * @example
10
- * const chat = ws.link('wss://chat.example.com')
11
- * chat.on('connection', ({ client }) => {
12
- * client.send('hello from server!')
13
- * })
14
- */
15
- declare function createWebSocketLinkHandler(url: Path): {
6
+ type WebSocketLink = {
7
+ /**
8
+ * A set of all WebSocket clients connected
9
+ * to this link.
10
+ */
16
11
  clients: Set<WebSocketClientConnectionProtocol>;
17
- on<K extends "connection">(event: K, listener: (...args: WebSocketHandlerEventMap[K]) => void): WebSocketHandler;
12
+ on<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: (...args: WebSocketHandlerEventMap[EventType]) => void): WebSocketHandler;
18
13
  /**
19
14
  * Broadcasts the given data to all WebSocket clients.
20
15
  *
@@ -37,8 +32,18 @@ declare function createWebSocketLinkHandler(url: Path): {
37
32
  */
38
33
  broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
39
34
  };
35
+ /**
36
+ * Intercepts outgoing WebSocket connections to the given URL.
37
+ *
38
+ * @example
39
+ * const chat = ws.link('wss://chat.example.com')
40
+ * chat.on('connection', ({ client }) => {
41
+ * client.send('hello from server!')
42
+ * })
43
+ */
44
+ declare function createWebSocketLinkHandler(url: Path): WebSocketLink;
40
45
  declare const ws: {
41
46
  link: typeof createWebSocketLinkHandler;
42
47
  };
43
48
 
44
- export { ws };
49
+ export { type WebSocketLink, ws };
@@ -1,20 +1,15 @@
1
1
  import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
2
- import { WebSocketHandlerEventMap, WebSocketHandler } from '../handlers/WebSocketHandler.js';
3
- import { Path } from '../utils/matching/matchRequestUrl.js';
2
+ import { WebSocketHandlerEventMap, WebSocketHandler } from './handlers/WebSocketHandler.js';
3
+ import { Path } from './utils/matching/matchRequestUrl.js';
4
4
  import 'strict-event-emitter';
5
5
 
6
- /**
7
- * Intercepts outgoing WebSocket connections to the given URL.
8
- *
9
- * @example
10
- * const chat = ws.link('wss://chat.example.com')
11
- * chat.on('connection', ({ client }) => {
12
- * client.send('hello from server!')
13
- * })
14
- */
15
- declare function createWebSocketLinkHandler(url: Path): {
6
+ type WebSocketLink = {
7
+ /**
8
+ * A set of all WebSocket clients connected
9
+ * to this link.
10
+ */
16
11
  clients: Set<WebSocketClientConnectionProtocol>;
17
- on<K extends "connection">(event: K, listener: (...args: WebSocketHandlerEventMap[K]) => void): WebSocketHandler;
12
+ on<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: (...args: WebSocketHandlerEventMap[EventType]) => void): WebSocketHandler;
18
13
  /**
19
14
  * Broadcasts the given data to all WebSocket clients.
20
15
  *
@@ -37,8 +32,18 @@ declare function createWebSocketLinkHandler(url: Path): {
37
32
  */
38
33
  broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
39
34
  };
35
+ /**
36
+ * Intercepts outgoing WebSocket connections to the given URL.
37
+ *
38
+ * @example
39
+ * const chat = ws.link('wss://chat.example.com')
40
+ * chat.on('connection', ({ client }) => {
41
+ * client.send('hello from server!')
42
+ * })
43
+ */
44
+ declare function createWebSocketLinkHandler(url: Path): WebSocketLink;
40
45
  declare const ws: {
41
46
  link: typeof createWebSocketLinkHandler;
42
47
  };
43
48
 
44
- export { ws };
49
+ export { type WebSocketLink, ws };
@@ -22,15 +22,15 @@ __export(ws_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(ws_exports);
24
24
  var import_outvariant = require("outvariant");
25
- var import_WebSocketHandler = require("../handlers/WebSocketHandler.js");
26
- var import_matchRequestUrl = require("../utils/matching/matchRequestUrl.js");
27
- var import_WebSocketClientManager = require("./WebSocketClientManager.js");
25
+ var import_WebSocketHandler = require("./handlers/WebSocketHandler.js");
26
+ var import_matchRequestUrl = require("./utils/matching/matchRequestUrl.js");
27
+ var import_WebSocketClientManager = require("./ws/WebSocketClientManager.js");
28
28
  const wsBroadcastChannel = new BroadcastChannel("msw:ws-client-manager");
29
29
  function createWebSocketLinkHandler(url) {
30
30
  (0, import_outvariant.invariant)(url, "Expected a WebSocket server URL but got undefined");
31
31
  (0, import_outvariant.invariant)(
32
32
  (0, import_matchRequestUrl.isPath)(url),
33
- "Expected a WebSocket server URL but got %s",
33
+ "Expected a WebSocket server URL to be a valid path but got %s",
34
34
  typeof url
35
35
  );
36
36
  const clientManager = new import_WebSocketClientManager.WebSocketClientManager(wsBroadcastChannel);
@@ -44,28 +44,9 @@ function createWebSocketLinkHandler(url) {
44
44
  handler[import_WebSocketHandler.kEmitter].on(event, listener);
45
45
  return handler;
46
46
  },
47
- /**
48
- * Broadcasts the given data to all WebSocket clients.
49
- *
50
- * @example
51
- * const service = ws.link('wss://example.com')
52
- * service.on('connection', () => {
53
- * service.broadcast('hello, everyone!')
54
- * })
55
- */
56
47
  broadcast(data) {
57
48
  this.broadcastExcept([], data);
58
49
  },
59
- /**
60
- * Broadcasts the given data to all WebSocket clients
61
- * except the ones provided in the `clients` argument.
62
- *
63
- * @example
64
- * const service = ws.link('wss://example.com')
65
- * service.on('connection', ({ client }) => {
66
- * service.broadcastExcept(client, 'hi, the rest of you!')
67
- * })
68
- */
69
50
  broadcastExcept(clients, data) {
70
51
  const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
71
52
  clientManager.clients.forEach((otherClient) => {
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/ws.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport type {\n WebSocketClientConnectionProtocol,\n WebSocketData,\n} from '@mswjs/interceptors/WebSocket'\nimport {\n WebSocketHandler,\n kEmitter,\n type WebSocketHandlerEventMap,\n} from './handlers/WebSocketHandler'\nimport { Path, isPath } from './utils/matching/matchRequestUrl'\nimport { WebSocketClientManager } from './ws/WebSocketClientManager'\n\nconst wsBroadcastChannel = new BroadcastChannel('msw:ws-client-manager')\n\nexport type WebSocketLink = {\n /**\n * A set of all WebSocket clients connected\n * to this link.\n */\n clients: Set<WebSocketClientConnectionProtocol>\n\n on<EventType extends keyof WebSocketHandlerEventMap>(\n event: EventType,\n listener: (...args: WebSocketHandlerEventMap[EventType]) => void,\n ): WebSocketHandler\n\n /**\n * Broadcasts the given data to all WebSocket clients.\n *\n * @example\n * const service = ws.link('wss://example.com')\n * service.on('connection', () => {\n * service.broadcast('hello, everyone!')\n * })\n */\n broadcast(data: WebSocketData): void\n\n /**\n * Broadcasts the given data to all WebSocket clients\n * except the ones provided in the `clients` argument.\n *\n * @example\n * const service = ws.link('wss://example.com')\n * service.on('connection', ({ client }) => {\n * service.broadcastExcept(client, 'hi, the rest of you!')\n * })\n */\n broadcastExcept(\n clients:\n | WebSocketClientConnectionProtocol\n | Array<WebSocketClientConnectionProtocol>,\n data: WebSocketData,\n ): void\n}\n\n/**\n * Intercepts outgoing WebSocket connections to the given URL.\n *\n * @example\n * const chat = ws.link('wss://chat.example.com')\n * chat.on('connection', ({ client }) => {\n * client.send('hello from server!')\n * })\n */\nfunction createWebSocketLinkHandler(url: Path): WebSocketLink {\n invariant(url, 'Expected a WebSocket server URL but got undefined')\n\n invariant(\n isPath(url),\n 'Expected a WebSocket server URL to be a valid path but got %s',\n typeof url,\n )\n\n const clientManager = new WebSocketClientManager(wsBroadcastChannel)\n\n return {\n clients: clientManager.clients,\n on(event, listener) {\n const handler = new WebSocketHandler(url)\n\n // Add the connection event listener for when the\n // handler matches and emits a connection event.\n // When that happens, store that connection in the\n // set of all connections for reference.\n handler[kEmitter].on('connection', ({ client }) => {\n clientManager.addConnection(client)\n })\n\n // The \"handleWebSocketEvent\" function will invoke\n // the \"run()\" method on the WebSocketHandler.\n // If the handler matches, it will emit the \"connection\"\n // event. Attach the user-defined listener to that event.\n handler[kEmitter].on(event, listener)\n\n return handler\n },\n\n broadcast(data) {\n // This will invoke \"send()\" on the immediate clients\n // in this runtime and post a message to the broadcast channel\n // to trigger send for the clients in other runtimes.\n this.broadcastExcept([], data)\n },\n\n broadcastExcept(clients, data) {\n const ignoreClients = Array.prototype\n .concat(clients)\n .map((client) => client.id)\n\n clientManager.clients.forEach((otherClient) => {\n if (!ignoreClients.includes(otherClient.id)) {\n otherClient.send(data)\n }\n })\n },\n }\n}\n\nexport const ws = {\n link: createWebSocketLinkHandler,\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA0B;AAK1B,8BAIO;AACP,6BAA6B;AAC7B,oCAAuC;AAEvC,MAAM,qBAAqB,IAAI,iBAAiB,uBAAuB;AAoDvE,SAAS,2BAA2B,KAA0B;AAC5D,mCAAU,KAAK,mDAAmD;AAElE;AAAA,QACE,+BAAO,GAAG;AAAA,IACV;AAAA,IACA,OAAO;AAAA,EACT;AAEA,QAAM,gBAAgB,IAAI,qDAAuB,kBAAkB;AAEnE,SAAO;AAAA,IACL,SAAS,cAAc;AAAA,IACvB,GAAG,OAAO,UAAU;AAClB,YAAM,UAAU,IAAI,yCAAiB,GAAG;AAMxC,cAAQ,gCAAQ,EAAE,GAAG,cAAc,CAAC,EAAE,OAAO,MAAM;AACjD,sBAAc,cAAc,MAAM;AAAA,MACpC,CAAC;AAMD,cAAQ,gCAAQ,EAAE,GAAG,OAAO,QAAQ;AAEpC,aAAO;AAAA,IACT;AAAA,IAEA,UAAU,MAAM;AAId,WAAK,gBAAgB,CAAC,GAAG,IAAI;AAAA,IAC/B;AAAA,IAEA,gBAAgB,SAAS,MAAM;AAC7B,YAAM,gBAAgB,MAAM,UACzB,OAAO,OAAO,EACd,IAAI,CAAC,WAAW,OAAO,EAAE;AAE5B,oBAAc,QAAQ,QAAQ,CAAC,gBAAgB;AAC7C,YAAI,CAAC,cAAc,SAAS,YAAY,EAAE,GAAG;AAC3C,sBAAY,KAAK,IAAI;AAAA,QACvB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEO,MAAM,KAAK;AAAA,EAChB,MAAM;AACR;","names":[]}
@@ -2,15 +2,15 @@ import { invariant } from "outvariant";
2
2
  import {
3
3
  WebSocketHandler,
4
4
  kEmitter
5
- } from '../handlers/WebSocketHandler.mjs';
6
- import { isPath } from '../utils/matching/matchRequestUrl.mjs';
7
- import { WebSocketClientManager } from './WebSocketClientManager.mjs';
5
+ } from './handlers/WebSocketHandler.mjs';
6
+ import { isPath } from './utils/matching/matchRequestUrl.mjs';
7
+ import { WebSocketClientManager } from './ws/WebSocketClientManager.mjs';
8
8
  const wsBroadcastChannel = new BroadcastChannel("msw:ws-client-manager");
9
9
  function createWebSocketLinkHandler(url) {
10
10
  invariant(url, "Expected a WebSocket server URL but got undefined");
11
11
  invariant(
12
12
  isPath(url),
13
- "Expected a WebSocket server URL but got %s",
13
+ "Expected a WebSocket server URL to be a valid path but got %s",
14
14
  typeof url
15
15
  );
16
16
  const clientManager = new WebSocketClientManager(wsBroadcastChannel);
@@ -24,28 +24,9 @@ function createWebSocketLinkHandler(url) {
24
24
  handler[kEmitter].on(event, listener);
25
25
  return handler;
26
26
  },
27
- /**
28
- * Broadcasts the given data to all WebSocket clients.
29
- *
30
- * @example
31
- * const service = ws.link('wss://example.com')
32
- * service.on('connection', () => {
33
- * service.broadcast('hello, everyone!')
34
- * })
35
- */
36
27
  broadcast(data) {
37
28
  this.broadcastExcept([], data);
38
29
  },
39
- /**
40
- * Broadcasts the given data to all WebSocket clients
41
- * except the ones provided in the `clients` argument.
42
- *
43
- * @example
44
- * const service = ws.link('wss://example.com')
45
- * service.on('connection', ({ client }) => {
46
- * service.broadcastExcept(client, 'hi, the rest of you!')
47
- * })
48
- */
49
30
  broadcastExcept(clients, data) {
50
31
  const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
51
32
  clientManager.clients.forEach((otherClient) => {
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/ws.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport type {\n WebSocketClientConnectionProtocol,\n WebSocketData,\n} from '@mswjs/interceptors/WebSocket'\nimport {\n WebSocketHandler,\n kEmitter,\n type WebSocketHandlerEventMap,\n} from './handlers/WebSocketHandler'\nimport { Path, isPath } from './utils/matching/matchRequestUrl'\nimport { WebSocketClientManager } from './ws/WebSocketClientManager'\n\nconst wsBroadcastChannel = new BroadcastChannel('msw:ws-client-manager')\n\nexport type WebSocketLink = {\n /**\n * A set of all WebSocket clients connected\n * to this link.\n */\n clients: Set<WebSocketClientConnectionProtocol>\n\n on<EventType extends keyof WebSocketHandlerEventMap>(\n event: EventType,\n listener: (...args: WebSocketHandlerEventMap[EventType]) => void,\n ): WebSocketHandler\n\n /**\n * Broadcasts the given data to all WebSocket clients.\n *\n * @example\n * const service = ws.link('wss://example.com')\n * service.on('connection', () => {\n * service.broadcast('hello, everyone!')\n * })\n */\n broadcast(data: WebSocketData): void\n\n /**\n * Broadcasts the given data to all WebSocket clients\n * except the ones provided in the `clients` argument.\n *\n * @example\n * const service = ws.link('wss://example.com')\n * service.on('connection', ({ client }) => {\n * service.broadcastExcept(client, 'hi, the rest of you!')\n * })\n */\n broadcastExcept(\n clients:\n | WebSocketClientConnectionProtocol\n | Array<WebSocketClientConnectionProtocol>,\n data: WebSocketData,\n ): void\n}\n\n/**\n * Intercepts outgoing WebSocket connections to the given URL.\n *\n * @example\n * const chat = ws.link('wss://chat.example.com')\n * chat.on('connection', ({ client }) => {\n * client.send('hello from server!')\n * })\n */\nfunction createWebSocketLinkHandler(url: Path): WebSocketLink {\n invariant(url, 'Expected a WebSocket server URL but got undefined')\n\n invariant(\n isPath(url),\n 'Expected a WebSocket server URL to be a valid path but got %s',\n typeof url,\n )\n\n const clientManager = new WebSocketClientManager(wsBroadcastChannel)\n\n return {\n clients: clientManager.clients,\n on(event, listener) {\n const handler = new WebSocketHandler(url)\n\n // Add the connection event listener for when the\n // handler matches and emits a connection event.\n // When that happens, store that connection in the\n // set of all connections for reference.\n handler[kEmitter].on('connection', ({ client }) => {\n clientManager.addConnection(client)\n })\n\n // The \"handleWebSocketEvent\" function will invoke\n // the \"run()\" method on the WebSocketHandler.\n // If the handler matches, it will emit the \"connection\"\n // event. Attach the user-defined listener to that event.\n handler[kEmitter].on(event, listener)\n\n return handler\n },\n\n broadcast(data) {\n // This will invoke \"send()\" on the immediate clients\n // in this runtime and post a message to the broadcast channel\n // to trigger send for the clients in other runtimes.\n this.broadcastExcept([], data)\n },\n\n broadcastExcept(clients, data) {\n const ignoreClients = Array.prototype\n .concat(clients)\n .map((client) => client.id)\n\n clientManager.clients.forEach((otherClient) => {\n if (!ignoreClients.includes(otherClient.id)) {\n otherClient.send(data)\n }\n })\n },\n }\n}\n\nexport const ws = {\n link: createWebSocketLinkHandler,\n}\n"],"mappings":"AAAA,SAAS,iBAAiB;AAK1B;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAe,cAAc;AAC7B,SAAS,8BAA8B;AAEvC,MAAM,qBAAqB,IAAI,iBAAiB,uBAAuB;AAoDvE,SAAS,2BAA2B,KAA0B;AAC5D,YAAU,KAAK,mDAAmD;AAElE;AAAA,IACE,OAAO,GAAG;AAAA,IACV;AAAA,IACA,OAAO;AAAA,EACT;AAEA,QAAM,gBAAgB,IAAI,uBAAuB,kBAAkB;AAEnE,SAAO;AAAA,IACL,SAAS,cAAc;AAAA,IACvB,GAAG,OAAO,UAAU;AAClB,YAAM,UAAU,IAAI,iBAAiB,GAAG;AAMxC,cAAQ,QAAQ,EAAE,GAAG,cAAc,CAAC,EAAE,OAAO,MAAM;AACjD,sBAAc,cAAc,MAAM;AAAA,MACpC,CAAC;AAMD,cAAQ,QAAQ,EAAE,GAAG,OAAO,QAAQ;AAEpC,aAAO;AAAA,IACT;AAAA,IAEA,UAAU,MAAM;AAId,WAAK,gBAAgB,CAAC,GAAG,IAAI;AAAA,IAC/B;AAAA,IAEA,gBAAgB,SAAS,MAAM;AAC7B,YAAM,gBAAgB,MAAM,UACzB,OAAO,OAAO,EACd,IAAI,CAAC,WAAW,OAAO,EAAE;AAE5B,oBAAc,QAAQ,QAAQ,CAAC,gBAAgB;AAC7C,YAAI,CAAC,cAAc,SAAS,YAAY,EAAE,GAAG;AAC3C,sBAAY,KAAK,IAAI;AAAA,QACvB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEO,MAAM,KAAK;AAAA,EAChB,MAAM;AACR;","names":[]}