msw 2.3.0-ws.rc-5 → 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/browser/index.js +2 -0
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +2 -0
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/ws/WebSocketClientManager.d.mts +9 -15
- package/lib/core/ws/WebSocketClientManager.d.ts +9 -15
- package/lib/core/ws/WebSocketClientManager.js +73 -34
- package/lib/core/ws/WebSocketClientManager.js.map +1 -1
- package/lib/core/ws/WebSocketClientManager.mjs +73 -34
- package/lib/core/ws/WebSocketClientManager.mjs.map +1 -1
- package/lib/core/ws.js +4 -2
- package/lib/core/ws.js.map +1 -1
- package/lib/core/ws.mjs +4 -2
- package/lib/core/ws.mjs.map +1 -1
- package/lib/iife/index.js +74 -34
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +1 -1
- package/src/browser/setupWorker/stop/createStop.ts +4 -0
- package/src/core/ws/WebSocketClientManager.test.ts +43 -45
- package/src/core/ws/WebSocketClientManager.ts +108 -44
- package/src/core/ws.ts +4 -2
package/lib/iife/index.js
CHANGED
|
@@ -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
|
|
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.
|
|
5669
|
-
this.
|
|
5670
|
-
|
|
5671
|
-
|
|
5672
|
-
|
|
5673
|
-
|
|
5674
|
-
|
|
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.
|
|
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
|
|
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
|
};
|