msw 2.3.0-ws.rc-6 → 2.3.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/cli/init.js +0 -0
- package/config/scripts/postinstall.js +0 -0
- package/lib/browser/index.d.mts +6 -7
- package/lib/browser/index.d.ts +6 -7
- package/lib/browser/index.js +156 -83
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +156 -83
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-DGyzNtRQ.d.ts → GraphQLHandler-COiPfZ8k.d.ts} +1 -1
- package/lib/core/{GraphQLHandler-CzcXr7At.d.mts → GraphQLHandler-Dq_WRbKe.d.mts} +1 -1
- package/lib/core/{HttpResponse-BOzDwzB6.d.ts → HttpResponse-B07UKAkU.d.ts} +2 -2
- package/lib/core/{HttpResponse-CSyJ1cbO.d.mts → HttpResponse-C7niBMwb.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 +12 -15
- package/lib/core/SetupApi.d.ts +12 -15
- package/lib/core/SetupApi.js +1 -3
- package/lib/core/SetupApi.js.map +1 -1
- package/lib/core/SetupApi.mjs +1 -3
- 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/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +2 -5
- package/lib/core/index.d.ts +2 -5
- package/lib/core/index.js +1 -5
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +1 -7
- 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 +0 -4
- package/lib/core/utils/executeHandlers.js.map +1 -1
- package/lib/core/utils/executeHandlers.mjs +0 -6
- 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/Disposable.d.mts +2 -2
- package/lib/core/utils/internal/Disposable.d.ts +2 -2
- package/lib/core/utils/internal/Disposable.js +5 -2
- package/lib/core/utils/internal/Disposable.js.map +1 -1
- package/lib/core/utils/internal/Disposable.mjs +5 -2
- package/lib/core/utils/internal/Disposable.mjs.map +1 -1
- package/lib/core/utils/internal/devUtils.d.mts +10 -1
- package/lib/core/utils/internal/devUtils.d.ts +10 -1
- package/lib/core/utils/internal/devUtils.js +7 -0
- package/lib/core/utils/internal/devUtils.js.map +1 -1
- package/lib/core/utils/internal/devUtils.mjs +7 -0
- package/lib/core/utils/internal/devUtils.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 +1 -4
- package/lib/core/utils/logging/getTimestamp.d.ts +1 -4
- package/lib/core/utils/logging/getTimestamp.js +2 -6
- package/lib/core/utils/logging/getTimestamp.js.map +1 -1
- package/lib/core/utils/logging/getTimestamp.mjs +2 -6
- package/lib/core/utils/logging/getTimestamp.mjs.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.d.mts +1 -2
- package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -2
- package/lib/core/utils/matching/matchRequestUrl.js +0 -4
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs +0 -4
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.js +3 -3
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.mjs +4 -4
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
- package/lib/iife/index.js +177 -1103
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +5 -6
- package/lib/native/index.d.ts +5 -6
- package/lib/native/index.js +5 -13
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +6 -14
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +7 -8
- package/lib/node/index.d.ts +7 -8
- package/lib/node/index.js +5 -13
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +6 -14
- package/lib/node/index.mjs.map +1 -1
- package/package.json +36 -29
- package/src/browser/setupWorker/glossary.ts +10 -10
- package/src/browser/setupWorker/setupWorker.ts +3 -34
- package/src/browser/setupWorker/stop/createStop.ts +0 -4
- package/src/core/SetupApi.ts +20 -28
- package/src/core/index.ts +1 -8
- package/src/core/utils/executeHandlers.ts +2 -6
- package/src/core/utils/handleRequest.ts +2 -1
- package/src/core/utils/internal/Disposable.ts +6 -3
- package/src/core/utils/internal/devUtils.test.ts +21 -0
- package/src/core/utils/internal/devUtils.ts +13 -0
- package/src/core/utils/logging/getTimestamp.test.ts +6 -20
- package/src/core/utils/logging/getTimestamp.ts +6 -11
- package/src/core/utils/matching/matchRequestUrl.test.ts +0 -44
- package/src/core/utils/matching/matchRequestUrl.ts +0 -4
- package/src/core/utils/request/onUnhandledRequest.test.ts +30 -4
- package/src/core/utils/request/onUnhandledRequest.ts +4 -4
- package/src/node/SetupServerApi.ts +7 -8
- package/src/node/SetupServerCommonApi.ts +8 -15
- package/src/node/glossary.ts +7 -5
- package/src/node/setupServer.ts +1 -2
- package/lib/core/handlers/WebSocketHandler.d.mts +0 -32
- package/lib/core/handlers/WebSocketHandler.d.ts +0 -32
- package/lib/core/handlers/WebSocketHandler.js +0 -62
- package/lib/core/handlers/WebSocketHandler.js.map +0 -1
- package/lib/core/handlers/WebSocketHandler.mjs +0 -44
- package/lib/core/handlers/WebSocketHandler.mjs.map +0 -1
- package/lib/core/ws/WebSocketClientManager.d.mts +0 -58
- package/lib/core/ws/WebSocketClientManager.d.ts +0 -58
- package/lib/core/ws/WebSocketClientManager.js +0 -162
- package/lib/core/ws/WebSocketClientManager.js.map +0 -1
- package/lib/core/ws/WebSocketClientManager.mjs +0 -142
- package/lib/core/ws/WebSocketClientManager.mjs.map +0 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +0 -16
- package/lib/core/ws/handleWebSocketEvent.d.ts +0 -16
- package/lib/core/ws/handleWebSocketEvent.js +0 -56
- package/lib/core/ws/handleWebSocketEvent.js.map +0 -1
- package/lib/core/ws/handleWebSocketEvent.mjs +0 -36
- package/lib/core/ws/handleWebSocketEvent.mjs.map +0 -1
- package/lib/core/ws/utils/attachWebSocketLogger.d.mts +0 -34
- package/lib/core/ws/utils/attachWebSocketLogger.d.ts +0 -34
- package/lib/core/ws/utils/attachWebSocketLogger.js +0 -217
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +0 -1
- package/lib/core/ws/utils/attachWebSocketLogger.mjs +0 -197
- package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +0 -1
- package/lib/core/ws/utils/getMessageLength.d.mts +0 -11
- package/lib/core/ws/utils/getMessageLength.d.ts +0 -11
- package/lib/core/ws/utils/getMessageLength.js +0 -33
- package/lib/core/ws/utils/getMessageLength.js.map +0 -1
- package/lib/core/ws/utils/getMessageLength.mjs +0 -13
- package/lib/core/ws/utils/getMessageLength.mjs.map +0 -1
- package/lib/core/ws/utils/getPublicData.d.mts +0 -5
- package/lib/core/ws/utils/getPublicData.d.ts +0 -5
- package/lib/core/ws/utils/getPublicData.js +0 -36
- package/lib/core/ws/utils/getPublicData.js.map +0 -1
- package/lib/core/ws/utils/getPublicData.mjs +0 -16
- package/lib/core/ws/utils/getPublicData.mjs.map +0 -1
- package/lib/core/ws/utils/truncateMessage.d.mts +0 -3
- package/lib/core/ws/utils/truncateMessage.d.ts +0 -3
- package/lib/core/ws/utils/truncateMessage.js +0 -31
- package/lib/core/ws/utils/truncateMessage.js.map +0 -1
- package/lib/core/ws/utils/truncateMessage.mjs +0 -11
- package/lib/core/ws/utils/truncateMessage.mjs.map +0 -1
- package/lib/core/ws/webSocketInterceptor.d.mts +0 -5
- package/lib/core/ws/webSocketInterceptor.d.ts +0 -5
- package/lib/core/ws/webSocketInterceptor.js +0 -26
- package/lib/core/ws/webSocketInterceptor.js.map +0 -1
- package/lib/core/ws/webSocketInterceptor.mjs +0 -6
- package/lib/core/ws/webSocketInterceptor.mjs.map +0 -1
- package/lib/core/ws.d.mts +0 -49
- package/lib/core/ws.d.ts +0 -49
- package/lib/core/ws.js +0 -65
- package/lib/core/ws.js.map +0 -1
- package/lib/core/ws.mjs +0 -48
- package/lib/core/ws.mjs.map +0 -1
- package/src/core/handlers/WebSocketHandler.ts +0 -71
- package/src/core/ws/WebSocketClientManager.test.ts +0 -157
- package/src/core/ws/WebSocketClientManager.ts +0 -234
- package/src/core/ws/handleWebSocketEvent.ts +0 -56
- package/src/core/ws/utils/attachWebSocketLogger.ts +0 -269
- package/src/core/ws/utils/getMessageLength.test.ts +0 -16
- package/src/core/ws/utils/getMessageLength.ts +0 -19
- package/src/core/ws/utils/getPublicData.test.ts +0 -38
- package/src/core/ws/utils/getPublicData.ts +0 -17
- package/src/core/ws/utils/truncateMessage.test.ts +0 -12
- package/src/core/ws/utils/truncateMessage.ts +0 -9
- package/src/core/ws/webSocketInterceptor.ts +0 -3
- package/src/core/ws.test.ts +0 -23
- package/src/core/ws.ts +0 -124
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var truncateMessage_exports = {};
|
|
20
|
-
__export(truncateMessage_exports, {
|
|
21
|
-
truncateMessage: () => truncateMessage
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(truncateMessage_exports);
|
|
24
|
-
const MAX_LENGTH = 24;
|
|
25
|
-
function truncateMessage(message) {
|
|
26
|
-
if (message.length <= MAX_LENGTH) {
|
|
27
|
-
return message;
|
|
28
|
-
}
|
|
29
|
-
return `${message.slice(0, MAX_LENGTH)}\u2026`;
|
|
30
|
-
}
|
|
31
|
-
//# sourceMappingURL=truncateMessage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAAA;AAAA;AAAA;AAAA;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 +0,0 @@
|
|
|
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,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var 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
|
|
@@ -1 +0,0 @@
|
|
|
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":[]}
|
|
@@ -1 +0,0 @@
|
|
|
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":[]}
|
package/lib/core/ws.d.mts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
type WebSocketLink = {
|
|
7
|
-
/**
|
|
8
|
-
* A set of all WebSocket clients connected
|
|
9
|
-
* to this link.
|
|
10
|
-
*/
|
|
11
|
-
clients: Set<WebSocketClientConnectionProtocol>;
|
|
12
|
-
on<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: (...args: WebSocketHandlerEventMap[EventType]) => void): WebSocketHandler;
|
|
13
|
-
/**
|
|
14
|
-
* Broadcasts the given data to all WebSocket clients.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* const service = ws.link('wss://example.com')
|
|
18
|
-
* service.on('connection', () => {
|
|
19
|
-
* service.broadcast('hello, everyone!')
|
|
20
|
-
* })
|
|
21
|
-
*/
|
|
22
|
-
broadcast(data: WebSocketData): void;
|
|
23
|
-
/**
|
|
24
|
-
* Broadcasts the given data to all WebSocket clients
|
|
25
|
-
* except the ones provided in the `clients` argument.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* const service = ws.link('wss://example.com')
|
|
29
|
-
* service.on('connection', ({ client }) => {
|
|
30
|
-
* service.broadcastExcept(client, 'hi, the rest of you!')
|
|
31
|
-
* })
|
|
32
|
-
*/
|
|
33
|
-
broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
|
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;
|
|
45
|
-
declare const ws: {
|
|
46
|
-
link: typeof createWebSocketLinkHandler;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export { type WebSocketLink, ws };
|
package/lib/core/ws.d.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
type WebSocketLink = {
|
|
7
|
-
/**
|
|
8
|
-
* A set of all WebSocket clients connected
|
|
9
|
-
* to this link.
|
|
10
|
-
*/
|
|
11
|
-
clients: Set<WebSocketClientConnectionProtocol>;
|
|
12
|
-
on<EventType extends keyof WebSocketHandlerEventMap>(event: EventType, listener: (...args: WebSocketHandlerEventMap[EventType]) => void): WebSocketHandler;
|
|
13
|
-
/**
|
|
14
|
-
* Broadcasts the given data to all WebSocket clients.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* const service = ws.link('wss://example.com')
|
|
18
|
-
* service.on('connection', () => {
|
|
19
|
-
* service.broadcast('hello, everyone!')
|
|
20
|
-
* })
|
|
21
|
-
*/
|
|
22
|
-
broadcast(data: WebSocketData): void;
|
|
23
|
-
/**
|
|
24
|
-
* Broadcasts the given data to all WebSocket clients
|
|
25
|
-
* except the ones provided in the `clients` argument.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* const service = ws.link('wss://example.com')
|
|
29
|
-
* service.on('connection', ({ client }) => {
|
|
30
|
-
* service.broadcastExcept(client, 'hi, the rest of you!')
|
|
31
|
-
* })
|
|
32
|
-
*/
|
|
33
|
-
broadcastExcept(clients: WebSocketClientConnectionProtocol | Array<WebSocketClientConnectionProtocol>, data: WebSocketData): void;
|
|
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;
|
|
45
|
-
declare const ws: {
|
|
46
|
-
link: typeof createWebSocketLinkHandler;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export { type WebSocketLink, ws };
|
package/lib/core/ws.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var 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
|
-
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 to be a valid path but got %s",
|
|
34
|
-
typeof url
|
|
35
|
-
);
|
|
36
|
-
const clientManager = new import_WebSocketClientManager.WebSocketClientManager(wsBroadcastChannel, url);
|
|
37
|
-
return {
|
|
38
|
-
get clients() {
|
|
39
|
-
return clientManager.clients;
|
|
40
|
-
},
|
|
41
|
-
on(event, listener) {
|
|
42
|
-
const handler = new import_WebSocketHandler.WebSocketHandler(url);
|
|
43
|
-
handler[import_WebSocketHandler.kEmitter].on("connection", ({ client }) => {
|
|
44
|
-
clientManager.addConnection(client);
|
|
45
|
-
});
|
|
46
|
-
handler[import_WebSocketHandler.kEmitter].on(event, listener);
|
|
47
|
-
return handler;
|
|
48
|
-
},
|
|
49
|
-
broadcast(data) {
|
|
50
|
-
this.broadcastExcept([], data);
|
|
51
|
-
},
|
|
52
|
-
broadcastExcept(clients, data) {
|
|
53
|
-
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
54
|
-
clientManager.clients.forEach((otherClient) => {
|
|
55
|
-
if (!ignoreClients.includes(otherClient.id)) {
|
|
56
|
-
otherClient.send(data);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
const ws = {
|
|
63
|
-
link: createWebSocketLinkHandler
|
|
64
|
-
};
|
|
65
|
-
//# sourceMappingURL=ws.js.map
|
package/lib/core/ws.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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, url)\n\n return {\n get clients() {\n return clientManager.clients\n },\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,oBAAoB,GAAG;AAExE,SAAO;AAAA,IACL,IAAI,UAAU;AACZ,aAAO,cAAc;AAAA,IACvB;AAAA,IACA,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":[]}
|
package/lib/core/ws.mjs
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
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 to be a valid path but got %s",
|
|
14
|
-
typeof url
|
|
15
|
-
);
|
|
16
|
-
const clientManager = new WebSocketClientManager(wsBroadcastChannel, url);
|
|
17
|
-
return {
|
|
18
|
-
get clients() {
|
|
19
|
-
return clientManager.clients;
|
|
20
|
-
},
|
|
21
|
-
on(event, listener) {
|
|
22
|
-
const handler = new WebSocketHandler(url);
|
|
23
|
-
handler[kEmitter].on("connection", ({ client }) => {
|
|
24
|
-
clientManager.addConnection(client);
|
|
25
|
-
});
|
|
26
|
-
handler[kEmitter].on(event, listener);
|
|
27
|
-
return handler;
|
|
28
|
-
},
|
|
29
|
-
broadcast(data) {
|
|
30
|
-
this.broadcastExcept([], data);
|
|
31
|
-
},
|
|
32
|
-
broadcastExcept(clients, data) {
|
|
33
|
-
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
34
|
-
clientManager.clients.forEach((otherClient) => {
|
|
35
|
-
if (!ignoreClients.includes(otherClient.id)) {
|
|
36
|
-
otherClient.send(data);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
const ws = {
|
|
43
|
-
link: createWebSocketLinkHandler
|
|
44
|
-
};
|
|
45
|
-
export {
|
|
46
|
-
ws
|
|
47
|
-
};
|
|
48
|
-
//# sourceMappingURL=ws.mjs.map
|
package/lib/core/ws.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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, url)\n\n return {\n get clients() {\n return clientManager.clients\n },\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,oBAAoB,GAAG;AAExE,SAAO;AAAA,IACL,IAAI,UAAU;AACZ,aAAO,cAAc;AAAA,IACvB;AAAA,IACA,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":[]}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Emitter } from 'strict-event-emitter'
|
|
2
|
-
import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
|
|
3
|
-
import {
|
|
4
|
-
type Match,
|
|
5
|
-
type Path,
|
|
6
|
-
type PathParams,
|
|
7
|
-
matchRequestUrl,
|
|
8
|
-
} from '../utils/matching/matchRequestUrl'
|
|
9
|
-
import { getCallFrame } from '../utils/internal/getCallFrame'
|
|
10
|
-
|
|
11
|
-
type WebSocketHandlerParsedResult = {
|
|
12
|
-
match: Match
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type WebSocketHandlerEventMap = {
|
|
16
|
-
connection: [args: WebSocketHandlerConnection]
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interface WebSocketHandlerConnection extends WebSocketConnectionData {
|
|
20
|
-
params: PathParams
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const kEmitter = Symbol('kEmitter')
|
|
24
|
-
export const kDispatchEvent = Symbol('kDispatchEvent')
|
|
25
|
-
export const kSender = Symbol('kSender')
|
|
26
|
-
|
|
27
|
-
export class WebSocketHandler {
|
|
28
|
-
public callFrame?: string
|
|
29
|
-
|
|
30
|
-
protected [kEmitter]: Emitter<WebSocketHandlerEventMap>
|
|
31
|
-
|
|
32
|
-
constructor(private readonly url: Path) {
|
|
33
|
-
this[kEmitter] = new Emitter()
|
|
34
|
-
this.callFrame = getCallFrame(new Error())
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public parse(args: {
|
|
38
|
-
event: MessageEvent<WebSocketConnectionData>
|
|
39
|
-
}): WebSocketHandlerParsedResult {
|
|
40
|
-
const connection = args.event.data
|
|
41
|
-
const match = matchRequestUrl(connection.client.url, this.url)
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
match,
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public predicate(args: {
|
|
49
|
-
event: MessageEvent<WebSocketConnectionData>
|
|
50
|
-
parsedResult: WebSocketHandlerParsedResult
|
|
51
|
-
}): boolean {
|
|
52
|
-
return args.parsedResult.match.matches
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async [kDispatchEvent](
|
|
56
|
-
event: MessageEvent<WebSocketConnectionData>,
|
|
57
|
-
): Promise<void> {
|
|
58
|
-
const parsedResult = this.parse({ event })
|
|
59
|
-
const connection = event.data
|
|
60
|
-
|
|
61
|
-
const resolvedConnection: WebSocketHandlerConnection = {
|
|
62
|
-
client: connection.client,
|
|
63
|
-
server: connection.server,
|
|
64
|
-
params: parsedResult.match.params || {},
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Emit the connection event on the handler.
|
|
68
|
-
// This is what the developer adds listeners for.
|
|
69
|
-
this[kEmitter].emit('connection', resolvedConnection)
|
|
70
|
-
}
|
|
71
|
-
}
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment node-websocket
|
|
3
|
-
*/
|
|
4
|
-
import {
|
|
5
|
-
WebSocketClientConnection,
|
|
6
|
-
WebSocketData,
|
|
7
|
-
WebSocketTransport,
|
|
8
|
-
} from '@mswjs/interceptors/WebSocket'
|
|
9
|
-
import {
|
|
10
|
-
WebSocketClientManager,
|
|
11
|
-
WebSocketBroadcastChannelMessage,
|
|
12
|
-
} from './WebSocketClientManager'
|
|
13
|
-
|
|
14
|
-
const channel = new BroadcastChannel('test:channel')
|
|
15
|
-
vi.spyOn(channel, 'postMessage')
|
|
16
|
-
|
|
17
|
-
const socket = new WebSocket('ws://localhost')
|
|
18
|
-
|
|
19
|
-
class TestWebSocketTransport extends EventTarget implements WebSocketTransport {
|
|
20
|
-
send(_data: WebSocketData): void {}
|
|
21
|
-
close(_code?: number | undefined, _reason?: string | undefined): void {}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
afterEach(() => {
|
|
25
|
-
vi.resetAllMocks()
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('adds a client from this runtime to the list of clients', () => {
|
|
29
|
-
const manager = new WebSocketClientManager(channel, '*')
|
|
30
|
-
const connection = new WebSocketClientConnection(
|
|
31
|
-
socket,
|
|
32
|
-
new TestWebSocketTransport(),
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
manager.addConnection(connection)
|
|
36
|
-
|
|
37
|
-
// Must add the client to the list of clients.
|
|
38
|
-
expect(Array.from(manager.clients.values())).toEqual([connection])
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('adds multiple clients from this runtime to the list of clients', () => {
|
|
42
|
-
const manager = new WebSocketClientManager(channel, '*')
|
|
43
|
-
const connectionOne = new WebSocketClientConnection(
|
|
44
|
-
socket,
|
|
45
|
-
new TestWebSocketTransport(),
|
|
46
|
-
)
|
|
47
|
-
manager.addConnection(connectionOne)
|
|
48
|
-
|
|
49
|
-
// Must add the client to the list of clients.
|
|
50
|
-
expect(Array.from(manager.clients.values())).toEqual([connectionOne])
|
|
51
|
-
|
|
52
|
-
const connectionTwo = new WebSocketClientConnection(
|
|
53
|
-
socket,
|
|
54
|
-
new TestWebSocketTransport(),
|
|
55
|
-
)
|
|
56
|
-
manager.addConnection(connectionTwo)
|
|
57
|
-
|
|
58
|
-
// Must add the new cilent to the list as well.
|
|
59
|
-
expect(Array.from(manager.clients.values())).toEqual([
|
|
60
|
-
connectionOne,
|
|
61
|
-
connectionTwo,
|
|
62
|
-
])
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('replays a "send" event coming from another runtime', async () => {
|
|
66
|
-
const manager = new WebSocketClientManager(channel, '*')
|
|
67
|
-
const connection = new WebSocketClientConnection(
|
|
68
|
-
socket,
|
|
69
|
-
new TestWebSocketTransport(),
|
|
70
|
-
)
|
|
71
|
-
manager.addConnection(connection)
|
|
72
|
-
vi.spyOn(connection, 'send')
|
|
73
|
-
|
|
74
|
-
// Emulate another runtime signaling this connection to receive data.
|
|
75
|
-
channel.dispatchEvent(
|
|
76
|
-
new MessageEvent<WebSocketBroadcastChannelMessage>('message', {
|
|
77
|
-
data: {
|
|
78
|
-
type: 'extraneous:send',
|
|
79
|
-
payload: {
|
|
80
|
-
clientId: connection.id,
|
|
81
|
-
data: 'hello',
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
}),
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
await vi.waitFor(() => {
|
|
88
|
-
// Must execute the requested operation on the connection.
|
|
89
|
-
expect(connection.send).toHaveBeenCalledWith('hello')
|
|
90
|
-
expect(connection.send).toHaveBeenCalledTimes(1)
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('replays a "close" event coming from another runtime', async () => {
|
|
95
|
-
const manager = new WebSocketClientManager(channel, '*')
|
|
96
|
-
const connection = new WebSocketClientConnection(
|
|
97
|
-
socket,
|
|
98
|
-
new TestWebSocketTransport(),
|
|
99
|
-
)
|
|
100
|
-
manager.addConnection(connection)
|
|
101
|
-
vi.spyOn(connection, 'close')
|
|
102
|
-
|
|
103
|
-
// Emulate another runtime signaling this connection to close.
|
|
104
|
-
channel.dispatchEvent(
|
|
105
|
-
new MessageEvent<WebSocketBroadcastChannelMessage>('message', {
|
|
106
|
-
data: {
|
|
107
|
-
type: 'extraneous:close',
|
|
108
|
-
payload: {
|
|
109
|
-
clientId: connection.id,
|
|
110
|
-
code: 1000,
|
|
111
|
-
reason: 'Normal closure',
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
}),
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
await vi.waitFor(() => {
|
|
118
|
-
// Must execute the requested operation on the connection.
|
|
119
|
-
expect(connection.close).toHaveBeenCalledWith(1000, 'Normal closure')
|
|
120
|
-
expect(connection.close).toHaveBeenCalledTimes(1)
|
|
121
|
-
})
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
it('removes the extraneous message listener when the connection closes', async () => {
|
|
125
|
-
const manager = new WebSocketClientManager(channel, '*')
|
|
126
|
-
const transport = new TestWebSocketTransport()
|
|
127
|
-
const connection = new WebSocketClientConnection(socket, transport)
|
|
128
|
-
vi.spyOn(connection, 'close').mockImplementationOnce(() => {
|
|
129
|
-
/**
|
|
130
|
-
* @note This is a nasty hack so we don't have to uncouple
|
|
131
|
-
* the connection from transport. Creating a mock transport
|
|
132
|
-
* is difficult because it relies on the `WebSocketOverride` class.
|
|
133
|
-
* All we care here is that closing the connection triggers
|
|
134
|
-
* the transport closure, which it always does.
|
|
135
|
-
*/
|
|
136
|
-
transport.dispatchEvent(new Event('close'))
|
|
137
|
-
})
|
|
138
|
-
vi.spyOn(connection, 'send')
|
|
139
|
-
|
|
140
|
-
manager.addConnection(connection)
|
|
141
|
-
connection.close()
|
|
142
|
-
|
|
143
|
-
// Signals from other runtimes have no effect on the closed connection.
|
|
144
|
-
channel.dispatchEvent(
|
|
145
|
-
new MessageEvent<WebSocketBroadcastChannelMessage>('message', {
|
|
146
|
-
data: {
|
|
147
|
-
type: 'extraneous:send',
|
|
148
|
-
payload: {
|
|
149
|
-
clientId: connection.id,
|
|
150
|
-
data: 'hello',
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
}),
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
expect(connection.send).not.toHaveBeenCalled()
|
|
157
|
-
})
|