@depup/msw 3.0.1-depup.0 → 3.0.3-depup.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.
Files changed (39) hide show
  1. package/README.md +5 -4
  2. package/changes.json +7 -3
  3. package/lib/_chunks/cookie-store.js +3 -2
  4. package/lib/_chunks/cookie-store.js.map +1 -1
  5. package/lib/_chunks/interceptor-source.d.ts +5 -1
  6. package/lib/_chunks/interceptor-source.js +18 -5
  7. package/lib/_chunks/interceptor-source.js.map +1 -1
  8. package/lib/_chunks/request-handler.d.ts +3 -2
  9. package/lib/browser/index.d.ts +4 -9
  10. package/lib/browser/index.js +21 -14
  11. package/lib/browser/index.js.map +1 -1
  12. package/lib/graphql/index.d.ts +19 -4
  13. package/lib/graphql/index.js +24 -12
  14. package/lib/graphql/index.js.map +1 -1
  15. package/lib/iife/index.js +66 -33
  16. package/lib/iife/index.js.map +1 -1
  17. package/lib/mockServiceWorker.js +1 -1
  18. package/lib/vite/index.js +10 -7
  19. package/lib/vite/index.js.map +1 -1
  20. package/lib/vite/runtime.d.ts +2 -3
  21. package/lib/vite/runtime.js +70 -68
  22. package/lib/vite/runtime.js.map +1 -1
  23. package/package.json +12 -9
  24. package/src/browser/default-network-options.ts +1 -1
  25. package/src/browser/setup-worker.ts +1 -4
  26. package/src/browser/sources/fallback-http-source.ts +11 -8
  27. package/src/browser/sources/service-worker-source.ts +12 -5
  28. package/src/browser/utils/should-invalidate-worker.test.ts +0 -16
  29. package/src/core/experimental/define-network.ts +1 -1
  30. package/src/core/experimental/handlers-controller.test.ts +61 -0
  31. package/src/core/experimental/handlers-controller.ts +39 -6
  32. package/src/core/experimental/sources/network-source.ts +8 -1
  33. package/src/core/handlers/handler.ts +3 -2
  34. package/src/graphql/graphql-subscription.ts +30 -12
  35. package/src/vite/plugin.test.ts +1 -1
  36. package/src/vite/plugin.ts +13 -10
  37. package/src/vite/runtime.ts +85 -85
  38. package/src/vite/tsconfig.build.json +1 -1
  39. package/src/vite/options.d.ts +0 -5
package/lib/iife/index.js CHANGED
@@ -35,8 +35,9 @@ var MockServiceWorker = (function(exports) {
35
35
  /**
36
36
  * Release the resources held by this handler.
37
37
  *
38
- * @note This method is invoked automatically when the network is
39
- * disabled (e.g. `server.close()`). Override it in the handlers that
38
+ * @note This method is invoked automatically when the handler leaves
39
+ * the network: when the network is disabled (e.g. `server.close()`)
40
+ * or when a reset removes it (e.g. `server.resetHandlers()`). Override it in the handlers that
40
41
  * hold onto anything beyond a single frame, like timers, connections,
41
42
  * or event listeners. Returning a promise makes the network await
42
43
  * this handler's disposal before it tears itself down.
@@ -30069,16 +30070,26 @@ accept: "msw/passthrough" },
30069
30070
  }
30070
30071
  reset(nextHandlers) {
30071
30072
  invariant$1(nextHandlers.length > 0 ? this.#validateHandlers(nextHandlers) : true, devUtils.formatMessage("Failed to replace initial handlers during reset: invalid handlers. Did you forget to spread the handlers array?"));
30072
- for (const handler of this.currentHandlers()) handler.reset();
30073
30073
  const { initialHandlers } = this.getState();
30074
+ const nextInitialHandlers = nextHandlers.length > 0 ? groupHandlersByKind(nextHandlers) : initialHandlers;
30075
+ const currentHandlers = new Set(this.currentHandlers());
30076
+ const nextHandlersSet = new Set(Object.values(nextInitialHandlers).flat().filter(Boolean));
30077
+ for (const handler of currentHandlers.intersection(nextHandlersSet)) handler.reset();
30078
+ for (const handler of currentHandlers.difference(nextHandlersSet)) try {
30079
+ const disposal = handler.dispose();
30080
+ if (disposal instanceof Promise) disposal.catch((error) => {
30081
+ this.#printDisposalError(handler, error);
30082
+ });
30083
+ } catch (error) {
30084
+ this.#printDisposalError(handler, error);
30085
+ }
30074
30086
  if (nextHandlers.length === 0) {
30075
30087
  this.setState({ handlers: { ...initialHandlers } });
30076
30088
  return;
30077
30089
  }
30078
- const normalizedNextHandlers = groupHandlersByKind(nextHandlers);
30079
30090
  this.setState({
30080
- initialHandlers: normalizedNextHandlers,
30081
- handlers: { ...normalizedNextHandlers }
30091
+ initialHandlers: nextInitialHandlers,
30092
+ handlers: { ...nextInitialHandlers }
30082
30093
  });
30083
30094
  }
30084
30095
  restore() {
@@ -30095,6 +30106,9 @@ accept: "msw/passthrough" },
30095
30106
  #validateHandlers(handlers) {
30096
30107
  return handlers.every((handler) => !Array.isArray(handler));
30097
30108
  }
30109
+ #printDisposalError(handler, error) {
30110
+ devUtils.error("Failed to dispose of \"%s\" handler removed during reset. Please see the original error below.\n%s", handler.kind, error);
30111
+ }
30098
30112
  };
30099
30113
  var InMemoryHandlersController = class extends HandlersController {
30100
30114
  #handlers;
@@ -30221,7 +30235,7 @@ accept: "msw/passthrough" },
30221
30235
  const handlers = frame.getHandlers(handlersController);
30222
30236
  await frame.resolve(handlers, resolvedOptions.onUnhandledFrame || "warn", resolvedOptions.context);
30223
30237
  });
30224
- return source.enable();
30238
+ return source.enable(resolvedOptions.context);
30225
30239
  }));
30226
30240
  },
30227
30241
  disable() {
@@ -30954,6 +30968,10 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
30954
30968
  }
30955
30969
  #options;
30956
30970
  /**
30971
+ * The resolved context of the network this source was last enabled with.
30972
+ */
30973
+ #context;
30974
+ /**
30957
30975
  * @note We cannot use `WeakMap` here as request/response
30958
30976
  * identity cannot be preserved through the client-worker channel.
30959
30977
  */
@@ -30975,7 +30993,8 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
30975
30993
  this.#workerState = "pending";
30976
30994
  this.#channel = new WorkerChannel({ getWorker: () => this.workerPromise.then(([worker]) => worker) });
30977
30995
  }
30978
- async enable() {
30996
+ async enable(context) {
30997
+ this.#context = context;
30979
30998
  this.#channel.removeAllListeners();
30980
30999
  this.#frames.clear();
30981
31000
  this.#listenerController = new AbortController();
@@ -30996,7 +31015,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
30996
31015
  clientConfirmationPromise.resolve(event.data.client);
30997
31016
  });
30998
31017
  await clientConfirmationPromise.promise;
30999
- if (!this.#options.quiet) this.#printStartMessage();
31018
+ if (!this.#context?.quiet) this.#printStartMessage();
31000
31019
  return registration;
31001
31020
  }
31002
31021
  async disable() {
@@ -31018,7 +31037,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
31018
31037
  * worker forwards after `stop()` can be answered with `PASSTHROUGH` by
31019
31038
  * `#handleRequest`. `#startWorker` swaps in a fresh deferred on re-enable.
31020
31039
  */
31021
- if (!this.#options.quiet) this.#printStopMessage();
31040
+ if (!this.#context?.quiet) this.#printStopMessage();
31022
31041
  }
31023
31042
  /**
31024
31043
  * Terminal teardown. Unregisters the Service Worker, tears down the channel,
@@ -31085,7 +31104,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
31085
31104
  this.#keepAliveInterval = window.setInterval(() => {
31086
31105
  this.#channel.postMessage("KEEPALIVE_REQUEST");
31087
31106
  }, 5e3);
31088
- if (!this.#options.quiet) validateWorkerScope(registration);
31107
+ if (!this.#context?.quiet) validateWorkerScope(registration);
31089
31108
  return [worker, registration];
31090
31109
  }
31091
31110
  async #handleRequest(event) {
@@ -32523,18 +32542,21 @@ You can also automate this process and make the worker script update automatical
32523
32542
  //#endregion
32524
32543
  //#region src/browser/sources/fallback-http-source.ts
32525
32544
  var FallbackHttpSource = class extends InterceptorSource {
32526
- options;
32527
- constructor(options) {
32545
+ /**
32546
+ * The resolved context of the network this source was last enabled with.
32547
+ */
32548
+ #context;
32549
+ constructor() {
32528
32550
  super({ interceptors: [new XMLHttpRequestInterceptor(), new FetchInterceptor()] });
32529
- this.options = options;
32530
32551
  }
32531
- enable() {
32552
+ enable(context) {
32553
+ this.#context = context;
32532
32554
  super.enable();
32533
- if (!this.options.quiet) this.#printStartMessage();
32555
+ if (!this.#context?.quiet) this.#printStartMessage();
32534
32556
  }
32535
32557
  disable() {
32536
32558
  super.disable();
32537
- if (!this.options.quiet) this.#printStopMessage();
32559
+ if (!this.#context?.quiet) this.#printStopMessage();
32538
32560
  }
32539
32561
  #printStartMessage() {
32540
32562
  console.groupCollapsed(`%c${devUtils.formatMessage("Mocking enabled (fallback mode).")}`, "color:orangered;font-weight:bold;");
@@ -32572,9 +32594,8 @@ You can also automate this process and make the worker script update automatical
32572
32594
  url: options?.serviceWorker?.url?.toString() || DEFAULT_WORKER_URL,
32573
32595
  options: options?.serviceWorker?.options
32574
32596
  },
32575
- findWorker: options?.findWorker,
32576
- quiet: options?.quiet
32577
- }) : new FallbackHttpSource({ quiet: options?.quiet });
32597
+ findWorker: options?.findWorker
32598
+ }) : new FallbackHttpSource();
32578
32599
  network.configure({
32579
32600
  sources: [httpSource, new InterceptorSource({ interceptors: [new WebSocketInterceptor()] })],
32580
32601
  onUnhandledFrame: options?.onUnhandledFrame ?? "warn",
@@ -32601,7 +32622,7 @@ You can also automate this process and make the worker script update automatical
32601
32622
  //#region src/browser/default-network-options.ts
32602
32623
  function createDefaultNetworkOptions(workerUrl = "/mockServiceWorker.js") {
32603
32624
  return {
32604
- sources: [supportsServiceWorker() ? new ServiceWorkerSource({ serviceWorker: { url: workerUrl } }) : new FallbackHttpSource({}), new InterceptorSource({ interceptors: [new WebSocketInterceptor()] })],
32625
+ sources: [supportsServiceWorker() ? new ServiceWorkerSource({ serviceWorker: { url: workerUrl } }) : new FallbackHttpSource(), new InterceptorSource({ interceptors: [new WebSocketInterceptor()] })],
32605
32626
  onUnhandledFrame: "warn"
32606
32627
  };
32607
32628
  }
@@ -35301,23 +35322,26 @@ Consider naming this operation or using the "operation()" request handler of "gr
35301
35322
  this.#endSubscription(connection, args.subscriptionId);
35302
35323
  }
35303
35324
  /**
35304
- * Drop this transport's subscribers and active subscriptions from the
35305
- * sessions it participates in. The sessions themselves are left intact:
35306
- * they are shared with the other transports of the same connection and
35307
- * own the protocol listeners for as long as the client stays connected.
35325
+ * Remove the given handler from the subscribers of every session
35326
+ * this transport participates in. The handler no longer matches
35327
+ * subscriptions on the connections opened while it was registered.
35328
+ */
35329
+ unsubscribe(handler) {
35330
+ for (const connection of connections.values()) connection.subscribers.delete(handler);
35331
+ }
35332
+ /**
35333
+ * End the active subscriptions of the sessions this transport
35334
+ * participates in. The sessions and their subscribers are left intact:
35335
+ * the handlers that survive the reset keep serving the connections
35336
+ * opened before it, while the removed handlers unsubscribe themselves
35337
+ * on disposal. The sessions own the protocol listeners for as long as
35338
+ * the client stays connected.
35308
35339
  *
35309
35340
  * @note This method is invoked automatically when the handlers
35310
35341
  * controller resets the handlers (e.g. `server.resetHandlers()`).
35311
35342
  */
35312
35343
  reset() {
35313
- for (const connection of connections.values()) {
35314
- let ownsConnection = false;
35315
- for (const [handler, entry] of connection.subscribers) if (entry.transport === this) {
35316
- connection.subscribers.delete(handler);
35317
- ownsConnection = true;
35318
- }
35319
- if (ownsConnection) this.#endAllSubscriptions(connection);
35320
- }
35344
+ for (const connection of connections.values()) if (Array.from(connection.subscribers.values()).some((entry) => entry.transport === this)) this.#endAllSubscriptions(connection);
35321
35345
  }
35322
35346
  /**
35323
35347
  * Forget the sessions of this transport, ending their subscriptions.
@@ -35463,6 +35487,15 @@ Consider naming this operation or using the "operation()" request handler of "gr
35463
35487
  this.isUsed = false;
35464
35488
  }
35465
35489
  /**
35490
+ * Stop serving the connections this handler was bound to.
35491
+ * @note This method is invoked automatically when the handler
35492
+ * is removed from the network, either by resetting the handlers
35493
+ * (e.g. `server.resetHandlers()`) or by disabling the network.
35494
+ */
35495
+ dispose() {
35496
+ this.#transport.unsubscribe(this);
35497
+ }
35498
+ /**
35466
35499
  * @note Individual subscription handlers stay silent. The subscription
35467
35500
  * transport owns the GraphQL-aware logging for the entire connection
35468
35501
  * (a logger is attached once per matching handler otherwise).