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.
- package/README.md +8 -3
- package/cli/init.js +1 -1
- package/lib/browser/index.d.mts +4 -4
- package/lib/browser/index.js +65 -60
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +65 -60
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-QGQY_9Rc.d.mts → GraphQLHandler-CzcXr7At.d.mts} +3 -3
- package/lib/core/{GraphQLHandler-Cbu12sb0.d.ts → GraphQLHandler-DGyzNtRQ.d.ts} +3 -3
- package/lib/core/{HttpResponse-BWB1yDNM.d.mts → HttpResponse-BOzDwzB6.d.ts} +3 -3
- package/lib/core/{HttpResponse-DeJBWGN5.d.ts → HttpResponse-CSyJ1cbO.d.mts} +3 -3
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/HttpResponse.js.map +1 -1
- package/lib/core/HttpResponse.mjs.map +1 -1
- package/lib/core/SetupApi.d.mts +1 -1
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/bypass.js +6 -1
- package/lib/core/bypass.js.map +1 -1
- package/lib/core/bypass.mjs +6 -1
- package/lib/core/bypass.mjs.map +1 -1
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/getResponse.js +2 -2
- package/lib/core/getResponse.js.map +1 -1
- package/lib/core/getResponse.mjs +2 -2
- package/lib/core/getResponse.mjs.map +1 -1
- package/lib/core/graphql.d.mts +8 -6
- package/lib/core/graphql.d.ts +8 -6
- package/lib/core/graphql.js.map +1 -1
- package/lib/core/graphql.mjs.map +1 -1
- package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.js.map +1 -1
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.js +1 -1
- package/lib/core/handlers/HttpHandler.js.map +1 -1
- package/lib/core/handlers/HttpHandler.mjs +1 -1
- package/lib/core/handlers/HttpHandler.mjs.map +1 -1
- package/lib/core/handlers/RequestHandler.d.mts +1 -1
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/WebSocketHandler.d.mts +11 -17
- package/lib/core/handlers/WebSocketHandler.d.ts +11 -17
- package/lib/core/handlers/WebSocketHandler.js +9 -12
- package/lib/core/handlers/WebSocketHandler.js.map +1 -1
- package/lib/core/handlers/WebSocketHandler.mjs +9 -12
- package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +5 -5
- package/lib/core/index.d.ts +5 -5
- package/lib/core/index.js +3 -1
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +5 -1
- package/lib/core/index.mjs.map +1 -1
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +1 -1
- package/lib/core/typeUtils.d.mts +6 -1
- package/lib/core/typeUtils.d.ts +6 -1
- package/lib/core/typeUtils.js.map +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/HttpResponse/decorators.js +4 -1
- package/lib/core/utils/HttpResponse/decorators.js.map +1 -1
- package/lib/core/utils/HttpResponse/decorators.mjs +4 -1
- package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -1
- package/lib/core/utils/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/handleRequest.d.mts +1 -1
- package/lib/core/utils/handleRequest.d.ts +1 -1
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/mergeRight.js +15 -12
- package/lib/core/utils/internal/mergeRight.js.map +1 -1
- package/lib/core/utils/internal/mergeRight.mjs +15 -12
- package/lib/core/utils/internal/mergeRight.mjs.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/logging/getTimestamp.d.mts +4 -1
- package/lib/core/utils/logging/getTimestamp.d.ts +4 -1
- package/lib/core/utils/logging/getTimestamp.js +6 -2
- package/lib/core/utils/logging/getTimestamp.js.map +1 -1
- package/lib/core/utils/logging/getTimestamp.mjs +6 -2
- package/lib/core/utils/logging/getTimestamp.mjs.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +16 -0
- package/lib/core/ws/handleWebSocketEvent.d.ts +16 -0
- package/lib/core/{utils → ws}/handleWebSocketEvent.js +21 -18
- package/lib/core/ws/handleWebSocketEvent.js.map +1 -0
- package/lib/core/ws/handleWebSocketEvent.mjs +39 -0
- package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -0
- package/lib/core/ws/utils/attachWebSocketLogger.d.mts +34 -0
- package/lib/core/ws/utils/attachWebSocketLogger.d.ts +34 -0
- package/lib/core/ws/utils/attachWebSocketLogger.js +211 -0
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -0
- package/lib/core/ws/utils/attachWebSocketLogger.mjs +191 -0
- package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -0
- package/lib/core/ws/utils/getMessageLength.d.mts +11 -0
- package/lib/core/ws/utils/getMessageLength.d.ts +11 -0
- package/lib/core/ws/utils/getMessageLength.js +33 -0
- package/lib/core/ws/utils/getMessageLength.js.map +1 -0
- package/lib/core/ws/utils/getMessageLength.mjs +13 -0
- package/lib/core/ws/utils/getMessageLength.mjs.map +1 -0
- package/lib/core/ws/utils/getPublicData.d.mts +5 -0
- package/lib/core/ws/utils/getPublicData.d.ts +5 -0
- package/lib/core/ws/utils/getPublicData.js +36 -0
- package/lib/core/ws/utils/getPublicData.js.map +1 -0
- package/lib/core/ws/utils/getPublicData.mjs +16 -0
- package/lib/core/ws/utils/getPublicData.mjs.map +1 -0
- package/lib/core/ws/utils/truncateMessage.d.mts +3 -0
- package/lib/core/ws/utils/truncateMessage.d.ts +3 -0
- package/lib/core/{utils/internal/randomId.js → ws/utils/truncateMessage.js} +11 -7
- package/lib/core/ws/utils/truncateMessage.js.map +1 -0
- package/lib/core/ws/utils/truncateMessage.mjs +11 -0
- package/lib/core/ws/utils/truncateMessage.mjs.map +1 -0
- package/lib/core/{ws/ws.d.mts → ws.d.mts} +19 -14
- package/lib/core/{ws/ws.d.ts → ws.d.ts} +19 -14
- package/lib/core/{ws/ws.js → ws.js} +4 -23
- package/lib/core/ws.js.map +1 -0
- package/lib/core/{ws/ws.mjs → ws.mjs} +4 -23
- package/lib/core/ws.mjs.map +1 -0
- package/lib/iife/index.js +516 -256
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +8 -11
- package/lib/native/index.d.mts +4 -4
- package/lib/native/index.js +9 -3
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +9 -3
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +6 -6
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +9 -3
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +9 -3
- package/lib/node/index.mjs.map +1 -1
- package/package.json +9 -12
- package/src/browser/setupWorker/glossary.ts +4 -1
- package/src/browser/setupWorker/setupWorker.ts +20 -3
- package/src/browser/setupWorker/start/createRequestListener.ts +1 -1
- package/src/browser/setupWorker/start/createStartHandler.ts +9 -19
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +1 -1
- package/src/browser/utils/checkWorkerIntegrity.ts +34 -0
- package/src/core/HttpResponse.ts +3 -2
- package/src/core/bypass.test.ts +22 -0
- package/src/core/bypass.ts +9 -1
- package/src/core/getResponse.ts +2 -2
- package/src/core/graphql.ts +6 -3
- package/src/core/handlers/GraphQLHandler.test.ts +4 -5
- package/src/core/handlers/GraphQLHandler.ts +7 -4
- package/src/core/handlers/HttpHandler.test.ts +5 -5
- package/src/core/handlers/HttpHandler.ts +1 -1
- package/src/core/handlers/WebSocketHandler.ts +21 -39
- package/src/core/index.ts +7 -3
- package/src/core/typeUtils.ts +16 -10
- package/src/core/utils/HttpResponse/decorators.ts +8 -4
- package/src/core/utils/handleRequest.test.ts +14 -14
- package/src/core/utils/handleRequest.ts +1 -1
- package/src/core/utils/internal/mergeRight.ts +16 -13
- package/src/core/utils/logging/getTimestamp.test.ts +20 -6
- package/src/core/utils/logging/getTimestamp.ts +11 -6
- package/src/core/ws/handleWebSocketEvent.ts +59 -0
- package/src/core/ws/utils/attachWebSocketLogger.ts +262 -0
- package/src/core/ws/utils/getMessageLength.test.ts +16 -0
- package/src/core/ws/utils/getMessageLength.ts +19 -0
- package/src/core/ws/utils/getPublicData.test.ts +38 -0
- package/src/core/ws/utils/getPublicData.ts +17 -0
- package/src/core/ws/utils/truncateMessage.test.ts +12 -0
- package/src/core/ws/utils/truncateMessage.ts +9 -0
- package/src/core/{ws/ws.ts → ws.ts} +49 -35
- package/src/mockServiceWorker.js +7 -10
- package/src/node/SetupServerApi.ts +4 -4
- package/src/node/SetupServerCommonApi.ts +7 -3
- package/src/node/glossary.ts +3 -3
- package/lib/core/utils/handleWebSocketEvent.d.mts +0 -10
- package/lib/core/utils/handleWebSocketEvent.d.ts +0 -10
- package/lib/core/utils/handleWebSocketEvent.js.map +0 -1
- package/lib/core/utils/handleWebSocketEvent.mjs +0 -40
- package/lib/core/utils/handleWebSocketEvent.mjs.map +0 -1
- package/lib/core/utils/internal/randomId.d.mts +0 -3
- package/lib/core/utils/internal/randomId.d.ts +0 -3
- package/lib/core/utils/internal/randomId.js.map +0 -1
- package/lib/core/utils/internal/randomId.mjs +0 -7
- package/lib/core/utils/internal/randomId.mjs.map +0 -1
- package/lib/core/ws/ws.js.map +0 -1
- package/lib/core/ws/ws.mjs.map +0 -1
- package/src/browser/utils/requestIntegrityCheck.ts +0 -23
- package/src/core/utils/handleWebSocketEvent.ts +0 -49
- package/src/core/utils/internal/randomId.ts +0 -3
- /package/src/core/{ws/ws.test.ts → ws.test.ts} +0 -0
|
@@ -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 '
|
|
3
|
-
import { Path } from '
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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<
|
|
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 '
|
|
3
|
-
import { Path } from '
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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<
|
|
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("
|
|
26
|
-
var import_matchRequestUrl = require("
|
|
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 '
|
|
6
|
-
import { isPath } from '
|
|
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":[]}
|