msw 0.38.2 → 0.39.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/esm/index.js CHANGED
@@ -745,13 +745,36 @@ function getRequestCookies(request) {
745
745
  }
746
746
  }
747
747
 
748
+ /**
749
+ * Sets relevant cookies on the request.
750
+ * Request cookies are taken from the following sources:
751
+ * - Immediate (own) request cookies (those in the "Cookie" request header);
752
+ * - From the `document.cookie` based on the request's `credentials` value;
753
+ * - From the internal cookie store that persists/hydrates cookies in Node.js
754
+ */
748
755
  function setRequestCookies(request) {
749
756
  var _a;
757
+ // Set mocked request cookies from the `cookie` header of the original request.
758
+ // No need to take `credentials` into account, because in Node.js requests are intercepted
759
+ // _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
760
+ // Unlike browser, where interception is on the worker level, _before_ the request happens.
761
+ const requestCookiesString = request.headers.get('cookie');
750
762
  store.hydrate();
751
- request.cookies = Object.assign(Object.assign({}, getRequestCookies(request)), Array.from((_a = store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => Object.assign(cookies, { [name]: value }), {}));
752
- request.headers.set('cookie', Object.entries(request.cookies)
753
- .map(([name, value]) => `${name}=${value}`)
754
- .join('; '));
763
+ const cookiesFromStore = Array.from((_a = store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
764
+ return Object.assign(cookies, { [name.trim()]: value });
765
+ }, {});
766
+ const cookiesFromDocument = getRequestCookies(request);
767
+ const forwardedCookies = Object.assign(Object.assign({}, cookiesFromDocument), cookiesFromStore);
768
+ // Ensure the persisted (document) cookies are propagated to the request.
769
+ // Propagated the cookies persisted in the Cookuie Store to the request headers.
770
+ // This forwards relevant request cookies based on the request's credentials.
771
+ for (const [name, value] of Object.entries(forwardedCookies)) {
772
+ request.headers.append('cookie', `${name}=${value}`);
773
+ }
774
+ const ownCookies = requestCookiesString
775
+ ? parse_1(requestCookiesString)
776
+ : {};
777
+ request.cookies = Object.assign(Object.assign(Object.assign({}, request.cookies), forwardedCookies), ownCookies);
755
778
  }
756
779
 
757
780
  /**
@@ -1333,16 +1356,32 @@ This is necessary to ensure that the Service Worker is in sync with the library
1333
1356
  If this message still persists after updating, please report an issue: https://github.com/open-draft/msw/issues\
1334
1357
  `);
1335
1358
  }
1336
- yield enableMocking(context, options).catch((err) => {
1337
- throw new Error(`Failed to enable mocking: ${err === null || err === void 0 ? void 0 : err.message}`);
1338
- });
1339
1359
  context.keepAliveInterval = window.setInterval(() => context.workerChannel.send('KEEPALIVE_REQUEST'), 5000);
1340
1360
  // Warn the user when loading the page that lies outside
1341
1361
  // of the worker's scope.
1342
1362
  validateWorkerScope(registration, context.startOptions);
1343
1363
  return registration;
1344
1364
  });
1345
- const workerRegistration = startWorkerInstance();
1365
+ const workerRegistration = startWorkerInstance().then((registration) => __awaiter(this, void 0, void 0, function* () {
1366
+ const pendingInstance = registration.installing || registration.waiting;
1367
+ // Wait until the worker is activated.
1368
+ // Assume the worker is already activated if there's no pending registration
1369
+ // (i.e. when reloading the page after a successful activation).
1370
+ if (pendingInstance) {
1371
+ yield new Promise((resolve) => {
1372
+ pendingInstance.addEventListener('statechange', () => {
1373
+ if (pendingInstance.state === 'activated') {
1374
+ return resolve();
1375
+ }
1376
+ });
1377
+ });
1378
+ }
1379
+ // Print the activation message only after the worker has been activated.
1380
+ yield enableMocking(context, options).catch((error) => {
1381
+ throw new Error(`Failed to enable mocking: ${error === null || error === void 0 ? void 0 : error.message}`);
1382
+ });
1383
+ return registration;
1384
+ }));
1346
1385
  // Defer any network requests until the Service Worker instance is ready.
1347
1386
  // This prevents a race condition between the Service Worker registration
1348
1387
  // and application's runtime requests (i.e. requests on mount).
@@ -1421,6 +1460,7 @@ function parseIsomorphicRequest(request) {
1421
1460
  url: request.url,
1422
1461
  method: request.method,
1423
1462
  body: parseBody(request.body, request.headers),
1463
+ credentials: request.credentials || 'same-origin',
1424
1464
  headers: request.headers,
1425
1465
  cookies: {},
1426
1466
  redirect: 'manual',
@@ -1432,21 +1472,9 @@ function parseIsomorphicRequest(request) {
1432
1472
  integrity: '',
1433
1473
  destination: 'document',
1434
1474
  bodyUsed: false,
1435
- credentials: 'same-origin',
1436
1475
  };
1437
- // Set mocked request cookies from the `cookie` header of the original request.
1438
- // No need to take `credentials` into account, because in Node.js requests are intercepted
1439
- // _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
1440
- // Unlike browser, where interception is on the worker level, _before_ the request happens.
1441
- const requestCookiesString = request.headers.get('cookie');
1442
1476
  // Attach all the cookies from the virtual cookie store.
1443
1477
  setRequestCookies(mockedRequest);
1444
- const requestCookies = requestCookiesString
1445
- ? parse_1(requestCookiesString)
1446
- : {};
1447
- // Merge both direct request cookies and the cookies inherited
1448
- // from other same-origin requests in the cookie store.
1449
- mockedRequest.cookies = Object.assign(Object.assign({}, mockedRequest.cookies), requestCookies);
1450
1478
  return mockedRequest;
1451
1479
  }
1452
1480
 
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (0.38.2).
5
+ * Mock Service Worker (0.39.2).
6
6
  * @see https://github.com/mswjs/msw
7
7
  * - Please do NOT modify this file.
8
8
  * - Please do NOT serve this file on production.