msw 2.14.3 → 2.14.4

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.
Files changed (69) hide show
  1. package/lib/core/{HttpResponse-aGiIzO91.d.ts → HttpResponse-BFS34nkx.d.ts} +16 -0
  2. package/lib/core/{HttpResponse-CxHR1nNN.d.mts → HttpResponse-CQwYpuKo.d.mts} +16 -0
  3. package/lib/core/HttpResponse.d.mts +1 -1
  4. package/lib/core/HttpResponse.d.ts +1 -1
  5. package/lib/core/experimental/compat.d.mts +1 -1
  6. package/lib/core/experimental/compat.d.ts +1 -1
  7. package/lib/core/experimental/define-network.d.mts +1 -1
  8. package/lib/core/experimental/define-network.d.ts +1 -1
  9. package/lib/core/experimental/frames/http-frame.d.mts +1 -1
  10. package/lib/core/experimental/frames/http-frame.d.ts +1 -1
  11. package/lib/core/experimental/frames/network-frame.d.mts +1 -1
  12. package/lib/core/experimental/frames/network-frame.d.ts +1 -1
  13. package/lib/core/experimental/frames/websocket-frame.d.mts +1 -1
  14. package/lib/core/experimental/frames/websocket-frame.d.ts +1 -1
  15. package/lib/core/experimental/handlers-controller.d.mts +1 -1
  16. package/lib/core/experimental/handlers-controller.d.ts +1 -1
  17. package/lib/core/experimental/index.d.mts +1 -1
  18. package/lib/core/experimental/index.d.ts +1 -1
  19. package/lib/core/experimental/on-unhandled-frame.d.mts +1 -1
  20. package/lib/core/experimental/on-unhandled-frame.d.ts +1 -1
  21. package/lib/core/experimental/setup-api.d.mts +1 -1
  22. package/lib/core/experimental/setup-api.d.ts +1 -1
  23. package/lib/core/experimental/sources/interceptor-source.d.mts +1 -1
  24. package/lib/core/experimental/sources/interceptor-source.d.ts +1 -1
  25. package/lib/core/experimental/sources/network-source.d.mts +1 -1
  26. package/lib/core/experimental/sources/network-source.d.ts +1 -1
  27. package/lib/core/getResponse.d.mts +1 -1
  28. package/lib/core/getResponse.d.ts +1 -1
  29. package/lib/core/graphql.d.mts +1 -1
  30. package/lib/core/graphql.d.ts +1 -1
  31. package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
  32. package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
  33. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  34. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  35. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  36. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  37. package/lib/core/handlers/RequestHandler.js +82 -1
  38. package/lib/core/handlers/RequestHandler.js.map +1 -1
  39. package/lib/core/handlers/RequestHandler.mjs +82 -1
  40. package/lib/core/handlers/RequestHandler.mjs.map +1 -1
  41. package/lib/core/http.d.mts +1 -1
  42. package/lib/core/http.d.ts +1 -1
  43. package/lib/core/index.d.mts +1 -1
  44. package/lib/core/index.d.ts +1 -1
  45. package/lib/core/passthrough.d.mts +1 -1
  46. package/lib/core/passthrough.d.ts +1 -1
  47. package/lib/core/sse.d.mts +1 -1
  48. package/lib/core/sse.d.ts +1 -1
  49. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  50. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  51. package/lib/core/utils/executeHandlers.d.mts +1 -1
  52. package/lib/core/utils/executeHandlers.d.ts +1 -1
  53. package/lib/core/utils/handleRequest.d.mts +1 -1
  54. package/lib/core/utils/handleRequest.d.ts +1 -1
  55. package/lib/core/utils/internal/attachSiblingHandlers.d.mts +1 -1
  56. package/lib/core/utils/internal/attachSiblingHandlers.d.ts +1 -1
  57. package/lib/core/utils/internal/isHandlerKind.d.mts +1 -1
  58. package/lib/core/utils/internal/isHandlerKind.d.ts +1 -1
  59. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
  60. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
  61. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  62. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  63. package/lib/core/ws/handleWebSocketEvent.d.mts +1 -1
  64. package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
  65. package/lib/iife/index.js +81 -1
  66. package/lib/iife/index.js.map +1 -1
  67. package/lib/mockServiceWorker.js +1 -1
  68. package/package.json +2 -2
  69. package/src/core/handlers/RequestHandler.ts +144 -10
package/lib/iife/index.js CHANGED
@@ -16166,7 +16166,9 @@ ${operationTypes.join("\n")}
16166
16166
  resolver;
16167
16167
  resolverIterator;
16168
16168
  resolverIteratorResult;
16169
+ resolverIteratorCleanups;
16169
16170
  options;
16171
+ scheduledCleanups;
16170
16172
  info;
16171
16173
  /**
16172
16174
  * Indicates whether this request handler has been used
@@ -16176,6 +16178,7 @@ ${operationTypes.join("\n")}
16176
16178
  constructor(args) {
16177
16179
  this.resolver = args.resolver;
16178
16180
  this.options = args.options;
16181
+ this.scheduledCleanups = /* @__PURE__ */ new Map();
16179
16182
  const callFrame = getCallFrame(new Error());
16180
16183
  this.info = {
16181
16184
  ...args.info,
@@ -16190,9 +16193,11 @@ ${operationTypes.join("\n")}
16190
16193
  * from a clean state.
16191
16194
  */
16192
16195
  reset() {
16196
+ this.scheduledCleanups.clear();
16193
16197
  const iterator = this.resolverIterator;
16194
16198
  this.resolverIterator = void 0;
16195
16199
  this.resolverIteratorResult = void 0;
16200
+ this.resolverIteratorCleanups = void 0;
16196
16201
  if (typeof iterator?.return === "function") {
16197
16202
  void Promise.resolve(iterator.return());
16198
16203
  }
@@ -16278,8 +16283,20 @@ ${operationTypes.join("\n")}
16278
16283
  request: args.request,
16279
16284
  parsedResult
16280
16285
  });
16286
+ const listenerController = new AbortController();
16287
+ args.request.signal.addEventListener(
16288
+ "abort",
16289
+ () => this.runScheduledCleanups(args.requestId),
16290
+ {
16291
+ once: true,
16292
+ signal: listenerController.signal
16293
+ }
16294
+ );
16281
16295
  const mockedResponsePromise = executeResolver({
16282
16296
  ...resolverExtras,
16297
+ finalize: (callback) => {
16298
+ this.scheduleCleanup(args.requestId, callback);
16299
+ },
16283
16300
  requestId: args.requestId,
16284
16301
  request: args.request
16285
16302
  }).catch((errorOrResponse) => {
@@ -16287,6 +16304,8 @@ ${operationTypes.join("\n")}
16287
16304
  return errorOrResponse;
16288
16305
  }
16289
16306
  throw errorOrResponse;
16307
+ }).finally(() => {
16308
+ listenerController.abort();
16290
16309
  });
16291
16310
  const mockedResponse = await mockedResponsePromise;
16292
16311
  if (mockedResponse) {
@@ -16305,10 +16324,22 @@ ${operationTypes.join("\n")}
16305
16324
  wrapResolver(resolver) {
16306
16325
  return async (info) => {
16307
16326
  if (!this.resolverIterator) {
16308
- const result = await resolver(info);
16327
+ let result;
16328
+ try {
16329
+ result = await resolver(info);
16330
+ } catch (error3) {
16331
+ await this.runScheduledCleanups(info.requestId);
16332
+ throw error3;
16333
+ }
16309
16334
  if (!isIterable(result)) {
16335
+ await this.runScheduledCleanups(info.requestId);
16310
16336
  return result;
16311
16337
  }
16338
+ const existingCleanups = this.scheduledCleanups.get(info.requestId);
16339
+ if (existingCleanups != null && existingCleanups.length > 0) {
16340
+ this.resolverIteratorCleanups = existingCleanups;
16341
+ this.scheduledCleanups.delete(info.requestId);
16342
+ }
16312
16343
  this.resolverIterator = Symbol.iterator in result ? result[Symbol.iterator]() : result[Symbol.asyncIterator]();
16313
16344
  }
16314
16345
  this.isUsed = false;
@@ -16319,6 +16350,7 @@ ${operationTypes.join("\n")}
16319
16350
  }
16320
16351
  if (done) {
16321
16352
  this.isUsed = true;
16353
+ await this.runScheduledCleanups(info.requestId);
16322
16354
  return this.resolverIteratorResult?.clone();
16323
16355
  }
16324
16356
  return nextResponse;
@@ -16333,6 +16365,54 @@ ${operationTypes.join("\n")}
16333
16365
  parsedResult: args.parsedResult
16334
16366
  };
16335
16367
  }
16368
+ scheduleCleanup(requestId, callback) {
16369
+ if (this.resolverIterator) {
16370
+ ;
16371
+ (this.resolverIteratorCleanups ||= []).unshift(callback);
16372
+ return;
16373
+ }
16374
+ const cleanups = this.scheduledCleanups.get(requestId) || [];
16375
+ cleanups.unshift(callback);
16376
+ this.scheduledCleanups.set(requestId, cleanups);
16377
+ }
16378
+ async exhaustCleanups(cleanups) {
16379
+ const errors = [];
16380
+ for (const cleanup of cleanups) {
16381
+ try {
16382
+ await cleanup();
16383
+ } catch (error3) {
16384
+ if (error3 instanceof Error) {
16385
+ errors.push(error3);
16386
+ }
16387
+ }
16388
+ }
16389
+ if (errors.length > 0) {
16390
+ devUtils.error(
16391
+ 'Failed to execute cleanup for request handler "%s"',
16392
+ this.info.header,
16393
+ new AggregateError(
16394
+ errors,
16395
+ `Failed to execute cleanup for request handler "${this.info.header}"`
16396
+ )
16397
+ );
16398
+ }
16399
+ }
16400
+ async runScheduledCleanups(requestId) {
16401
+ if (this.resolverIterator && this.resolverIteratorCleanups != null && this.resolverIteratorCleanups.length > 0) {
16402
+ try {
16403
+ await this.exhaustCleanups(this.resolverIteratorCleanups);
16404
+ } finally {
16405
+ this.resolverIteratorCleanups = void 0;
16406
+ }
16407
+ return;
16408
+ }
16409
+ const cleanups = this.scheduledCleanups.get(requestId);
16410
+ if (!cleanups || cleanups.length == 0) {
16411
+ return;
16412
+ }
16413
+ await this.exhaustCleanups(cleanups);
16414
+ this.scheduledCleanups.delete(requestId);
16415
+ }
16336
16416
  };
16337
16417
  function forwardResponseCookies(response) {
16338
16418
  if (typeof document === "undefined") {