msw 0.39.0 → 0.40.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/umd/index.js CHANGED
@@ -2004,6 +2004,12 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
2004
2004
  console.groupCollapsed(`%c${devUtils.formatMessage(message)}`, 'color:orangered;font-weight:bold;');
2005
2005
  console.log('%cDocumentation: %chttps://mswjs.io/docs', 'font-weight:bold', 'font-weight:normal');
2006
2006
  console.log('Found an issue? https://github.com/mswjs/msw/issues');
2007
+ if (args.workerUrl) {
2008
+ console.log('Worker script URL:', args.workerUrl);
2009
+ }
2010
+ if (args.workerScope) {
2011
+ console.log('Worker scope:', args.workerScope);
2012
+ }
2007
2013
  console.groupEnd();
2008
2014
  }
2009
2015
 
@@ -2011,10 +2017,22 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
2011
2017
  * Signals the worker to enable the interception of requests.
2012
2018
  */
2013
2019
  function enableMocking(context, options) {
2020
+ var _a, _b;
2014
2021
  return __awaiter$3(this, void 0, void 0, function* () {
2015
2022
  context.workerChannel.send('MOCK_ACTIVATE');
2016
- return context.events.once('MOCKING_ENABLED').then(() => {
2017
- printStartMessage({ quiet: options.quiet });
2023
+ yield context.events.once('MOCKING_ENABLED');
2024
+ // Warn the developer on multiple "worker.start()" calls.
2025
+ // While this will not affect the worker in any way,
2026
+ // it likely indicates an issue with the developer's code.
2027
+ if (context.isMockingEnabled) {
2028
+ devUtils.warn(`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.`);
2029
+ return;
2030
+ }
2031
+ context.isMockingEnabled = true;
2032
+ printStartMessage({
2033
+ quiet: options.quiet,
2034
+ workerScope: (_a = context.registration) === null || _a === void 0 ? void 0 : _a.scope,
2035
+ workerUrl: (_b = context.worker) === null || _b === void 0 ? void 0 : _b.scriptURL,
2018
2036
  });
2019
2037
  });
2020
2038
  }
@@ -2044,6 +2062,196 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
2044
2062
  }
2045
2063
  }
2046
2064
 
2065
+ /**
2066
+ * Composes a given list of functions into a new function that
2067
+ * executes from right to left.
2068
+ */
2069
+ function compose(...fns) {
2070
+ return (...args) => {
2071
+ return fns.reduceRight((leftFn, rightFn) => {
2072
+ return leftFn instanceof Promise
2073
+ ? Promise.resolve(leftFn).then(rightFn)
2074
+ : rightFn(leftFn);
2075
+ }, args[0]);
2076
+ };
2077
+ }
2078
+
2079
+ const defaultResponse = {
2080
+ status: 200,
2081
+ statusText: 'OK',
2082
+ body: null,
2083
+ delay: 0,
2084
+ once: false,
2085
+ passthrough: false,
2086
+ };
2087
+ const defaultResponseTransformers = [];
2088
+ function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
2089
+ return (...transformers) => __awaiter$3(this, void 0, void 0, function* () {
2090
+ const initialResponse = Object.assign({}, defaultResponse, {
2091
+ headers: new lib$7.Headers({
2092
+ 'x-powered-by': 'msw',
2093
+ }),
2094
+ }, responseOverrides);
2095
+ const resolvedTransformers = [
2096
+ ...defaultTransformers,
2097
+ ...transformers,
2098
+ ].filter(Boolean);
2099
+ const resolvedResponse = resolvedTransformers.length > 0
2100
+ ? compose(...resolvedTransformers)(initialResponse)
2101
+ : initialResponse;
2102
+ return resolvedResponse;
2103
+ });
2104
+ }
2105
+ const response = Object.assign(createResponseComposition(), {
2106
+ once: createResponseComposition({ once: true }),
2107
+ networkError(message) {
2108
+ throw new NetworkError(message);
2109
+ },
2110
+ });
2111
+
2112
+ const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
2113
+ /**
2114
+ * Return the stack trace frame of a function's invocation.
2115
+ */
2116
+ function getCallFrame(error) {
2117
+ // In <IE11, new Error may return an undefined stack
2118
+ const stack = error.stack;
2119
+ if (!stack) {
2120
+ return;
2121
+ }
2122
+ const frames = stack.split('\n').slice(1);
2123
+ // Get the first frame that doesn't reference the library's internal trace.
2124
+ // Assume that frame is the invocation frame.
2125
+ const declarationFrame = frames.find((frame) => {
2126
+ return !BUILD_FRAME.test(frame);
2127
+ });
2128
+ if (!declarationFrame) {
2129
+ return;
2130
+ }
2131
+ // Extract file reference from the stack frame.
2132
+ const declarationPath = declarationFrame
2133
+ .replace(/\s*at [^()]*\(([^)]+)\)/, '$1')
2134
+ .replace(/^@/, '');
2135
+ return declarationPath;
2136
+ }
2137
+
2138
+ /**
2139
+ * Determines if the given function is an iterator.
2140
+ */
2141
+ function isIterable(fn) {
2142
+ if (!fn) {
2143
+ return false;
2144
+ }
2145
+ return typeof fn[Symbol.iterator] == 'function';
2146
+ }
2147
+
2148
+ const defaultContext = {
2149
+ status,
2150
+ set,
2151
+ delay,
2152
+ fetch: fetch$1,
2153
+ };
2154
+ class RequestHandler {
2155
+ constructor(options) {
2156
+ this.shouldSkip = false;
2157
+ this.ctx = options.ctx || defaultContext;
2158
+ this.resolver = options.resolver;
2159
+ const callFrame = getCallFrame(new Error());
2160
+ this.info = Object.assign(Object.assign({}, options.info), { callFrame });
2161
+ }
2162
+ /**
2163
+ * Parse the captured request to extract additional information from it.
2164
+ * Parsed result is then exposed to other methods of this request handler.
2165
+ */
2166
+ parse(_request, _resolutionContext) {
2167
+ return null;
2168
+ }
2169
+ /**
2170
+ * Test if this handler matches the given request.
2171
+ */
2172
+ test(request, resolutionContext) {
2173
+ return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
2174
+ }
2175
+ /**
2176
+ * Derive the publicly exposed request (`req`) instance of the response resolver
2177
+ * from the captured request and its parsed result.
2178
+ */
2179
+ getPublicRequest(request, _parsedResult) {
2180
+ return request;
2181
+ }
2182
+ markAsSkipped(shouldSkip = true) {
2183
+ this.shouldSkip = shouldSkip;
2184
+ }
2185
+ /**
2186
+ * Execute this request handler and produce a mocked response
2187
+ * using the given resolver function.
2188
+ */
2189
+ run(request, resolutionContext) {
2190
+ return __awaiter$3(this, void 0, void 0, function* () {
2191
+ if (this.shouldSkip) {
2192
+ return null;
2193
+ }
2194
+ const parsedResult = this.parse(request, resolutionContext);
2195
+ const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
2196
+ if (!shouldIntercept) {
2197
+ return null;
2198
+ }
2199
+ const publicRequest = this.getPublicRequest(request, parsedResult);
2200
+ // Create a response extraction wrapper around the resolver
2201
+ // since it can be both an async function and a generator.
2202
+ const executeResolver = this.wrapResolver(this.resolver);
2203
+ const mockedResponse = yield executeResolver(publicRequest, response, this.ctx);
2204
+ return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
2205
+ });
2206
+ }
2207
+ wrapResolver(resolver) {
2208
+ return (req, res, ctx) => __awaiter$3(this, void 0, void 0, function* () {
2209
+ const result = this.resolverGenerator || (yield resolver(req, res, ctx));
2210
+ if (isIterable(result)) {
2211
+ const { value, done } = result[Symbol.iterator]().next();
2212
+ const nextResponse = yield value;
2213
+ // If the generator is done and there is no next value,
2214
+ // return the previous generator's value.
2215
+ if (!nextResponse && done) {
2216
+ return this.resolverGeneratorResult;
2217
+ }
2218
+ if (!this.resolverGenerator) {
2219
+ this.resolverGenerator = result;
2220
+ }
2221
+ this.resolverGeneratorResult = nextResponse;
2222
+ return nextResponse;
2223
+ }
2224
+ return result;
2225
+ });
2226
+ }
2227
+ createExecutionResult(parsedResult, request, response) {
2228
+ return {
2229
+ handler: this,
2230
+ parsedResult: parsedResult || null,
2231
+ request,
2232
+ response: response || null,
2233
+ };
2234
+ }
2235
+ }
2236
+ /**
2237
+ * Bypass this intercepted request.
2238
+ * This will make a call to the actual endpoint requested.
2239
+ */
2240
+ function passthrough() {
2241
+ // Constructing a dummy "101 Continue" mocked response
2242
+ // to keep the return type of the resolver consistent.
2243
+ return {
2244
+ status: 101,
2245
+ statusText: 'Continue',
2246
+ headers: new lib$7.Headers(),
2247
+ body: null,
2248
+ // Setting "passthrough" to true will signal the response pipeline
2249
+ // to perform this intercepted request as-is.
2250
+ passthrough: true,
2251
+ once: false,
2252
+ };
2253
+ }
2254
+
2047
2255
  var lib$2 = {};
2048
2256
 
2049
2257
  var CookieStore = {};
@@ -2463,13 +2671,36 @@ Invalid value has been removed from localStorage to prevent subsequent failed pa
2463
2671
  }
2464
2672
  }
2465
2673
 
2674
+ /**
2675
+ * Sets relevant cookies on the request.
2676
+ * Request cookies are taken from the following sources:
2677
+ * - Immediate (own) request cookies (those in the "Cookie" request header);
2678
+ * - From the `document.cookie` based on the request's `credentials` value;
2679
+ * - From the internal cookie store that persists/hydrates cookies in Node.js
2680
+ */
2466
2681
  function setRequestCookies(request) {
2467
2682
  var _a;
2683
+ // Set mocked request cookies from the `cookie` header of the original request.
2684
+ // No need to take `credentials` into account, because in Node.js requests are intercepted
2685
+ // _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
2686
+ // Unlike browser, where interception is on the worker level, _before_ the request happens.
2687
+ const requestCookiesString = request.headers.get('cookie');
2468
2688
  lib$2.store.hydrate();
2469
- request.cookies = Object.assign(Object.assign({}, getRequestCookies(request)), Array.from((_a = lib$2.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 }), {}));
2470
- request.headers.set('cookie', Object.entries(request.cookies)
2471
- .map(([name, value]) => `${name}=${value}`)
2472
- .join('; '));
2689
+ const cookiesFromStore = Array.from((_a = lib$2.store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
2690
+ return Object.assign(cookies, { [name.trim()]: value });
2691
+ }, {});
2692
+ const cookiesFromDocument = getRequestCookies(request);
2693
+ const forwardedCookies = Object.assign(Object.assign({}, cookiesFromDocument), cookiesFromStore);
2694
+ // Ensure the persisted (document) cookies are propagated to the request.
2695
+ // Propagated the cookies persisted in the Cookuie Store to the request headers.
2696
+ // This forwards relevant request cookies based on the request's credentials.
2697
+ for (const [name, value] of Object.entries(forwardedCookies)) {
2698
+ request.headers.append('cookie', `${name}=${value}`);
2699
+ }
2700
+ const ownCookies = requestCookiesString
2701
+ ? parse_1(requestCookiesString)
2702
+ : {};
2703
+ request.cookies = Object.assign(Object.assign(Object.assign({}, request.cookies), forwardedCookies), ownCookies);
2473
2704
  }
2474
2705
 
2475
2706
  function parseContentHeaders(headersString) {
@@ -2612,6 +2843,7 @@ Invalid value has been removed from localStorage to prevent subsequent failed pa
2612
2843
  body: pruneGetRequestBody(rawRequest),
2613
2844
  bodyUsed: rawRequest.bodyUsed,
2614
2845
  headers: new lib$7.Headers(rawRequest.headers),
2846
+ passthrough,
2615
2847
  };
2616
2848
  // Set document cookies on the request.
2617
2849
  setRequestCookies(request);
@@ -25807,177 +26039,6 @@ spurious results.`);
25807
26039
  };
25808
26040
  }
25809
26041
 
25810
- /**
25811
- * Composes a given list of functions into a new function that
25812
- * executes from right to left.
25813
- */
25814
- function compose(...fns) {
25815
- return (...args) => {
25816
- return fns.reduceRight((leftFn, rightFn) => {
25817
- return leftFn instanceof Promise
25818
- ? Promise.resolve(leftFn).then(rightFn)
25819
- : rightFn(leftFn);
25820
- }, args[0]);
25821
- };
25822
- }
25823
-
25824
- const defaultResponse = {
25825
- status: 200,
25826
- statusText: 'OK',
25827
- body: null,
25828
- delay: 0,
25829
- once: false,
25830
- };
25831
- const defaultResponseTransformers = [];
25832
- function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
25833
- return (...transformers) => __awaiter$3(this, void 0, void 0, function* () {
25834
- const initialResponse = Object.assign({}, defaultResponse, {
25835
- headers: new lib$7.Headers({
25836
- 'x-powered-by': 'msw',
25837
- }),
25838
- }, responseOverrides);
25839
- const resolvedTransformers = [
25840
- ...defaultTransformers,
25841
- ...transformers,
25842
- ].filter(Boolean);
25843
- const resolvedResponse = resolvedTransformers.length > 0
25844
- ? compose(...resolvedTransformers)(initialResponse)
25845
- : initialResponse;
25846
- return resolvedResponse;
25847
- });
25848
- }
25849
- const response = Object.assign(createResponseComposition(), {
25850
- once: createResponseComposition({ once: true }),
25851
- networkError(message) {
25852
- throw new NetworkError(message);
25853
- },
25854
- });
25855
-
25856
- const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
25857
- /**
25858
- * Return the stack trace frame of a function's invocation.
25859
- */
25860
- function getCallFrame(error) {
25861
- // In <IE11, new Error may return an undefined stack
25862
- const stack = error.stack;
25863
- if (!stack) {
25864
- return;
25865
- }
25866
- const frames = stack.split('\n').slice(1);
25867
- // Get the first frame that doesn't reference the library's internal trace.
25868
- // Assume that frame is the invocation frame.
25869
- const declarationFrame = frames.find((frame) => {
25870
- return !BUILD_FRAME.test(frame);
25871
- });
25872
- if (!declarationFrame) {
25873
- return;
25874
- }
25875
- // Extract file reference from the stack frame.
25876
- const declarationPath = declarationFrame
25877
- .replace(/\s*at [^()]*\(([^)]+)\)/, '$1')
25878
- .replace(/^@/, '');
25879
- return declarationPath;
25880
- }
25881
-
25882
- /**
25883
- * Determines if the given function is an iterator.
25884
- */
25885
- function isIterable(fn) {
25886
- if (!fn) {
25887
- return false;
25888
- }
25889
- return typeof fn[Symbol.iterator] == 'function';
25890
- }
25891
-
25892
- const defaultContext = {
25893
- status,
25894
- set,
25895
- delay,
25896
- fetch: fetch$1,
25897
- };
25898
- class RequestHandler {
25899
- constructor(options) {
25900
- this.shouldSkip = false;
25901
- this.ctx = options.ctx || defaultContext;
25902
- this.resolver = options.resolver;
25903
- const callFrame = getCallFrame(new Error());
25904
- this.info = Object.assign(Object.assign({}, options.info), { callFrame });
25905
- }
25906
- /**
25907
- * Parse the captured request to extract additional information from it.
25908
- * Parsed result is then exposed to other methods of this request handler.
25909
- */
25910
- parse(_request, _resolutionContext) {
25911
- return null;
25912
- }
25913
- /**
25914
- * Test if this handler matches the given request.
25915
- */
25916
- test(request, resolutionContext) {
25917
- return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
25918
- }
25919
- /**
25920
- * Derive the publicly exposed request (`req`) instance of the response resolver
25921
- * from the captured request and its parsed result.
25922
- */
25923
- getPublicRequest(request, _parsedResult) {
25924
- return request;
25925
- }
25926
- markAsSkipped(shouldSkip = true) {
25927
- this.shouldSkip = shouldSkip;
25928
- }
25929
- /**
25930
- * Execute this request handler and produce a mocked response
25931
- * using the given resolver function.
25932
- */
25933
- run(request, resolutionContext) {
25934
- return __awaiter$3(this, void 0, void 0, function* () {
25935
- if (this.shouldSkip) {
25936
- return null;
25937
- }
25938
- const parsedResult = this.parse(request, resolutionContext);
25939
- const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
25940
- if (!shouldIntercept) {
25941
- return null;
25942
- }
25943
- const publicRequest = this.getPublicRequest(request, parsedResult);
25944
- // Create a response extraction wrapper around the resolver
25945
- // since it can be both an async function and a generator.
25946
- const executeResolver = this.wrapResolver(this.resolver);
25947
- const mockedResponse = yield executeResolver(publicRequest, response, this.ctx);
25948
- return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
25949
- });
25950
- }
25951
- wrapResolver(resolver) {
25952
- return (req, res, ctx) => __awaiter$3(this, void 0, void 0, function* () {
25953
- const result = this.resolverGenerator || (yield resolver(req, res, ctx));
25954
- if (isIterable(result)) {
25955
- const { value, done } = result[Symbol.iterator]().next();
25956
- const nextResponse = yield value;
25957
- // If the generator is done and there is no next value,
25958
- // return the previous generator's value.
25959
- if (!nextResponse && done) {
25960
- return this.resolverGeneratorResult;
25961
- }
25962
- if (!this.resolverGenerator) {
25963
- this.resolverGenerator = result;
25964
- }
25965
- this.resolverGeneratorResult = nextResponse;
25966
- return nextResponse;
25967
- }
25968
- return result;
25969
- });
25970
- }
25971
- createExecutionResult(parsedResult, request, response) {
25972
- return {
25973
- handler: this,
25974
- parsedResult: parsedResult || null,
25975
- request,
25976
- response: response || null,
25977
- };
25978
- }
25979
- }
25980
-
25981
26042
  exports.RESTMethods = void 0;
25982
26043
  (function (RESTMethods) {
25983
26044
  RESTMethods["HEAD"] = "HEAD";
@@ -25988,17 +26049,11 @@ spurious results.`);
25988
26049
  RESTMethods["OPTIONS"] = "OPTIONS";
25989
26050
  RESTMethods["DELETE"] = "DELETE";
25990
26051
  })(exports.RESTMethods || (exports.RESTMethods = {}));
25991
- const restContext = {
25992
- set,
25993
- status,
25994
- cookie,
26052
+ const restContext = Object.assign(Object.assign({}, defaultContext), { cookie,
25995
26053
  body,
25996
26054
  text,
25997
26055
  json,
25998
- xml,
25999
- delay,
26000
- fetch: fetch$1,
26001
- };
26056
+ xml });
26002
26057
  /**
26003
26058
  * Request handler for REST API requests.
26004
26059
  * Provides request matching based on method and URL.
@@ -26069,16 +26124,10 @@ spurious results.`);
26069
26124
  }
26070
26125
  }
26071
26126
 
26072
- const graphqlContext = {
26073
- set,
26074
- status,
26075
- delay,
26076
- fetch: fetch$1,
26077
- data,
26127
+ const graphqlContext = Object.assign(Object.assign({}, defaultContext), { data,
26078
26128
  extensions,
26079
26129
  errors,
26080
- cookie,
26081
- };
26130
+ cookie });
26082
26131
  function isDocumentNode(value) {
26083
26132
  if (value == null) {
26084
26133
  return false;
@@ -26298,17 +26347,24 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
26298
26347
  }
26299
26348
 
26300
26349
  function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
26301
- var _a, _b, _c;
26350
+ var _a, _b, _c, _d;
26302
26351
  return __awaiter$3(this, void 0, void 0, function* () {
26303
26352
  emitter.emit('request:start', request);
26304
26353
  // Perform bypassed requests (i.e. issued via "ctx.fetch") as-is.
26305
26354
  if (request.headers.get('x-msw-bypass') === 'true') {
26306
26355
  emitter.emit('request:end', request);
26307
- (_a = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _a === void 0 ? void 0 : _a.call(handleRequestOptions, request);
26356
+ (_a = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _a === void 0 ? void 0 : _a.call(handleRequestOptions, request);
26308
26357
  return;
26309
26358
  }
26310
26359
  // Resolve a mocked response from the list of request handlers.
26311
- const lookupResult = yield getResponse(request, handlers, handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.resolutionContext);
26360
+ const [lookupError, lookupResult] = yield until(() => {
26361
+ return getResponse(request, handlers, handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.resolutionContext);
26362
+ });
26363
+ if (lookupError) {
26364
+ // Allow developers to react to unhandled exceptions in request handlers.
26365
+ emitter.emit('unhandledException', lookupError, request);
26366
+ throw lookupError;
26367
+ }
26312
26368
  const { handler, response } = lookupResult;
26313
26369
  // When there's no handler for the request, consider it unhandled.
26314
26370
  // Allow the developer to react to such cases.
@@ -26316,7 +26372,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
26316
26372
  onUnhandledRequest(request, handlers, options.onUnhandledRequest);
26317
26373
  emitter.emit('request:unhandled', request);
26318
26374
  emitter.emit('request:end', request);
26319
- (_b = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _b === void 0 ? void 0 : _b.call(handleRequestOptions, request);
26375
+ (_b = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _b === void 0 ? void 0 : _b.call(handleRequestOptions, request);
26320
26376
  return;
26321
26377
  }
26322
26378
  // When the handled request returned no mocked response, warn the developer,
@@ -26329,7 +26385,14 @@ Expected response resolver to return a mocked response Object, but got %s. The o
26329
26385
  %s\
26330
26386
  `, response, handler.info.header, handler.info.callFrame);
26331
26387
  emitter.emit('request:end', request);
26332
- (_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _c === void 0 ? void 0 : _c.call(handleRequestOptions, request);
26388
+ (_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _c === void 0 ? void 0 : _c.call(handleRequestOptions, request);
26389
+ return;
26390
+ }
26391
+ // When the developer explicitly returned "req.passthrough()" do not warn them.
26392
+ // Perform the request as-is.
26393
+ if (response.passthrough) {
26394
+ emitter.emit('request:end', request);
26395
+ (_d = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _d === void 0 ? void 0 : _d.call(handleRequestOptions, request);
26333
26396
  return;
26334
26397
  }
26335
26398
  // Store all the received response cookies in the virtual cookie store.
@@ -26360,7 +26423,7 @@ Expected response resolver to return a mocked response Object, but got %s. The o
26360
26423
  transformResponse(response) {
26361
26424
  return Object.assign(Object.assign({}, response), { headers: response.headers.all() });
26362
26425
  },
26363
- onBypassResponse() {
26426
+ onPassthroughResponse() {
26364
26427
  return channel.send({
26365
26428
  type: 'MOCK_NOT_FOUND',
26366
26429
  });
@@ -26582,12 +26645,19 @@ If this message still persists after updating, please report an issue: https://g
26582
26645
  const createStop = (context) => {
26583
26646
  return function stop() {
26584
26647
  var _a;
26648
+ // Warn developers calling "worker.stop()" more times than necessary.
26649
+ // This likely indicates a mistake in their code.
26650
+ if (!context.isMockingEnabled) {
26651
+ devUtils.warn('Found a redundant "worker.stop()" call. Note that stopping the worker while mocking already stopped has no effect. Consider removing this "worker.stop()" call.');
26652
+ return;
26653
+ }
26585
26654
  /**
26586
26655
  * Signal the Service Worker to disable mocking for this client.
26587
26656
  * Use this an an explicit way to stop the mocking, while preserving
26588
26657
  * the worker-client relation. Does not affect the worker's lifecycle.
26589
26658
  */
26590
26659
  context.workerChannel.send('MOCK_DEACTIVATE');
26660
+ context.isMockingEnabled = false;
26591
26661
  window.clearInterval(context.keepAliveInterval);
26592
26662
  printStopMessage({ quiet: (_a = context.startOptions) === null || _a === void 0 ? void 0 : _a.quiet });
26593
26663
  };
@@ -31233,6 +31303,7 @@ If this message still persists after updating, please report an issue: https://g
31233
31303
  url: request.url,
31234
31304
  method: request.method,
31235
31305
  body: parseBody(request.body, request.headers),
31306
+ credentials: request.credentials || 'same-origin',
31236
31307
  headers: request.headers,
31237
31308
  cookies: {},
31238
31309
  redirect: 'manual',
@@ -31244,21 +31315,10 @@ If this message still persists after updating, please report an issue: https://g
31244
31315
  integrity: '',
31245
31316
  destination: 'document',
31246
31317
  bodyUsed: false,
31247
- credentials: 'same-origin',
31318
+ passthrough,
31248
31319
  };
31249
- // Set mocked request cookies from the `cookie` header of the original request.
31250
- // No need to take `credentials` into account, because in Node.js requests are intercepted
31251
- // _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
31252
- // Unlike browser, where interception is on the worker level, _before_ the request happens.
31253
- const requestCookiesString = request.headers.get('cookie');
31254
31320
  // Attach all the cookies from the virtual cookie store.
31255
31321
  setRequestCookies(mockedRequest);
31256
- const requestCookies = requestCookiesString
31257
- ? parse_1(requestCookiesString)
31258
- : {};
31259
- // Merge both direct request cookies and the cookies inherited
31260
- // from other same-origin requests in the cookie store.
31261
- mockedRequest.cookies = Object.assign(Object.assign({}, mockedRequest.cookies), requestCookies);
31262
31322
  return mockedRequest;
31263
31323
  }
31264
31324
 
@@ -31350,6 +31410,9 @@ If this message still persists after updating, please report an issue: https://g
31350
31410
  const publicEmitter = new lib$5.StrictEventEmitter();
31351
31411
  pipeEvents(emitter, publicEmitter);
31352
31412
  const context = {
31413
+ // Mocking is not considered enabled until the worker
31414
+ // signals back the successful activation event.
31415
+ isMockingEnabled: false,
31353
31416
  startOptions: undefined,
31354
31417
  worker: null,
31355
31418
  registration: null,
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (0.39.0).
5
+ * Mock Service Worker (0.40.0).
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.