msw 0.20.4 → 0.20.5

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
@@ -60,11 +60,20 @@ exports.until = until.until;
60
60
  });
61
61
 
62
62
  /**
63
- * Attempts to resolve a Service Worker instance from any of its states:
64
- * active, installing, or waiting.
63
+ * Attempts to resolve a Service Worker instance from a given registration,
64
+ * regardless of its state (active, installing, waiting).
65
65
  */
66
- const getWorkerByRegistration = (registration) => {
67
- return registration.active || registration.installing || registration.waiting;
66
+ const getWorkerByRegistration = (registration, absoluteWorkerUrl) => {
67
+ const allStates = [
68
+ registration.active,
69
+ registration.installing,
70
+ registration.waiting,
71
+ ];
72
+ const existingStates = allStates.filter(Boolean);
73
+ const mockWorker = existingStates.find((worker) => {
74
+ return worker.scriptURL === absoluteWorkerUrl;
75
+ });
76
+ return mockWorker || null;
68
77
  };
69
78
 
70
79
  /**
@@ -85,9 +94,7 @@ const getWorkerInstance = (url, options) => __awaiter(void 0, void 0, void 0, fu
85
94
  const [, mockRegistrations] = yield lib.until(() => __awaiter(void 0, void 0, void 0, function* () {
86
95
  const registrations = yield navigator.serviceWorker.getRegistrations();
87
96
  return registrations.filter((registration) => {
88
- const worker = getWorkerByRegistration(registration);
89
- // Filter out other workers that can be associated with this page
90
- return (worker === null || worker === void 0 ? void 0 : worker.scriptURL) === absoluteWorkerUrl;
97
+ return getWorkerByRegistration(registration, absoluteWorkerUrl);
91
98
  });
92
99
  }));
93
100
  if (!navigator.serviceWorker.controller && mockRegistrations.length > 0) {
@@ -99,21 +106,24 @@ const getWorkerInstance = (url, options) => __awaiter(void 0, void 0, void 0, fu
99
106
  // at this point we are sure it's hard reload that falls into this clause.
100
107
  location.reload();
101
108
  }
102
- const [, existingRegistration] = yield lib.until(() => {
103
- return navigator.serviceWorker.getRegistration(url);
104
- });
109
+ const [existingRegistration] = mockRegistrations;
105
110
  if (existingRegistration) {
106
111
  // Update existing service worker to ensure it's up-to-date
107
112
  return existingRegistration.update().then(() => {
108
113
  return [
109
- getWorkerByRegistration(existingRegistration),
114
+ getWorkerByRegistration(existingRegistration, absoluteWorkerUrl),
110
115
  existingRegistration,
111
116
  ];
112
117
  });
113
118
  }
114
119
  const [error, instance] = yield lib.until(() => __awaiter(void 0, void 0, void 0, function* () {
115
120
  const registration = yield navigator.serviceWorker.register(url, options);
116
- return [getWorkerByRegistration(registration), registration];
121
+ return [
122
+ // Compare existing worker registration by its worker URL,
123
+ // to prevent irrelevant workers to resolve here (such as Codesandbox worker).
124
+ getWorkerByRegistration(registration, absoluteWorkerUrl),
125
+ registration,
126
+ ];
117
127
  }));
118
128
  if (error) {
119
129
  const isWorkerMissing = error.message.includes('(404)');
@@ -3,6 +3,6 @@ export { setupWorker } from './setupWorker/setupWorker';
3
3
  export { MockedResponse, ResponseTransformer, response } from './response';
4
4
  export { context };
5
5
  export { MockedRequest, RequestHandler, RequestParams, RequestQuery, ResponseResolver, ResponseResolverReturnType, AsyncResponseResolverReturnType, defaultContext, } from './handlers/requestHandler';
6
- export { rest, restContext, RESTMethods } from './rest';
6
+ export { rest, restContext, RESTMethods, ParsedRestRequest } from './rest';
7
7
  export { graphql, graphqlContext, GraphQLMockedRequest, GraphQLMockedContext, GraphQLRequestPayload, GraphQLResponseResolver, } from './graphql';
8
8
  export { matchRequestUrl } from './utils/matching/matchRequest';
@@ -21,7 +21,7 @@ export declare const restContext: {
21
21
  delay: (durationMs?: number | undefined) => import("./response").ResponseTransformer;
22
22
  fetch: <ResponseType_1 = any>(input: string | MockedRequest, requestInit?: RequestInit) => Promise<ResponseType_1>;
23
23
  };
24
- interface ParsedRestRequest {
24
+ export interface ParsedRestRequest {
25
25
  match: ReturnType<typeof matchRequestUrl>;
26
26
  }
27
27
  export declare const rest: {
@@ -32,4 +32,3 @@ export declare const rest: {
32
32
  patch: (mask: Mask, resolver: ResponseResolver<MockedRequest, typeof restContext>) => RequestHandler<MockedRequest, typeof restContext, ParsedRestRequest>;
33
33
  options: (mask: Mask, resolver: ResponseResolver<MockedRequest, typeof restContext>) => RequestHandler<MockedRequest, typeof restContext, ParsedRestRequest>;
34
34
  };
35
- export {};
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Attempts to resolve a Service Worker instance from any of its states:
3
- * active, installing, or waiting.
2
+ * Attempts to resolve a Service Worker instance from a given registration,
3
+ * regardless of its state (active, installing, waiting).
4
4
  */
5
- export declare const getWorkerByRegistration: (registration: ServiceWorkerRegistration) => ServiceWorker | null;
5
+ export declare const getWorkerByRegistration: (registration: ServiceWorkerRegistration, absoluteWorkerUrl: string) => ServiceWorker | null;
package/lib/umd/index.js CHANGED
@@ -776,11 +776,20 @@
776
776
  });
777
777
 
778
778
  /**
779
- * Attempts to resolve a Service Worker instance from any of its states:
780
- * active, installing, or waiting.
779
+ * Attempts to resolve a Service Worker instance from a given registration,
780
+ * regardless of its state (active, installing, waiting).
781
781
  */
782
- const getWorkerByRegistration = (registration) => {
783
- return registration.active || registration.installing || registration.waiting;
782
+ const getWorkerByRegistration = (registration, absoluteWorkerUrl) => {
783
+ const allStates = [
784
+ registration.active,
785
+ registration.installing,
786
+ registration.waiting,
787
+ ];
788
+ const existingStates = allStates.filter(Boolean);
789
+ const mockWorker = existingStates.find((worker) => {
790
+ return worker.scriptURL === absoluteWorkerUrl;
791
+ });
792
+ return mockWorker || null;
784
793
  };
785
794
 
786
795
  /**
@@ -801,9 +810,7 @@
801
810
  const [, mockRegistrations] = yield lib$1.until(() => __awaiter(void 0, void 0, void 0, function* () {
802
811
  const registrations = yield navigator.serviceWorker.getRegistrations();
803
812
  return registrations.filter((registration) => {
804
- const worker = getWorkerByRegistration(registration);
805
- // Filter out other workers that can be associated with this page
806
- return (worker === null || worker === void 0 ? void 0 : worker.scriptURL) === absoluteWorkerUrl;
813
+ return getWorkerByRegistration(registration, absoluteWorkerUrl);
807
814
  });
808
815
  }));
809
816
  if (!navigator.serviceWorker.controller && mockRegistrations.length > 0) {
@@ -815,21 +822,24 @@
815
822
  // at this point we are sure it's hard reload that falls into this clause.
816
823
  location.reload();
817
824
  }
818
- const [, existingRegistration] = yield lib$1.until(() => {
819
- return navigator.serviceWorker.getRegistration(url);
820
- });
825
+ const [existingRegistration] = mockRegistrations;
821
826
  if (existingRegistration) {
822
827
  // Update existing service worker to ensure it's up-to-date
823
828
  return existingRegistration.update().then(() => {
824
829
  return [
825
- getWorkerByRegistration(existingRegistration),
830
+ getWorkerByRegistration(existingRegistration, absoluteWorkerUrl),
826
831
  existingRegistration,
827
832
  ];
828
833
  });
829
834
  }
830
835
  const [error, instance] = yield lib$1.until(() => __awaiter(void 0, void 0, void 0, function* () {
831
836
  const registration = yield navigator.serviceWorker.register(url, options);
832
- return [getWorkerByRegistration(registration), registration];
837
+ return [
838
+ // Compare existing worker registration by its worker URL,
839
+ // to prevent irrelevant workers to resolve here (such as Codesandbox worker).
840
+ getWorkerByRegistration(registration, absoluteWorkerUrl),
841
+ registration,
842
+ ];
833
843
  }));
834
844
  if (error) {
835
845
  const isWorkerMissing = error.message.includes('(404)');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "0.20.4",
3
+ "version": "0.20.5",
4
4
  "description": "Client-side API mocking using Service Workers.",
5
5
  "main": "lib/umd/index.js",
6
6
  "module": "lib/esm/index.js",
@@ -69,7 +69,7 @@
69
69
  "headers-utils": "^1.2.0",
70
70
  "node-fetch": "^2.6.0",
71
71
  "node-match-path": "^0.4.4",
72
- "node-request-interceptor": "^0.3.4",
72
+ "node-request-interceptor": "^0.3.5",
73
73
  "statuses": "^2.0.0",
74
74
  "yargs": "^15.4.1"
75
75
  },