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
package/lib/iife/index.js
CHANGED
|
@@ -13336,6 +13336,7 @@ ${operationTypes.join("\n")}
|
|
|
13336
13336
|
SET_TIMEOUT_MAX_ALLOWED_INT: () => SET_TIMEOUT_MAX_ALLOWED_INT,
|
|
13337
13337
|
SetupApi: () => SetupApi,
|
|
13338
13338
|
SetupWorkerApi: () => SetupWorkerApi,
|
|
13339
|
+
WebSocketHandler: () => WebSocketHandler,
|
|
13339
13340
|
bypass: () => bypass,
|
|
13340
13341
|
cleanUrl: () => cleanUrl,
|
|
13341
13342
|
delay: () => delay,
|
|
@@ -13345,7 +13346,8 @@ ${operationTypes.join("\n")}
|
|
|
13345
13346
|
http: () => http,
|
|
13346
13347
|
matchRequestUrl: () => matchRequestUrl,
|
|
13347
13348
|
passthrough: () => passthrough,
|
|
13348
|
-
setupWorker: () => setupWorker
|
|
13349
|
+
setupWorker: () => setupWorker,
|
|
13350
|
+
ws: () => ws
|
|
13349
13351
|
});
|
|
13350
13352
|
|
|
13351
13353
|
// node_modules/.pnpm/outvariant@1.4.3/node_modules/outvariant/lib/index.mjs
|
|
@@ -13717,7 +13719,9 @@ ${operationTypes.join("\n")}
|
|
|
13717
13719
|
}
|
|
13718
13720
|
restoreHandlers() {
|
|
13719
13721
|
this.handlersController.currentHandlers().forEach((handler) => {
|
|
13720
|
-
|
|
13722
|
+
if ("isUsed" in handler) {
|
|
13723
|
+
handler.isUsed = false;
|
|
13724
|
+
}
|
|
13721
13725
|
});
|
|
13722
13726
|
}
|
|
13723
13727
|
resetHandlers(...nextHandlers) {
|
|
@@ -13932,9 +13936,13 @@ ${operationTypes.join("\n")}
|
|
|
13932
13936
|
}
|
|
13933
13937
|
|
|
13934
13938
|
// src/core/utils/logging/getTimestamp.ts
|
|
13935
|
-
function getTimestamp() {
|
|
13939
|
+
function getTimestamp(options) {
|
|
13936
13940
|
const now = /* @__PURE__ */ new Date();
|
|
13937
|
-
|
|
13941
|
+
const timestamp = `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}`;
|
|
13942
|
+
if (options?.milliseconds) {
|
|
13943
|
+
return `${timestamp}.${now.getMilliseconds().toString().padStart(3, "0")}`;
|
|
13944
|
+
}
|
|
13945
|
+
return timestamp;
|
|
13938
13946
|
}
|
|
13939
13947
|
|
|
13940
13948
|
// src/core/utils/logging/serializeRequest.ts
|
|
@@ -15057,6 +15065,9 @@ ${operationTypes.join("\n")}
|
|
|
15057
15065
|
params
|
|
15058
15066
|
};
|
|
15059
15067
|
}
|
|
15068
|
+
function isPath(value) {
|
|
15069
|
+
return typeof value === "string" || value instanceof RegExp;
|
|
15070
|
+
}
|
|
15060
15071
|
|
|
15061
15072
|
// src/core/utils/request/toPublicUrl.ts
|
|
15062
15073
|
function toPublicUrl(url) {
|
|
@@ -28154,6 +28165,458 @@ Consider naming this operation or using "graphql.operation()" request handler to
|
|
|
28154
28165
|
link: createGraphQLLink
|
|
28155
28166
|
};
|
|
28156
28167
|
|
|
28168
|
+
// src/core/handlers/WebSocketHandler.ts
|
|
28169
|
+
var kEmitter = Symbol("kEmitter");
|
|
28170
|
+
var kDispatchEvent = Symbol("kDispatchEvent");
|
|
28171
|
+
var kSender = Symbol("kSender");
|
|
28172
|
+
var kStopPropagationPatched = Symbol("kStopPropagationPatched");
|
|
28173
|
+
var KOnStopPropagation = Symbol("KOnStopPropagation");
|
|
28174
|
+
var WebSocketHandler = class {
|
|
28175
|
+
constructor(url) {
|
|
28176
|
+
this.url = url;
|
|
28177
|
+
this.id = createRequestId();
|
|
28178
|
+
this[kEmitter] = new Emitter();
|
|
28179
|
+
this.callFrame = getCallFrame(new Error());
|
|
28180
|
+
}
|
|
28181
|
+
id;
|
|
28182
|
+
callFrame;
|
|
28183
|
+
[kEmitter];
|
|
28184
|
+
parse(args) {
|
|
28185
|
+
const connection = args.event.data;
|
|
28186
|
+
const match2 = matchRequestUrl(connection.client.url, this.url);
|
|
28187
|
+
return {
|
|
28188
|
+
match: match2
|
|
28189
|
+
};
|
|
28190
|
+
}
|
|
28191
|
+
predicate(args) {
|
|
28192
|
+
return args.parsedResult.match.matches;
|
|
28193
|
+
}
|
|
28194
|
+
async [kDispatchEvent](event) {
|
|
28195
|
+
const parsedResult = this.parse({ event });
|
|
28196
|
+
const connection = event.data;
|
|
28197
|
+
const resolvedConnection = {
|
|
28198
|
+
...connection,
|
|
28199
|
+
params: parsedResult.match.params || {}
|
|
28200
|
+
};
|
|
28201
|
+
connection.client.addEventListener(
|
|
28202
|
+
"message",
|
|
28203
|
+
createStopPropagationListener(this)
|
|
28204
|
+
);
|
|
28205
|
+
connection.client.addEventListener(
|
|
28206
|
+
"close",
|
|
28207
|
+
createStopPropagationListener(this)
|
|
28208
|
+
);
|
|
28209
|
+
connection.server.addEventListener(
|
|
28210
|
+
"open",
|
|
28211
|
+
createStopPropagationListener(this)
|
|
28212
|
+
);
|
|
28213
|
+
connection.server.addEventListener(
|
|
28214
|
+
"message",
|
|
28215
|
+
createStopPropagationListener(this)
|
|
28216
|
+
);
|
|
28217
|
+
connection.server.addEventListener(
|
|
28218
|
+
"error",
|
|
28219
|
+
createStopPropagationListener(this)
|
|
28220
|
+
);
|
|
28221
|
+
connection.server.addEventListener(
|
|
28222
|
+
"close",
|
|
28223
|
+
createStopPropagationListener(this)
|
|
28224
|
+
);
|
|
28225
|
+
this[kEmitter].emit("connection", resolvedConnection);
|
|
28226
|
+
}
|
|
28227
|
+
};
|
|
28228
|
+
function createStopPropagationListener(handler) {
|
|
28229
|
+
return function stopPropagationListener(event) {
|
|
28230
|
+
const propagationStoppedAt = Reflect.get(event, "kPropagationStoppedAt");
|
|
28231
|
+
if (propagationStoppedAt && handler.id !== propagationStoppedAt) {
|
|
28232
|
+
event.stopImmediatePropagation();
|
|
28233
|
+
return;
|
|
28234
|
+
}
|
|
28235
|
+
Object.defineProperty(event, KOnStopPropagation, {
|
|
28236
|
+
value() {
|
|
28237
|
+
Object.defineProperty(event, "kPropagationStoppedAt", {
|
|
28238
|
+
value: handler.id
|
|
28239
|
+
});
|
|
28240
|
+
},
|
|
28241
|
+
configurable: true
|
|
28242
|
+
});
|
|
28243
|
+
if (!Reflect.get(event, kStopPropagationPatched)) {
|
|
28244
|
+
event.stopPropagation = new Proxy(event.stopPropagation, {
|
|
28245
|
+
apply: (target, thisArg, args) => {
|
|
28246
|
+
Reflect.get(event, KOnStopPropagation)?.call(handler);
|
|
28247
|
+
return Reflect.apply(target, thisArg, args);
|
|
28248
|
+
}
|
|
28249
|
+
});
|
|
28250
|
+
Object.defineProperty(event, kStopPropagationPatched, {
|
|
28251
|
+
value: true,
|
|
28252
|
+
// If something else attempts to redefine this, throw.
|
|
28253
|
+
configurable: false
|
|
28254
|
+
});
|
|
28255
|
+
}
|
|
28256
|
+
};
|
|
28257
|
+
}
|
|
28258
|
+
|
|
28259
|
+
// src/core/ws/WebSocketMemoryClientStore.ts
|
|
28260
|
+
var WebSocketMemoryClientStore = class {
|
|
28261
|
+
store;
|
|
28262
|
+
constructor() {
|
|
28263
|
+
this.store = /* @__PURE__ */ new Map();
|
|
28264
|
+
}
|
|
28265
|
+
async add(client) {
|
|
28266
|
+
this.store.set(client.id, { id: client.id, url: client.url.href });
|
|
28267
|
+
}
|
|
28268
|
+
getAll() {
|
|
28269
|
+
return Promise.resolve(Array.from(this.store.values()));
|
|
28270
|
+
}
|
|
28271
|
+
async deleteMany(clientIds) {
|
|
28272
|
+
for (const clientId of clientIds) {
|
|
28273
|
+
this.store.delete(clientId);
|
|
28274
|
+
}
|
|
28275
|
+
}
|
|
28276
|
+
};
|
|
28277
|
+
|
|
28278
|
+
// node_modules/.pnpm/@open-draft+deferred-promise@2.2.0/node_modules/@open-draft/deferred-promise/build/index.mjs
|
|
28279
|
+
function createDeferredExecutor() {
|
|
28280
|
+
const executor = (resolve, reject) => {
|
|
28281
|
+
executor.state = "pending";
|
|
28282
|
+
executor.resolve = (data) => {
|
|
28283
|
+
if (executor.state !== "pending") {
|
|
28284
|
+
return;
|
|
28285
|
+
}
|
|
28286
|
+
executor.result = data;
|
|
28287
|
+
const onFulfilled = (value) => {
|
|
28288
|
+
executor.state = "fulfilled";
|
|
28289
|
+
return value;
|
|
28290
|
+
};
|
|
28291
|
+
return resolve(
|
|
28292
|
+
data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
|
|
28293
|
+
);
|
|
28294
|
+
};
|
|
28295
|
+
executor.reject = (reason) => {
|
|
28296
|
+
if (executor.state !== "pending") {
|
|
28297
|
+
return;
|
|
28298
|
+
}
|
|
28299
|
+
queueMicrotask(() => {
|
|
28300
|
+
executor.state = "rejected";
|
|
28301
|
+
});
|
|
28302
|
+
return reject(executor.rejectionReason = reason);
|
|
28303
|
+
};
|
|
28304
|
+
};
|
|
28305
|
+
return executor;
|
|
28306
|
+
}
|
|
28307
|
+
var DeferredPromise = class extends Promise {
|
|
28308
|
+
#executor;
|
|
28309
|
+
resolve;
|
|
28310
|
+
reject;
|
|
28311
|
+
constructor(executor = null) {
|
|
28312
|
+
const deferredExecutor = createDeferredExecutor();
|
|
28313
|
+
super((originalResolve, originalReject) => {
|
|
28314
|
+
deferredExecutor(originalResolve, originalReject);
|
|
28315
|
+
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
|
28316
|
+
});
|
|
28317
|
+
this.#executor = deferredExecutor;
|
|
28318
|
+
this.resolve = this.#executor.resolve;
|
|
28319
|
+
this.reject = this.#executor.reject;
|
|
28320
|
+
}
|
|
28321
|
+
get state() {
|
|
28322
|
+
return this.#executor.state;
|
|
28323
|
+
}
|
|
28324
|
+
get rejectionReason() {
|
|
28325
|
+
return this.#executor.rejectionReason;
|
|
28326
|
+
}
|
|
28327
|
+
then(onFulfilled, onRejected) {
|
|
28328
|
+
return this.#decorate(super.then(onFulfilled, onRejected));
|
|
28329
|
+
}
|
|
28330
|
+
catch(onRejected) {
|
|
28331
|
+
return this.#decorate(super.catch(onRejected));
|
|
28332
|
+
}
|
|
28333
|
+
finally(onfinally) {
|
|
28334
|
+
return this.#decorate(super.finally(onfinally));
|
|
28335
|
+
}
|
|
28336
|
+
#decorate(promise) {
|
|
28337
|
+
return Object.defineProperties(promise, {
|
|
28338
|
+
resolve: { configurable: true, value: this.resolve },
|
|
28339
|
+
reject: { configurable: true, value: this.reject }
|
|
28340
|
+
});
|
|
28341
|
+
}
|
|
28342
|
+
};
|
|
28343
|
+
|
|
28344
|
+
// src/core/ws/WebSocketIndexedDBClientStore.ts
|
|
28345
|
+
var DB_NAME = "msw-websocket-clients";
|
|
28346
|
+
var DB_STORE_NAME = "clients";
|
|
28347
|
+
var WebSocketIndexedDBClientStore = class {
|
|
28348
|
+
db;
|
|
28349
|
+
constructor() {
|
|
28350
|
+
this.db = this.createDatabase();
|
|
28351
|
+
}
|
|
28352
|
+
async add(client) {
|
|
28353
|
+
const promise = new DeferredPromise();
|
|
28354
|
+
const store2 = await this.getStore();
|
|
28355
|
+
const request = store2.put({
|
|
28356
|
+
id: client.id,
|
|
28357
|
+
url: client.url.href
|
|
28358
|
+
});
|
|
28359
|
+
request.onsuccess = () => {
|
|
28360
|
+
promise.resolve();
|
|
28361
|
+
};
|
|
28362
|
+
request.onerror = () => {
|
|
28363
|
+
console.error(request.error);
|
|
28364
|
+
promise.reject(
|
|
28365
|
+
new Error(
|
|
28366
|
+
`Failed to add WebSocket client "${client.id}". There is likely an additional output above.`
|
|
28367
|
+
)
|
|
28368
|
+
);
|
|
28369
|
+
};
|
|
28370
|
+
return promise;
|
|
28371
|
+
}
|
|
28372
|
+
async getAll() {
|
|
28373
|
+
const promise = new DeferredPromise();
|
|
28374
|
+
const store2 = await this.getStore();
|
|
28375
|
+
const request = store2.getAll();
|
|
28376
|
+
request.onsuccess = () => {
|
|
28377
|
+
promise.resolve(request.result);
|
|
28378
|
+
};
|
|
28379
|
+
request.onerror = () => {
|
|
28380
|
+
console.log(request.error);
|
|
28381
|
+
promise.reject(
|
|
28382
|
+
new Error(
|
|
28383
|
+
`Failed to get all WebSocket clients. There is likely an additional output above.`
|
|
28384
|
+
)
|
|
28385
|
+
);
|
|
28386
|
+
};
|
|
28387
|
+
return promise;
|
|
28388
|
+
}
|
|
28389
|
+
async deleteMany(clientIds) {
|
|
28390
|
+
const promise = new DeferredPromise();
|
|
28391
|
+
const store2 = await this.getStore();
|
|
28392
|
+
for (const clientId of clientIds) {
|
|
28393
|
+
store2.delete(clientId);
|
|
28394
|
+
}
|
|
28395
|
+
store2.transaction.oncomplete = () => {
|
|
28396
|
+
promise.resolve();
|
|
28397
|
+
};
|
|
28398
|
+
store2.transaction.onerror = () => {
|
|
28399
|
+
console.error(store2.transaction.error);
|
|
28400
|
+
promise.reject(
|
|
28401
|
+
new Error(
|
|
28402
|
+
`Failed to delete WebSocket clients [${clientIds.join(", ")}]. There is likely an additional output above.`
|
|
28403
|
+
)
|
|
28404
|
+
);
|
|
28405
|
+
};
|
|
28406
|
+
return promise;
|
|
28407
|
+
}
|
|
28408
|
+
async createDatabase() {
|
|
28409
|
+
const promise = new DeferredPromise();
|
|
28410
|
+
const request = indexedDB.open(DB_NAME, 1);
|
|
28411
|
+
request.onsuccess = ({ currentTarget }) => {
|
|
28412
|
+
const db = Reflect.get(currentTarget, "result");
|
|
28413
|
+
if (db.objectStoreNames.contains(DB_STORE_NAME)) {
|
|
28414
|
+
return promise.resolve(db);
|
|
28415
|
+
}
|
|
28416
|
+
};
|
|
28417
|
+
request.onupgradeneeded = async ({ currentTarget }) => {
|
|
28418
|
+
const db = Reflect.get(currentTarget, "result");
|
|
28419
|
+
if (db.objectStoreNames.contains(DB_STORE_NAME)) {
|
|
28420
|
+
return;
|
|
28421
|
+
}
|
|
28422
|
+
const store2 = db.createObjectStore(DB_STORE_NAME, { keyPath: "id" });
|
|
28423
|
+
store2.transaction.oncomplete = () => {
|
|
28424
|
+
promise.resolve(db);
|
|
28425
|
+
};
|
|
28426
|
+
store2.transaction.onerror = () => {
|
|
28427
|
+
console.error(store2.transaction.error);
|
|
28428
|
+
promise.reject(
|
|
28429
|
+
new Error(
|
|
28430
|
+
"Failed to create WebSocket client store. There is likely an additional output above."
|
|
28431
|
+
)
|
|
28432
|
+
);
|
|
28433
|
+
};
|
|
28434
|
+
};
|
|
28435
|
+
request.onerror = () => {
|
|
28436
|
+
console.error(request.error);
|
|
28437
|
+
promise.reject(
|
|
28438
|
+
new Error(
|
|
28439
|
+
"Failed to open an IndexedDB database. There is likely an additional output above."
|
|
28440
|
+
)
|
|
28441
|
+
);
|
|
28442
|
+
};
|
|
28443
|
+
return promise;
|
|
28444
|
+
}
|
|
28445
|
+
async getStore() {
|
|
28446
|
+
const db = await this.db;
|
|
28447
|
+
return db.transaction(DB_STORE_NAME, "readwrite").objectStore(DB_STORE_NAME);
|
|
28448
|
+
}
|
|
28449
|
+
};
|
|
28450
|
+
|
|
28451
|
+
// src/core/ws/WebSocketClientManager.ts
|
|
28452
|
+
var WebSocketClientManager = class {
|
|
28453
|
+
constructor(channel) {
|
|
28454
|
+
this.channel = channel;
|
|
28455
|
+
this.store = typeof indexedDB !== "undefined" ? new WebSocketIndexedDBClientStore() : new WebSocketMemoryClientStore();
|
|
28456
|
+
this.runtimeClients = /* @__PURE__ */ new Map();
|
|
28457
|
+
this.allClients = /* @__PURE__ */ new Set();
|
|
28458
|
+
this.channel.addEventListener("message", (message3) => {
|
|
28459
|
+
if (message3.data?.type === "db:update") {
|
|
28460
|
+
this.flushDatabaseToMemory();
|
|
28461
|
+
}
|
|
28462
|
+
});
|
|
28463
|
+
if (typeof window !== "undefined") {
|
|
28464
|
+
window.addEventListener("message", async (message3) => {
|
|
28465
|
+
if (message3.data?.type === "msw/worker:stop") {
|
|
28466
|
+
await this.removeRuntimeClients();
|
|
28467
|
+
}
|
|
28468
|
+
});
|
|
28469
|
+
}
|
|
28470
|
+
}
|
|
28471
|
+
store;
|
|
28472
|
+
runtimeClients;
|
|
28473
|
+
allClients;
|
|
28474
|
+
async flushDatabaseToMemory() {
|
|
28475
|
+
const storedClients = await this.store.getAll();
|
|
28476
|
+
this.allClients = new Set(
|
|
28477
|
+
storedClients.map((client) => {
|
|
28478
|
+
const runtimeClient = this.runtimeClients.get(client.id);
|
|
28479
|
+
if (runtimeClient) {
|
|
28480
|
+
return runtimeClient;
|
|
28481
|
+
}
|
|
28482
|
+
return new WebSocketRemoteClientConnection(
|
|
28483
|
+
client.id,
|
|
28484
|
+
new URL(client.url),
|
|
28485
|
+
this.channel
|
|
28486
|
+
);
|
|
28487
|
+
})
|
|
28488
|
+
);
|
|
28489
|
+
}
|
|
28490
|
+
async removeRuntimeClients() {
|
|
28491
|
+
await this.store.deleteMany(Array.from(this.runtimeClients.keys()));
|
|
28492
|
+
this.runtimeClients.clear();
|
|
28493
|
+
await this.flushDatabaseToMemory();
|
|
28494
|
+
this.notifyOthersAboutDatabaseUpdate();
|
|
28495
|
+
}
|
|
28496
|
+
/**
|
|
28497
|
+
* All active WebSocket client connections.
|
|
28498
|
+
*/
|
|
28499
|
+
get clients() {
|
|
28500
|
+
return this.allClients;
|
|
28501
|
+
}
|
|
28502
|
+
/**
|
|
28503
|
+
* Notify other runtimes about the database update
|
|
28504
|
+
* using the shared `BroadcastChannel` instance.
|
|
28505
|
+
*/
|
|
28506
|
+
notifyOthersAboutDatabaseUpdate() {
|
|
28507
|
+
this.channel.postMessage({ type: "db:update" });
|
|
28508
|
+
}
|
|
28509
|
+
async addClient(client) {
|
|
28510
|
+
await this.store.add(client);
|
|
28511
|
+
await this.flushDatabaseToMemory();
|
|
28512
|
+
this.notifyOthersAboutDatabaseUpdate();
|
|
28513
|
+
}
|
|
28514
|
+
/**
|
|
28515
|
+
* Adds the given `WebSocket` client connection to the set
|
|
28516
|
+
* of all connections. The given connection is always the complete
|
|
28517
|
+
* connection object because `addConnection()` is called only
|
|
28518
|
+
* for the opened connections in the same runtime.
|
|
28519
|
+
*/
|
|
28520
|
+
async addConnection(client) {
|
|
28521
|
+
this.runtimeClients.set(client.id, client);
|
|
28522
|
+
await this.addClient(client);
|
|
28523
|
+
const handleExtraneousMessage = (message3) => {
|
|
28524
|
+
const { type, payload } = message3.data;
|
|
28525
|
+
if (typeof payload === "object" && "clientId" in payload && payload.clientId !== client.id) {
|
|
28526
|
+
return;
|
|
28527
|
+
}
|
|
28528
|
+
switch (type) {
|
|
28529
|
+
case "extraneous:send": {
|
|
28530
|
+
client.send(payload.data);
|
|
28531
|
+
break;
|
|
28532
|
+
}
|
|
28533
|
+
case "extraneous:close": {
|
|
28534
|
+
client.close(payload.code, payload.reason);
|
|
28535
|
+
break;
|
|
28536
|
+
}
|
|
28537
|
+
}
|
|
28538
|
+
};
|
|
28539
|
+
const abortController = new AbortController();
|
|
28540
|
+
this.channel.addEventListener("message", handleExtraneousMessage, {
|
|
28541
|
+
signal: abortController.signal
|
|
28542
|
+
});
|
|
28543
|
+
client.addEventListener("close", () => abortController.abort(), {
|
|
28544
|
+
once: true
|
|
28545
|
+
});
|
|
28546
|
+
}
|
|
28547
|
+
};
|
|
28548
|
+
var WebSocketRemoteClientConnection = class {
|
|
28549
|
+
constructor(id, url, channel) {
|
|
28550
|
+
this.id = id;
|
|
28551
|
+
this.url = url;
|
|
28552
|
+
this.channel = channel;
|
|
28553
|
+
}
|
|
28554
|
+
send(data) {
|
|
28555
|
+
this.channel.postMessage({
|
|
28556
|
+
type: "extraneous:send",
|
|
28557
|
+
payload: {
|
|
28558
|
+
clientId: this.id,
|
|
28559
|
+
data
|
|
28560
|
+
}
|
|
28561
|
+
});
|
|
28562
|
+
}
|
|
28563
|
+
close(code, reason) {
|
|
28564
|
+
this.channel.postMessage({
|
|
28565
|
+
type: "extraneous:close",
|
|
28566
|
+
payload: {
|
|
28567
|
+
clientId: this.id,
|
|
28568
|
+
code,
|
|
28569
|
+
reason
|
|
28570
|
+
}
|
|
28571
|
+
});
|
|
28572
|
+
}
|
|
28573
|
+
};
|
|
28574
|
+
|
|
28575
|
+
// src/core/ws.ts
|
|
28576
|
+
function isBroadcastChannelWithUnref(channel) {
|
|
28577
|
+
return typeof Reflect.get(channel, "unref") !== "undefined";
|
|
28578
|
+
}
|
|
28579
|
+
var webSocketChannel = new BroadcastChannel("msw:websocket-client-manager");
|
|
28580
|
+
if (isBroadcastChannelWithUnref(webSocketChannel)) {
|
|
28581
|
+
webSocketChannel.unref();
|
|
28582
|
+
}
|
|
28583
|
+
function createWebSocketLinkHandler(url) {
|
|
28584
|
+
invariant(url, "Expected a WebSocket server URL but got undefined");
|
|
28585
|
+
invariant(
|
|
28586
|
+
isPath(url),
|
|
28587
|
+
"Expected a WebSocket server URL to be a valid path but got %s",
|
|
28588
|
+
typeof url
|
|
28589
|
+
);
|
|
28590
|
+
const clientManager = new WebSocketClientManager(webSocketChannel);
|
|
28591
|
+
return {
|
|
28592
|
+
get clients() {
|
|
28593
|
+
return clientManager.clients;
|
|
28594
|
+
},
|
|
28595
|
+
addEventListener(event, listener) {
|
|
28596
|
+
const handler = new WebSocketHandler(url);
|
|
28597
|
+
handler[kEmitter].on("connection", async ({ client }) => {
|
|
28598
|
+
await clientManager.addConnection(client);
|
|
28599
|
+
});
|
|
28600
|
+
handler[kEmitter].on(event, listener);
|
|
28601
|
+
return handler;
|
|
28602
|
+
},
|
|
28603
|
+
broadcast(data) {
|
|
28604
|
+
this.broadcastExcept([], data);
|
|
28605
|
+
},
|
|
28606
|
+
broadcastExcept(clients, data) {
|
|
28607
|
+
const ignoreClients = Array.prototype.concat(clients).map((client) => client.id);
|
|
28608
|
+
clientManager.clients.forEach((otherClient) => {
|
|
28609
|
+
if (!ignoreClients.includes(otherClient.id)) {
|
|
28610
|
+
otherClient.send(data);
|
|
28611
|
+
}
|
|
28612
|
+
});
|
|
28613
|
+
}
|
|
28614
|
+
};
|
|
28615
|
+
}
|
|
28616
|
+
var ws = {
|
|
28617
|
+
link: createWebSocketLinkHandler
|
|
28618
|
+
};
|
|
28619
|
+
|
|
28157
28620
|
// node_modules/.pnpm/@open-draft+until@2.1.0/node_modules/@open-draft/until/lib/index.mjs
|
|
28158
28621
|
var until = async (promise) => {
|
|
28159
28622
|
try {
|
|
@@ -28176,6 +28639,9 @@ Consider naming this operation or using "graphql.operation()" request handler to
|
|
|
28176
28639
|
let matchingHandler = null;
|
|
28177
28640
|
let result = null;
|
|
28178
28641
|
for (const handler of handlers) {
|
|
28642
|
+
if (!(handler instanceof RequestHandler)) {
|
|
28643
|
+
continue;
|
|
28644
|
+
}
|
|
28179
28645
|
result = await handler.run({ request, requestId, resolutionContext });
|
|
28180
28646
|
if (result !== null) {
|
|
28181
28647
|
matchingHandler = handler;
|
|
@@ -28714,7 +29180,9 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
|
|
|
28714
29180
|
await handleRequest(
|
|
28715
29181
|
request,
|
|
28716
29182
|
requestId,
|
|
28717
|
-
context.getRequestHandlers()
|
|
29183
|
+
context.getRequestHandlers().filter((handler) => {
|
|
29184
|
+
return handler instanceof HttpHandler || handler instanceof GraphQLHandler;
|
|
29185
|
+
}),
|
|
28718
29186
|
options,
|
|
28719
29187
|
context.emitter,
|
|
28720
29188
|
{
|
|
@@ -28892,6 +29360,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
28892
29360
|
context.workerChannel.send("CLIENT_CLOSED");
|
|
28893
29361
|
}
|
|
28894
29362
|
window.clearInterval(context.keepAliveInterval);
|
|
29363
|
+
window.postMessage({ type: "msw/worker:stop" });
|
|
28895
29364
|
});
|
|
28896
29365
|
await checkWorkerIntegrity(context).catch((error3) => {
|
|
28897
29366
|
devUtils.error(
|
|
@@ -28951,6 +29420,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
28951
29420
|
context.workerChannel.send("MOCK_DEACTIVATE");
|
|
28952
29421
|
context.isMockingEnabled = false;
|
|
28953
29422
|
window.clearInterval(context.keepAliveInterval);
|
|
29423
|
+
window.postMessage({ type: "msw/worker:stop" });
|
|
28954
29424
|
printStopMessage({ quiet: context.startOptions?.quiet });
|
|
28955
29425
|
};
|
|
28956
29426
|
};
|
|
@@ -28994,87 +29464,21 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
28994
29464
|
}
|
|
28995
29465
|
};
|
|
28996
29466
|
|
|
28997
|
-
// node_modules/.pnpm/@
|
|
28998
|
-
|
|
28999
|
-
|
|
29000
|
-
|
|
29001
|
-
|
|
29002
|
-
|
|
29003
|
-
return;
|
|
29004
|
-
}
|
|
29005
|
-
executor.result = data;
|
|
29006
|
-
const onFulfilled = (value) => {
|
|
29007
|
-
executor.state = "fulfilled";
|
|
29008
|
-
return value;
|
|
29009
|
-
};
|
|
29010
|
-
return resolve(
|
|
29011
|
-
data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
|
|
29012
|
-
);
|
|
29013
|
-
};
|
|
29014
|
-
executor.reject = (reason) => {
|
|
29015
|
-
if (executor.state !== "pending") {
|
|
29016
|
-
return;
|
|
29017
|
-
}
|
|
29018
|
-
queueMicrotask(() => {
|
|
29019
|
-
executor.state = "rejected";
|
|
29020
|
-
});
|
|
29021
|
-
return reject(executor.rejectionReason = reason);
|
|
29022
|
-
};
|
|
29023
|
-
};
|
|
29024
|
-
return executor;
|
|
29025
|
-
}
|
|
29026
|
-
var DeferredPromise = class extends Promise {
|
|
29027
|
-
#executor;
|
|
29028
|
-
resolve;
|
|
29029
|
-
reject;
|
|
29030
|
-
constructor(executor = null) {
|
|
29031
|
-
const deferredExecutor = createDeferredExecutor();
|
|
29032
|
-
super((originalResolve, originalReject) => {
|
|
29033
|
-
deferredExecutor(originalResolve, originalReject);
|
|
29034
|
-
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
|
29035
|
-
});
|
|
29036
|
-
this.#executor = deferredExecutor;
|
|
29037
|
-
this.resolve = this.#executor.resolve;
|
|
29038
|
-
this.reject = this.#executor.reject;
|
|
29039
|
-
}
|
|
29040
|
-
get state() {
|
|
29041
|
-
return this.#executor.state;
|
|
29467
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.36.5/node_modules/@mswjs/interceptors/lib/browser/chunk-THPGBWJQ.mjs
|
|
29468
|
+
var InterceptorError = class extends Error {
|
|
29469
|
+
constructor(message3) {
|
|
29470
|
+
super(message3);
|
|
29471
|
+
this.name = "InterceptorError";
|
|
29472
|
+
Object.setPrototypeOf(this, InterceptorError.prototype);
|
|
29042
29473
|
}
|
|
29043
|
-
|
|
29044
|
-
|
|
29045
|
-
|
|
29046
|
-
|
|
29047
|
-
|
|
29048
|
-
|
|
29049
|
-
|
|
29050
|
-
|
|
29051
|
-
}
|
|
29052
|
-
finally(onfinally) {
|
|
29053
|
-
return this.#decorate(super.finally(onfinally));
|
|
29054
|
-
}
|
|
29055
|
-
#decorate(promise) {
|
|
29056
|
-
return Object.defineProperties(promise, {
|
|
29057
|
-
resolve: { configurable: true, value: this.resolve },
|
|
29058
|
-
reject: { configurable: true, value: this.reject }
|
|
29059
|
-
});
|
|
29060
|
-
}
|
|
29061
|
-
};
|
|
29062
|
-
|
|
29063
|
-
// node_modules/.pnpm/@mswjs+interceptors@0.36.5/node_modules/@mswjs/interceptors/lib/browser/chunk-THPGBWJQ.mjs
|
|
29064
|
-
var InterceptorError = class extends Error {
|
|
29065
|
-
constructor(message3) {
|
|
29066
|
-
super(message3);
|
|
29067
|
-
this.name = "InterceptorError";
|
|
29068
|
-
Object.setPrototypeOf(this, InterceptorError.prototype);
|
|
29069
|
-
}
|
|
29070
|
-
};
|
|
29071
|
-
var kRequestHandled = Symbol("kRequestHandled");
|
|
29072
|
-
var kResponsePromise = Symbol("kResponsePromise");
|
|
29073
|
-
var RequestController = class {
|
|
29074
|
-
constructor(request) {
|
|
29075
|
-
this.request = request;
|
|
29076
|
-
this[kRequestHandled] = false;
|
|
29077
|
-
this[kResponsePromise] = new DeferredPromise();
|
|
29474
|
+
};
|
|
29475
|
+
var kRequestHandled = Symbol("kRequestHandled");
|
|
29476
|
+
var kResponsePromise = Symbol("kResponsePromise");
|
|
29477
|
+
var RequestController = class {
|
|
29478
|
+
constructor(request) {
|
|
29479
|
+
this.request = request;
|
|
29480
|
+
this[kRequestHandled] = false;
|
|
29481
|
+
this[kResponsePromise] = new DeferredPromise();
|
|
29078
29482
|
}
|
|
29079
29483
|
/**
|
|
29080
29484
|
* Respond to this request with the given `Response` instance.
|
|
@@ -30296,6 +30700,911 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
30296
30700
|
}
|
|
30297
30701
|
}
|
|
30298
30702
|
|
|
30703
|
+
// node_modules/.pnpm/@mswjs+interceptors@0.36.5/node_modules/@mswjs/interceptors/lib/browser/interceptors/WebSocket/index.mjs
|
|
30704
|
+
function bindEvent(target, event) {
|
|
30705
|
+
Object.defineProperties(event, {
|
|
30706
|
+
target: {
|
|
30707
|
+
value: target,
|
|
30708
|
+
enumerable: true,
|
|
30709
|
+
writable: true
|
|
30710
|
+
},
|
|
30711
|
+
currentTarget: {
|
|
30712
|
+
value: target,
|
|
30713
|
+
enumerable: true,
|
|
30714
|
+
writable: true
|
|
30715
|
+
}
|
|
30716
|
+
});
|
|
30717
|
+
return event;
|
|
30718
|
+
}
|
|
30719
|
+
var kCancelable = Symbol("kCancelable");
|
|
30720
|
+
var kDefaultPrevented = Symbol("kDefaultPrevented");
|
|
30721
|
+
var CancelableMessageEvent = class extends MessageEvent {
|
|
30722
|
+
constructor(type, init) {
|
|
30723
|
+
super(type, init);
|
|
30724
|
+
this[kCancelable] = !!init.cancelable;
|
|
30725
|
+
this[kDefaultPrevented] = false;
|
|
30726
|
+
}
|
|
30727
|
+
get cancelable() {
|
|
30728
|
+
return this[kCancelable];
|
|
30729
|
+
}
|
|
30730
|
+
set cancelable(nextCancelable) {
|
|
30731
|
+
this[kCancelable] = nextCancelable;
|
|
30732
|
+
}
|
|
30733
|
+
get defaultPrevented() {
|
|
30734
|
+
return this[kDefaultPrevented];
|
|
30735
|
+
}
|
|
30736
|
+
set defaultPrevented(nextDefaultPrevented) {
|
|
30737
|
+
this[kDefaultPrevented] = nextDefaultPrevented;
|
|
30738
|
+
}
|
|
30739
|
+
preventDefault() {
|
|
30740
|
+
if (this.cancelable && !this[kDefaultPrevented]) {
|
|
30741
|
+
this[kDefaultPrevented] = true;
|
|
30742
|
+
}
|
|
30743
|
+
}
|
|
30744
|
+
};
|
|
30745
|
+
var CloseEvent = class extends Event {
|
|
30746
|
+
constructor(type, init = {}) {
|
|
30747
|
+
super(type, init);
|
|
30748
|
+
this.code = init.code === void 0 ? 0 : init.code;
|
|
30749
|
+
this.reason = init.reason === void 0 ? "" : init.reason;
|
|
30750
|
+
this.wasClean = init.wasClean === void 0 ? false : init.wasClean;
|
|
30751
|
+
}
|
|
30752
|
+
};
|
|
30753
|
+
var CancelableCloseEvent = class extends CloseEvent {
|
|
30754
|
+
constructor(type, init = {}) {
|
|
30755
|
+
super(type, init);
|
|
30756
|
+
this[kCancelable] = !!init.cancelable;
|
|
30757
|
+
this[kDefaultPrevented] = false;
|
|
30758
|
+
}
|
|
30759
|
+
get cancelable() {
|
|
30760
|
+
return this[kCancelable];
|
|
30761
|
+
}
|
|
30762
|
+
set cancelable(nextCancelable) {
|
|
30763
|
+
this[kCancelable] = nextCancelable;
|
|
30764
|
+
}
|
|
30765
|
+
get defaultPrevented() {
|
|
30766
|
+
return this[kDefaultPrevented];
|
|
30767
|
+
}
|
|
30768
|
+
set defaultPrevented(nextDefaultPrevented) {
|
|
30769
|
+
this[kDefaultPrevented] = nextDefaultPrevented;
|
|
30770
|
+
}
|
|
30771
|
+
preventDefault() {
|
|
30772
|
+
if (this.cancelable && !this[kDefaultPrevented]) {
|
|
30773
|
+
this[kDefaultPrevented] = true;
|
|
30774
|
+
}
|
|
30775
|
+
}
|
|
30776
|
+
};
|
|
30777
|
+
var kEmitter2 = Symbol("kEmitter");
|
|
30778
|
+
var kBoundListener = Symbol("kBoundListener");
|
|
30779
|
+
var WebSocketClientConnection = class {
|
|
30780
|
+
constructor(socket, transport) {
|
|
30781
|
+
this.socket = socket;
|
|
30782
|
+
this.transport = transport;
|
|
30783
|
+
this.id = createRequestId();
|
|
30784
|
+
this.url = new URL(socket.url);
|
|
30785
|
+
this[kEmitter2] = new EventTarget();
|
|
30786
|
+
this.transport.addEventListener("outgoing", (event) => {
|
|
30787
|
+
const message3 = bindEvent(
|
|
30788
|
+
this.socket,
|
|
30789
|
+
new CancelableMessageEvent("message", {
|
|
30790
|
+
data: event.data,
|
|
30791
|
+
origin: event.origin,
|
|
30792
|
+
cancelable: true
|
|
30793
|
+
})
|
|
30794
|
+
);
|
|
30795
|
+
this[kEmitter2].dispatchEvent(message3);
|
|
30796
|
+
if (message3.defaultPrevented) {
|
|
30797
|
+
event.preventDefault();
|
|
30798
|
+
}
|
|
30799
|
+
});
|
|
30800
|
+
this.transport.addEventListener("close", (event) => {
|
|
30801
|
+
this[kEmitter2].dispatchEvent(
|
|
30802
|
+
bindEvent(this.socket, new CloseEvent("close", event))
|
|
30803
|
+
);
|
|
30804
|
+
});
|
|
30805
|
+
}
|
|
30806
|
+
/**
|
|
30807
|
+
* Listen for the outgoing events from the connected WebSocket client.
|
|
30808
|
+
*/
|
|
30809
|
+
addEventListener(type, listener, options) {
|
|
30810
|
+
if (!Reflect.has(listener, kBoundListener)) {
|
|
30811
|
+
const boundListener = listener.bind(this.socket);
|
|
30812
|
+
Object.defineProperty(listener, kBoundListener, {
|
|
30813
|
+
value: boundListener,
|
|
30814
|
+
enumerable: false,
|
|
30815
|
+
configurable: false
|
|
30816
|
+
});
|
|
30817
|
+
}
|
|
30818
|
+
this[kEmitter2].addEventListener(
|
|
30819
|
+
type,
|
|
30820
|
+
Reflect.get(listener, kBoundListener),
|
|
30821
|
+
options
|
|
30822
|
+
);
|
|
30823
|
+
}
|
|
30824
|
+
/**
|
|
30825
|
+
* Removes the listener for the given event.
|
|
30826
|
+
*/
|
|
30827
|
+
removeEventListener(event, listener, options) {
|
|
30828
|
+
this[kEmitter2].removeEventListener(
|
|
30829
|
+
event,
|
|
30830
|
+
Reflect.get(listener, kBoundListener),
|
|
30831
|
+
options
|
|
30832
|
+
);
|
|
30833
|
+
}
|
|
30834
|
+
/**
|
|
30835
|
+
* Send data to the connected client.
|
|
30836
|
+
*/
|
|
30837
|
+
send(data) {
|
|
30838
|
+
this.transport.send(data);
|
|
30839
|
+
}
|
|
30840
|
+
/**
|
|
30841
|
+
* Close the WebSocket connection.
|
|
30842
|
+
* @param {number} code A status code (see https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1).
|
|
30843
|
+
* @param {string} reason A custom connection close reason.
|
|
30844
|
+
*/
|
|
30845
|
+
close(code, reason) {
|
|
30846
|
+
this.transport.close(code, reason);
|
|
30847
|
+
}
|
|
30848
|
+
};
|
|
30849
|
+
var WEBSOCKET_CLOSE_CODE_RANGE_ERROR = "InvalidAccessError: close code out of user configurable range";
|
|
30850
|
+
var kPassthroughPromise = Symbol("kPassthroughPromise");
|
|
30851
|
+
var kOnSend = Symbol("kOnSend");
|
|
30852
|
+
var kClose = Symbol("kClose");
|
|
30853
|
+
var WebSocketOverride = class extends EventTarget {
|
|
30854
|
+
constructor(url, protocols) {
|
|
30855
|
+
super();
|
|
30856
|
+
this.CONNECTING = 0;
|
|
30857
|
+
this.OPEN = 1;
|
|
30858
|
+
this.CLOSING = 2;
|
|
30859
|
+
this.CLOSED = 3;
|
|
30860
|
+
this._onopen = null;
|
|
30861
|
+
this._onmessage = null;
|
|
30862
|
+
this._onerror = null;
|
|
30863
|
+
this._onclose = null;
|
|
30864
|
+
this.url = url.toString();
|
|
30865
|
+
this.protocol = "";
|
|
30866
|
+
this.extensions = "";
|
|
30867
|
+
this.binaryType = "blob";
|
|
30868
|
+
this.readyState = this.CONNECTING;
|
|
30869
|
+
this.bufferedAmount = 0;
|
|
30870
|
+
this[kPassthroughPromise] = new DeferredPromise();
|
|
30871
|
+
queueMicrotask(async () => {
|
|
30872
|
+
if (await this[kPassthroughPromise]) {
|
|
30873
|
+
return;
|
|
30874
|
+
}
|
|
30875
|
+
this.protocol = typeof protocols === "string" ? protocols : Array.isArray(protocols) && protocols.length > 0 ? protocols[0] : "";
|
|
30876
|
+
if (this.readyState === this.CONNECTING) {
|
|
30877
|
+
this.readyState = this.OPEN;
|
|
30878
|
+
this.dispatchEvent(bindEvent(this, new Event("open")));
|
|
30879
|
+
}
|
|
30880
|
+
});
|
|
30881
|
+
}
|
|
30882
|
+
set onopen(listener) {
|
|
30883
|
+
this.removeEventListener("open", this._onopen);
|
|
30884
|
+
this._onopen = listener;
|
|
30885
|
+
if (listener !== null) {
|
|
30886
|
+
this.addEventListener("open", listener);
|
|
30887
|
+
}
|
|
30888
|
+
}
|
|
30889
|
+
get onopen() {
|
|
30890
|
+
return this._onopen;
|
|
30891
|
+
}
|
|
30892
|
+
set onmessage(listener) {
|
|
30893
|
+
this.removeEventListener(
|
|
30894
|
+
"message",
|
|
30895
|
+
this._onmessage
|
|
30896
|
+
);
|
|
30897
|
+
this._onmessage = listener;
|
|
30898
|
+
if (listener !== null) {
|
|
30899
|
+
this.addEventListener("message", listener);
|
|
30900
|
+
}
|
|
30901
|
+
}
|
|
30902
|
+
get onmessage() {
|
|
30903
|
+
return this._onmessage;
|
|
30904
|
+
}
|
|
30905
|
+
set onerror(listener) {
|
|
30906
|
+
this.removeEventListener("error", this._onerror);
|
|
30907
|
+
this._onerror = listener;
|
|
30908
|
+
if (listener !== null) {
|
|
30909
|
+
this.addEventListener("error", listener);
|
|
30910
|
+
}
|
|
30911
|
+
}
|
|
30912
|
+
get onerror() {
|
|
30913
|
+
return this._onerror;
|
|
30914
|
+
}
|
|
30915
|
+
set onclose(listener) {
|
|
30916
|
+
this.removeEventListener("close", this._onclose);
|
|
30917
|
+
this._onclose = listener;
|
|
30918
|
+
if (listener !== null) {
|
|
30919
|
+
this.addEventListener("close", listener);
|
|
30920
|
+
}
|
|
30921
|
+
}
|
|
30922
|
+
get onclose() {
|
|
30923
|
+
return this._onclose;
|
|
30924
|
+
}
|
|
30925
|
+
/**
|
|
30926
|
+
* @see https://websockets.spec.whatwg.org/#ref-for-dom-websocket-send%E2%91%A0
|
|
30927
|
+
*/
|
|
30928
|
+
send(data) {
|
|
30929
|
+
if (this.readyState === this.CONNECTING) {
|
|
30930
|
+
this.close();
|
|
30931
|
+
throw new DOMException("InvalidStateError");
|
|
30932
|
+
}
|
|
30933
|
+
if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) {
|
|
30934
|
+
return;
|
|
30935
|
+
}
|
|
30936
|
+
this.bufferedAmount += getDataSize(data);
|
|
30937
|
+
queueMicrotask(() => {
|
|
30938
|
+
var _a2;
|
|
30939
|
+
this.bufferedAmount = 0;
|
|
30940
|
+
(_a2 = this[kOnSend]) == null ? void 0 : _a2.call(this, data);
|
|
30941
|
+
});
|
|
30942
|
+
}
|
|
30943
|
+
close(code = 1e3, reason) {
|
|
30944
|
+
invariant(code, WEBSOCKET_CLOSE_CODE_RANGE_ERROR);
|
|
30945
|
+
invariant(
|
|
30946
|
+
code === 1e3 || code >= 3e3 && code <= 4999,
|
|
30947
|
+
WEBSOCKET_CLOSE_CODE_RANGE_ERROR
|
|
30948
|
+
);
|
|
30949
|
+
this[kClose](code, reason);
|
|
30950
|
+
}
|
|
30951
|
+
[(kPassthroughPromise, kOnSend, kClose)](code = 1e3, reason, wasClean = true) {
|
|
30952
|
+
if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) {
|
|
30953
|
+
return;
|
|
30954
|
+
}
|
|
30955
|
+
this.readyState = this.CLOSING;
|
|
30956
|
+
queueMicrotask(() => {
|
|
30957
|
+
this.readyState = this.CLOSED;
|
|
30958
|
+
this.dispatchEvent(
|
|
30959
|
+
bindEvent(
|
|
30960
|
+
this,
|
|
30961
|
+
new CloseEvent("close", {
|
|
30962
|
+
code,
|
|
30963
|
+
reason,
|
|
30964
|
+
wasClean
|
|
30965
|
+
})
|
|
30966
|
+
)
|
|
30967
|
+
);
|
|
30968
|
+
this._onopen = null;
|
|
30969
|
+
this._onmessage = null;
|
|
30970
|
+
this._onerror = null;
|
|
30971
|
+
this._onclose = null;
|
|
30972
|
+
});
|
|
30973
|
+
}
|
|
30974
|
+
addEventListener(type, listener, options) {
|
|
30975
|
+
return super.addEventListener(
|
|
30976
|
+
type,
|
|
30977
|
+
listener,
|
|
30978
|
+
options
|
|
30979
|
+
);
|
|
30980
|
+
}
|
|
30981
|
+
removeEventListener(type, callback, options) {
|
|
30982
|
+
return super.removeEventListener(type, callback, options);
|
|
30983
|
+
}
|
|
30984
|
+
};
|
|
30985
|
+
WebSocketOverride.CONNECTING = 0;
|
|
30986
|
+
WebSocketOverride.OPEN = 1;
|
|
30987
|
+
WebSocketOverride.CLOSING = 2;
|
|
30988
|
+
WebSocketOverride.CLOSED = 3;
|
|
30989
|
+
function getDataSize(data) {
|
|
30990
|
+
if (typeof data === "string") {
|
|
30991
|
+
return data.length;
|
|
30992
|
+
}
|
|
30993
|
+
if (data instanceof Blob) {
|
|
30994
|
+
return data.size;
|
|
30995
|
+
}
|
|
30996
|
+
return data.byteLength;
|
|
30997
|
+
}
|
|
30998
|
+
var kEmitter22 = Symbol("kEmitter");
|
|
30999
|
+
var kBoundListener2 = Symbol("kBoundListener");
|
|
31000
|
+
var kSend = Symbol("kSend");
|
|
31001
|
+
var WebSocketServerConnection = class {
|
|
31002
|
+
constructor(client, transport, createConnection) {
|
|
31003
|
+
this.client = client;
|
|
31004
|
+
this.transport = transport;
|
|
31005
|
+
this.createConnection = createConnection;
|
|
31006
|
+
this[kEmitter22] = new EventTarget();
|
|
31007
|
+
this.mockCloseController = new AbortController();
|
|
31008
|
+
this.realCloseController = new AbortController();
|
|
31009
|
+
this.transport.addEventListener("outgoing", (event) => {
|
|
31010
|
+
if (typeof this.realWebSocket === "undefined") {
|
|
31011
|
+
return;
|
|
31012
|
+
}
|
|
31013
|
+
queueMicrotask(() => {
|
|
31014
|
+
if (!event.defaultPrevented) {
|
|
31015
|
+
this[kSend](event.data);
|
|
31016
|
+
}
|
|
31017
|
+
});
|
|
31018
|
+
});
|
|
31019
|
+
this.transport.addEventListener(
|
|
31020
|
+
"incoming",
|
|
31021
|
+
this.handleIncomingMessage.bind(this)
|
|
31022
|
+
);
|
|
31023
|
+
}
|
|
31024
|
+
/**
|
|
31025
|
+
* The `WebSocket` instance connected to the original server.
|
|
31026
|
+
* Accessing this before calling `server.connect()` will throw.
|
|
31027
|
+
*/
|
|
31028
|
+
get socket() {
|
|
31029
|
+
invariant(
|
|
31030
|
+
this.realWebSocket,
|
|
31031
|
+
'Cannot access "socket" on the original WebSocket server object: the connection is not open. Did you forget to call `server.connect()`?'
|
|
31032
|
+
);
|
|
31033
|
+
return this.realWebSocket;
|
|
31034
|
+
}
|
|
31035
|
+
/**
|
|
31036
|
+
* Open connection to the original WebSocket server.
|
|
31037
|
+
*/
|
|
31038
|
+
connect() {
|
|
31039
|
+
invariant(
|
|
31040
|
+
!this.realWebSocket || this.realWebSocket.readyState !== WebSocket.OPEN,
|
|
31041
|
+
'Failed to call "connect()" on the original WebSocket instance: the connection already open'
|
|
31042
|
+
);
|
|
31043
|
+
const realWebSocket = this.createConnection();
|
|
31044
|
+
realWebSocket.binaryType = this.client.binaryType;
|
|
31045
|
+
realWebSocket.addEventListener(
|
|
31046
|
+
"open",
|
|
31047
|
+
(event) => {
|
|
31048
|
+
this[kEmitter22].dispatchEvent(
|
|
31049
|
+
bindEvent(this.realWebSocket, new Event("open", event))
|
|
31050
|
+
);
|
|
31051
|
+
},
|
|
31052
|
+
{ once: true }
|
|
31053
|
+
);
|
|
31054
|
+
realWebSocket.addEventListener("message", (event) => {
|
|
31055
|
+
this.transport.dispatchEvent(
|
|
31056
|
+
bindEvent(
|
|
31057
|
+
this.realWebSocket,
|
|
31058
|
+
new MessageEvent("incoming", {
|
|
31059
|
+
data: event.data,
|
|
31060
|
+
origin: event.origin
|
|
31061
|
+
})
|
|
31062
|
+
)
|
|
31063
|
+
);
|
|
31064
|
+
});
|
|
31065
|
+
this.client.addEventListener(
|
|
31066
|
+
"close",
|
|
31067
|
+
(event) => {
|
|
31068
|
+
this.handleMockClose(event);
|
|
31069
|
+
},
|
|
31070
|
+
{
|
|
31071
|
+
signal: this.mockCloseController.signal
|
|
31072
|
+
}
|
|
31073
|
+
);
|
|
31074
|
+
realWebSocket.addEventListener(
|
|
31075
|
+
"close",
|
|
31076
|
+
(event) => {
|
|
31077
|
+
this.handleRealClose(event);
|
|
31078
|
+
},
|
|
31079
|
+
{
|
|
31080
|
+
signal: this.realCloseController.signal
|
|
31081
|
+
}
|
|
31082
|
+
);
|
|
31083
|
+
realWebSocket.addEventListener("error", () => {
|
|
31084
|
+
const errorEvent = bindEvent(
|
|
31085
|
+
realWebSocket,
|
|
31086
|
+
new Event("error", { cancelable: true })
|
|
31087
|
+
);
|
|
31088
|
+
this[kEmitter22].dispatchEvent(errorEvent);
|
|
31089
|
+
if (!errorEvent.defaultPrevented) {
|
|
31090
|
+
this.client.dispatchEvent(bindEvent(this.client, new Event("error")));
|
|
31091
|
+
}
|
|
31092
|
+
});
|
|
31093
|
+
this.realWebSocket = realWebSocket;
|
|
31094
|
+
}
|
|
31095
|
+
/**
|
|
31096
|
+
* Listen for the incoming events from the original WebSocket server.
|
|
31097
|
+
*/
|
|
31098
|
+
addEventListener(event, listener, options) {
|
|
31099
|
+
if (!Reflect.has(listener, kBoundListener2)) {
|
|
31100
|
+
const boundListener = listener.bind(this.client);
|
|
31101
|
+
Object.defineProperty(listener, kBoundListener2, {
|
|
31102
|
+
value: boundListener,
|
|
31103
|
+
enumerable: false
|
|
31104
|
+
});
|
|
31105
|
+
}
|
|
31106
|
+
this[kEmitter22].addEventListener(
|
|
31107
|
+
event,
|
|
31108
|
+
Reflect.get(listener, kBoundListener2),
|
|
31109
|
+
options
|
|
31110
|
+
);
|
|
31111
|
+
}
|
|
31112
|
+
/**
|
|
31113
|
+
* Remove the listener for the given event.
|
|
31114
|
+
*/
|
|
31115
|
+
removeEventListener(event, listener, options) {
|
|
31116
|
+
this[kEmitter22].removeEventListener(
|
|
31117
|
+
event,
|
|
31118
|
+
Reflect.get(listener, kBoundListener2),
|
|
31119
|
+
options
|
|
31120
|
+
);
|
|
31121
|
+
}
|
|
31122
|
+
/**
|
|
31123
|
+
* Send data to the original WebSocket server.
|
|
31124
|
+
* @example
|
|
31125
|
+
* server.send('hello')
|
|
31126
|
+
* server.send(new Blob(['hello']))
|
|
31127
|
+
* server.send(new TextEncoder().encode('hello'))
|
|
31128
|
+
*/
|
|
31129
|
+
send(data) {
|
|
31130
|
+
this[kSend](data);
|
|
31131
|
+
}
|
|
31132
|
+
[(kEmitter22, kSend)](data) {
|
|
31133
|
+
const { realWebSocket } = this;
|
|
31134
|
+
invariant(
|
|
31135
|
+
realWebSocket,
|
|
31136
|
+
'Failed to call "server.send()" for "%s": the connection is not open. Did you forget to call "server.connect()"?',
|
|
31137
|
+
this.client.url
|
|
31138
|
+
);
|
|
31139
|
+
if (realWebSocket.readyState === WebSocket.CLOSING || realWebSocket.readyState === WebSocket.CLOSED) {
|
|
31140
|
+
return;
|
|
31141
|
+
}
|
|
31142
|
+
if (realWebSocket.readyState === WebSocket.CONNECTING) {
|
|
31143
|
+
realWebSocket.addEventListener(
|
|
31144
|
+
"open",
|
|
31145
|
+
() => {
|
|
31146
|
+
realWebSocket.send(data);
|
|
31147
|
+
},
|
|
31148
|
+
{ once: true }
|
|
31149
|
+
);
|
|
31150
|
+
return;
|
|
31151
|
+
}
|
|
31152
|
+
realWebSocket.send(data);
|
|
31153
|
+
}
|
|
31154
|
+
/**
|
|
31155
|
+
* Close the actual server connection.
|
|
31156
|
+
*/
|
|
31157
|
+
close() {
|
|
31158
|
+
const { realWebSocket } = this;
|
|
31159
|
+
invariant(
|
|
31160
|
+
realWebSocket,
|
|
31161
|
+
'Failed to close server connection for "%s": the connection is not open. Did you forget to call "server.connect()"?',
|
|
31162
|
+
this.client.url
|
|
31163
|
+
);
|
|
31164
|
+
this.realCloseController.abort();
|
|
31165
|
+
if (realWebSocket.readyState === WebSocket.CLOSING || realWebSocket.readyState === WebSocket.CLOSED) {
|
|
31166
|
+
return;
|
|
31167
|
+
}
|
|
31168
|
+
realWebSocket.close();
|
|
31169
|
+
queueMicrotask(() => {
|
|
31170
|
+
this[kEmitter22].dispatchEvent(
|
|
31171
|
+
bindEvent(
|
|
31172
|
+
this.realWebSocket,
|
|
31173
|
+
new CancelableCloseEvent("close", {
|
|
31174
|
+
/**
|
|
31175
|
+
* @note `server.close()` in the interceptor
|
|
31176
|
+
* always results in clean closures.
|
|
31177
|
+
*/
|
|
31178
|
+
code: 1e3,
|
|
31179
|
+
cancelable: true
|
|
31180
|
+
})
|
|
31181
|
+
)
|
|
31182
|
+
);
|
|
31183
|
+
});
|
|
31184
|
+
}
|
|
31185
|
+
handleIncomingMessage(event) {
|
|
31186
|
+
const messageEvent = bindEvent(
|
|
31187
|
+
event.target,
|
|
31188
|
+
new CancelableMessageEvent("message", {
|
|
31189
|
+
data: event.data,
|
|
31190
|
+
origin: event.origin,
|
|
31191
|
+
cancelable: true
|
|
31192
|
+
})
|
|
31193
|
+
);
|
|
31194
|
+
this[kEmitter22].dispatchEvent(messageEvent);
|
|
31195
|
+
if (!messageEvent.defaultPrevented) {
|
|
31196
|
+
this.client.dispatchEvent(
|
|
31197
|
+
bindEvent(
|
|
31198
|
+
/**
|
|
31199
|
+
* @note Bind the forwarded original server events
|
|
31200
|
+
* to the mock WebSocket instance so it would
|
|
31201
|
+
* dispatch them straight away.
|
|
31202
|
+
*/
|
|
31203
|
+
this.client,
|
|
31204
|
+
// Clone the message event again to prevent
|
|
31205
|
+
// the "already being dispatched" exception.
|
|
31206
|
+
new MessageEvent("message", {
|
|
31207
|
+
data: event.data,
|
|
31208
|
+
origin: event.origin
|
|
31209
|
+
})
|
|
31210
|
+
)
|
|
31211
|
+
);
|
|
31212
|
+
}
|
|
31213
|
+
}
|
|
31214
|
+
handleMockClose(_event) {
|
|
31215
|
+
if (this.realWebSocket) {
|
|
31216
|
+
this.realWebSocket.close();
|
|
31217
|
+
}
|
|
31218
|
+
}
|
|
31219
|
+
handleRealClose(event) {
|
|
31220
|
+
this.mockCloseController.abort();
|
|
31221
|
+
const closeEvent = bindEvent(
|
|
31222
|
+
this.realWebSocket,
|
|
31223
|
+
new CancelableCloseEvent("close", {
|
|
31224
|
+
code: event.code,
|
|
31225
|
+
reason: event.reason,
|
|
31226
|
+
wasClean: event.wasClean,
|
|
31227
|
+
cancelable: true
|
|
31228
|
+
})
|
|
31229
|
+
);
|
|
31230
|
+
this[kEmitter22].dispatchEvent(closeEvent);
|
|
31231
|
+
if (!closeEvent.defaultPrevented) {
|
|
31232
|
+
this.client[kClose](event.code, event.reason);
|
|
31233
|
+
}
|
|
31234
|
+
}
|
|
31235
|
+
};
|
|
31236
|
+
var WebSocketClassTransport = class extends EventTarget {
|
|
31237
|
+
constructor(socket) {
|
|
31238
|
+
super();
|
|
31239
|
+
this.socket = socket;
|
|
31240
|
+
this.socket.addEventListener("close", (event) => {
|
|
31241
|
+
this.dispatchEvent(bindEvent(this.socket, new CloseEvent("close", event)));
|
|
31242
|
+
});
|
|
31243
|
+
this.socket[kOnSend] = (data) => {
|
|
31244
|
+
this.dispatchEvent(
|
|
31245
|
+
bindEvent(
|
|
31246
|
+
this.socket,
|
|
31247
|
+
// Dispatch this as cancelable because "client" connection
|
|
31248
|
+
// re-creates this message event (cannot dispatch the same event).
|
|
31249
|
+
new CancelableMessageEvent("outgoing", {
|
|
31250
|
+
data,
|
|
31251
|
+
origin: this.socket.url,
|
|
31252
|
+
cancelable: true
|
|
31253
|
+
})
|
|
31254
|
+
)
|
|
31255
|
+
);
|
|
31256
|
+
};
|
|
31257
|
+
}
|
|
31258
|
+
addEventListener(type, callback, options) {
|
|
31259
|
+
return super.addEventListener(type, callback, options);
|
|
31260
|
+
}
|
|
31261
|
+
dispatchEvent(event) {
|
|
31262
|
+
return super.dispatchEvent(event);
|
|
31263
|
+
}
|
|
31264
|
+
send(data) {
|
|
31265
|
+
queueMicrotask(() => {
|
|
31266
|
+
this.socket.dispatchEvent(
|
|
31267
|
+
bindEvent(
|
|
31268
|
+
/**
|
|
31269
|
+
* @note Setting this event's "target" to the
|
|
31270
|
+
* WebSocket override instance is important.
|
|
31271
|
+
* This way it can tell apart original incoming events
|
|
31272
|
+
* (must be forwarded to the transport) from the
|
|
31273
|
+
* mocked message events like the one below
|
|
31274
|
+
* (must be dispatched on the client instance).
|
|
31275
|
+
*/
|
|
31276
|
+
this.socket,
|
|
31277
|
+
new MessageEvent("message", {
|
|
31278
|
+
data,
|
|
31279
|
+
origin: this.socket.url
|
|
31280
|
+
})
|
|
31281
|
+
)
|
|
31282
|
+
);
|
|
31283
|
+
});
|
|
31284
|
+
}
|
|
31285
|
+
close(code, reason) {
|
|
31286
|
+
this.socket[kClose](code, reason);
|
|
31287
|
+
}
|
|
31288
|
+
};
|
|
31289
|
+
var _WebSocketInterceptor = class extends Interceptor {
|
|
31290
|
+
constructor() {
|
|
31291
|
+
super(_WebSocketInterceptor.symbol);
|
|
31292
|
+
}
|
|
31293
|
+
checkEnvironment() {
|
|
31294
|
+
return typeof globalThis.WebSocket !== "undefined";
|
|
31295
|
+
}
|
|
31296
|
+
setup() {
|
|
31297
|
+
const originalWebSocket = globalThis.WebSocket;
|
|
31298
|
+
const webSocketProxy = new Proxy(globalThis.WebSocket, {
|
|
31299
|
+
construct: (target, args, newTarget) => {
|
|
31300
|
+
const [url, protocols] = args;
|
|
31301
|
+
const createConnection = () => {
|
|
31302
|
+
return Reflect.construct(target, args, newTarget);
|
|
31303
|
+
};
|
|
31304
|
+
const socket = new WebSocketOverride(url, protocols);
|
|
31305
|
+
const transport = new WebSocketClassTransport(socket);
|
|
31306
|
+
queueMicrotask(() => {
|
|
31307
|
+
try {
|
|
31308
|
+
const server = new WebSocketServerConnection(
|
|
31309
|
+
socket,
|
|
31310
|
+
transport,
|
|
31311
|
+
createConnection
|
|
31312
|
+
);
|
|
31313
|
+
const hasConnectionListeners = this.emitter.emit("connection", {
|
|
31314
|
+
client: new WebSocketClientConnection(socket, transport),
|
|
31315
|
+
server,
|
|
31316
|
+
info: {
|
|
31317
|
+
protocols
|
|
31318
|
+
}
|
|
31319
|
+
});
|
|
31320
|
+
if (hasConnectionListeners) {
|
|
31321
|
+
socket[kPassthroughPromise].resolve(false);
|
|
31322
|
+
} else {
|
|
31323
|
+
socket[kPassthroughPromise].resolve(true);
|
|
31324
|
+
server.connect();
|
|
31325
|
+
server.addEventListener("open", () => {
|
|
31326
|
+
socket.dispatchEvent(bindEvent(socket, new Event("open")));
|
|
31327
|
+
if (server["realWebSocket"]) {
|
|
31328
|
+
socket.protocol = server["realWebSocket"].protocol;
|
|
31329
|
+
}
|
|
31330
|
+
});
|
|
31331
|
+
}
|
|
31332
|
+
} catch (error3) {
|
|
31333
|
+
if (error3 instanceof Error) {
|
|
31334
|
+
socket.dispatchEvent(new Event("error"));
|
|
31335
|
+
if (socket.readyState !== WebSocket.CLOSING && socket.readyState !== WebSocket.CLOSED) {
|
|
31336
|
+
socket[kClose](1011, error3.message, false);
|
|
31337
|
+
}
|
|
31338
|
+
console.error(error3);
|
|
31339
|
+
}
|
|
31340
|
+
}
|
|
31341
|
+
});
|
|
31342
|
+
return socket;
|
|
31343
|
+
}
|
|
31344
|
+
});
|
|
31345
|
+
globalThis.WebSocket = webSocketProxy;
|
|
31346
|
+
this.subscriptions.push(() => {
|
|
31347
|
+
globalThis.WebSocket = originalWebSocket;
|
|
31348
|
+
});
|
|
31349
|
+
}
|
|
31350
|
+
};
|
|
31351
|
+
var WebSocketInterceptor = _WebSocketInterceptor;
|
|
31352
|
+
WebSocketInterceptor.symbol = Symbol("websocket");
|
|
31353
|
+
|
|
31354
|
+
// src/core/ws/webSocketInterceptor.ts
|
|
31355
|
+
var webSocketInterceptor = new WebSocketInterceptor();
|
|
31356
|
+
|
|
31357
|
+
// src/core/ws/handleWebSocketEvent.ts
|
|
31358
|
+
function handleWebSocketEvent(options) {
|
|
31359
|
+
webSocketInterceptor.on("connection", async (connection) => {
|
|
31360
|
+
const handlers = options.getHandlers();
|
|
31361
|
+
const connectionEvent = new MessageEvent("connection", {
|
|
31362
|
+
data: connection
|
|
31363
|
+
});
|
|
31364
|
+
const matchingHandlers = [];
|
|
31365
|
+
for (const handler of handlers) {
|
|
31366
|
+
if (handler instanceof WebSocketHandler && handler.predicate({
|
|
31367
|
+
event: connectionEvent,
|
|
31368
|
+
parsedResult: handler.parse({
|
|
31369
|
+
event: connectionEvent
|
|
31370
|
+
})
|
|
31371
|
+
})) {
|
|
31372
|
+
matchingHandlers.push(handler);
|
|
31373
|
+
}
|
|
31374
|
+
}
|
|
31375
|
+
if (matchingHandlers.length > 0) {
|
|
31376
|
+
options?.onMockedConnection(connection);
|
|
31377
|
+
for (const handler of matchingHandlers) {
|
|
31378
|
+
handler[kDispatchEvent](connectionEvent);
|
|
31379
|
+
}
|
|
31380
|
+
} else {
|
|
31381
|
+
const request = new Request(connection.client.url, {
|
|
31382
|
+
headers: {
|
|
31383
|
+
upgrade: "websocket",
|
|
31384
|
+
connection: "upgrade"
|
|
31385
|
+
}
|
|
31386
|
+
});
|
|
31387
|
+
await onUnhandledRequest(
|
|
31388
|
+
request,
|
|
31389
|
+
options.getUnhandledRequestStrategy()
|
|
31390
|
+
).catch((error3) => {
|
|
31391
|
+
const errorEvent = new Event("error");
|
|
31392
|
+
Object.defineProperty(errorEvent, "cause", {
|
|
31393
|
+
enumerable: true,
|
|
31394
|
+
configurable: false,
|
|
31395
|
+
value: error3
|
|
31396
|
+
});
|
|
31397
|
+
connection.client.socket.dispatchEvent(errorEvent);
|
|
31398
|
+
});
|
|
31399
|
+
options?.onPassthroughConnection(connection);
|
|
31400
|
+
connection.server.connect();
|
|
31401
|
+
}
|
|
31402
|
+
});
|
|
31403
|
+
}
|
|
31404
|
+
|
|
31405
|
+
// src/core/ws/utils/getMessageLength.ts
|
|
31406
|
+
function getMessageLength(data) {
|
|
31407
|
+
if (data instanceof Blob) {
|
|
31408
|
+
return data.size;
|
|
31409
|
+
}
|
|
31410
|
+
if (data instanceof ArrayBuffer) {
|
|
31411
|
+
return data.byteLength;
|
|
31412
|
+
}
|
|
31413
|
+
return new Blob([data]).size;
|
|
31414
|
+
}
|
|
31415
|
+
|
|
31416
|
+
// src/core/ws/utils/truncateMessage.ts
|
|
31417
|
+
var MAX_LENGTH = 24;
|
|
31418
|
+
function truncateMessage(message3) {
|
|
31419
|
+
if (message3.length <= MAX_LENGTH) {
|
|
31420
|
+
return message3;
|
|
31421
|
+
}
|
|
31422
|
+
return `${message3.slice(0, MAX_LENGTH)}\u2026`;
|
|
31423
|
+
}
|
|
31424
|
+
|
|
31425
|
+
// src/core/ws/utils/getPublicData.ts
|
|
31426
|
+
async function getPublicData(data) {
|
|
31427
|
+
if (data instanceof Blob) {
|
|
31428
|
+
const text = await data.text();
|
|
31429
|
+
return `Blob(${truncateMessage(text)})`;
|
|
31430
|
+
}
|
|
31431
|
+
if (typeof data === "object" && "byteLength" in data) {
|
|
31432
|
+
const text = new TextDecoder().decode(data);
|
|
31433
|
+
return `ArrayBuffer(${truncateMessage(text)})`;
|
|
31434
|
+
}
|
|
31435
|
+
return truncateMessage(data);
|
|
31436
|
+
}
|
|
31437
|
+
|
|
31438
|
+
// src/core/ws/utils/attachWebSocketLogger.ts
|
|
31439
|
+
var colors = {
|
|
31440
|
+
system: "#3b82f6",
|
|
31441
|
+
outgoing: "#22c55e",
|
|
31442
|
+
incoming: "#ef4444",
|
|
31443
|
+
mocked: "#ff6a33"
|
|
31444
|
+
};
|
|
31445
|
+
function attachWebSocketLogger(connection) {
|
|
31446
|
+
const { client, server } = connection;
|
|
31447
|
+
logConnectionOpen(client);
|
|
31448
|
+
client.addEventListener("message", (event) => {
|
|
31449
|
+
logOutgoingClientMessage(event);
|
|
31450
|
+
});
|
|
31451
|
+
client.addEventListener("close", (event) => {
|
|
31452
|
+
logConnectionClose(event);
|
|
31453
|
+
});
|
|
31454
|
+
client.socket.addEventListener("error", (event) => {
|
|
31455
|
+
logClientError(event);
|
|
31456
|
+
});
|
|
31457
|
+
client.send = new Proxy(client.send, {
|
|
31458
|
+
apply(target, thisArg, args) {
|
|
31459
|
+
const [data] = args;
|
|
31460
|
+
const messageEvent = new MessageEvent("message", { data });
|
|
31461
|
+
Object.defineProperties(messageEvent, {
|
|
31462
|
+
currentTarget: {
|
|
31463
|
+
enumerable: true,
|
|
31464
|
+
writable: false,
|
|
31465
|
+
value: client.socket
|
|
31466
|
+
},
|
|
31467
|
+
target: {
|
|
31468
|
+
enumerable: true,
|
|
31469
|
+
writable: false,
|
|
31470
|
+
value: client.socket
|
|
31471
|
+
}
|
|
31472
|
+
});
|
|
31473
|
+
queueMicrotask(() => {
|
|
31474
|
+
logIncomingMockedClientMessage(messageEvent);
|
|
31475
|
+
});
|
|
31476
|
+
return Reflect.apply(target, thisArg, args);
|
|
31477
|
+
}
|
|
31478
|
+
});
|
|
31479
|
+
server.addEventListener(
|
|
31480
|
+
"open",
|
|
31481
|
+
() => {
|
|
31482
|
+
server.addEventListener("message", (event) => {
|
|
31483
|
+
logIncomingServerMessage(event);
|
|
31484
|
+
});
|
|
31485
|
+
},
|
|
31486
|
+
{ once: true }
|
|
31487
|
+
);
|
|
31488
|
+
server.send = new Proxy(server.send, {
|
|
31489
|
+
apply(target, thisArg, args) {
|
|
31490
|
+
const [data] = args;
|
|
31491
|
+
const messageEvent = new MessageEvent("message", { data });
|
|
31492
|
+
Object.defineProperties(messageEvent, {
|
|
31493
|
+
currentTarget: {
|
|
31494
|
+
enumerable: true,
|
|
31495
|
+
writable: false,
|
|
31496
|
+
value: server.socket
|
|
31497
|
+
},
|
|
31498
|
+
target: {
|
|
31499
|
+
enumerable: true,
|
|
31500
|
+
writable: false,
|
|
31501
|
+
value: server.socket
|
|
31502
|
+
}
|
|
31503
|
+
});
|
|
31504
|
+
logOutgoingMockedClientMessage(messageEvent);
|
|
31505
|
+
return Reflect.apply(target, thisArg, args);
|
|
31506
|
+
}
|
|
31507
|
+
});
|
|
31508
|
+
}
|
|
31509
|
+
function logConnectionOpen(client) {
|
|
31510
|
+
const publicUrl = toPublicUrl(client.url);
|
|
31511
|
+
console.groupCollapsed(
|
|
31512
|
+
devUtils.formatMessage(`${getTimestamp()} %c\u25B6%c ${publicUrl}`),
|
|
31513
|
+
`color:${colors.system}`,
|
|
31514
|
+
"color:inherit"
|
|
31515
|
+
);
|
|
31516
|
+
console.log("Client:", client.socket);
|
|
31517
|
+
console.groupEnd();
|
|
31518
|
+
}
|
|
31519
|
+
function logConnectionClose(event) {
|
|
31520
|
+
const target = event.target;
|
|
31521
|
+
const publicUrl = toPublicUrl(target.url);
|
|
31522
|
+
console.groupCollapsed(
|
|
31523
|
+
devUtils.formatMessage(
|
|
31524
|
+
`${getTimestamp({ milliseconds: true })} %c\u25A0%c ${publicUrl}`
|
|
31525
|
+
),
|
|
31526
|
+
`color:${colors.system}`,
|
|
31527
|
+
"color:inherit"
|
|
31528
|
+
);
|
|
31529
|
+
console.log(event);
|
|
31530
|
+
console.groupEnd();
|
|
31531
|
+
}
|
|
31532
|
+
function logClientError(event) {
|
|
31533
|
+
const socket = event.target;
|
|
31534
|
+
const publicUrl = toPublicUrl(socket.url);
|
|
31535
|
+
console.groupCollapsed(
|
|
31536
|
+
devUtils.formatMessage(
|
|
31537
|
+
`${getTimestamp({ milliseconds: true })} %c\xD7%c ${publicUrl}`
|
|
31538
|
+
),
|
|
31539
|
+
`color:${colors.system}`,
|
|
31540
|
+
"color:inherit"
|
|
31541
|
+
);
|
|
31542
|
+
console.log(event);
|
|
31543
|
+
console.groupEnd();
|
|
31544
|
+
}
|
|
31545
|
+
async function logOutgoingClientMessage(event) {
|
|
31546
|
+
const byteLength = getMessageLength(event.data);
|
|
31547
|
+
const publicData = await getPublicData(event.data);
|
|
31548
|
+
const arrow = event.defaultPrevented ? "\u21E1" : "\u2B06";
|
|
31549
|
+
console.groupCollapsed(
|
|
31550
|
+
devUtils.formatMessage(
|
|
31551
|
+
`${getTimestamp({ milliseconds: true })} %c${arrow}%c ${publicData} %c${byteLength}%c`
|
|
31552
|
+
),
|
|
31553
|
+
`color:${colors.outgoing}`,
|
|
31554
|
+
"color:inherit",
|
|
31555
|
+
"color:gray;font-weight:normal",
|
|
31556
|
+
"color:inherit;font-weight:inherit"
|
|
31557
|
+
);
|
|
31558
|
+
console.log(event);
|
|
31559
|
+
console.groupEnd();
|
|
31560
|
+
}
|
|
31561
|
+
async function logOutgoingMockedClientMessage(event) {
|
|
31562
|
+
const byteLength = getMessageLength(event.data);
|
|
31563
|
+
const publicData = await getPublicData(event.data);
|
|
31564
|
+
console.groupCollapsed(
|
|
31565
|
+
devUtils.formatMessage(
|
|
31566
|
+
`${getTimestamp({ milliseconds: true })} %c\u2B06%c ${publicData} %c${byteLength}%c`
|
|
31567
|
+
),
|
|
31568
|
+
`color:${colors.mocked}`,
|
|
31569
|
+
"color:inherit",
|
|
31570
|
+
"color:gray;font-weight:normal",
|
|
31571
|
+
"color:inherit;font-weight:inherit"
|
|
31572
|
+
);
|
|
31573
|
+
console.log(event);
|
|
31574
|
+
console.groupEnd();
|
|
31575
|
+
}
|
|
31576
|
+
async function logIncomingMockedClientMessage(event) {
|
|
31577
|
+
const byteLength = getMessageLength(event.data);
|
|
31578
|
+
const publicData = await getPublicData(event.data);
|
|
31579
|
+
console.groupCollapsed(
|
|
31580
|
+
devUtils.formatMessage(
|
|
31581
|
+
`${getTimestamp({ milliseconds: true })} %c\u2B07%c ${publicData} %c${byteLength}%c`
|
|
31582
|
+
),
|
|
31583
|
+
`color:${colors.mocked}`,
|
|
31584
|
+
"color:inherit",
|
|
31585
|
+
"color:gray;font-weight:normal",
|
|
31586
|
+
"color:inherit;font-weight:inherit"
|
|
31587
|
+
);
|
|
31588
|
+
console.log(event);
|
|
31589
|
+
console.groupEnd();
|
|
31590
|
+
}
|
|
31591
|
+
async function logIncomingServerMessage(event) {
|
|
31592
|
+
const byteLength = getMessageLength(event.data);
|
|
31593
|
+
const publicData = await getPublicData(event.data);
|
|
31594
|
+
const arrow = event.defaultPrevented ? "\u21E3" : "\u2B07";
|
|
31595
|
+
console.groupCollapsed(
|
|
31596
|
+
devUtils.formatMessage(
|
|
31597
|
+
`${getTimestamp({ milliseconds: true })} %c${arrow}%c ${publicData} %c${byteLength}%c`
|
|
31598
|
+
),
|
|
31599
|
+
`color:${colors.incoming}`,
|
|
31600
|
+
"color:inherit",
|
|
31601
|
+
"color:gray;font-weight:normal",
|
|
31602
|
+
"color:inherit;font-weight:inherit"
|
|
31603
|
+
);
|
|
31604
|
+
console.log(event);
|
|
31605
|
+
console.groupEnd();
|
|
31606
|
+
}
|
|
31607
|
+
|
|
30299
31608
|
// src/browser/setupWorker/setupWorker.ts
|
|
30300
31609
|
var SetupWorkerApi = class extends SetupApi {
|
|
30301
31610
|
context;
|
|
@@ -30412,6 +31721,25 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
30412
31721
|
DEFAULT_START_OPTIONS,
|
|
30413
31722
|
options
|
|
30414
31723
|
);
|
|
31724
|
+
handleWebSocketEvent({
|
|
31725
|
+
getUnhandledRequestStrategy: () => {
|
|
31726
|
+
return this.context.startOptions.onUnhandledRequest;
|
|
31727
|
+
},
|
|
31728
|
+
getHandlers: () => {
|
|
31729
|
+
return this.handlersController.currentHandlers();
|
|
31730
|
+
},
|
|
31731
|
+
onMockedConnection: (connection) => {
|
|
31732
|
+
if (!this.context.startOptions.quiet) {
|
|
31733
|
+
attachWebSocketLogger(connection);
|
|
31734
|
+
}
|
|
31735
|
+
},
|
|
31736
|
+
onPassthroughConnection() {
|
|
31737
|
+
}
|
|
31738
|
+
});
|
|
31739
|
+
webSocketInterceptor.apply();
|
|
31740
|
+
this.subscriptions.push(() => {
|
|
31741
|
+
webSocketInterceptor.dispose();
|
|
31742
|
+
});
|
|
30415
31743
|
return await this.startHandler(this.context.startOptions, options);
|
|
30416
31744
|
}
|
|
30417
31745
|
stop() {
|