@peers-app/peers-sdk 0.31.5 → 0.31.7

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.
@@ -1,18 +1,32 @@
1
1
  import type { IDeviceConnection, INetworkInfo } from "../types/peer-device";
2
+ /** Local connections and remote topology snapshots used to choose a group's sync edges. */
2
3
  export interface IElectionData {
3
4
  deviceId: string;
4
5
  myConnections: IDeviceConnection[];
5
6
  allNetworkInfo: INetworkInfo[];
6
7
  }
8
+ /**
9
+ * Choose the devices this one syncs with in a group: its preferred devices, plus the devices
10
+ * that prefer it.
11
+ *
12
+ * Once preferences are established, ongoing changes move along these edges in either direction:
13
+ * a write notifies the writer's preferred and preferred-by devices, and periodic resync pulls
14
+ * only from preferred devices. The edges must therefore join every device in the group.
15
+ *
16
+ * @param data - This device, its direct connections, and each connected device's network info.
17
+ * @returns The devices to prefer, and the devices whose network info says they prefer this one.
18
+ */
7
19
  export declare function electDevices({ deviceId, myConnections, allNetworkInfo }: IElectionData): {
8
20
  preferredDeviceIds: string[];
9
21
  preferredByDeviceIds: string[];
10
22
  };
23
+ /** A remote topology snapshot annotated with its local link and incoming preferences. */
11
24
  export interface ISortableConnection extends INetworkInfo {
12
25
  connectedDeviceIds: string[];
13
26
  preferredByDeviceIds: string[];
14
27
  myConnection: IDeviceConnection | undefined;
15
28
  }
29
+ /** Candidate connections and the devices not yet covered by the greedy election pass. */
16
30
  export interface ISortConnectionsArgs {
17
31
  myDeviceId: string;
18
32
  unconnectedDeviceIds: Set<string>;
@@ -3,6 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.electDevices = electDevices;
4
4
  exports.getLeastPreferredConnection = getLeastPreferredConnection;
5
5
  const lodash_1 = require("lodash");
6
+ /**
7
+ * Choose the devices this one syncs with in a group: its preferred devices, plus the devices
8
+ * that prefer it.
9
+ *
10
+ * Once preferences are established, ongoing changes move along these edges in either direction:
11
+ * a write notifies the writer's preferred and preferred-by devices, and periodic resync pulls
12
+ * only from preferred devices. The edges must therefore join every device in the group.
13
+ *
14
+ * @param data - This device, its direct connections, and each connected device's network info.
15
+ * @returns The devices to prefer, and the devices whose network info says they prefer this one.
16
+ */
6
17
  function electDevices({ deviceId, myConnections, allNetworkInfo }) {
7
18
  const allDeviceIdsSet = new Set();
8
19
  const _preferredByDeviceIdsSet = new Set();
@@ -47,11 +58,55 @@ function electDevices({ deviceId, myConnections, allNetworkInfo }) {
47
58
  break;
48
59
  }
49
60
  }
61
+ const preferredDeviceIds = preferredConnections.map((c) => c.deviceId);
62
+ preferredDeviceIds.push(...connectCompleteViews({
63
+ deviceId,
64
+ myConnections,
65
+ preferredDeviceIds,
66
+ connections: allConnectionInfos,
67
+ }));
50
68
  return {
51
- preferredDeviceIds: preferredConnections.map((c) => c.deviceId),
69
+ preferredDeviceIds,
52
70
  preferredByDeviceIds: Array.from(_preferredByDeviceIdsSet),
53
71
  };
54
72
  }
73
+ /**
74
+ * Add stable sync edges when this device has network info for every known device directly.
75
+ *
76
+ * The greedy pass counts a device as covered once a preferred device is connected to it, but
77
+ * ongoing changes move along preference edges. In a full mesh the greedy pass prefers one other,
78
+ * and two devices that pick each other form an island: their writes never reach the rest, and
79
+ * the rest's never reach them.
80
+ *
81
+ * Devices connected to everyone form an id-ordered chain: each prefers its predecessor. The
82
+ * first also prefers every device without a complete view, since those cannot build the chain
83
+ * themselves. In a full mesh this adds at most one preference per device and at most two incident
84
+ * edges per device, rather than concentrating the extra sync traffic on one hub.
85
+ *
86
+ * These edges depend only on topology, never on cached preferences. Bridging only the components
87
+ * in a snapshot is insufficient: concurrent elections can each rely on edges the others remove,
88
+ * repeatedly splitting a full mesh even when just one device adds bridges. Once topology and
89
+ * network-info availability settle, every complete view builds the same chain regardless of
90
+ * election order or stale preferences. Partial views still use only the greedy pass.
91
+ */
92
+ function connectCompleteViews({ deviceId, myConnections, preferredDeviceIds, connections, }) {
93
+ const direct = connections.filter((c) => c.myConnection);
94
+ const visibleIds = new Set(direct.map((c) => c.deviceId));
95
+ const knownIds = new Set([
96
+ deviceId,
97
+ ...myConnections.map((c) => c.deviceId),
98
+ ...connections.flatMap((c) => c.connectedDeviceIds),
99
+ ]);
100
+ if ([...knownIds].some((id) => id !== deviceId && !visibleIds.has(id)))
101
+ return [];
102
+ const seesEveryone = (c) => [...knownIds].every((id) => c.connectedDeviceIds.includes(id));
103
+ const completeIds = [deviceId, ...direct.filter(seesEveryone).map((c) => c.deviceId)].sort();
104
+ const index = completeIds.indexOf(deviceId);
105
+ const backbone = index > 0
106
+ ? [completeIds[index - 1]]
107
+ : direct.filter((c) => !seesEveryone(c)).map((c) => c.deviceId);
108
+ return backbone.filter((id) => !preferredDeviceIds.includes(id));
109
+ }
55
110
  /**
56
111
  * Sort connections from most preferred to least preferred
57
112
  * The most preferred connections are ones that link us to the most new devices
@@ -66,12 +121,9 @@ function sortConnections({ myDeviceId, connections, unconnectedDeviceIds }) {
66
121
  const bLatency = (b.myConnection?.latencyMs || 1) * (b.myConnection?.errorRate || 0.00001);
67
122
  return aLatency - bLatency;
68
123
  });
69
- // sort by preferred by other devices count (higher is better)
70
- connections.sort((a, b) => {
71
- const aPreferredCount = a.preferredDeviceIds.length;
72
- const bPreferredCount = b.preferredDeviceIds.length;
73
- return bPreferredCount - aPreferredCount;
74
- });
124
+ // sort by how many other devices prefer it (higher is better), so devices gather on one
125
+ // another's choices instead of pairing off
126
+ connections.sort((a, b) => b.preferredByDeviceIds.length - a.preferredByDeviceIds.length);
75
127
  // sort by links to new devices - in general, this is most important except for the later edge-cases
76
128
  connections.sort((a, b) => {
77
129
  const linksToDeviceA = a.connectedDeviceIds.filter((id) => unconnectedDeviceIds.has(id)).length;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-sdk",
3
- "version": "0.31.5",
3
+ "version": "0.31.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"