@fairfox/polly 0.52.0 → 0.53.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.
package/dist/src/mesh.js CHANGED
@@ -1943,6 +1943,9 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
1943
1943
  iceServers;
1944
1944
  dataChannelLabel;
1945
1945
  knownPeers;
1946
+ keyringSource;
1947
+ knownPeersRefreshTimer;
1948
+ knownPeersRefreshIntervalMs;
1946
1949
  presentPeers = new Set;
1947
1950
  localPeerId;
1948
1951
  RTCPeerConnectionCtor;
@@ -1950,8 +1953,17 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
1950
1953
  ready = false;
1951
1954
  readyResolver;
1952
1955
  get knownPeerIds() {
1956
+ if (this.keyringSource !== undefined) {
1957
+ return [...this.keyringSource().knownPeers.keys()].filter((id) => id !== this.localPeerId);
1958
+ }
1953
1959
  return [...this.knownPeers];
1954
1960
  }
1961
+ hasKnownPeer(remotePeerId) {
1962
+ if (this.keyringSource !== undefined) {
1963
+ return this.keyringSource().knownPeers.has(remotePeerId);
1964
+ }
1965
+ return this.knownPeers.has(remotePeerId);
1966
+ }
1955
1967
  onBlobMessage;
1956
1968
  constructor(options) {
1957
1969
  super();
@@ -1959,6 +1971,8 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
1959
1971
  this.iceServers = options.iceServers ?? DEFAULT_ICE_SERVERS;
1960
1972
  this.dataChannelLabel = options.dataChannelLabel ?? "polly-mesh";
1961
1973
  this.knownPeers = new Set(options.knownPeerIds ?? []);
1974
+ this.keyringSource = options.keyringSource;
1975
+ this.knownPeersRefreshIntervalMs = options.knownPeersRefreshIntervalMs ?? 2000;
1962
1976
  this.localPeerId = options.peerId;
1963
1977
  const PC = options.RTCPeerConnection ?? globalThis.RTCPeerConnection;
1964
1978
  if (typeof PC !== "function") {
@@ -1972,6 +1986,41 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
1972
1986
  peerSlotCount() {
1973
1987
  return this.slots.size;
1974
1988
  }
1989
+ getPeerStateSnapshot() {
1990
+ const knownPeerIds = this.knownPeerIds;
1991
+ const presentPeerIds = [...this.presentPeers];
1992
+ const knownPeerSet = new Set(knownPeerIds);
1993
+ const allPeers = new Set;
1994
+ for (const id of knownPeerIds)
1995
+ allPeers.add(id);
1996
+ for (const id of presentPeerIds)
1997
+ allPeers.add(id);
1998
+ for (const id of this.slots.keys())
1999
+ allPeers.add(id);
2000
+ const peers = [];
2001
+ for (const peerId of allPeers) {
2002
+ const slot = this.slots.get(peerId);
2003
+ peers.push({
2004
+ peerId,
2005
+ knownInKeyring: knownPeerSet.has(peerId),
2006
+ presentInSignalling: this.presentPeers.has(peerId),
2007
+ slot: slot ? {
2008
+ signalingState: slot.connection.signalingState,
2009
+ iceConnectionState: slot.connection.iceConnectionState,
2010
+ connectionState: slot.connection.connectionState,
2011
+ dataChannelState: slot.channel?.readyState ?? "no-channel",
2012
+ pendingSendCount: slot.pendingSends.length,
2013
+ pendingRemoteIceCount: slot.pendingRemoteIce.length
2014
+ } : undefined
2015
+ });
2016
+ }
2017
+ return {
2018
+ localPeerId: this.localPeerId,
2019
+ knownPeerIds,
2020
+ presentPeerIds,
2021
+ peers
2022
+ };
2023
+ }
1975
2024
  handlePeerJoined(remotePeerId) {
1976
2025
  this.presentPeers.add(remotePeerId);
1977
2026
  if (!this.shouldInitiateTo(remotePeerId))
@@ -1998,19 +2047,28 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
1998
2047
  addKnownPeer(remotePeerId) {
1999
2048
  if (remotePeerId === this.localPeerId)
2000
2049
  return;
2001
- if (this.knownPeers.has(remotePeerId))
2002
- return;
2003
- this.knownPeers.add(remotePeerId);
2050
+ if (this.keyringSource === undefined) {
2051
+ if (this.knownPeers.has(remotePeerId))
2052
+ return;
2053
+ this.knownPeers.add(remotePeerId);
2054
+ }
2004
2055
  if (!this.presentPeers.has(remotePeerId))
2005
2056
  return;
2006
2057
  if (!this.shouldInitiateTo(remotePeerId))
2007
2058
  return;
2008
2059
  this.createInitiatingSlot(remotePeerId);
2009
2060
  }
2061
+ refreshKnownPeers() {
2062
+ for (const remotePeerId of this.presentPeers) {
2063
+ if (!this.shouldInitiateTo(remotePeerId))
2064
+ continue;
2065
+ this.createInitiatingSlot(remotePeerId);
2066
+ }
2067
+ }
2010
2068
  shouldInitiateTo(remotePeerId) {
2011
2069
  if (remotePeerId === this.localPeerId)
2012
2070
  return false;
2013
- if (!this.knownPeers.has(remotePeerId))
2071
+ if (!this.hasKnownPeer(remotePeerId))
2014
2072
  return false;
2015
2073
  if (this.slots.has(remotePeerId))
2016
2074
  return false;
@@ -2032,8 +2090,10 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
2032
2090
  }
2033
2091
  this.ready = true;
2034
2092
  this.readyResolver?.();
2093
+ this.startKnownPeersSweep();
2035
2094
  }
2036
2095
  disconnect() {
2096
+ this.stopKnownPeersSweep();
2037
2097
  for (const slot of this.slots.values()) {
2038
2098
  slot.channel?.close();
2039
2099
  slot.connection.close();
@@ -2043,6 +2103,23 @@ class MeshWebRTCAdapter extends NetworkAdapter2 {
2043
2103
  this.ready = false;
2044
2104
  this.emit("close");
2045
2105
  }
2106
+ startKnownPeersSweep() {
2107
+ if (this.keyringSource === undefined)
2108
+ return;
2109
+ if (this.knownPeersRefreshIntervalMs <= 0)
2110
+ return;
2111
+ if (this.knownPeersRefreshTimer !== undefined)
2112
+ return;
2113
+ this.knownPeersRefreshTimer = setInterval(() => {
2114
+ this.refreshKnownPeers();
2115
+ }, this.knownPeersRefreshIntervalMs);
2116
+ }
2117
+ stopKnownPeersSweep() {
2118
+ if (this.knownPeersRefreshTimer === undefined)
2119
+ return;
2120
+ clearInterval(this.knownPeersRefreshTimer);
2121
+ this.knownPeersRefreshTimer = undefined;
2122
+ }
2046
2123
  send(message) {
2047
2124
  const targetId = message.targetId;
2048
2125
  const bytes = this.serialiseMessage(message);
@@ -2332,12 +2409,16 @@ async function createMeshClient(options) {
2332
2409
  signaling: undefined,
2333
2410
  peerId: options.signaling.peerId,
2334
2411
  knownPeerIds,
2412
+ keyringSource,
2335
2413
  ...resolvedIceServers !== undefined && { iceServers: resolvedIceServers },
2336
2414
  ...options.rtc?.dataChannelLabel !== undefined && {
2337
2415
  dataChannelLabel: options.rtc.dataChannelLabel
2338
2416
  },
2339
2417
  ...options.rtc?.RTCPeerConnection !== undefined && {
2340
2418
  RTCPeerConnection: options.rtc.RTCPeerConnection
2419
+ },
2420
+ ...options.rtc?.knownPeersRefreshIntervalMs !== undefined && {
2421
+ knownPeersRefreshIntervalMs: options.rtc.knownPeersRefreshIntervalMs
2341
2422
  }
2342
2423
  };
2343
2424
  let webrtcAdapter;
@@ -2384,6 +2465,20 @@ async function createMeshClient(options) {
2384
2465
  signaling,
2385
2466
  networkAdapter,
2386
2467
  webrtcAdapter,
2468
+ refreshKnownPeers: () => {
2469
+ webrtcAdapter?.refreshKnownPeers();
2470
+ },
2471
+ getPeerStateSnapshot: () => {
2472
+ if (!webrtcAdapter) {
2473
+ return {
2474
+ localPeerId: options.signaling.peerId,
2475
+ knownPeerIds: [],
2476
+ presentPeerIds: [],
2477
+ peers: []
2478
+ };
2479
+ }
2480
+ return webrtcAdapter.getPeerStateSnapshot();
2481
+ },
2387
2482
  close: async () => {
2388
2483
  signaling.close();
2389
2484
  webrtcAdapter?.disconnect();
@@ -2798,4 +2893,4 @@ export {
2798
2893
  $meshCounter
2799
2894
  };
2800
2895
 
2801
- //# debugId=0A403673455F136164756E2164756E21
2896
+ //# debugId=51457A626E5A2F2B64756E2164756E21