msw 2.5.2 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/index.d.mts +7 -6
- package/lib/browser/index.d.ts +7 -6
- package/lib/browser/index.js +29 -1
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +29 -1
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-ClMB0BOy.d.mts → GraphQLHandler-Doool6Q_.d.mts} +1 -1
- package/lib/core/{GraphQLHandler-D6mLMXGZ.d.ts → GraphQLHandler-udzgBRPf.d.ts} +1 -1
- package/lib/core/{HttpResponse-vn-Pb4Bi.d.mts → HttpResponse-BLGmJolh.d.mts} +1 -1
- package/lib/core/{HttpResponse-DaYkf3ml.d.ts → HttpResponse-Cgbkdkje.d.ts} +1 -1
- 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 +33 -0
- package/lib/core/handlers/WebSocketHandler.d.ts +33 -0
- package/lib/core/handlers/WebSocketHandler.js +120 -0
- package/lib/core/handlers/WebSocketHandler.js.map +1 -0
- package/lib/core/handlers/WebSocketHandler.mjs +102 -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/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 +63 -0
- package/lib/core/ws/WebSocketClientManager.d.ts +63 -0
- package/lib/core/ws/WebSocketClientManager.js +149 -0
- package/lib/core/ws/WebSocketClientManager.js.map +1 -0
- package/lib/core/ws/WebSocketClientManager.mjs +129 -0
- package/lib/core/ws/WebSocketClientManager.mjs.map +1 -0
- package/lib/core/ws/WebSocketClientStore.d.mts +13 -0
- package/lib/core/ws/WebSocketClientStore.d.ts +13 -0
- package/lib/core/ws/WebSocketClientStore.js +26 -0
- package/lib/core/ws/WebSocketClientStore.js.map +1 -0
- package/lib/core/ws/WebSocketClientStore.mjs +6 -0
- package/lib/core/ws/WebSocketClientStore.mjs.map +1 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.d.mts +15 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.d.ts +15 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.js +130 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs +110 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -0
- package/lib/core/ws/WebSocketMemoryClientStore.d.mts +13 -0
- package/lib/core/ws/WebSocketMemoryClientStore.d.ts +13 -0
- package/lib/core/ws/WebSocketMemoryClientStore.js +41 -0
- package/lib/core/ws/WebSocketMemoryClientStore.js.map +1 -0
- package/lib/core/ws/WebSocketMemoryClientStore.mjs +21 -0
- package/lib/core/ws/WebSocketMemoryClientStore.mjs.map +1 -0
- package/lib/core/ws/handleWebSocketEvent.d.mts +19 -0
- package/lib/core/ws/handleWebSocketEvent.d.ts +19 -0
- package/lib/core/ws/handleWebSocketEvent.js +73 -0
- package/lib/core/ws/handleWebSocketEvent.js.map +1 -0
- package/lib/core/ws/handleWebSocketEvent.mjs +55 -0
- package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -0
- package/lib/core/ws/utils/attachWebSocketLogger.d.mts +12 -0
- package/lib/core/ws/utils/attachWebSocketLogger.d.ts +12 -0
- package/lib/core/ws/utils/attachWebSocketLogger.js +198 -0
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -0
- package/lib/core/ws/utils/attachWebSocketLogger.mjs +178 -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.d.mts +75 -0
- package/lib/core/ws.d.ts +75 -0
- package/lib/core/ws.js +71 -0
- package/lib/core/ws.js.map +1 -0
- package/lib/core/ws.mjs +54 -0
- package/lib/core/ws.mjs.map +1 -0
- package/lib/iife/index.js +1413 -85
- 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 +22 -4
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +22 -4
- 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 +22 -4
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +22 -4
- package/lib/node/index.mjs.map +1 -1
- package/package.json +10 -1
- package/src/browser/setupWorker/glossary.ts +10 -10
- package/src/browser/setupWorker/setupWorker.ts +32 -3
- package/src/browser/setupWorker/start/createRequestListener.ts +7 -1
- package/src/browser/setupWorker/start/createStartHandler.ts +5 -0
- package/src/browser/setupWorker/stop/createStop.ts +6 -0
- package/src/core/SetupApi.ts +28 -20
- package/src/core/handlers/WebSocketHandler.ts +142 -0
- package/src/core/index.ts +11 -1
- package/src/core/utils/executeHandlers.ts +6 -2
- package/src/core/utils/handleRequest.ts +1 -1
- 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 +164 -0
- package/src/core/ws/WebSocketClientManager.ts +211 -0
- package/src/core/ws/WebSocketClientStore.ts +14 -0
- package/src/core/ws/WebSocketIndexedDBClientStore.ts +145 -0
- package/src/core/ws/WebSocketMemoryClientStore.ts +27 -0
- package/src/core/ws/handleWebSocketEvent.ts +82 -0
- package/src/core/ws/utils/attachWebSocketLogger.ts +259 -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.test.ts +23 -0
- package/src/core/ws.ts +166 -0
- package/src/node/SetupServerApi.ts +8 -7
- package/src/node/SetupServerCommonApi.ts +29 -5
- package/src/node/glossary.ts +5 -7
- package/src/node/setupServer.ts +2 -1
|
@@ -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,75 @@
|
|
|
1
|
+
import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
|
|
2
|
+
export { WebSocketData } from '@mswjs/interceptors/WebSocket';
|
|
3
|
+
import { WebSocketHandlerEventMap, WebSocketHandler } from './handlers/WebSocketHandler.mjs';
|
|
4
|
+
import { Path } from './utils/matching/matchRequestUrl.mjs';
|
|
5
|
+
import 'strict-event-emitter';
|
|
6
|
+
|
|
7
|
+
type WebSocketEventListener<EventType extends keyof WebSocketHandlerEventMap> = (...args: WebSocketHandlerEventMap[EventType]) => void;
|
|
8
|
+
type WebSocketLink = {
|
|
9
|
+
/**
|
|
10
|
+
* A set of all WebSocket clients connected
|
|
11
|
+
* to this link.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://mswjs.io/docs/api/ws#clients `clients` API reference}
|
|
14
|
+
*/
|
|
15
|
+
clients: Set<WebSocketClientConnectionProtocol>;
|
|
16
|
+
/**
|
|
17
|
+
* Adds an event listener to this WebSocket link.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const chat = ws.link('wss://chat.example.com')
|
|
21
|
+
* chat.addEventListener('connection', listener)
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://mswjs.io/docs/api/ws#onevent-listener `on()` API reference}
|
|
24
|
+
*/
|
|
25
|
+
addEventListener<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: WebSocketEventListener<EventType>): WebSocketHandler;
|
|
26
|
+
/**
|
|
27
|
+
* Broadcasts the given data to all WebSocket clients.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const service = ws.link('wss://example.com')
|
|
31
|
+
* service.addEventListener('connection', () => {
|
|
32
|
+
* service.broadcast('hello, everyone!')
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://mswjs.io/docs/api/ws#broadcastdata `broadcast()` API reference}
|
|
36
|
+
*/
|
|
37
|
+
broadcast(data: WebSocketData): void;
|
|
38
|
+
/**
|
|
39
|
+
* Broadcasts the given data to all WebSocket clients
|
|
40
|
+
* except the ones provided in the `clients` argument.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const service = ws.link('wss://example.com')
|
|
44
|
+
* service.addEventListener('connection', ({ client }) => {
|
|
45
|
+
* service.broadcastExcept(client, 'hi, the rest of you!')
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://mswjs.io/docs/api/ws#broadcastexceptclients-data `broadcast()` API reference}
|
|
49
|
+
*/
|
|
50
|
+
broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Intercepts outgoing WebSocket connections to the given URL.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const chat = ws.link('wss://chat.example.com')
|
|
57
|
+
* chat.addEventListener('connection', ({ client }) => {
|
|
58
|
+
* client.send('hello from server!')
|
|
59
|
+
* })
|
|
60
|
+
*/
|
|
61
|
+
declare function createWebSocketLinkHandler(url: Path): WebSocketLink;
|
|
62
|
+
/**
|
|
63
|
+
* A namespace to intercept and mock WebSocket connections.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const chat = ws.link('wss://chat.example.com')
|
|
67
|
+
*
|
|
68
|
+
* @see {@link https://mswjs.io/docs/api/ws `ws` API reference}
|
|
69
|
+
* @see {@link https://mswjs.io/docs/basics/handling-websocket-events Handling WebSocket events}
|
|
70
|
+
*/
|
|
71
|
+
declare const ws: {
|
|
72
|
+
link: typeof createWebSocketLinkHandler;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { type WebSocketEventListener, type WebSocketLink, ws };
|
package/lib/core/ws.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { WebSocketClientConnectionProtocol, WebSocketData } from '@mswjs/interceptors/WebSocket';
|
|
2
|
+
export { WebSocketData } from '@mswjs/interceptors/WebSocket';
|
|
3
|
+
import { WebSocketHandlerEventMap, WebSocketHandler } from './handlers/WebSocketHandler.js';
|
|
4
|
+
import { Path } from './utils/matching/matchRequestUrl.js';
|
|
5
|
+
import 'strict-event-emitter';
|
|
6
|
+
|
|
7
|
+
type WebSocketEventListener<EventType extends keyof WebSocketHandlerEventMap> = (...args: WebSocketHandlerEventMap[EventType]) => void;
|
|
8
|
+
type WebSocketLink = {
|
|
9
|
+
/**
|
|
10
|
+
* A set of all WebSocket clients connected
|
|
11
|
+
* to this link.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://mswjs.io/docs/api/ws#clients `clients` API reference}
|
|
14
|
+
*/
|
|
15
|
+
clients: Set<WebSocketClientConnectionProtocol>;
|
|
16
|
+
/**
|
|
17
|
+
* Adds an event listener to this WebSocket link.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const chat = ws.link('wss://chat.example.com')
|
|
21
|
+
* chat.addEventListener('connection', listener)
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://mswjs.io/docs/api/ws#onevent-listener `on()` API reference}
|
|
24
|
+
*/
|
|
25
|
+
addEventListener<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: WebSocketEventListener<EventType>): WebSocketHandler;
|
|
26
|
+
/**
|
|
27
|
+
* Broadcasts the given data to all WebSocket clients.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const service = ws.link('wss://example.com')
|
|
31
|
+
* service.addEventListener('connection', () => {
|
|
32
|
+
* service.broadcast('hello, everyone!')
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://mswjs.io/docs/api/ws#broadcastdata `broadcast()` API reference}
|
|
36
|
+
*/
|
|
37
|
+
broadcast(data: WebSocketData): void;
|
|
38
|
+
/**
|
|
39
|
+
* Broadcasts the given data to all WebSocket clients
|
|
40
|
+
* except the ones provided in the `clients` argument.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const service = ws.link('wss://example.com')
|
|
44
|
+
* service.addEventListener('connection', ({ client }) => {
|
|
45
|
+
* service.broadcastExcept(client, 'hi, the rest of you!')
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://mswjs.io/docs/api/ws#broadcastexceptclients-data `broadcast()` API reference}
|
|
49
|
+
*/
|
|
50
|
+
broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Intercepts outgoing WebSocket connections to the given URL.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const chat = ws.link('wss://chat.example.com')
|
|
57
|
+
* chat.addEventListener('connection', ({ client }) => {
|
|
58
|
+
* client.send('hello from server!')
|
|
59
|
+
* })
|
|
60
|
+
*/
|
|
61
|
+
declare function createWebSocketLinkHandler(url: Path): WebSocketLink;
|
|
62
|
+
/**
|
|
63
|
+
* A namespace to intercept and mock WebSocket connections.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const chat = ws.link('wss://chat.example.com')
|
|
67
|
+
*
|
|
68
|
+
* @see {@link https://mswjs.io/docs/api/ws `ws` API reference}
|
|
69
|
+
* @see {@link https://mswjs.io/docs/basics/handling-websocket-events Handling WebSocket events}
|
|
70
|
+
*/
|
|
71
|
+
declare const ws: {
|
|
72
|
+
link: typeof createWebSocketLinkHandler;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { type WebSocketEventListener, type WebSocketLink, ws };
|
package/lib/core/ws.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
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("./ws/WebSocketClientManager.js");
|
|
28
|
+
function isBroadcastChannelWithUnref(channel) {
|
|
29
|
+
return typeof Reflect.get(channel, "unref") !== "undefined";
|
|
30
|
+
}
|
|
31
|
+
const webSocketChannel = new BroadcastChannel("msw:websocket-client-manager");
|
|
32
|
+
if (isBroadcastChannelWithUnref(webSocketChannel)) {
|
|
33
|
+
webSocketChannel.unref();
|
|
34
|
+
}
|
|
35
|
+
function createWebSocketLinkHandler(url) {
|
|
36
|
+
(0, import_outvariant.invariant)(url, "Expected a WebSocket server URL but got undefined");
|
|
37
|
+
(0, import_outvariant.invariant)(
|
|
38
|
+
(0, import_matchRequestUrl.isPath)(url),
|
|
39
|
+
"Expected a WebSocket server URL to be a valid path but got %s",
|
|
40
|
+
typeof url
|
|
41
|
+
);
|
|
42
|
+
const clientManager = new import_WebSocketClientManager.WebSocketClientManager(webSocketChannel);
|
|
43
|
+
return {
|
|
44
|
+
get clients() {
|
|
45
|
+
return clientManager.clients;
|
|
46
|
+
},
|
|
47
|
+
addEventListener(event, listener) {
|
|
48
|
+
const handler = new import_WebSocketHandler.WebSocketHandler(url);
|
|
49
|
+
handler[import_WebSocketHandler.kEmitter].on("connection", async ({ client }) => {
|
|
50
|
+
await clientManager.addConnection(client);
|
|
51
|
+
});
|
|
52
|
+
handler[import_WebSocketHandler.kEmitter].on(event, listener);
|
|
53
|
+
return handler;
|
|
54
|
+
},
|
|
55
|
+
broadcast(data) {
|
|
56
|
+
this.broadcastExcept([], data);
|
|
57
|
+
},
|
|
58
|
+
broadcastExcept(clients, data) {
|
|
59
|
+
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
60
|
+
clientManager.clients.forEach((otherClient) => {
|
|
61
|
+
if (!ignoreClients.includes(otherClient.id)) {
|
|
62
|
+
otherClient.send(data);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const ws = {
|
|
69
|
+
link: createWebSocketLinkHandler
|
|
70
|
+
};
|
|
71
|
+
//# sourceMappingURL=ws.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/ws.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport type {\n WebSocketData,\n WebSocketClientConnectionProtocol,\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\nfunction isBroadcastChannelWithUnref(\n channel: BroadcastChannel,\n): channel is BroadcastChannel & NodeJS.RefCounted {\n return typeof Reflect.get(channel, 'unref') !== 'undefined'\n}\n\nconst webSocketChannel = new BroadcastChannel('msw:websocket-client-manager')\n\nif (isBroadcastChannelWithUnref(webSocketChannel)) {\n // Allows the Node.js thread to exit if it is the only active handle in the event system.\n // https://nodejs.org/api/worker_threads.html#broadcastchannelunref\n webSocketChannel.unref()\n}\n\nexport type WebSocketEventListener<\n EventType extends keyof WebSocketHandlerEventMap,\n> = (...args: WebSocketHandlerEventMap[EventType]) => void\n\nexport type WebSocketLink = {\n /**\n * A set of all WebSocket clients connected\n * to this link.\n *\n * @see {@link https://mswjs.io/docs/api/ws#clients `clients` API reference}\n */\n clients: Set<WebSocketClientConnectionProtocol>\n\n /**\n * Adds an event listener to this WebSocket link.\n *\n * @example\n * const chat = ws.link('wss://chat.example.com')\n * chat.addEventListener('connection', listener)\n *\n * @see {@link https://mswjs.io/docs/api/ws#onevent-listener `on()` API reference}\n */\n addEventListener<EventType extends keyof WebSocketHandlerEventMap>(\n event: EventType,\n listener: WebSocketEventListener<EventType>,\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.addEventListener('connection', () => {\n * service.broadcast('hello, everyone!')\n * })\n *\n * @see {@link https://mswjs.io/docs/api/ws#broadcastdata `broadcast()` API reference}\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.addEventListener('connection', ({ client }) => {\n * service.broadcastExcept(client, 'hi, the rest of you!')\n * })\n *\n * @see {@link https://mswjs.io/docs/api/ws#broadcastexceptclients-data `broadcast()` API reference}\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.addEventListener('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(webSocketChannel)\n\n return {\n get clients() {\n return clientManager.clients\n },\n addEventListener(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', async ({ client }) => {\n await 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\n/**\n * A namespace to intercept and mock WebSocket connections.\n *\n * @example\n * const chat = ws.link('wss://chat.example.com')\n *\n * @see {@link https://mswjs.io/docs/api/ws `ws` API reference}\n * @see {@link https://mswjs.io/docs/basics/handling-websocket-events Handling WebSocket events}\n */\nexport const ws = {\n link: createWebSocketLinkHandler,\n}\n\nexport { WebSocketData }\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA0B;AAK1B,8BAIO;AACP,6BAA6B;AAC7B,oCAAuC;AAEvC,SAAS,4BACP,SACiD;AACjD,SAAO,OAAO,QAAQ,IAAI,SAAS,OAAO,MAAM;AAClD;AAEA,MAAM,mBAAmB,IAAI,iBAAiB,8BAA8B;AAE5E,IAAI,4BAA4B,gBAAgB,GAAG;AAGjD,mBAAiB,MAAM;AACzB;AAuEA,SAAS,2BAA2B,KAA0B;AAC5D,mCAAU,KAAK,mDAAmD;AAElE;AAAA,QACE,+BAAO,GAAG;AAAA,IACV;AAAA,IACA,OAAO;AAAA,EACT;AAEA,QAAM,gBAAgB,IAAI,qDAAuB,gBAAgB;AAEjE,SAAO;AAAA,IACL,IAAI,UAAU;AACZ,aAAO,cAAc;AAAA,IACvB;AAAA,IACA,iBAAiB,OAAO,UAAU;AAChC,YAAM,UAAU,IAAI,yCAAiB,GAAG;AAMxC,cAAQ,gCAAQ,EAAE,GAAG,cAAc,OAAO,EAAE,OAAO,MAAM;AACvD,cAAM,cAAc,cAAc,MAAM;AAAA,MAC1C,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;AAWO,MAAM,KAAK;AAAA,EAChB,MAAM;AACR;","names":[]}
|
package/lib/core/ws.mjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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 './ws/WebSocketClientManager.mjs';
|
|
8
|
+
function isBroadcastChannelWithUnref(channel) {
|
|
9
|
+
return typeof Reflect.get(channel, "unref") !== "undefined";
|
|
10
|
+
}
|
|
11
|
+
const webSocketChannel = new BroadcastChannel("msw:websocket-client-manager");
|
|
12
|
+
if (isBroadcastChannelWithUnref(webSocketChannel)) {
|
|
13
|
+
webSocketChannel.unref();
|
|
14
|
+
}
|
|
15
|
+
function createWebSocketLinkHandler(url) {
|
|
16
|
+
invariant(url, "Expected a WebSocket server URL but got undefined");
|
|
17
|
+
invariant(
|
|
18
|
+
isPath(url),
|
|
19
|
+
"Expected a WebSocket server URL to be a valid path but got %s",
|
|
20
|
+
typeof url
|
|
21
|
+
);
|
|
22
|
+
const clientManager = new WebSocketClientManager(webSocketChannel);
|
|
23
|
+
return {
|
|
24
|
+
get clients() {
|
|
25
|
+
return clientManager.clients;
|
|
26
|
+
},
|
|
27
|
+
addEventListener(event, listener) {
|
|
28
|
+
const handler = new WebSocketHandler(url);
|
|
29
|
+
handler[kEmitter].on("connection", async ({ client }) => {
|
|
30
|
+
await clientManager.addConnection(client);
|
|
31
|
+
});
|
|
32
|
+
handler[kEmitter].on(event, listener);
|
|
33
|
+
return handler;
|
|
34
|
+
},
|
|
35
|
+
broadcast(data) {
|
|
36
|
+
this.broadcastExcept([], data);
|
|
37
|
+
},
|
|
38
|
+
broadcastExcept(clients, data) {
|
|
39
|
+
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
40
|
+
clientManager.clients.forEach((otherClient) => {
|
|
41
|
+
if (!ignoreClients.includes(otherClient.id)) {
|
|
42
|
+
otherClient.send(data);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const ws = {
|
|
49
|
+
link: createWebSocketLinkHandler
|
|
50
|
+
};
|
|
51
|
+
export {
|
|
52
|
+
ws
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=ws.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/ws.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport type {\n WebSocketData,\n WebSocketClientConnectionProtocol,\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\nfunction isBroadcastChannelWithUnref(\n channel: BroadcastChannel,\n): channel is BroadcastChannel & NodeJS.RefCounted {\n return typeof Reflect.get(channel, 'unref') !== 'undefined'\n}\n\nconst webSocketChannel = new BroadcastChannel('msw:websocket-client-manager')\n\nif (isBroadcastChannelWithUnref(webSocketChannel)) {\n // Allows the Node.js thread to exit if it is the only active handle in the event system.\n // https://nodejs.org/api/worker_threads.html#broadcastchannelunref\n webSocketChannel.unref()\n}\n\nexport type WebSocketEventListener<\n EventType extends keyof WebSocketHandlerEventMap,\n> = (...args: WebSocketHandlerEventMap[EventType]) => void\n\nexport type WebSocketLink = {\n /**\n * A set of all WebSocket clients connected\n * to this link.\n *\n * @see {@link https://mswjs.io/docs/api/ws#clients `clients` API reference}\n */\n clients: Set<WebSocketClientConnectionProtocol>\n\n /**\n * Adds an event listener to this WebSocket link.\n *\n * @example\n * const chat = ws.link('wss://chat.example.com')\n * chat.addEventListener('connection', listener)\n *\n * @see {@link https://mswjs.io/docs/api/ws#onevent-listener `on()` API reference}\n */\n addEventListener<EventType extends keyof WebSocketHandlerEventMap>(\n event: EventType,\n listener: WebSocketEventListener<EventType>,\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.addEventListener('connection', () => {\n * service.broadcast('hello, everyone!')\n * })\n *\n * @see {@link https://mswjs.io/docs/api/ws#broadcastdata `broadcast()` API reference}\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.addEventListener('connection', ({ client }) => {\n * service.broadcastExcept(client, 'hi, the rest of you!')\n * })\n *\n * @see {@link https://mswjs.io/docs/api/ws#broadcastexceptclients-data `broadcast()` API reference}\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.addEventListener('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(webSocketChannel)\n\n return {\n get clients() {\n return clientManager.clients\n },\n addEventListener(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', async ({ client }) => {\n await 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\n/**\n * A namespace to intercept and mock WebSocket connections.\n *\n * @example\n * const chat = ws.link('wss://chat.example.com')\n *\n * @see {@link https://mswjs.io/docs/api/ws `ws` API reference}\n * @see {@link https://mswjs.io/docs/basics/handling-websocket-events Handling WebSocket events}\n */\nexport const ws = {\n link: createWebSocketLinkHandler,\n}\n\nexport { WebSocketData }\n"],"mappings":"AAAA,SAAS,iBAAiB;AAK1B;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAe,cAAc;AAC7B,SAAS,8BAA8B;AAEvC,SAAS,4BACP,SACiD;AACjD,SAAO,OAAO,QAAQ,IAAI,SAAS,OAAO,MAAM;AAClD;AAEA,MAAM,mBAAmB,IAAI,iBAAiB,8BAA8B;AAE5E,IAAI,4BAA4B,gBAAgB,GAAG;AAGjD,mBAAiB,MAAM;AACzB;AAuEA,SAAS,2BAA2B,KAA0B;AAC5D,YAAU,KAAK,mDAAmD;AAElE;AAAA,IACE,OAAO,GAAG;AAAA,IACV;AAAA,IACA,OAAO;AAAA,EACT;AAEA,QAAM,gBAAgB,IAAI,uBAAuB,gBAAgB;AAEjE,SAAO;AAAA,IACL,IAAI,UAAU;AACZ,aAAO,cAAc;AAAA,IACvB;AAAA,IACA,iBAAiB,OAAO,UAAU;AAChC,YAAM,UAAU,IAAI,iBAAiB,GAAG;AAMxC,cAAQ,QAAQ,EAAE,GAAG,cAAc,OAAO,EAAE,OAAO,MAAM;AACvD,cAAM,cAAc,cAAc,MAAM;AAAA,MAC1C,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;AAWO,MAAM,KAAK;AAAA,EAChB,MAAM;AACR;","names":[]}
|