msw 2.2.13 → 2.3.0-ws.rc-2
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/cli/init.js +0 -0
- package/config/scripts/postinstall.js +0 -0
- package/lib/browser/index.d.mts +7 -6
- package/lib/browser/index.d.ts +7 -6
- package/lib/browser/index.js +26 -7
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +26 -7
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-bom2Dn82.d.ts → GraphQLHandler-3gvpA65n.d.ts} +1 -1
- package/lib/core/{GraphQLHandler-yJ_I6j54.d.mts → GraphQLHandler-4DPdxG0R.d.mts} +1 -1
- package/lib/core/{HttpResponse-vQNlixkj.d.ts → HttpResponse-aJY-D0oG.d.ts} +2 -2
- package/lib/core/{HttpResponse-3-NyzyF7.d.mts → HttpResponse-xuSipbNt.d.mts} +2 -2
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/SetupApi.d.mts +15 -12
- package/lib/core/SetupApi.d.ts +15 -12
- package/lib/core/SetupApi.js +3 -1
- package/lib/core/SetupApi.js.map +1 -1
- package/lib/core/SetupApi.mjs +3 -1
- package/lib/core/SetupApi.mjs.map +1 -1
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/graphql.d.mts +2 -2
- package/lib/core/graphql.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +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 +32 -0
- package/lib/core/handlers/WebSocketHandler.d.ts +32 -0
- package/lib/core/handlers/WebSocketHandler.js +62 -0
- package/lib/core/handlers/WebSocketHandler.js.map +1 -0
- package/lib/core/handlers/WebSocketHandler.mjs +44 -0
- package/lib/core/handlers/WebSocketHandler.mjs.map +1 -0
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +5 -2
- package/lib/core/index.d.ts +5 -2
- package/lib/core/index.js +5 -1
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +7 -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/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/executeHandlers.js +4 -0
- package/lib/core/utils/executeHandlers.js.map +1 -1
- package/lib/core/utils/executeHandlers.mjs +6 -0
- package/lib/core/utils/executeHandlers.mjs.map +1 -1
- package/lib/core/utils/handleRequest.d.mts +2 -2
- package/lib/core/utils/handleRequest.d.ts +2 -2
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/handleWebSocketEvent.d.mts +16 -0
- package/lib/core/utils/handleWebSocketEvent.d.ts +16 -0
- package/lib/core/utils/handleWebSocketEvent.js +59 -0
- package/lib/core/utils/handleWebSocketEvent.js.map +1 -0
- package/lib/core/utils/handleWebSocketEvent.mjs +39 -0
- package/lib/core/utils/handleWebSocketEvent.mjs.map +1 -0
- 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/utils/matching/matchRequestUrl.d.mts +2 -1
- package/lib/core/utils/matching/matchRequestUrl.d.ts +2 -1
- package/lib/core/utils/matching/matchRequestUrl.js +4 -0
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs +4 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
- package/lib/core/ws/WebSocketClientManager.d.mts +64 -0
- package/lib/core/ws/WebSocketClientManager.d.ts +64 -0
- package/lib/core/ws/WebSocketClientManager.js +123 -0
- package/lib/core/ws/WebSocketClientManager.js.map +1 -0
- package/lib/core/ws/WebSocketClientManager.mjs +103 -0
- package/lib/core/ws/WebSocketClientManager.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/ws/utils/truncateMessage.js +31 -0
- 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/webSocketInterceptor.d.mts +5 -0
- package/lib/core/ws/webSocketInterceptor.d.ts +5 -0
- package/lib/core/ws/webSocketInterceptor.js +26 -0
- package/lib/core/ws/webSocketInterceptor.js.map +1 -0
- package/lib/core/ws/webSocketInterceptor.mjs +6 -0
- package/lib/core/ws/webSocketInterceptor.mjs.map +1 -0
- package/lib/core/ws/ws.d.mts +44 -0
- package/lib/core/ws/ws.d.ts +44 -0
- package/lib/core/ws/ws.js +82 -0
- package/lib/core/ws/ws.js.map +1 -0
- package/lib/core/ws/ws.mjs +65 -0
- package/lib/core/ws/ws.mjs.map +1 -0
- package/lib/iife/index.js +967 -11
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +6 -5
- package/lib/native/index.d.ts +6 -5
- package/lib/native/index.js +13 -0
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +13 -0
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +8 -7
- package/lib/node/index.d.ts +8 -7
- package/lib/node/index.js +13 -0
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +13 -0
- package/lib/node/index.mjs.map +1 -1
- package/package.json +26 -21
- package/src/browser/setupWorker/glossary.ts +10 -10
- package/src/browser/setupWorker/setupWorker.ts +34 -3
- package/src/core/SetupApi.ts +28 -20
- package/src/core/handlers/WebSocketHandler.ts +71 -0
- package/src/core/index.ts +8 -1
- package/src/core/utils/executeHandlers.ts +6 -2
- package/src/core/utils/handleRequest.ts +1 -2
- package/src/core/utils/handleWebSocketEvent.ts +59 -0
- package/src/core/utils/logging/getTimestamp.test.ts +20 -6
- package/src/core/utils/logging/getTimestamp.ts +11 -6
- package/src/core/utils/matching/matchRequestUrl.test.ts +44 -0
- package/src/core/utils/matching/matchRequestUrl.ts +4 -0
- package/src/core/ws/WebSocketClientManager.test.ts +159 -0
- package/src/core/ws/WebSocketClientManager.ts +170 -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/webSocketInterceptor.ts +3 -0
- package/src/core/ws/ws.test.ts +23 -0
- package/src/core/ws/ws.ts +108 -0
- package/src/node/SetupServerApi.ts +8 -7
- package/src/node/SetupServerCommonApi.ts +14 -1
- package/src/node/glossary.ts +5 -7
- package/src/node/setupServer.ts +2 -1
|
@@ -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":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
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 webSocketInterceptor_exports = {};
|
|
20
|
+
__export(webSocketInterceptor_exports, {
|
|
21
|
+
webSocketInterceptor: () => webSocketInterceptor
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(webSocketInterceptor_exports);
|
|
24
|
+
var import_WebSocket = require("@mswjs/interceptors/WebSocket");
|
|
25
|
+
const webSocketInterceptor = new import_WebSocket.WebSocketInterceptor();
|
|
26
|
+
//# sourceMappingURL=webSocketInterceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/ws/webSocketInterceptor.ts"],"sourcesContent":["import { WebSocketInterceptor } from '@mswjs/interceptors/WebSocket'\n\nexport const webSocketInterceptor = new WebSocketInterceptor()\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAqC;AAE9B,MAAM,uBAAuB,IAAI,sCAAqB;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/ws/webSocketInterceptor.ts"],"sourcesContent":["import { WebSocketInterceptor } from '@mswjs/interceptors/WebSocket'\n\nexport const webSocketInterceptor = new WebSocketInterceptor()\n"],"mappings":"AAAA,SAAS,4BAA4B;AAE9B,MAAM,uBAAuB,IAAI,qBAAqB;","names":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
|
|
2
|
+
import { WebSocketHandlerEventMap, WebSocketHandler } from '../handlers/WebSocketHandler.mjs';
|
|
3
|
+
import { Path } from '../utils/matching/matchRequestUrl.mjs';
|
|
4
|
+
import 'strict-event-emitter';
|
|
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): {
|
|
16
|
+
clients: Set<WebSocketClientConnectionProtocol>;
|
|
17
|
+
on<K extends "connection">(event: K, listener: (...args: WebSocketHandlerEventMap[K]) => void): WebSocketHandler;
|
|
18
|
+
/**
|
|
19
|
+
* Broadcasts the given data to all WebSocket clients.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const service = ws.link('wss://example.com')
|
|
23
|
+
* service.on('connection', () => {
|
|
24
|
+
* service.broadcast('hello, everyone!')
|
|
25
|
+
* })
|
|
26
|
+
*/
|
|
27
|
+
broadcast(data: WebSocketData): void;
|
|
28
|
+
/**
|
|
29
|
+
* Broadcasts the given data to all WebSocket clients
|
|
30
|
+
* except the ones provided in the `clients` argument.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const service = ws.link('wss://example.com')
|
|
34
|
+
* service.on('connection', ({ client }) => {
|
|
35
|
+
* service.broadcastExcept(client, 'hi, the rest of you!')
|
|
36
|
+
* })
|
|
37
|
+
*/
|
|
38
|
+
broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
|
39
|
+
};
|
|
40
|
+
declare const ws: {
|
|
41
|
+
link: typeof createWebSocketLinkHandler;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { ws };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
|
|
2
|
+
import { WebSocketHandlerEventMap, WebSocketHandler } from '../handlers/WebSocketHandler.js';
|
|
3
|
+
import { Path } from '../utils/matching/matchRequestUrl.js';
|
|
4
|
+
import 'strict-event-emitter';
|
|
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): {
|
|
16
|
+
clients: Set<WebSocketClientConnectionProtocol>;
|
|
17
|
+
on<K extends "connection">(event: K, listener: (...args: WebSocketHandlerEventMap[K]) => void): WebSocketHandler;
|
|
18
|
+
/**
|
|
19
|
+
* Broadcasts the given data to all WebSocket clients.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const service = ws.link('wss://example.com')
|
|
23
|
+
* service.on('connection', () => {
|
|
24
|
+
* service.broadcast('hello, everyone!')
|
|
25
|
+
* })
|
|
26
|
+
*/
|
|
27
|
+
broadcast(data: WebSocketData): void;
|
|
28
|
+
/**
|
|
29
|
+
* Broadcasts the given data to all WebSocket clients
|
|
30
|
+
* except the ones provided in the `clients` argument.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const service = ws.link('wss://example.com')
|
|
34
|
+
* service.on('connection', ({ client }) => {
|
|
35
|
+
* service.broadcastExcept(client, 'hi, the rest of you!')
|
|
36
|
+
* })
|
|
37
|
+
*/
|
|
38
|
+
broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
|
39
|
+
};
|
|
40
|
+
declare const ws: {
|
|
41
|
+
link: typeof createWebSocketLinkHandler;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { ws };
|
|
@@ -0,0 +1,82 @@
|
|
|
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 ws_exports = {};
|
|
20
|
+
__export(ws_exports, {
|
|
21
|
+
ws: () => ws
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(ws_exports);
|
|
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");
|
|
28
|
+
const wsBroadcastChannel = new BroadcastChannel("msw:ws-client-manager");
|
|
29
|
+
function createWebSocketLinkHandler(url) {
|
|
30
|
+
(0, import_outvariant.invariant)(url, "Expected a WebSocket server URL but got undefined");
|
|
31
|
+
(0, import_outvariant.invariant)(
|
|
32
|
+
(0, import_matchRequestUrl.isPath)(url),
|
|
33
|
+
"Expected a WebSocket server URL but got %s",
|
|
34
|
+
typeof url
|
|
35
|
+
);
|
|
36
|
+
const clientManager = new import_WebSocketClientManager.WebSocketClientManager(wsBroadcastChannel);
|
|
37
|
+
return {
|
|
38
|
+
clients: clientManager.clients,
|
|
39
|
+
on(event, listener) {
|
|
40
|
+
const handler = new import_WebSocketHandler.WebSocketHandler(url);
|
|
41
|
+
handler[import_WebSocketHandler.kEmitter].on("connection", ({ client }) => {
|
|
42
|
+
clientManager.addConnection(client);
|
|
43
|
+
});
|
|
44
|
+
handler[import_WebSocketHandler.kEmitter].on(event, listener);
|
|
45
|
+
return handler;
|
|
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
|
+
broadcast(data) {
|
|
57
|
+
this.broadcastExcept([], data);
|
|
58
|
+
},
|
|
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
|
+
broadcastExcept(clients, data) {
|
|
70
|
+
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
71
|
+
clientManager.clients.forEach((otherClient) => {
|
|
72
|
+
if (!ignoreClients.includes(otherClient.id)) {
|
|
73
|
+
otherClient.send(data);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const ws = {
|
|
80
|
+
link: createWebSocketLinkHandler
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=ws.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/ws/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 './WebSocketClientManager'\n\nconst wsBroadcastChannel = new BroadcastChannel('msw:ws-client-manager')\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) {\n invariant(url, 'Expected a WebSocket server URL but got undefined')\n\n invariant(\n isPath(url),\n 'Expected a WebSocket server URL but got %s',\n typeof url,\n )\n\n const clientManager = new WebSocketClientManager(wsBroadcastChannel)\n\n return {\n clients: clientManager.clients,\n on<K extends keyof WebSocketHandlerEventMap>(\n event: K,\n listener: (...args: WebSocketHandlerEventMap[K]) => void,\n ): WebSocketHandler {\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 /**\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 // 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 /**\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 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;AAWvE,SAAS,2BAA2B,KAAW;AAC7C,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,GACE,OACA,UACkB;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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAWA,UAAU,MAA2B;AAInC,WAAK,gBAAgB,CAAC,GAAG,IAAI;AAAA,IAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAYA,gBACE,SAGA,MACM;AACN,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":[]}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { invariant } from "outvariant";
|
|
2
|
+
import {
|
|
3
|
+
WebSocketHandler,
|
|
4
|
+
kEmitter
|
|
5
|
+
} from '../handlers/WebSocketHandler.mjs';
|
|
6
|
+
import { isPath } from '../utils/matching/matchRequestUrl.mjs';
|
|
7
|
+
import { WebSocketClientManager } from './WebSocketClientManager.mjs';
|
|
8
|
+
const wsBroadcastChannel = new BroadcastChannel("msw:ws-client-manager");
|
|
9
|
+
function createWebSocketLinkHandler(url) {
|
|
10
|
+
invariant(url, "Expected a WebSocket server URL but got undefined");
|
|
11
|
+
invariant(
|
|
12
|
+
isPath(url),
|
|
13
|
+
"Expected a WebSocket server URL but got %s",
|
|
14
|
+
typeof url
|
|
15
|
+
);
|
|
16
|
+
const clientManager = new WebSocketClientManager(wsBroadcastChannel);
|
|
17
|
+
return {
|
|
18
|
+
clients: clientManager.clients,
|
|
19
|
+
on(event, listener) {
|
|
20
|
+
const handler = new WebSocketHandler(url);
|
|
21
|
+
handler[kEmitter].on("connection", ({ client }) => {
|
|
22
|
+
clientManager.addConnection(client);
|
|
23
|
+
});
|
|
24
|
+
handler[kEmitter].on(event, listener);
|
|
25
|
+
return handler;
|
|
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
|
+
broadcast(data) {
|
|
37
|
+
this.broadcastExcept([], data);
|
|
38
|
+
},
|
|
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
|
+
broadcastExcept(clients, data) {
|
|
50
|
+
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
51
|
+
clientManager.clients.forEach((otherClient) => {
|
|
52
|
+
if (!ignoreClients.includes(otherClient.id)) {
|
|
53
|
+
otherClient.send(data);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const ws = {
|
|
60
|
+
link: createWebSocketLinkHandler
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
ws
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=ws.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/ws/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 './WebSocketClientManager'\n\nconst wsBroadcastChannel = new BroadcastChannel('msw:ws-client-manager')\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) {\n invariant(url, 'Expected a WebSocket server URL but got undefined')\n\n invariant(\n isPath(url),\n 'Expected a WebSocket server URL but got %s',\n typeof url,\n )\n\n const clientManager = new WebSocketClientManager(wsBroadcastChannel)\n\n return {\n clients: clientManager.clients,\n on<K extends keyof WebSocketHandlerEventMap>(\n event: K,\n listener: (...args: WebSocketHandlerEventMap[K]) => void,\n ): WebSocketHandler {\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 /**\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 // 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 /**\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 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;AAWvE,SAAS,2BAA2B,KAAW;AAC7C,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,GACE,OACA,UACkB;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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAWA,UAAU,MAA2B;AAInC,WAAK,gBAAgB,CAAC,GAAG,IAAI;AAAA,IAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAYA,gBACE,SAGA,MACM;AACN,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":[]}
|