msw 3.0.3 → 3.0.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/iife/index.js CHANGED
@@ -673,7 +673,7 @@ var MockServiceWorker = (function(exports) {
673
673
  }
674
674
  };
675
675
  //#endregion
676
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/create-request-id-YobUnJNO.js
676
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/create-request-id-YobUnJNO.js
677
677
  var __create = Object.create;
678
678
  var __defProp = Object.defineProperty;
679
679
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -1379,7 +1379,7 @@ var MockServiceWorker = (function(exports) {
1379
1379
  return Math.random().toString(16).slice(2);
1380
1380
  }
1381
1381
  //#endregion
1382
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/fetch-utils-C6P6gLLo.js
1382
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/fetch-utils-C6P6gLLo.js
1383
1383
  var InterceptorError = class InterceptorError extends Error {
1384
1384
  constructor(message) {
1385
1385
  super(message);
@@ -1786,7 +1786,7 @@ var MockServiceWorker = (function(exports) {
1786
1786
  }
1787
1787
  };
1788
1788
  //#endregion
1789
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/buffer-utils-DJj7YzLG.js
1789
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/buffer-utils-DJj7YzLG.js
1790
1790
  const encoder = new TextEncoder();
1791
1791
  function encodeBuffer(text) {
1792
1792
  return encoder.encode(text);
@@ -1803,7 +1803,7 @@ var MockServiceWorker = (function(exports) {
1803
1803
  return array.buffer.slice(array.byteOffset, array.byteOffset + array.byteLength);
1804
1804
  }
1805
1805
  //#endregion
1806
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/resolve-web-socket-url-CSvNPLGi.js
1806
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/resolve-web-socket-url-CSvNPLGi.js
1807
1807
  /**
1808
1808
  * Resolve potentially relative WebSocket URLs the same way
1809
1809
  * the browser does (replace the protocol, use the origin, etc).
@@ -1825,7 +1825,7 @@ var MockServiceWorker = (function(exports) {
1825
1825
  return url.href;
1826
1826
  }
1827
1827
  //#endregion
1828
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/index.js
1828
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/index.js
1829
1829
  function addEventBridge(interceptor, type, listener) {
1830
1830
  interceptor.on(type, listener);
1831
1831
  }
@@ -27473,7 +27473,7 @@ var MockServiceWorker = (function(exports) {
27473
27473
  options: createHttpHandler("OPTIONS")
27474
27474
  };
27475
27475
  //#endregion
27476
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/has-configurable-global-Cew-dYqk.js
27476
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/has-configurable-global-Cew-dYqk.js
27477
27477
  var PatchesRegistry = class {
27478
27478
  #replacements = /* @__PURE__ */ new Map();
27479
27479
  applyPatch(owner, key, getNextValue) {
@@ -27561,7 +27561,7 @@ var MockServiceWorker = (function(exports) {
27561
27561
  return true;
27562
27562
  }
27563
27563
  //#endregion
27564
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/interceptors/WebSocket/index.js
27564
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/interceptors/WebSocket/index.js
27565
27565
  var WebSocketConnectionEvent$2 = class extends TypedEvent {
27566
27566
  constructor(data) {
27567
27567
  super("connection", { data });
@@ -29981,7 +29981,7 @@ accept: "msw/passthrough" },
29981
29981
  if (/(fonts\.googleapis\.com)/.test(url.hostname)) return true;
29982
29982
  if (/node_modules/.test(url.pathname)) return true;
29983
29983
  if (url.pathname.includes("@vite")) return true;
29984
- return /\.(s?css|less|m?jsx?|m?tsx?|html|ttf|otf|woff|woff2|eot|gif|jpe?g|png|avif|webp|svg|mp4|webm|ogg|mov|mp3|wav|ogg|flac|aac|pdf|txt|csv|json|xml|md|zip|tar|gz|rar|7z)$/i.test(url.pathname);
29984
+ return /\.(s?css|less|m?jsx?|m?tsx?|html|ttf|otf|woff|woff2|eot|gif|jpe?g|png|avif|webp|ico|svg|mp4|webm|ogg|mov|mp3|wav|ogg|flac|aac|pdf|txt|csv|json|xml|md|zip|tar|gz|rar|7z)$/i.test(url.pathname);
29985
29985
  }
29986
29986
  //#endregion
29987
29987
  //#region src/core/experimental/sources/network-source.ts
@@ -30386,6 +30386,17 @@ accept: "msw/passthrough" },
30386
30386
  this.request = data.request;
30387
30387
  }
30388
30388
  };
30389
+ var RequestMatchEvent = class extends TypedEvent {
30390
+ requestId;
30391
+ request;
30392
+ handler;
30393
+ constructor(type, data) {
30394
+ super(...[type, {}]);
30395
+ this.requestId = data.requestId;
30396
+ this.request = data.request;
30397
+ this.handler = data.handler;
30398
+ }
30399
+ };
30389
30400
  var ResponseEvent = class extends TypedEvent {
30390
30401
  requestId;
30391
30402
  request;
@@ -30495,9 +30506,10 @@ accept: "msw/passthrough" },
30495
30506
  return false;
30496
30507
  }
30497
30508
  const { response, handler, parsedResult } = lookupResult;
30498
- this.events.emit(new RequestEvent("request:match", {
30509
+ this.events.emit(new RequestMatchEvent("request:match", {
30499
30510
  requestId,
30500
- request
30511
+ request,
30512
+ handler
30501
30513
  }));
30502
30514
  if (response == null) {
30503
30515
  this.events.emit(new RequestEvent("request:end", {
@@ -31190,7 +31202,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
31190
31202
  this.#channel.postMessage("INTEGRITY_CHECK_REQUEST");
31191
31203
  this.#channel.once("INTEGRITY_CHECK_RESPONSE", (event) => {
31192
31204
  const { checksum, packageVersion } = event.data;
31193
- if (checksum !== "5cd5cf8b54c3a90f82960cedcd637772") devUtils.warn(`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.
31205
+ if (checksum !== "0bd5af64c8dc112df5e0c5e9c8a2b15a") devUtils.warn(`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.
31194
31206
 
31195
31207
  It's recommended you update your worker script by running this command:
31196
31208
 
@@ -31279,7 +31291,7 @@ You can also automate this process and make the worker script update automatical
31279
31291
  }
31280
31292
  }
31281
31293
  //#endregion
31282
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/handle-request-y7ajyve5.js
31294
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/handle-request-0FGy-JT2.js
31283
31295
  var HttpRequestEvent = class extends TypedEvent {
31284
31296
  constructor(data) {
31285
31297
  super(...["request", {}]);
@@ -31392,7 +31404,12 @@ You can also automate this process and make the worker script update automatical
31392
31404
  });
31393
31405
  options.request.signal?.removeEventListener("abort", onAbort);
31394
31406
  if (isRequestAborted) {
31395
- await options.controller.errorWith(requestAbortReason);
31407
+ /**
31408
+ * @note The abort can land after a listener has already handled the
31409
+ * request (e.g. responded to it) but before that is observed here.
31410
+ * The controller state is final by then, so the abort is a no-op.
31411
+ */
31412
+ if (options.controller.readyState === RequestController.PENDING) options.controller.errorWith(requestAbortReason);
31396
31413
  return;
31397
31414
  }
31398
31415
  if (resultError) {
@@ -31435,7 +31452,7 @@ You can also automate this process and make the worker script update automatical
31435
31452
  return options.controller.handled;
31436
31453
  }
31437
31454
  //#endregion
31438
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/web-CCLERSZF.js
31455
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/web-Br-3dIB5.js
31439
31456
  function createNetworkError(cause) {
31440
31457
  return Object.assign(/* @__PURE__ */ new TypeError("Failed to fetch"), { cause });
31441
31458
  }
@@ -31748,7 +31765,7 @@ You can also automate this process and make the worker script update automatical
31748
31765
  }
31749
31766
  };
31750
31767
  //#endregion
31751
- //#region node_modules/.pnpm/@mswjs+interceptors@0.45.6_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/web-BRBQ3YPp.js
31768
+ //#region node_modules/.pnpm/@mswjs+interceptors@0.45.11_supports-color@8.1.1/node_modules/@mswjs/interceptors/lib/browser/web-CL5gOWPS.js
31752
31769
  /**
31753
31770
  * Concatenate two `Uint8Array` buffers.
31754
31771
  */
@@ -31959,6 +31976,7 @@ You can also automate this process and make the worker script update automatical
31959
31976
  this.requestId = createRequestId();
31960
31977
  this.requestHeaders = new Headers();
31961
31978
  this.responseBuffer = /* @__PURE__ */ new Uint8Array();
31979
+ this.requestAbortController = new AbortController();
31962
31980
  this.request = createProxy(initialRequest, { methodCall: ([methodName, args], invoke) => {
31963
31981
  switch (methodName) {
31964
31982
  case "open": {
@@ -32012,7 +32030,13 @@ You can also automate this process and make the worker script update automatical
32012
32030
  }
32013
32031
  });
32014
32032
  const requestBody = typeof body === "string" ? encodeBuffer(body) : body;
32015
- const fetchRequest = this.toFetchApiRequest(requestBody);
32033
+ /**
32034
+ * @note Create a new abort controller per request so that
32035
+ * calling `.abort()` aborts the pending request handling
32036
+ * and the mocked response delivery of this request only.
32037
+ */
32038
+ this.requestAbortController = new AbortController();
32039
+ const fetchRequest = this.toFetchApiRequest(requestBody, this.requestAbortController.signal);
32016
32040
  this[kFetchRequest] = fetchRequest.clone();
32017
32041
  /**
32018
32042
  * @note Start request handling on the next tick so that the user
@@ -32031,6 +32055,10 @@ You can also automate this process and make the worker script update automatical
32031
32055
  });
32032
32056
  break;
32033
32057
  }
32058
+ case "abort":
32059
+ this.logger.verbose("abort");
32060
+ this.requestAbortController.abort();
32061
+ return invoke();
32034
32062
  default: return invoke();
32035
32063
  }
32036
32064
  } });
@@ -32042,7 +32070,7 @@ You can also automate this process and make the worker script update automatical
32042
32070
  switch (propertyName) {
32043
32071
  case "onloadstart":
32044
32072
  case "onprogress":
32045
- case "onaboart":
32073
+ case "onabort":
32046
32074
  case "onerror":
32047
32075
  case "onload":
32048
32076
  case "ontimeout":
@@ -32155,39 +32183,27 @@ You can also automate this process and make the worker script update automatical
32155
32183
  } else processRequestEndOfBody();
32156
32184
  }
32157
32185
  let timedOut = false;
32158
- const responseReadController = new AbortController();
32159
- const requestErrorSteps = (event) => {
32160
- this.setReadyState(this.request.DONE);
32161
- if (!uploadComplete) {
32162
- this.trigger(event, this.request.upload, {
32163
- loaded: 0,
32164
- total: 0
32165
- });
32166
- this.trigger("loadend", this.request.upload, {
32167
- loaded: 0,
32168
- total: 0
32169
- });
32170
- }
32171
- this.trigger(event, this.request, {
32172
- loaded: 0,
32173
- total: 0
32174
- });
32175
- this.trigger("loadend", this.request, {
32176
- loaded: 0,
32177
- total: 0
32178
- });
32179
- };
32186
+ const abortSignal = this.requestAbortController.signal;
32180
32187
  const processResponse = async (response) => {
32181
32188
  const handleErrors = () => {
32182
- if (timedOut) requestErrorSteps("timeout");
32183
- else if (responseReadController.signal.aborted) requestErrorSteps("abort");
32184
- else if (isResponseError(response)) requestErrorSteps("error");
32189
+ if (timedOut) {
32190
+ this.requestErrorSteps("timeout", uploadComplete);
32191
+ return true;
32192
+ }
32193
+ if (abortSignal.aborted) {
32194
+ this.requestErrorSteps("abort", uploadComplete);
32195
+ return true;
32196
+ }
32197
+ if (isResponseError(response)) {
32198
+ this.requestErrorSteps("error", uploadComplete);
32199
+ return true;
32200
+ }
32201
+ return false;
32185
32202
  };
32186
- handleErrors();
32203
+ if (handleErrors()) return;
32187
32204
  define(this.request, "status", response.status);
32188
32205
  define(this.request, "statusText", response.statusText);
32189
32206
  if (!this.request.responseURL) define(this.request, "responseURL", response.url);
32190
- if (isResponseError(response)) return;
32191
32207
  /**
32192
32208
  * @note The response body length is derived ONLY from the "content-length" header.
32193
32209
  * If that response header is not set, the "total" in all progress events must be 0.
@@ -32210,11 +32226,10 @@ You can also automate this process and make the worker script update automatical
32210
32226
  });
32211
32227
  };
32212
32228
  const processResponseBodyError = () => {
32213
- requestErrorSteps("error");
32229
+ this.requestErrorSteps("error", uploadComplete);
32214
32230
  };
32215
32231
  const processResponseEndOfBody = async () => {
32216
- handleErrors();
32217
- if (isResponseError(response)) return;
32232
+ if (handleErrors()) return;
32218
32233
  let transmitted = receivedBytes;
32219
32234
  this.setReadyState(this.request.DONE);
32220
32235
  this.trigger("load", this.request, {
@@ -32230,7 +32245,10 @@ You can also automate this process and make the worker script update automatical
32230
32245
  else {
32231
32246
  const reader = response.body.getReader();
32232
32247
  while (true) {
32233
- if (responseReadController.signal.aborted) break;
32248
+ if (abortSignal.aborted) {
32249
+ processResponseEndOfBody();
32250
+ return;
32251
+ }
32234
32252
  try {
32235
32253
  const { value, done } = await reader.read();
32236
32254
  if (done) {
@@ -32253,7 +32271,7 @@ You can also automate this process and make the worker script update automatical
32253
32271
  if (this.request.timeout) setTimeout(() => {
32254
32272
  if (this.request.readyState !== this.request.DONE) {
32255
32273
  timedOut = true;
32256
- responseReadController.abort();
32274
+ this.requestAbortController.abort();
32257
32275
  }
32258
32276
  }, this.request.timeout);
32259
32277
  this.request.getResponseHeader = new Proxy(this.request.getResponseHeader, { apply: (_, __, args) => {
@@ -32361,12 +32379,48 @@ You can also automate this process and make the worker script update automatical
32361
32379
  * This prevents the controller from trying to perform this request as-is.
32362
32380
  */
32363
32381
  this[kIsRequestHandled] = true;
32382
+ if (this.requestAbortController.signal.aborted) {
32383
+ this.logger.verbose("aborting the request");
32384
+ this.requestErrorSteps("abort", false);
32385
+ return;
32386
+ }
32364
32387
  this.logger.verbose("responding with an error");
32365
32388
  this.setReadyState(this.request.DONE);
32366
32389
  this.trigger("error", this.request);
32367
32390
  this.trigger("loadend", this.request);
32368
32391
  }
32369
32392
  /**
32393
+ * Run the request error steps for the given event.
32394
+ * @see https://xhr.spec.whatwg.org/#request-error-steps
32395
+ */
32396
+ requestErrorSteps(event, uploadComplete) {
32397
+ this.setReadyState(this.request.DONE);
32398
+ if (!uploadComplete) {
32399
+ this.trigger(event, this.request.upload, {
32400
+ loaded: 0,
32401
+ total: 0
32402
+ });
32403
+ this.trigger("loadend", this.request.upload, {
32404
+ loaded: 0,
32405
+ total: 0
32406
+ });
32407
+ }
32408
+ this.trigger(event, this.request, {
32409
+ loaded: 0,
32410
+ total: 0
32411
+ });
32412
+ this.trigger("loadend", this.request, {
32413
+ loaded: 0,
32414
+ total: 0
32415
+ });
32416
+ /**
32417
+ * @note An aborted request ends up in the "UNSENT" state
32418
+ * without dispatching another "readystatechange" event.
32419
+ * @see https://xhr.spec.whatwg.org/#the-abort()-method
32420
+ */
32421
+ if (event === "abort") this.setReadyState(this.request.UNSENT, false);
32422
+ }
32423
+ /**
32370
32424
  * Transitions this request's `readyState` to the given one.
32371
32425
  */
32372
32426
  setReadyState(nextReadyState, triggerReadyStateChangeEvent = true) {
@@ -32403,7 +32457,7 @@ You can also automate this process and make the worker script update automatical
32403
32457
  /**
32404
32458
  * Converts this `XMLHttpRequest` instance into a Fetch API `Request` instance.
32405
32459
  */
32406
- toFetchApiRequest(body) {
32460
+ toFetchApiRequest(body, signal) {
32407
32461
  this.logger.verbose("converting request to a Fetch API Request...");
32408
32462
  const resolvedBody = body instanceof Document ? body.documentElement.innerText : body;
32409
32463
  const fetchRequest = new FetchRequest(this.url.href, {
@@ -32413,7 +32467,8 @@ You can also automate this process and make the worker script update automatical
32413
32467
  * @see https://xhr.spec.whatwg.org/#cross-origin-credentials
32414
32468
  */
32415
32469
  credentials: this.request.withCredentials ? "include" : "same-origin",
32416
- body: resolvedBody
32470
+ body: resolvedBody,
32471
+ signal
32417
32472
  });
32418
32473
  const headers = fetchRequest.headers;
32419
32474
  define(fetchRequest, "headers", createProxy(headers, { methodCall: ([methodName, args], invoke) => {