msw 2.11.1 → 2.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/index.d.mts +0 -3
- package/lib/browser/index.d.ts +0 -3
- package/lib/browser/index.js +536 -318
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +536 -318
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/handlers/GraphQLHandler.js.map +1 -1
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
- package/lib/core/handlers/HttpHandler.js.map +1 -1
- package/lib/core/handlers/HttpHandler.mjs.map +1 -1
- package/lib/core/utils/handleRequest.js +8 -8
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs +8 -8
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/devUtils.js.map +1 -1
- package/lib/core/utils/internal/devUtils.mjs.map +1 -1
- package/lib/core/utils/internal/getCallFrame.js +2 -2
- package/lib/core/utils/internal/getCallFrame.js.map +1 -1
- package/lib/core/utils/internal/getCallFrame.mjs +2 -2
- package/lib/core/utils/internal/getCallFrame.mjs.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.js +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
- package/lib/core/utils/url/cleanUrl.js +1 -1
- package/lib/core/utils/url/cleanUrl.js.map +1 -1
- package/lib/core/utils/url/cleanUrl.mjs +1 -1
- package/lib/core/utils/url/cleanUrl.mjs.map +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.js +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.mjs +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -1
- package/lib/iife/index.js +2536 -2314
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +16 -12
- package/package.json +22 -15
- package/src/browser/setupWorker/glossary.ts +9 -114
- package/src/browser/setupWorker/setupWorker.ts +82 -119
- package/src/browser/setupWorker/start/createRequestListener.ts +20 -24
- package/src/browser/setupWorker/start/createResponseListener.ts +15 -22
- package/src/browser/setupWorker/start/createStartHandler.ts +24 -18
- package/src/browser/setupWorker/start/utils/enableMocking.ts +18 -21
- package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +17 -16
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +0 -2
- package/src/browser/utils/checkWorkerIntegrity.ts +22 -14
- package/src/browser/utils/deferNetworkRequestsUntil.ts +1 -1
- package/src/browser/utils/workerChannel.ts +146 -0
- package/src/core/handlers/GraphQLHandler.ts +0 -3
- package/src/core/handlers/HttpHandler.ts +0 -2
- package/src/core/utils/handleRequest.ts +8 -8
- package/src/core/utils/internal/devUtils.ts +0 -2
- package/src/core/utils/internal/getCallFrame.ts +2 -2
- package/src/core/utils/internal/parseGraphQLRequest.ts +1 -0
- package/src/core/utils/matching/matchRequestUrl.ts +2 -2
- package/src/core/utils/request/onUnhandledRequest.test.ts +1 -1
- package/src/core/utils/url/cleanUrl.ts +1 -1
- package/src/core/utils/url/isAbsoluteUrl.ts +1 -1
- package/src/core/ws/WebSocketIndexedDBClientStore.ts +0 -4
- package/src/core/ws/utils/attachWebSocketLogger.ts +0 -14
- package/src/mockServiceWorker.js +14 -10
- package/src/browser/setupWorker/start/createFallbackStart.ts +0 -21
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +0 -32
- package/src/browser/setupWorker/stop/createFallbackStop.ts +0 -11
- package/src/browser/setupWorker/stop/createStop.ts +0 -35
package/lib/browser/index.mjs
CHANGED
|
@@ -97,20 +97,34 @@ function isNodeProcess() {
|
|
|
97
97
|
return false;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
// src/browser/setupWorker/start/utils/prepareStartHandler.ts
|
|
101
|
+
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
102
|
+
var DEFAULT_START_OPTIONS = {
|
|
103
|
+
serviceWorker: {
|
|
104
|
+
url: "/mockServiceWorker.js",
|
|
105
|
+
options: null
|
|
106
|
+
},
|
|
107
|
+
quiet: false,
|
|
108
|
+
waitUntilReady: true,
|
|
109
|
+
onUnhandledRequest: "warn",
|
|
110
|
+
findWorker(scriptURL, mockServiceWorkerUrl) {
|
|
111
|
+
return scriptURL === mockServiceWorkerUrl;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
100
115
|
// src/browser/setupWorker/start/createStartHandler.ts
|
|
101
|
-
import { devUtils as
|
|
116
|
+
import { devUtils as devUtils6 } from '../core/utils/internal/devUtils.mjs';
|
|
102
117
|
|
|
103
|
-
// node_modules/.pnpm
|
|
104
|
-
|
|
118
|
+
// node_modules/.pnpm/until-async@3.0.2/node_modules/until-async/lib/index.js
|
|
119
|
+
async function until(callback) {
|
|
105
120
|
try {
|
|
106
|
-
|
|
121
|
+
return [null, await callback().catch((error2) => {
|
|
107
122
|
throw error2;
|
|
108
|
-
});
|
|
109
|
-
return { error: null, data };
|
|
123
|
+
})];
|
|
110
124
|
} catch (error2) {
|
|
111
|
-
return
|
|
125
|
+
return [error2, null];
|
|
112
126
|
}
|
|
113
|
-
}
|
|
127
|
+
}
|
|
114
128
|
|
|
115
129
|
// src/browser/setupWorker/start/utils/getWorkerInstance.ts
|
|
116
130
|
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
@@ -159,19 +173,17 @@ var getWorkerInstance = async (url, options = {}, findWorker) => {
|
|
|
159
173
|
existingRegistration
|
|
160
174
|
];
|
|
161
175
|
}
|
|
162
|
-
const registrationResult = await until(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (registrationResult.error) {
|
|
174
|
-
const isWorkerMissing = registrationResult.error.message.includes("(404)");
|
|
176
|
+
const [registrationError, registrationResult] = await until(async () => {
|
|
177
|
+
const registration = await navigator.serviceWorker.register(url, options);
|
|
178
|
+
return [
|
|
179
|
+
// Compare existing worker registration by its worker URL,
|
|
180
|
+
// to prevent irrelevant workers to resolve here (such as Codesandbox worker).
|
|
181
|
+
getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker),
|
|
182
|
+
registration
|
|
183
|
+
];
|
|
184
|
+
});
|
|
185
|
+
if (registrationError) {
|
|
186
|
+
const isWorkerMissing = registrationError.message.includes("(404)");
|
|
175
187
|
if (isWorkerMissing) {
|
|
176
188
|
const scopeUrl = new URL(options?.scope || "/", location.href);
|
|
177
189
|
throw new Error(
|
|
@@ -185,15 +197,78 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
|
|
|
185
197
|
throw new Error(
|
|
186
198
|
devUtils.formatMessage(
|
|
187
199
|
"Failed to register the Service Worker:\n\n%s",
|
|
188
|
-
|
|
200
|
+
registrationError.message
|
|
189
201
|
)
|
|
190
202
|
);
|
|
191
203
|
}
|
|
192
|
-
return registrationResult
|
|
204
|
+
return registrationResult;
|
|
193
205
|
};
|
|
194
206
|
|
|
195
|
-
//
|
|
196
|
-
|
|
207
|
+
// node_modules/.pnpm/@open-draft+deferred-promise@2.2.0/node_modules/@open-draft/deferred-promise/build/index.mjs
|
|
208
|
+
function createDeferredExecutor() {
|
|
209
|
+
const executor = (resolve, reject) => {
|
|
210
|
+
executor.state = "pending";
|
|
211
|
+
executor.resolve = (data) => {
|
|
212
|
+
if (executor.state !== "pending") {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
executor.result = data;
|
|
216
|
+
const onFulfilled = (value) => {
|
|
217
|
+
executor.state = "fulfilled";
|
|
218
|
+
return value;
|
|
219
|
+
};
|
|
220
|
+
return resolve(
|
|
221
|
+
data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
executor.reject = (reason) => {
|
|
225
|
+
if (executor.state !== "pending") {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
queueMicrotask(() => {
|
|
229
|
+
executor.state = "rejected";
|
|
230
|
+
});
|
|
231
|
+
return reject(executor.rejectionReason = reason);
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
return executor;
|
|
235
|
+
}
|
|
236
|
+
var DeferredPromise = class extends Promise {
|
|
237
|
+
#executor;
|
|
238
|
+
resolve;
|
|
239
|
+
reject;
|
|
240
|
+
constructor(executor = null) {
|
|
241
|
+
const deferredExecutor = createDeferredExecutor();
|
|
242
|
+
super((originalResolve, originalReject) => {
|
|
243
|
+
deferredExecutor(originalResolve, originalReject);
|
|
244
|
+
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
|
245
|
+
});
|
|
246
|
+
this.#executor = deferredExecutor;
|
|
247
|
+
this.resolve = this.#executor.resolve;
|
|
248
|
+
this.reject = this.#executor.reject;
|
|
249
|
+
}
|
|
250
|
+
get state() {
|
|
251
|
+
return this.#executor.state;
|
|
252
|
+
}
|
|
253
|
+
get rejectionReason() {
|
|
254
|
+
return this.#executor.rejectionReason;
|
|
255
|
+
}
|
|
256
|
+
then(onFulfilled, onRejected) {
|
|
257
|
+
return this.#decorate(super.then(onFulfilled, onRejected));
|
|
258
|
+
}
|
|
259
|
+
catch(onRejected) {
|
|
260
|
+
return this.#decorate(super.catch(onRejected));
|
|
261
|
+
}
|
|
262
|
+
finally(onfinally) {
|
|
263
|
+
return this.#decorate(super.finally(onfinally));
|
|
264
|
+
}
|
|
265
|
+
#decorate(promise) {
|
|
266
|
+
return Object.defineProperties(promise, {
|
|
267
|
+
resolve: { configurable: true, value: this.resolve },
|
|
268
|
+
reject: { configurable: true, value: this.reject }
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
};
|
|
197
272
|
|
|
198
273
|
// src/browser/setupWorker/start/utils/printStartMessage.ts
|
|
199
274
|
import { devUtils as devUtils2 } from '../core/utils/internal/devUtils.mjs';
|
|
@@ -225,35 +300,23 @@ function printStartMessage(args = {}) {
|
|
|
225
300
|
}
|
|
226
301
|
|
|
227
302
|
// src/browser/setupWorker/start/utils/enableMocking.ts
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
workerUrl: context.worker?.scriptURL,
|
|
242
|
-
client: payload.client
|
|
303
|
+
function enableMocking(context, options) {
|
|
304
|
+
const mockingEnabledPromise = new DeferredPromise();
|
|
305
|
+
context.workerChannel.postMessage("MOCK_ACTIVATE");
|
|
306
|
+
context.workerChannel.once("MOCKING_ENABLED", async (event) => {
|
|
307
|
+
context.isMockingEnabled = true;
|
|
308
|
+
const worker = await context.workerPromise;
|
|
309
|
+
printStartMessage({
|
|
310
|
+
quiet: options.quiet,
|
|
311
|
+
workerScope: context.registration?.scope,
|
|
312
|
+
workerUrl: worker.scriptURL,
|
|
313
|
+
client: event.data.client
|
|
314
|
+
});
|
|
315
|
+
mockingEnabledPromise.resolve(true);
|
|
243
316
|
});
|
|
317
|
+
return mockingEnabledPromise;
|
|
244
318
|
}
|
|
245
319
|
|
|
246
|
-
// src/browser/setupWorker/start/utils/createMessageChannel.ts
|
|
247
|
-
var WorkerChannel = class {
|
|
248
|
-
constructor(port) {
|
|
249
|
-
this.port = port;
|
|
250
|
-
}
|
|
251
|
-
postMessage(event, ...rest) {
|
|
252
|
-
const [data, transfer] = rest;
|
|
253
|
-
this.port.postMessage({ type: event, data }, { transfer });
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
|
|
257
320
|
// src/browser/utils/pruneGetRequestBody.ts
|
|
258
321
|
function pruneGetRequestBody(request) {
|
|
259
322
|
if (["HEAD", "GET"].includes(request.method)) {
|
|
@@ -273,14 +336,17 @@ function deserializeRequest(serializedRequest) {
|
|
|
273
336
|
// src/browser/setupWorker/start/createRequestListener.ts
|
|
274
337
|
import { RequestHandler } from '../core/handlers/RequestHandler.mjs';
|
|
275
338
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
276
|
-
import { devUtils as
|
|
339
|
+
import { devUtils as devUtils3 } from '../core/utils/internal/devUtils.mjs';
|
|
277
340
|
import { toResponseInit } from '../core/utils/toResponseInit.mjs';
|
|
278
341
|
import { isHandlerKind } from '../core/utils/internal/isHandlerKind.mjs';
|
|
279
342
|
var createRequestListener = (context, options) => {
|
|
280
|
-
return async (event
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
343
|
+
return async (event) => {
|
|
344
|
+
if (!context.isMockingEnabled && context.workerStoppedAt && event.data.interceptedAt > context.workerStoppedAt) {
|
|
345
|
+
event.postMessage("PASSTHROUGH");
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const requestId = event.data.id;
|
|
349
|
+
const request = deserializeRequest(event.data);
|
|
284
350
|
const requestCloneForLogs = request.clone();
|
|
285
351
|
const requestClone = request.clone();
|
|
286
352
|
RequestHandler.cache.set(request, requestClone);
|
|
@@ -293,7 +359,7 @@ var createRequestListener = (context, options) => {
|
|
|
293
359
|
context.emitter,
|
|
294
360
|
{
|
|
295
361
|
onPassthroughResponse() {
|
|
296
|
-
|
|
362
|
+
event.postMessage("PASSTHROUGH");
|
|
297
363
|
},
|
|
298
364
|
async onMockedResponse(response, { handler, parsedResult }) {
|
|
299
365
|
const responseClone = response.clone();
|
|
@@ -301,7 +367,7 @@ var createRequestListener = (context, options) => {
|
|
|
301
367
|
const responseInit = toResponseInit(response);
|
|
302
368
|
if (context.supports.readableStreamTransfer) {
|
|
303
369
|
const responseStreamOrNull = response.body;
|
|
304
|
-
|
|
370
|
+
event.postMessage(
|
|
305
371
|
"MOCK_RESPONSE",
|
|
306
372
|
{
|
|
307
373
|
...responseInit,
|
|
@@ -311,7 +377,7 @@ var createRequestListener = (context, options) => {
|
|
|
311
377
|
);
|
|
312
378
|
} else {
|
|
313
379
|
const responseBufferOrNull = response.body === null ? null : await responseClone.arrayBuffer();
|
|
314
|
-
|
|
380
|
+
event.postMessage("MOCK_RESPONSE", {
|
|
315
381
|
...responseInit,
|
|
316
382
|
body: responseBufferOrNull
|
|
317
383
|
});
|
|
@@ -330,7 +396,7 @@ var createRequestListener = (context, options) => {
|
|
|
330
396
|
);
|
|
331
397
|
} catch (error2) {
|
|
332
398
|
if (error2 instanceof Error) {
|
|
333
|
-
|
|
399
|
+
devUtils3.error(
|
|
334
400
|
`Uncaught exception in the request handler for "%s %s":
|
|
335
401
|
|
|
336
402
|
%s
|
|
@@ -340,7 +406,7 @@ This exception has been gracefully handled as a 500 response, however, it's stro
|
|
|
340
406
|
request.url,
|
|
341
407
|
error2.stack ?? error2
|
|
342
408
|
);
|
|
343
|
-
|
|
409
|
+
event.postMessage("MOCK_RESPONSE", {
|
|
344
410
|
status: 500,
|
|
345
411
|
statusText: "Request Handler Error",
|
|
346
412
|
headers: {
|
|
@@ -358,21 +424,26 @@ This exception has been gracefully handled as a 500 response, however, it's stro
|
|
|
358
424
|
};
|
|
359
425
|
|
|
360
426
|
// src/browser/utils/checkWorkerIntegrity.ts
|
|
361
|
-
import { devUtils as
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
427
|
+
import { devUtils as devUtils4 } from '../core/utils/internal/devUtils.mjs';
|
|
428
|
+
function checkWorkerIntegrity(context) {
|
|
429
|
+
const integrityCheckPromise = new DeferredPromise();
|
|
430
|
+
context.workerChannel.postMessage("INTEGRITY_CHECK_REQUEST");
|
|
431
|
+
context.workerChannel.once("INTEGRITY_CHECK_RESPONSE", (event) => {
|
|
432
|
+
const { checksum, packageVersion } = event.data;
|
|
433
|
+
if (checksum !== "4db4a41e972cec1b64cc569c66952d82") {
|
|
434
|
+
devUtils4.warn(
|
|
435
|
+
`The currently registered Service Worker has been generated by a different version of MSW (${packageVersion}) and may not be fully compatible with the installed version.
|
|
368
436
|
|
|
369
437
|
It's recommended you update your worker script by running this command:
|
|
370
438
|
|
|
371
439
|
\u2022 npx msw init <PUBLIC_DIR>
|
|
372
440
|
|
|
373
441
|
You can also automate this process and make the worker script update automatically upon the library installations. Read more: https://mswjs.io/docs/cli/init.`
|
|
374
|
-
|
|
375
|
-
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
integrityCheckPromise.resolve();
|
|
445
|
+
});
|
|
446
|
+
return integrityCheckPromise;
|
|
376
447
|
}
|
|
377
448
|
|
|
378
449
|
// node_modules/.pnpm/@mswjs+interceptors@0.39.1/node_modules/@mswjs/interceptors/lib/browser/chunk-6HYIRFX2.mjs
|
|
@@ -1115,22 +1186,22 @@ var BatchInterceptor = class extends Interceptor {
|
|
|
1115
1186
|
|
|
1116
1187
|
// src/browser/setupWorker/start/createResponseListener.ts
|
|
1117
1188
|
function createResponseListener(context) {
|
|
1118
|
-
return (
|
|
1119
|
-
const
|
|
1120
|
-
const request = deserializeRequest(
|
|
1121
|
-
if (
|
|
1189
|
+
return (event) => {
|
|
1190
|
+
const responseMessage = event.data;
|
|
1191
|
+
const request = deserializeRequest(responseMessage.request);
|
|
1192
|
+
if (responseMessage.response.type?.includes("opaque")) {
|
|
1122
1193
|
return;
|
|
1123
1194
|
}
|
|
1124
|
-
const response =
|
|
1195
|
+
const response = responseMessage.response.status === 0 ? Response.error() : new FetchResponse(
|
|
1125
1196
|
/**
|
|
1126
1197
|
* Responses may be streams here, but when we create a response object
|
|
1127
1198
|
* with null-body status codes, like 204, 205, 304 Response will
|
|
1128
1199
|
* throw when passed a non-null body, so ensure it's null here
|
|
1129
1200
|
* for those codes
|
|
1130
1201
|
*/
|
|
1131
|
-
FetchResponse.isResponseWithBody(
|
|
1202
|
+
FetchResponse.isResponseWithBody(responseMessage.response.status) ? responseMessage.response.body : null,
|
|
1132
1203
|
{
|
|
1133
|
-
...
|
|
1204
|
+
...responseMessage,
|
|
1134
1205
|
/**
|
|
1135
1206
|
* Set response URL if it's not set already.
|
|
1136
1207
|
* @see https://github.com/mswjs/msw/issues/2030
|
|
@@ -1140,9 +1211,9 @@ function createResponseListener(context) {
|
|
|
1140
1211
|
}
|
|
1141
1212
|
);
|
|
1142
1213
|
context.emitter.emit(
|
|
1143
|
-
|
|
1214
|
+
responseMessage.isMockedResponse ? "response:mocked" : "response:bypass",
|
|
1144
1215
|
{
|
|
1145
|
-
requestId:
|
|
1216
|
+
requestId: responseMessage.request.id,
|
|
1146
1217
|
request,
|
|
1147
1218
|
response
|
|
1148
1219
|
}
|
|
@@ -1151,10 +1222,10 @@ function createResponseListener(context) {
|
|
|
1151
1222
|
}
|
|
1152
1223
|
|
|
1153
1224
|
// src/browser/setupWorker/start/utils/validateWorkerScope.ts
|
|
1154
|
-
import { devUtils as
|
|
1225
|
+
import { devUtils as devUtils5 } from '../core/utils/internal/devUtils.mjs';
|
|
1155
1226
|
function validateWorkerScope(registration, options) {
|
|
1156
1227
|
if (!options?.quiet && !location.href.startsWith(registration.scope)) {
|
|
1157
|
-
|
|
1228
|
+
devUtils5.warn(
|
|
1158
1229
|
`Cannot intercept requests on this page because it's outside of the worker's scope ("${registration.scope}"). If you wish to mock API requests on this page, you must resolve this scope issue.
|
|
1159
1230
|
|
|
1160
1231
|
- (Recommended) Register the worker at the root level ("/") of your application.
|
|
@@ -1167,7 +1238,7 @@ function validateWorkerScope(registration, options) {
|
|
|
1167
1238
|
var createStartHandler = (context) => {
|
|
1168
1239
|
return function start(options, customOptions) {
|
|
1169
1240
|
const startWorkerInstance = async () => {
|
|
1170
|
-
context.
|
|
1241
|
+
context.workerChannel.removeAllListeners();
|
|
1171
1242
|
context.workerChannel.on(
|
|
1172
1243
|
"REQUEST",
|
|
1173
1244
|
createRequestListener(context, options)
|
|
@@ -1180,14 +1251,14 @@ var createStartHandler = (context) => {
|
|
|
1180
1251
|
);
|
|
1181
1252
|
const [worker, registration] = instance;
|
|
1182
1253
|
if (!worker) {
|
|
1183
|
-
const missingWorkerMessage = customOptions?.findWorker ?
|
|
1254
|
+
const missingWorkerMessage = customOptions?.findWorker ? devUtils6.formatMessage(
|
|
1184
1255
|
`Failed to locate the Service Worker registration using a custom "findWorker" predicate.
|
|
1185
1256
|
|
|
1186
1257
|
Please ensure that the custom predicate properly locates the Service Worker registration at "%s".
|
|
1187
1258
|
More details: https://mswjs.io/docs/api/setup-worker/start#findworker
|
|
1188
1259
|
`,
|
|
1189
1260
|
options.serviceWorker.url
|
|
1190
|
-
) :
|
|
1261
|
+
) : devUtils6.formatMessage(
|
|
1191
1262
|
`Failed to locate the Service Worker registration.
|
|
1192
1263
|
|
|
1193
1264
|
This most likely means that the worker script URL "%s" cannot resolve against the actual public hostname (%s). This may happen if your application runs behind a proxy, or has a dynamic hostname.
|
|
@@ -1198,23 +1269,23 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
1198
1269
|
);
|
|
1199
1270
|
throw new Error(missingWorkerMessage);
|
|
1200
1271
|
}
|
|
1201
|
-
context.worker
|
|
1272
|
+
context.workerPromise.resolve(worker);
|
|
1202
1273
|
context.registration = registration;
|
|
1203
|
-
|
|
1274
|
+
window.addEventListener("beforeunload", () => {
|
|
1204
1275
|
if (worker.state !== "redundant") {
|
|
1205
|
-
context.workerChannel.
|
|
1276
|
+
context.workerChannel.postMessage("CLIENT_CLOSED");
|
|
1206
1277
|
}
|
|
1207
1278
|
window.clearInterval(context.keepAliveInterval);
|
|
1208
1279
|
window.postMessage({ type: "msw/worker:stop" });
|
|
1209
1280
|
});
|
|
1210
1281
|
await checkWorkerIntegrity(context).catch((error2) => {
|
|
1211
|
-
|
|
1212
|
-
"Error while checking the worker script integrity. Please report this on GitHub (https://github.com/mswjs/msw/issues)
|
|
1282
|
+
devUtils6.error(
|
|
1283
|
+
"Error while checking the worker script integrity. Please report this on GitHub (https://github.com/mswjs/msw/issues) and include the original error below."
|
|
1213
1284
|
);
|
|
1214
1285
|
console.error(error2);
|
|
1215
1286
|
});
|
|
1216
1287
|
context.keepAliveInterval = window.setInterval(
|
|
1217
|
-
() => context.workerChannel.
|
|
1288
|
+
() => context.workerChannel.postMessage("KEEPALIVE_REQUEST"),
|
|
1218
1289
|
5e3
|
|
1219
1290
|
);
|
|
1220
1291
|
validateWorkerScope(registration, context.startOptions);
|
|
@@ -1224,16 +1295,19 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
1224
1295
|
async (registration) => {
|
|
1225
1296
|
const pendingInstance = registration.installing || registration.waiting;
|
|
1226
1297
|
if (pendingInstance) {
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
});
|
|
1298
|
+
const activationPromise = new DeferredPromise();
|
|
1299
|
+
pendingInstance.addEventListener("statechange", () => {
|
|
1300
|
+
if (pendingInstance.state === "activated") {
|
|
1301
|
+
activationPromise.resolve();
|
|
1302
|
+
}
|
|
1233
1303
|
});
|
|
1304
|
+
await activationPromise;
|
|
1234
1305
|
}
|
|
1235
1306
|
await enableMocking(context, options).catch((error2) => {
|
|
1236
|
-
|
|
1307
|
+
devUtils6.error(
|
|
1308
|
+
"Failed to enable mocking. Please report this on GitHub (https://github.com/mswjs/msw/issues) and include the original error below."
|
|
1309
|
+
);
|
|
1310
|
+
throw error2;
|
|
1237
1311
|
});
|
|
1238
1312
|
return registration;
|
|
1239
1313
|
}
|
|
@@ -1242,116 +1316,324 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
1242
1316
|
};
|
|
1243
1317
|
};
|
|
1244
1318
|
|
|
1245
|
-
// src/browser/setupWorker/
|
|
1246
|
-
import { devUtils as devUtils9 } from '../core/utils/internal/devUtils.mjs';
|
|
1247
|
-
|
|
1248
|
-
// src/browser/setupWorker/stop/utils/printStopMessage.ts
|
|
1319
|
+
// src/browser/setupWorker/setupWorker.ts
|
|
1249
1320
|
import { devUtils as devUtils8 } from '../core/utils/internal/devUtils.mjs';
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1321
|
+
import { SetupApi } from '../core/SetupApi.mjs';
|
|
1322
|
+
import { mergeRight as mergeRight2 } from '../core/utils/internal/mergeRight.mjs';
|
|
1323
|
+
|
|
1324
|
+
// src/browser/utils/supportsReadableStreamTransfer.ts
|
|
1325
|
+
function supportsReadableStreamTransfer() {
|
|
1326
|
+
try {
|
|
1327
|
+
const stream = new ReadableStream({
|
|
1328
|
+
start: (controller) => controller.close()
|
|
1329
|
+
});
|
|
1330
|
+
const message = new MessageChannel();
|
|
1331
|
+
message.port1.postMessage(stream, [stream]);
|
|
1332
|
+
return true;
|
|
1333
|
+
} catch {
|
|
1334
|
+
return false;
|
|
1253
1335
|
}
|
|
1254
|
-
console.log(
|
|
1255
|
-
`%c${devUtils8.formatMessage("Mocking disabled.")}`,
|
|
1256
|
-
"color:orangered;font-weight:bold;"
|
|
1257
|
-
);
|
|
1258
1336
|
}
|
|
1259
1337
|
|
|
1260
|
-
// src/browser/setupWorker/
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
devUtils9.warn(
|
|
1265
|
-
'Found a redundant "worker.stop()" call. Note that stopping the worker while mocking already stopped has no effect. Consider removing this "worker.stop()" call.'
|
|
1266
|
-
);
|
|
1267
|
-
return;
|
|
1268
|
-
}
|
|
1269
|
-
context.workerChannel.send("MOCK_DEACTIVATE");
|
|
1270
|
-
context.isMockingEnabled = false;
|
|
1271
|
-
window.clearInterval(context.keepAliveInterval);
|
|
1272
|
-
window.postMessage({ type: "msw/worker:stop" });
|
|
1273
|
-
printStopMessage({ quiet: context.startOptions?.quiet });
|
|
1274
|
-
};
|
|
1275
|
-
};
|
|
1338
|
+
// src/browser/setupWorker/setupWorker.ts
|
|
1339
|
+
import { webSocketInterceptor } from '../core/ws/webSocketInterceptor.mjs';
|
|
1340
|
+
import { handleWebSocketEvent } from '../core/ws/handleWebSocketEvent.mjs';
|
|
1341
|
+
import { attachWebSocketLogger } from '../core/ws/utils/attachWebSocketLogger.mjs';
|
|
1276
1342
|
|
|
1277
|
-
//
|
|
1278
|
-
|
|
1279
|
-
var
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1343
|
+
// node_modules/.pnpm/rettime@0.7.0/node_modules/rettime/build/index.js
|
|
1344
|
+
var kDefaultPrevented = Symbol("kDefaultPrevented");
|
|
1345
|
+
var kPropagationStopped = Symbol("kPropagationStopped");
|
|
1346
|
+
var kImmediatePropagationStopped = Symbol("kImmediatePropagationStopped");
|
|
1347
|
+
var TypedEvent = class extends MessageEvent {
|
|
1348
|
+
/**
|
|
1349
|
+
* @note Keep a placeholder property with the return type
|
|
1350
|
+
* because the type must be set somewhere in order to be
|
|
1351
|
+
* correctly associated and inferred from the event.
|
|
1352
|
+
*/
|
|
1353
|
+
#returnType;
|
|
1354
|
+
[kDefaultPrevented];
|
|
1355
|
+
[kPropagationStopped];
|
|
1356
|
+
[kImmediatePropagationStopped];
|
|
1357
|
+
constructor(...args) {
|
|
1358
|
+
super(args[0], args[1]);
|
|
1359
|
+
this[kDefaultPrevented] = false;
|
|
1360
|
+
}
|
|
1361
|
+
get defaultPrevented() {
|
|
1362
|
+
return this[kDefaultPrevented];
|
|
1363
|
+
}
|
|
1364
|
+
preventDefault() {
|
|
1365
|
+
super.preventDefault();
|
|
1366
|
+
this[kDefaultPrevented] = true;
|
|
1367
|
+
}
|
|
1368
|
+
stopImmediatePropagation() {
|
|
1369
|
+
super.stopImmediatePropagation();
|
|
1370
|
+
this[kImmediatePropagationStopped] = true;
|
|
1289
1371
|
}
|
|
1290
1372
|
};
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1373
|
+
var kListenerOptions = Symbol("kListenerOptions");
|
|
1374
|
+
var Emitter2 = class {
|
|
1375
|
+
#listeners;
|
|
1376
|
+
constructor() {
|
|
1377
|
+
this.#listeners = {};
|
|
1378
|
+
}
|
|
1379
|
+
/**
|
|
1380
|
+
* Adds a listener for the given event type.
|
|
1381
|
+
*
|
|
1382
|
+
* @returns {AbortController} An `AbortController` that can be used to remove the listener.
|
|
1383
|
+
*/
|
|
1384
|
+
on(type, listener, options) {
|
|
1385
|
+
return this.#addListener(type, listener, options);
|
|
1386
|
+
}
|
|
1387
|
+
/**
|
|
1388
|
+
* Adds a one-time listener for the given event type.
|
|
1389
|
+
*
|
|
1390
|
+
* @returns {AbortController} An `AbortController` that can be used to remove the listener.
|
|
1391
|
+
*/
|
|
1392
|
+
once(type, listener, options) {
|
|
1393
|
+
return this.on(type, listener, { ...options || {}, once: true });
|
|
1394
|
+
}
|
|
1395
|
+
/**
|
|
1396
|
+
* Prepends a listener for the given event type.
|
|
1397
|
+
*
|
|
1398
|
+
* @returns {AbortController} An `AbortController` that can be used to remove the listener.
|
|
1399
|
+
*/
|
|
1400
|
+
earlyOn(type, listener, options) {
|
|
1401
|
+
return this.#addListener(type, listener, options, "prepend");
|
|
1402
|
+
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Prepends a one-time listener for the given event type.
|
|
1405
|
+
*/
|
|
1406
|
+
earlyOnce(type, listener, options) {
|
|
1407
|
+
return this.earlyOn(type, listener, { ...options || {}, once: true });
|
|
1408
|
+
}
|
|
1409
|
+
/**
|
|
1410
|
+
* Emits the given typed event.
|
|
1411
|
+
*
|
|
1412
|
+
* @returns {boolean} Returns `true` if the event had any listeners, `false` otherwise.
|
|
1413
|
+
*/
|
|
1414
|
+
emit(event) {
|
|
1415
|
+
if (this.listenerCount(event.type) === 0) {
|
|
1416
|
+
return false;
|
|
1417
|
+
}
|
|
1418
|
+
const proxiedEvent = this.#proxyEvent(event);
|
|
1419
|
+
for (const listener of this.#listeners[event.type]) {
|
|
1420
|
+
if (proxiedEvent.event[kPropagationStopped] != null && proxiedEvent.event[kPropagationStopped] !== this) {
|
|
1421
|
+
return false;
|
|
1299
1422
|
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1423
|
+
if (proxiedEvent.event[kImmediatePropagationStopped]) {
|
|
1424
|
+
break;
|
|
1425
|
+
}
|
|
1426
|
+
this.#callListener(proxiedEvent.event, listener);
|
|
1427
|
+
}
|
|
1428
|
+
proxiedEvent.revoke();
|
|
1429
|
+
return true;
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* Emits the given typed event and returns a promise that resolves
|
|
1433
|
+
* when all the listeners for that event have settled.
|
|
1434
|
+
*
|
|
1435
|
+
* @returns {Promise<Array<Emitter.ListenerReturnType>>} A promise that resolves
|
|
1436
|
+
* with the return values of all listeners.
|
|
1437
|
+
*/
|
|
1438
|
+
async emitAsPromise(event) {
|
|
1439
|
+
if (this.listenerCount(event.type) === 0) {
|
|
1440
|
+
return [];
|
|
1441
|
+
}
|
|
1442
|
+
const pendingListeners = [];
|
|
1443
|
+
const proxiedEvent = this.#proxyEvent(event);
|
|
1444
|
+
for (const listener of this.#listeners[event.type]) {
|
|
1445
|
+
if (proxiedEvent.event[kPropagationStopped] != null && proxiedEvent.event[kPropagationStopped] !== this) {
|
|
1446
|
+
return [];
|
|
1447
|
+
}
|
|
1448
|
+
if (proxiedEvent.event[kImmediatePropagationStopped]) {
|
|
1449
|
+
break;
|
|
1450
|
+
}
|
|
1451
|
+
pendingListeners.push(
|
|
1452
|
+
// Awaiting individual listeners guarantees their call order.
|
|
1453
|
+
await Promise.resolve(this.#callListener(proxiedEvent.event, listener))
|
|
1307
1454
|
);
|
|
1308
|
-
}
|
|
1309
|
-
|
|
1310
|
-
|
|
1455
|
+
}
|
|
1456
|
+
proxiedEvent.revoke();
|
|
1457
|
+
return Promise.allSettled(pendingListeners).then((results) => {
|
|
1458
|
+
return results.map(
|
|
1459
|
+
(result) => result.status === "fulfilled" ? result.value : result.reason
|
|
1460
|
+
);
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
/**
|
|
1464
|
+
* Emits the given event and returns a generator that yields
|
|
1465
|
+
* the result of each listener in the order of their registration.
|
|
1466
|
+
* This way, you stop exhausting the listeners once you get the expected value.
|
|
1467
|
+
*/
|
|
1468
|
+
*emitAsGenerator(event) {
|
|
1469
|
+
if (this.listenerCount(event.type) === 0) {
|
|
1470
|
+
return;
|
|
1471
|
+
}
|
|
1472
|
+
const proxiedEvent = this.#proxyEvent(event);
|
|
1473
|
+
for (const listener of this.#listeners[event.type]) {
|
|
1474
|
+
if (proxiedEvent.event[kPropagationStopped] != null && proxiedEvent.event[kPropagationStopped] !== this) {
|
|
1311
1475
|
return;
|
|
1312
1476
|
}
|
|
1313
|
-
|
|
1314
|
-
|
|
1477
|
+
if (proxiedEvent.event[kImmediatePropagationStopped]) {
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
yield this.#callListener(proxiedEvent.event, listener);
|
|
1481
|
+
}
|
|
1482
|
+
proxiedEvent.revoke();
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* Removes a listener for the given event type.
|
|
1486
|
+
*/
|
|
1487
|
+
removeListener(type, listener) {
|
|
1488
|
+
if (this.listenerCount(type) === 0) {
|
|
1489
|
+
return;
|
|
1490
|
+
}
|
|
1491
|
+
const nextListeners = [];
|
|
1492
|
+
for (const existingListener of this.#listeners[type]) {
|
|
1493
|
+
if (existingListener !== listener) {
|
|
1494
|
+
nextListeners.push(existingListener);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
this.#listeners[type] = nextListeners;
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* Removes all listeners for the given event type.
|
|
1501
|
+
* If no event type is provided, removes all existing listeners.
|
|
1502
|
+
*/
|
|
1503
|
+
removeAllListeners(type) {
|
|
1504
|
+
if (type == null) {
|
|
1505
|
+
this.#listeners = {};
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
this.#listeners[type] = [];
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Returns the list of listeners for the given event type.
|
|
1512
|
+
* If no even type is provided, returns all listeners.
|
|
1513
|
+
*/
|
|
1514
|
+
listeners(type) {
|
|
1515
|
+
if (type == null) {
|
|
1516
|
+
return Object.values(this.#listeners).flat();
|
|
1517
|
+
}
|
|
1518
|
+
return this.#listeners[type] || [];
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Returns the number of listeners for the given event type.
|
|
1522
|
+
* If no even type is provided, returns the total number of listeners.
|
|
1523
|
+
*/
|
|
1524
|
+
listenerCount(type) {
|
|
1525
|
+
return this.listeners(type).length;
|
|
1526
|
+
}
|
|
1527
|
+
#addListener(type, listener, options, insertMode = "append") {
|
|
1528
|
+
this.#listeners[type] ??= [];
|
|
1529
|
+
if (insertMode === "prepend") {
|
|
1530
|
+
this.#listeners[type].unshift(listener);
|
|
1531
|
+
} else {
|
|
1532
|
+
this.#listeners[type].push(listener);
|
|
1533
|
+
}
|
|
1534
|
+
if (options) {
|
|
1535
|
+
Object.defineProperty(listener, kListenerOptions, {
|
|
1536
|
+
value: options,
|
|
1537
|
+
enumerable: false,
|
|
1538
|
+
writable: false
|
|
1315
1539
|
});
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1540
|
+
if (options.signal) {
|
|
1541
|
+
options.signal.addEventListener(
|
|
1542
|
+
"abort",
|
|
1543
|
+
() => {
|
|
1544
|
+
this.removeListener(type, listener);
|
|
1545
|
+
},
|
|
1546
|
+
{ once: true }
|
|
1547
|
+
);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
return this;
|
|
1551
|
+
}
|
|
1552
|
+
#proxyEvent(event) {
|
|
1553
|
+
const { stopPropagation } = event;
|
|
1554
|
+
event.stopPropagation = new Proxy(event.stopPropagation, {
|
|
1555
|
+
apply: (target, thisArg, argArray) => {
|
|
1556
|
+
event[kPropagationStopped] = this;
|
|
1557
|
+
return Reflect.apply(target, thisArg, argArray);
|
|
1558
|
+
}
|
|
1330
1559
|
});
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1560
|
+
return {
|
|
1561
|
+
event,
|
|
1562
|
+
revoke() {
|
|
1563
|
+
event.stopPropagation = stopPropagation;
|
|
1564
|
+
}
|
|
1565
|
+
};
|
|
1334
1566
|
}
|
|
1335
|
-
|
|
1336
|
-
|
|
1567
|
+
#callListener(event, listener) {
|
|
1568
|
+
const returnValue = listener.call(this, event);
|
|
1569
|
+
if (listener[kListenerOptions]?.once) {
|
|
1570
|
+
this.removeListener(event.type, listener);
|
|
1571
|
+
}
|
|
1572
|
+
return returnValue;
|
|
1337
1573
|
}
|
|
1338
|
-
|
|
1339
|
-
|
|
1574
|
+
};
|
|
1575
|
+
|
|
1576
|
+
// src/browser/utils/workerChannel.ts
|
|
1577
|
+
import { isObject } from '../core/utils/internal/isObject.mjs';
|
|
1578
|
+
var WorkerEvent = class extends TypedEvent {
|
|
1579
|
+
#workerEvent;
|
|
1580
|
+
constructor(workerEvent) {
|
|
1581
|
+
const type = workerEvent.data.type;
|
|
1582
|
+
const data = workerEvent.data.payload;
|
|
1583
|
+
super(
|
|
1584
|
+
// @ts-expect-error Troublesome `TypedEvent` extension.
|
|
1585
|
+
type,
|
|
1586
|
+
{ data }
|
|
1587
|
+
);
|
|
1588
|
+
this.#workerEvent = workerEvent;
|
|
1340
1589
|
}
|
|
1341
|
-
|
|
1342
|
-
return this.#
|
|
1590
|
+
get ports() {
|
|
1591
|
+
return this.#workerEvent.ports;
|
|
1343
1592
|
}
|
|
1344
|
-
|
|
1345
|
-
|
|
1593
|
+
/**
|
|
1594
|
+
* Reply directly to this event using its `MessagePort`.
|
|
1595
|
+
*/
|
|
1596
|
+
postMessage(type, ...rest) {
|
|
1597
|
+
this.#workerEvent.ports[0].postMessage(
|
|
1598
|
+
{ type, data: rest[0] },
|
|
1599
|
+
{ transfer: rest[1] }
|
|
1600
|
+
);
|
|
1346
1601
|
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1602
|
+
};
|
|
1603
|
+
var WorkerChannel = class extends Emitter2 {
|
|
1604
|
+
constructor(options) {
|
|
1605
|
+
super();
|
|
1606
|
+
this.options = options;
|
|
1607
|
+
navigator.serviceWorker.addEventListener("message", async (event) => {
|
|
1608
|
+
const worker = await this.options.worker;
|
|
1609
|
+
if (event.source != null && event.source !== worker) {
|
|
1610
|
+
return;
|
|
1611
|
+
}
|
|
1612
|
+
if (event.data && isObject(event.data) && "type" in event.data) {
|
|
1613
|
+
this.emit(new WorkerEvent(event));
|
|
1614
|
+
}
|
|
1615
|
+
});
|
|
1349
1616
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1617
|
+
/**
|
|
1618
|
+
* Send data to the Service Worker controlling this client.
|
|
1619
|
+
* This triggers the `message` event listener on ServiceWorkerGlobalScope.
|
|
1620
|
+
*/
|
|
1621
|
+
postMessage(type) {
|
|
1622
|
+
this.options.worker.then((worker) => {
|
|
1623
|
+
worker.postMessage(type);
|
|
1624
|
+
});
|
|
1625
|
+
}
|
|
1626
|
+
};
|
|
1627
|
+
|
|
1628
|
+
// node_modules/.pnpm/@open-draft+until@2.1.0/node_modules/@open-draft/until/lib/index.mjs
|
|
1629
|
+
var until2 = async (promise) => {
|
|
1630
|
+
try {
|
|
1631
|
+
const data = await promise().catch((error2) => {
|
|
1632
|
+
throw error2;
|
|
1354
1633
|
});
|
|
1634
|
+
return { error: null, data };
|
|
1635
|
+
} catch (error2) {
|
|
1636
|
+
return { error: error2, data: null };
|
|
1355
1637
|
}
|
|
1356
1638
|
};
|
|
1357
1639
|
|
|
@@ -1418,7 +1700,7 @@ async function emitAsync(emitter, eventName, ...data) {
|
|
|
1418
1700
|
await listener.apply(emitter, data);
|
|
1419
1701
|
}
|
|
1420
1702
|
}
|
|
1421
|
-
function
|
|
1703
|
+
function isObject2(value, loose = false) {
|
|
1422
1704
|
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
1423
1705
|
}
|
|
1424
1706
|
function isPropertyAccessible(obj, key) {
|
|
@@ -1451,7 +1733,7 @@ function isResponseError(response) {
|
|
|
1451
1733
|
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
1452
1734
|
}
|
|
1453
1735
|
function isResponseLike(value) {
|
|
1454
|
-
return
|
|
1736
|
+
return isObject2(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
1455
1737
|
}
|
|
1456
1738
|
function isNodeLikeError(error2) {
|
|
1457
1739
|
if (error2 == null) {
|
|
@@ -1476,7 +1758,7 @@ async function handleRequest2(options) {
|
|
|
1476
1758
|
await options.onResponse(response);
|
|
1477
1759
|
return true;
|
|
1478
1760
|
}
|
|
1479
|
-
if (
|
|
1761
|
+
if (isObject2(response)) {
|
|
1480
1762
|
options.onError(response);
|
|
1481
1763
|
return true;
|
|
1482
1764
|
}
|
|
@@ -1517,7 +1799,7 @@ async function handleRequest2(options) {
|
|
|
1517
1799
|
);
|
|
1518
1800
|
}
|
|
1519
1801
|
}
|
|
1520
|
-
const result = await
|
|
1802
|
+
const result = await until2(async () => {
|
|
1521
1803
|
const requestListenersPromise = emitAsync(options.emitter, "request", {
|
|
1522
1804
|
requestId: options.requestId,
|
|
1523
1805
|
request: options.request,
|
|
@@ -1553,7 +1835,7 @@ async function handleRequest2(options) {
|
|
|
1553
1835
|
unhandledExceptionController[kResponsePromise].resolve(void 0);
|
|
1554
1836
|
}
|
|
1555
1837
|
});
|
|
1556
|
-
const nextResult = await
|
|
1838
|
+
const nextResult = await until2(
|
|
1557
1839
|
() => unhandledExceptionController[kResponsePromise]
|
|
1558
1840
|
);
|
|
1559
1841
|
if (nextResult.error) {
|
|
@@ -2687,162 +2969,66 @@ function createFallbackRequestListener(context, options) {
|
|
|
2687
2969
|
return interceptor;
|
|
2688
2970
|
}
|
|
2689
2971
|
|
|
2690
|
-
// src/browser/setupWorker/
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
options
|
|
2696
|
-
);
|
|
2697
|
-
printStartMessage({
|
|
2698
|
-
message: "Mocking enabled (fallback mode).",
|
|
2699
|
-
quiet: options.quiet
|
|
2700
|
-
});
|
|
2701
|
-
return void 0;
|
|
2702
|
-
};
|
|
2703
|
-
}
|
|
2704
|
-
|
|
2705
|
-
// src/browser/setupWorker/stop/createFallbackStop.ts
|
|
2706
|
-
function createFallbackStop(context) {
|
|
2707
|
-
return function stop() {
|
|
2708
|
-
context.fallbackInterceptor?.dispose();
|
|
2709
|
-
printStopMessage({ quiet: context.startOptions?.quiet });
|
|
2710
|
-
};
|
|
2711
|
-
}
|
|
2712
|
-
|
|
2713
|
-
// src/browser/setupWorker/setupWorker.ts
|
|
2714
|
-
import { devUtils as devUtils10 } from '../core/utils/internal/devUtils.mjs';
|
|
2715
|
-
import { SetupApi } from '../core/SetupApi.mjs';
|
|
2716
|
-
import { mergeRight as mergeRight2 } from '../core/utils/internal/mergeRight.mjs';
|
|
2717
|
-
|
|
2718
|
-
// src/browser/utils/supportsReadableStreamTransfer.ts
|
|
2719
|
-
function supportsReadableStreamTransfer() {
|
|
2720
|
-
try {
|
|
2721
|
-
const stream = new ReadableStream({
|
|
2722
|
-
start: (controller) => controller.close()
|
|
2723
|
-
});
|
|
2724
|
-
const message = new MessageChannel();
|
|
2725
|
-
message.port1.postMessage(stream, [stream]);
|
|
2726
|
-
return true;
|
|
2727
|
-
} catch {
|
|
2728
|
-
return false;
|
|
2972
|
+
// src/browser/setupWorker/stop/utils/printStopMessage.ts
|
|
2973
|
+
import { devUtils as devUtils7 } from '../core/utils/internal/devUtils.mjs';
|
|
2974
|
+
function printStopMessage(args = {}) {
|
|
2975
|
+
if (args.quiet) {
|
|
2976
|
+
return;
|
|
2729
2977
|
}
|
|
2978
|
+
console.log(
|
|
2979
|
+
`%c${devUtils7.formatMessage("Mocking disabled.")}`,
|
|
2980
|
+
"color:orangered;font-weight:bold;"
|
|
2981
|
+
);
|
|
2730
2982
|
}
|
|
2731
2983
|
|
|
2732
2984
|
// src/browser/setupWorker/setupWorker.ts
|
|
2733
|
-
import { webSocketInterceptor } from '../core/ws/webSocketInterceptor.mjs';
|
|
2734
|
-
import { handleWebSocketEvent } from '../core/ws/handleWebSocketEvent.mjs';
|
|
2735
|
-
import { attachWebSocketLogger } from '../core/ws/utils/attachWebSocketLogger.mjs';
|
|
2736
2985
|
var SetupWorkerApi = class extends SetupApi {
|
|
2737
2986
|
context;
|
|
2738
|
-
startHandler = null;
|
|
2739
|
-
stopHandler = null;
|
|
2740
|
-
listeners;
|
|
2741
2987
|
constructor(...handlers) {
|
|
2742
2988
|
super(...handlers);
|
|
2743
2989
|
invariant(
|
|
2744
2990
|
!isNodeProcess(),
|
|
2745
|
-
|
|
2991
|
+
devUtils8.formatMessage(
|
|
2746
2992
|
"Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead."
|
|
2747
2993
|
)
|
|
2748
2994
|
);
|
|
2749
|
-
this.listeners = [];
|
|
2750
2995
|
this.context = this.createWorkerContext();
|
|
2751
2996
|
}
|
|
2752
2997
|
createWorkerContext() {
|
|
2753
|
-
const
|
|
2998
|
+
const workerPromise = new DeferredPromise();
|
|
2999
|
+
return {
|
|
2754
3000
|
// Mocking is not considered enabled until the worker
|
|
2755
3001
|
// signals back the successful activation event.
|
|
2756
3002
|
isMockingEnabled: false,
|
|
2757
3003
|
startOptions: null,
|
|
2758
|
-
|
|
3004
|
+
workerPromise,
|
|
3005
|
+
registration: void 0,
|
|
2759
3006
|
getRequestHandlers: () => {
|
|
2760
3007
|
return this.handlersController.currentHandlers();
|
|
2761
3008
|
},
|
|
2762
|
-
registration: null,
|
|
2763
3009
|
emitter: this.emitter,
|
|
2764
|
-
workerChannel: {
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
if (event.source !== this.context.worker) {
|
|
2768
|
-
return;
|
|
2769
|
-
}
|
|
2770
|
-
const message = event.data;
|
|
2771
|
-
if (!message) {
|
|
2772
|
-
return;
|
|
2773
|
-
}
|
|
2774
|
-
if (message.type === eventType) {
|
|
2775
|
-
callback(event, message);
|
|
2776
|
-
}
|
|
2777
|
-
});
|
|
2778
|
-
},
|
|
2779
|
-
send: (type) => {
|
|
2780
|
-
this.context.worker?.postMessage(type);
|
|
2781
|
-
}
|
|
2782
|
-
},
|
|
2783
|
-
events: {
|
|
2784
|
-
addListener: (target, eventType, callback) => {
|
|
2785
|
-
target.addEventListener(eventType, callback);
|
|
2786
|
-
this.listeners.push({
|
|
2787
|
-
eventType,
|
|
2788
|
-
target,
|
|
2789
|
-
callback
|
|
2790
|
-
});
|
|
2791
|
-
return () => {
|
|
2792
|
-
target.removeEventListener(eventType, callback);
|
|
2793
|
-
};
|
|
2794
|
-
},
|
|
2795
|
-
removeAllListeners: () => {
|
|
2796
|
-
for (const { target, eventType, callback } of this.listeners) {
|
|
2797
|
-
target.removeEventListener(eventType, callback);
|
|
2798
|
-
}
|
|
2799
|
-
this.listeners = [];
|
|
2800
|
-
},
|
|
2801
|
-
once: (eventType) => {
|
|
2802
|
-
const bindings = [];
|
|
2803
|
-
return new Promise((resolve, reject) => {
|
|
2804
|
-
const handleIncomingMessage = (event) => {
|
|
2805
|
-
try {
|
|
2806
|
-
const message = event.data;
|
|
2807
|
-
if (message.type === eventType) {
|
|
2808
|
-
resolve(message);
|
|
2809
|
-
}
|
|
2810
|
-
} catch (error2) {
|
|
2811
|
-
reject(error2);
|
|
2812
|
-
}
|
|
2813
|
-
};
|
|
2814
|
-
bindings.push(
|
|
2815
|
-
this.context.events.addListener(
|
|
2816
|
-
navigator.serviceWorker,
|
|
2817
|
-
"message",
|
|
2818
|
-
handleIncomingMessage
|
|
2819
|
-
),
|
|
2820
|
-
this.context.events.addListener(
|
|
2821
|
-
navigator.serviceWorker,
|
|
2822
|
-
"messageerror",
|
|
2823
|
-
reject
|
|
2824
|
-
)
|
|
2825
|
-
);
|
|
2826
|
-
}).finally(() => {
|
|
2827
|
-
bindings.forEach((unbind) => unbind());
|
|
2828
|
-
});
|
|
2829
|
-
}
|
|
2830
|
-
},
|
|
3010
|
+
workerChannel: new WorkerChannel({
|
|
3011
|
+
worker: workerPromise
|
|
3012
|
+
}),
|
|
2831
3013
|
supports: {
|
|
2832
|
-
serviceWorkerApi:
|
|
3014
|
+
serviceWorkerApi: "serviceWorker" in navigator && location.protocol !== "file:",
|
|
2833
3015
|
readableStreamTransfer: supportsReadableStreamTransfer()
|
|
2834
3016
|
}
|
|
2835
3017
|
};
|
|
2836
|
-
this.startHandler = context.supports.serviceWorkerApi ? createFallbackStart(context) : createStartHandler(context);
|
|
2837
|
-
this.stopHandler = context.supports.serviceWorkerApi ? createFallbackStop(context) : createStop(context);
|
|
2838
|
-
return context;
|
|
2839
3018
|
}
|
|
2840
3019
|
async start(options = {}) {
|
|
2841
|
-
if (
|
|
2842
|
-
|
|
3020
|
+
if ("waitUntilReady" in options) {
|
|
3021
|
+
devUtils8.warn(
|
|
2843
3022
|
'The "waitUntilReady" option has been deprecated. Please remove it from this "worker.start()" call. Follow the recommended Browser integration (https://mswjs.io/docs/integrations/browser) to eliminate any race conditions between the Service Worker registration and any requests made by your application on initial render.'
|
|
2844
3023
|
);
|
|
2845
3024
|
}
|
|
3025
|
+
if (this.context.isMockingEnabled) {
|
|
3026
|
+
devUtils8.warn(
|
|
3027
|
+
`Found a redundant "worker.start()" call. Note that starting the worker while mocking is already enabled will have no effect. Consider removing this "worker.start()" call.`
|
|
3028
|
+
);
|
|
3029
|
+
return this.context.registration;
|
|
3030
|
+
}
|
|
3031
|
+
this.context.workerStoppedAt = void 0;
|
|
2846
3032
|
this.context.startOptions = mergeRight2(
|
|
2847
3033
|
DEFAULT_START_OPTIONS,
|
|
2848
3034
|
options
|
|
@@ -2866,13 +3052,45 @@ var SetupWorkerApi = class extends SetupApi {
|
|
|
2866
3052
|
this.subscriptions.push(() => {
|
|
2867
3053
|
webSocketInterceptor.dispose();
|
|
2868
3054
|
});
|
|
2869
|
-
|
|
3055
|
+
if (!this.context.supports.serviceWorkerApi) {
|
|
3056
|
+
const fallbackInterceptor = createFallbackRequestListener(
|
|
3057
|
+
this.context,
|
|
3058
|
+
this.context.startOptions
|
|
3059
|
+
);
|
|
3060
|
+
this.subscriptions.push(() => {
|
|
3061
|
+
fallbackInterceptor.dispose();
|
|
3062
|
+
});
|
|
3063
|
+
this.context.isMockingEnabled = true;
|
|
3064
|
+
printStartMessage({
|
|
3065
|
+
message: "Mocking enabled (fallback mode).",
|
|
3066
|
+
quiet: this.context.startOptions.quiet
|
|
3067
|
+
});
|
|
3068
|
+
return void 0;
|
|
3069
|
+
}
|
|
3070
|
+
const startHandler = createStartHandler(this.context);
|
|
3071
|
+
const registration = await startHandler(this.context.startOptions, options);
|
|
3072
|
+
this.context.isMockingEnabled = true;
|
|
3073
|
+
return registration;
|
|
2870
3074
|
}
|
|
2871
3075
|
stop() {
|
|
2872
3076
|
super.dispose();
|
|
2873
|
-
this.context.
|
|
3077
|
+
if (!this.context.isMockingEnabled) {
|
|
3078
|
+
devUtils8.warn(
|
|
3079
|
+
'Found a redundant "worker.stop()" call. Notice that stopping the worker after it has already been stopped has no effect. Consider removing this "worker.stop()" call.'
|
|
3080
|
+
);
|
|
3081
|
+
return;
|
|
3082
|
+
}
|
|
3083
|
+
this.context.isMockingEnabled = false;
|
|
3084
|
+
this.context.workerStoppedAt = Date.now();
|
|
2874
3085
|
this.context.emitter.removeAllListeners();
|
|
2875
|
-
this.
|
|
3086
|
+
if (this.context.supports.serviceWorkerApi) {
|
|
3087
|
+
this.context.workerChannel.removeAllListeners("RESPONSE");
|
|
3088
|
+
window.clearInterval(this.context.keepAliveInterval);
|
|
3089
|
+
}
|
|
3090
|
+
window.postMessage({ type: "msw/worker:stop" });
|
|
3091
|
+
printStopMessage({
|
|
3092
|
+
quiet: this.context.startOptions?.quiet
|
|
3093
|
+
});
|
|
2876
3094
|
}
|
|
2877
3095
|
};
|
|
2878
3096
|
function setupWorker(...handlers) {
|