msw 2.3.0-ws.rc-4 → 2.3.0-ws.rc-6

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
@@ -1129,7 +1129,7 @@ var MockServiceWorker = (() => {
1129
1129
  return stringToRegexp(path, keys, options);
1130
1130
  }
1131
1131
 
1132
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/chunk-UJZOJSMP.mjs
1132
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/chunk-UJZOJSMP.mjs
1133
1133
  var encoder = new TextEncoder();
1134
1134
  function encodeBuffer(text) {
1135
1135
  return encoder.encode(text);
@@ -1155,7 +1155,7 @@ var MockServiceWorker = (() => {
1155
1155
  return RESPONSE_STATUS_CODES_WITHOUT_BODY.has(status);
1156
1156
  }
1157
1157
 
1158
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/chunk-HAGW22AN.mjs
1158
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/chunk-HAGW22AN.mjs
1159
1159
  var IS_PATCHED_MODULE = Symbol("isPatchedModule");
1160
1160
 
1161
1161
  // node_modules/.pnpm/is-node-process@1.2.0/node_modules/is-node-process/lib/index.mjs
@@ -1445,7 +1445,7 @@ var MockServiceWorker = (() => {
1445
1445
  return message3.toString();
1446
1446
  }
1447
1447
 
1448
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/chunk-QED3Q6Z2.mjs
1448
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/chunk-QED3Q6Z2.mjs
1449
1449
  var INTERNAL_REQUEST_ID_HEADER_NAME = "x-interceptors-internal-request-id";
1450
1450
  function getGlobalSymbol(symbol) {
1451
1451
  return (
@@ -1593,7 +1593,7 @@ var MockServiceWorker = (() => {
1593
1593
  return Math.random().toString(16).slice(2);
1594
1594
  }
1595
1595
 
1596
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/index.mjs
1596
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/index.mjs
1597
1597
  var BatchInterceptor = class extends Interceptor {
1598
1598
  constructor(options) {
1599
1599
  BatchInterceptor.symbol = Symbol(options.name);
@@ -5661,25 +5661,81 @@ Consider naming this operation or using "graphql.operation()" request handler to
5661
5661
  };
5662
5662
 
5663
5663
  // src/core/ws/WebSocketClientManager.ts
5664
- var kAddByClientId = Symbol("kAddByClientId");
5664
+ var MSW_WEBSOCKET_CLIENTS_KEY = "msw:ws:clients";
5665
5665
  var WebSocketClientManager = class {
5666
- constructor(channel) {
5666
+ constructor(channel, url) {
5667
5667
  this.channel = channel;
5668
- this.clients = /* @__PURE__ */ new Set();
5669
- this.channel.addEventListener("message", (message3) => {
5670
- const { type, payload } = message3.data;
5671
- switch (type) {
5672
- case "connection:open": {
5673
- this.onRemoteConnection(payload.clientId, new URL(payload.url));
5674
- break;
5668
+ this.url = url;
5669
+ this.inMemoryClients = /* @__PURE__ */ new Set();
5670
+ if (typeof localStorage !== "undefined") {
5671
+ localStorage.removeItem = new Proxy(localStorage.removeItem, {
5672
+ apply: (target, thisArg, args) => {
5673
+ const [key] = args;
5674
+ if (key === MSW_WEBSOCKET_CLIENTS_KEY) {
5675
+ this.inMemoryClients.clear();
5676
+ }
5677
+ return Reflect.apply(target, thisArg, args);
5675
5678
  }
5676
- }
5677
- });
5679
+ });
5680
+ }
5678
5681
  }
5682
+ inMemoryClients;
5679
5683
  /**
5680
5684
  * All active WebSocket client connections.
5681
5685
  */
5682
- clients;
5686
+ get clients() {
5687
+ if (typeof localStorage !== "undefined") {
5688
+ const inMemoryClients = Array.from(this.inMemoryClients);
5689
+ console.log("get clients()", inMemoryClients, this.getSerializedClients());
5690
+ return new Set(
5691
+ inMemoryClients.concat(
5692
+ this.getSerializedClients().filter((serializedClient) => {
5693
+ if (inMemoryClients.every(
5694
+ (client) => client.id !== serializedClient.clientId
5695
+ )) {
5696
+ return serializedClient;
5697
+ }
5698
+ }).map((serializedClient) => {
5699
+ return new WebSocketRemoteClientConnection(
5700
+ serializedClient.clientId,
5701
+ new URL(serializedClient.url),
5702
+ this.channel
5703
+ );
5704
+ })
5705
+ )
5706
+ );
5707
+ }
5708
+ return this.inMemoryClients;
5709
+ }
5710
+ getSerializedClients() {
5711
+ invariant(
5712
+ typeof localStorage !== "undefined",
5713
+ "Failed to call WebSocketClientManager#getSerializedClients() in a non-browser environment. This is likely a bug in MSW. Please, report it on GitHub: https://github.com/mswjs/msw"
5714
+ );
5715
+ const clientsJson = localStorage.getItem(MSW_WEBSOCKET_CLIENTS_KEY);
5716
+ if (!clientsJson) {
5717
+ return [];
5718
+ }
5719
+ const allClients = JSON.parse(clientsJson);
5720
+ const matchingClients = allClients.filter((client) => {
5721
+ return matchRequestUrl(new URL(client.url), this.url).matches;
5722
+ });
5723
+ return matchingClients;
5724
+ }
5725
+ addClient(client) {
5726
+ this.inMemoryClients.add(client);
5727
+ if (typeof localStorage !== "undefined") {
5728
+ const serializedClients = this.getSerializedClients();
5729
+ const nextSerializedClients = serializedClients.concat({
5730
+ clientId: client.id,
5731
+ url: client.url.href
5732
+ });
5733
+ localStorage.setItem(
5734
+ MSW_WEBSOCKET_CLIENTS_KEY,
5735
+ JSON.stringify(nextSerializedClients)
5736
+ );
5737
+ }
5738
+ }
5683
5739
  /**
5684
5740
  * Adds the given `WebSocket` client connection to the set
5685
5741
  * of all connections. The given connection is always the complete
@@ -5687,14 +5743,7 @@ Consider naming this operation or using "graphql.operation()" request handler to
5687
5743
  * for the opened connections in the same runtime.
5688
5744
  */
5689
5745
  addConnection(client) {
5690
- this.clients.add(client);
5691
- this.channel.postMessage({
5692
- type: "connection:open",
5693
- payload: {
5694
- clientId: client.id,
5695
- url: client.url.toString()
5696
- }
5697
- });
5746
+ this.addClient(client);
5698
5747
  const handleExtraneousMessage = (message3) => {
5699
5748
  const { type, payload } = message3.data;
5700
5749
  if (typeof payload === "object" && "clientId" in payload && payload.clientId !== client.id) {
@@ -5719,18 +5768,6 @@ Consider naming this operation or using "graphql.operation()" request handler to
5719
5768
  once: true
5720
5769
  });
5721
5770
  }
5722
- /**
5723
- * Adds a client connection wrapper to operate with
5724
- * WebSocket client connections in other runtimes.
5725
- */
5726
- onRemoteConnection(id, url) {
5727
- this.clients.add(
5728
- // Create a connection-compatible instance that can
5729
- // operate with this client from a different runtime
5730
- // using the BroadcastChannel messages.
5731
- new WebSocketRemoteClientConnection(id, url, this.channel)
5732
- );
5733
- }
5734
5771
  };
5735
5772
  var WebSocketRemoteClientConnection = class {
5736
5773
  constructor(id, url, channel) {
@@ -5768,9 +5805,11 @@ Consider naming this operation or using "graphql.operation()" request handler to
5768
5805
  "Expected a WebSocket server URL to be a valid path but got %s",
5769
5806
  typeof url
5770
5807
  );
5771
- const clientManager = new WebSocketClientManager(wsBroadcastChannel);
5808
+ const clientManager = new WebSocketClientManager(wsBroadcastChannel, url);
5772
5809
  return {
5773
- clients: clientManager.clients,
5810
+ get clients() {
5811
+ return clientManager.clients;
5812
+ },
5774
5813
  on(event, listener) {
5775
5814
  const handler = new WebSocketHandler(url);
5776
5815
  handler[kEmitter].on("connection", ({ client }) => {
@@ -6565,6 +6604,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
6565
6604
  context.workerChannel.send("MOCK_DEACTIVATE");
6566
6605
  context.isMockingEnabled = false;
6567
6606
  window.clearInterval(context.keepAliveInterval);
6607
+ localStorage.removeItem(MSW_WEBSOCKET_CLIENTS_KEY);
6568
6608
  printStopMessage({ quiet: context.startOptions?.quiet });
6569
6609
  };
6570
6610
  };
@@ -6674,7 +6714,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
6674
6714
  }
6675
6715
  };
6676
6716
 
6677
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/chunk-OUWBQF3Z.mjs
6717
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/chunk-OUWBQF3Z.mjs
6678
6718
  var RequestController = class {
6679
6719
  constructor(request) {
6680
6720
  this.request = request;
@@ -6712,7 +6752,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
6712
6752
  }
6713
6753
  }
6714
6754
 
6715
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/chunk-3FNUI33J.mjs
6755
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/chunk-3FNUI33J.mjs
6716
6756
  function isPropertyAccessible2(obj, key) {
6717
6757
  try {
6718
6758
  obj[key];
@@ -6859,7 +6899,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
6859
6899
  });
6860
6900
  }
6861
6901
 
6862
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/chunk-VYFS2IF2.mjs
6902
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/chunk-VYFS2IF2.mjs
6863
6903
  function concatArrayBuffer(left, right) {
6864
6904
  const result = new Uint8Array(left.byteLength + right.byteLength);
6865
6905
  result.set(left, 0);
@@ -7684,7 +7724,7 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
7684
7724
  }
7685
7725
  }
7686
7726
 
7687
- // node_modules/.pnpm/@mswjs+interceptors@0.27.0/node_modules/@mswjs/interceptors/lib/browser/interceptors/WebSocket/index.mjs
7727
+ // node_modules/.pnpm/@mswjs+interceptors@0.27.1/node_modules/@mswjs/interceptors/lib/browser/interceptors/WebSocket/index.mjs
7688
7728
  function bindEvent(target, event) {
7689
7729
  Object.defineProperties(event, {
7690
7730
  target: {
@@ -7711,9 +7751,15 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
7711
7751
  get cancelable() {
7712
7752
  return this[kCancelable];
7713
7753
  }
7754
+ set cancelable(nextCancelable) {
7755
+ this[kCancelable] = nextCancelable;
7756
+ }
7714
7757
  get defaultPrevented() {
7715
7758
  return this[kDefaultPrevented];
7716
7759
  }
7760
+ set defaultPrevented(nextDefaultPrevented) {
7761
+ this[kDefaultPrevented] = nextDefaultPrevented;
7762
+ }
7717
7763
  preventDefault() {
7718
7764
  if (this.cancelable && !this[kDefaultPrevented]) {
7719
7765
  this[kDefaultPrevented] = true;