msw 0.44.1 → 0.44.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/iife/index.js +48 -88
- package/lib/iife/index.js.map +1 -1
- package/lib/index.js +43 -83
- package/lib/index.js.map +1 -1
- package/lib/mockServiceWorker.js +4 -68
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -425,17 +425,15 @@ async function enableMocking(context, options) {
|
|
|
425
425
|
}
|
|
426
426
|
|
|
427
427
|
// src/setupWorker/start/utils/createMessageChannel.ts
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
};
|
|
438
|
-
}
|
|
428
|
+
var WorkerChannel = class {
|
|
429
|
+
constructor(port) {
|
|
430
|
+
this.port = port;
|
|
431
|
+
}
|
|
432
|
+
postMessage(event, ...rest2) {
|
|
433
|
+
const [data2, transfer] = rest2;
|
|
434
|
+
this.port.postMessage({ type: event, data: data2 }, { transfer });
|
|
435
|
+
}
|
|
436
|
+
};
|
|
439
437
|
|
|
440
438
|
// src/utils/NetworkError.ts
|
|
441
439
|
var NetworkError = class extends Error {
|
|
@@ -1375,69 +1373,27 @@ async function handleRequest(request, handlers, options, emitter, handleRequestO
|
|
|
1375
1373
|
return transformedResponse;
|
|
1376
1374
|
}
|
|
1377
1375
|
|
|
1378
|
-
// src/setupWorker/start/utils/streamResponse.ts
|
|
1379
|
-
var import_outvariant3 = require("outvariant");
|
|
1380
|
-
async function streamResponse(operationChannel, messageChannel, mockedResponse) {
|
|
1381
|
-
const response2 = new Response(mockedResponse.body, mockedResponse);
|
|
1382
|
-
delete mockedResponse.body;
|
|
1383
|
-
messageChannel.send({
|
|
1384
|
-
type: "MOCK_RESPONSE_START",
|
|
1385
|
-
payload: mockedResponse
|
|
1386
|
-
});
|
|
1387
|
-
(0, import_outvariant3.invariant)(response2.body, "Failed to stream mocked response with no body");
|
|
1388
|
-
const reader = response2.body.getReader();
|
|
1389
|
-
while (true) {
|
|
1390
|
-
const { done, value } = await reader.read();
|
|
1391
|
-
if (!done) {
|
|
1392
|
-
operationChannel.postMessage({
|
|
1393
|
-
type: "MOCK_RESPONSE_CHUNK",
|
|
1394
|
-
payload: value
|
|
1395
|
-
});
|
|
1396
|
-
continue;
|
|
1397
|
-
}
|
|
1398
|
-
operationChannel.postMessage({
|
|
1399
|
-
type: "MOCK_RESPONSE_END"
|
|
1400
|
-
});
|
|
1401
|
-
operationChannel.close();
|
|
1402
|
-
reader.releaseLock();
|
|
1403
|
-
break;
|
|
1404
|
-
}
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
// src/utils/internal/StrictBroadcastChannel.ts
|
|
1408
|
-
var ParentClass = typeof BroadcastChannel == "undefined" ? class UnsupportedEnvironment {
|
|
1409
|
-
constructor() {
|
|
1410
|
-
throw new Error("Cannot construct BroadcastChannel in a non-browser environment");
|
|
1411
|
-
}
|
|
1412
|
-
} : BroadcastChannel;
|
|
1413
|
-
var StrictBroadcastChannel = class extends ParentClass {
|
|
1414
|
-
postMessage(message) {
|
|
1415
|
-
return super.postMessage(message);
|
|
1416
|
-
}
|
|
1417
|
-
};
|
|
1418
|
-
|
|
1419
1376
|
// src/setupWorker/start/createRequestListener.ts
|
|
1420
1377
|
var createRequestListener = (context, options) => {
|
|
1421
1378
|
return async (event, message) => {
|
|
1422
|
-
const messageChannel =
|
|
1379
|
+
const messageChannel = new WorkerChannel(event.ports[0]);
|
|
1380
|
+
const request = parseWorkerRequest(message.payload);
|
|
1423
1381
|
try {
|
|
1424
|
-
const request = parseWorkerRequest(message.payload);
|
|
1425
|
-
const operationChannel = new StrictBroadcastChannel(`msw-response-stream-${request.id}`);
|
|
1426
1382
|
await handleRequest(request, context.requestHandlers, options, context.emitter, {
|
|
1427
1383
|
transformResponse,
|
|
1428
1384
|
onPassthroughResponse() {
|
|
1429
|
-
|
|
1430
|
-
type: "MOCK_NOT_FOUND"
|
|
1431
|
-
});
|
|
1385
|
+
messageChannel.postMessage("NOT_FOUND");
|
|
1432
1386
|
},
|
|
1433
|
-
onMockedResponse(response2) {
|
|
1434
|
-
if (response2.body
|
|
1435
|
-
|
|
1436
|
-
type: "MOCK_RESPONSE",
|
|
1437
|
-
payload: response2
|
|
1438
|
-
});
|
|
1387
|
+
async onMockedResponse(response2) {
|
|
1388
|
+
if (response2.body instanceof ReadableStream) {
|
|
1389
|
+
throw new Error(devUtils.formatMessage('Failed to construct a mocked response with a "ReadableStream" body: mocked streams are not supported. Follow https://github.com/mswjs/msw/issues/1336 for more details.'));
|
|
1439
1390
|
}
|
|
1440
|
-
|
|
1391
|
+
const responseInstance = new Response(response2.body, response2);
|
|
1392
|
+
const responseBodyBuffer = await responseInstance.arrayBuffer();
|
|
1393
|
+
const responseBody = response2.body == null ? null : responseBodyBuffer;
|
|
1394
|
+
messageChannel.postMessage("MOCK_RESPONSE", __spreadProps(__spreadValues({}, response2), {
|
|
1395
|
+
body: responseBody
|
|
1396
|
+
}), [responseBodyBuffer]);
|
|
1441
1397
|
},
|
|
1442
1398
|
onMockedResponseSent(response2, { handler, publicRequest, parsedRequest }) {
|
|
1443
1399
|
if (options.quiet) {
|
|
@@ -1448,25 +1404,29 @@ var createRequestListener = (context, options) => {
|
|
|
1448
1404
|
});
|
|
1449
1405
|
} catch (error2) {
|
|
1450
1406
|
if (error2 instanceof NetworkError) {
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
name: error2.name,
|
|
1455
|
-
message: error2.message
|
|
1456
|
-
}
|
|
1407
|
+
messageChannel.postMessage("NETWORK_ERROR", {
|
|
1408
|
+
name: error2.name,
|
|
1409
|
+
message: error2.message
|
|
1457
1410
|
});
|
|
1411
|
+
return;
|
|
1458
1412
|
}
|
|
1459
1413
|
if (error2 instanceof Error) {
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1414
|
+
devUtils.error(`Uncaught exception in the request handler for "%s %s":
|
|
1415
|
+
|
|
1416
|
+
%s
|
|
1417
|
+
|
|
1418
|
+
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses`, request.method, request.url, error2);
|
|
1419
|
+
messageChannel.postMessage("MOCK_RESPONSE", {
|
|
1420
|
+
status: 500,
|
|
1421
|
+
statusText: "Request Handler Error",
|
|
1422
|
+
headers: {
|
|
1423
|
+
"Content-Type": "application/json"
|
|
1424
|
+
},
|
|
1425
|
+
body: JSON.stringify({
|
|
1426
|
+
name: error2.name,
|
|
1427
|
+
message: error2.message,
|
|
1428
|
+
stack: error2.stack
|
|
1429
|
+
})
|
|
1470
1430
|
});
|
|
1471
1431
|
}
|
|
1472
1432
|
}
|
|
@@ -1486,8 +1446,8 @@ function transformResponse(response2) {
|
|
|
1486
1446
|
async function requestIntegrityCheck(context, serviceWorker) {
|
|
1487
1447
|
context.workerChannel.send("INTEGRITY_CHECK_REQUEST");
|
|
1488
1448
|
const { payload: actualChecksum } = await context.events.once("INTEGRITY_CHECK_RESPONSE");
|
|
1489
|
-
if (actualChecksum !== "
|
|
1490
|
-
throw new Error(`Currently active Service Worker (${actualChecksum}) is behind the latest published one (${"
|
|
1449
|
+
if (actualChecksum !== "b3066ef78c2f9090b4ce87e874965995") {
|
|
1450
|
+
throw new Error(`Currently active Service Worker (${actualChecksum}) is behind the latest published one (${"b3066ef78c2f9090b4ce87e874965995"}).`);
|
|
1491
1451
|
}
|
|
1492
1452
|
return serviceWorker;
|
|
1493
1453
|
}
|