@antseed/node 0.2.128 → 0.2.129

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.
@@ -0,0 +1,14 @@
1
+ import type { PeerId, PeerInfo } from "../types/peer.js";
2
+ import type { ConnectionManager, PeerConnection } from "./connection-manager.js";
3
+ export declare function parsePeerAddress(address: string): {
4
+ host: string;
5
+ port: number;
6
+ };
7
+ /**
8
+ * Reuse the live outbound connection to `peer` on `connectionManager`, or
9
+ * open a new one. A connection whose peer moved to a new endpoint (e.g. IP
10
+ * rotation) is closed and replaced. `onOpened` runs once for each newly
11
+ * opened connection so the caller can wire its frame handling.
12
+ */
13
+ export declare function getOrOpenOutboundConnection(connectionManager: ConnectionManager, peer: PeerInfo, peerCapabilities: Map<PeerId, Set<string>>, onOpened: (conn: PeerConnection) => void): Promise<PeerConnection>;
14
+ //# sourceMappingURL=outbound-connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outbound-connection.d.ts","sourceRoot":"","sources":["../../src/p2p/outbound-connection.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEjF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAGhF;AA2BD;;;;;GAKG;AACH,wBAAsB,2BAA2B,CAC/C,iBAAiB,EAAE,iBAAiB,EACpC,IAAI,EAAE,QAAQ,EACd,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,EAC1C,QAAQ,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,GACvC,OAAO,CAAC,cAAc,CAAC,CAuDzB"}
@@ -0,0 +1,88 @@
1
+ import { ConnectionState } from "../types/connection.js";
2
+ import { debugLog, debugWarn } from "../utils/debug.js";
3
+ export function parsePeerAddress(address) {
4
+ const parts = address.split(":");
5
+ return { host: parts[0], port: parseInt(parts[1] ?? "6882", 10) };
6
+ }
7
+ function waitForOpen(conn, peerId) {
8
+ return new Promise((resolve, reject) => {
9
+ const onState = (state) => {
10
+ debugLog(`[Node] Connection state: ${state}`);
11
+ if (state === ConnectionState.Open || state === ConnectionState.Authenticated) {
12
+ conn.off("stateChange", onState);
13
+ resolve();
14
+ }
15
+ else if (state === ConnectionState.Failed || state === ConnectionState.Closed) {
16
+ conn.off("stateChange", onState);
17
+ reject(new Error(`Connection to ${peerId} failed`));
18
+ }
19
+ };
20
+ conn.on("stateChange", onState);
21
+ });
22
+ }
23
+ /**
24
+ * The endpoint each outbound connection was opened against. Kept per
25
+ * connection rather than read from ConnectionManager's static endpoint map,
26
+ * which every buyer identity shares: once one identity reconnects to a moved
27
+ * peer, the shared map already holds the new endpoint and would hide the move
28
+ * from the other identities' stale connections.
29
+ */
30
+ const connectionEndpoints = new WeakMap();
31
+ /**
32
+ * Reuse the live outbound connection to `peer` on `connectionManager`, or
33
+ * open a new one. A connection whose peer moved to a new endpoint (e.g. IP
34
+ * rotation) is closed and replaced. `onOpened` runs once for each newly
35
+ * opened connection so the caller can wire its frame handling.
36
+ */
37
+ export async function getOrOpenOutboundConnection(connectionManager, peer, peerCapabilities, onOpened) {
38
+ const existing = connectionManager.getConnection(peer.peerId);
39
+ const capabilities = new Set(peer.capabilities ?? peer.metadata?.capabilities ?? []);
40
+ peerCapabilities.set(peer.peerId, capabilities);
41
+ let endpointChanged = false;
42
+ // Only outbound connections have a registered endpoint; inbound ones are
43
+ // not subject to pinned-peer routing.
44
+ if (existing && peer.publicAddress) {
45
+ const currentEndpoint = connectionEndpoints.get(existing);
46
+ const { host: newHost, port: newPort } = parsePeerAddress(peer.publicAddress);
47
+ if (currentEndpoint && (currentEndpoint.host !== newHost || currentEndpoint.port !== newPort)) {
48
+ debugLog(`[Node] Peer ${peer.peerId.slice(0, 12)}... endpoint changed from ${currentEndpoint.host}:${currentEndpoint.port} to ${newHost}:${newPort}, reconnecting`);
49
+ existing.close();
50
+ peerCapabilities.set(peer.peerId, capabilities);
51
+ endpointChanged = true;
52
+ }
53
+ }
54
+ if (existing && !endpointChanged &&
55
+ existing.state !== ConnectionState.Closed &&
56
+ existing.state !== ConnectionState.Failed) {
57
+ debugLog(`[Node] Reusing existing connection to ${peer.peerId.slice(0, 12)}... (state=${existing.state})`);
58
+ if (existing.state === ConnectionState.Connecting) {
59
+ debugLog(`[Node] Waiting for connection to open...`);
60
+ await waitForOpen(existing, peer.peerId);
61
+ }
62
+ return existing;
63
+ }
64
+ let endpoint;
65
+ if (peer.publicAddress) {
66
+ const { host, port } = parsePeerAddress(peer.publicAddress);
67
+ endpoint = { host, port };
68
+ connectionManager.registerPeerEndpoint(peer.peerId, endpoint);
69
+ debugLog(`[Node] Connecting to ${peer.peerId.slice(0, 12)}... at ${host}:${port}`);
70
+ }
71
+ else {
72
+ debugWarn(`[Node] Peer ${peer.peerId.slice(0, 12)}... has no public address`);
73
+ }
74
+ const connConfig = {
75
+ remotePeerId: peer.peerId,
76
+ isInitiator: true,
77
+ remoteCapabilities: [...capabilities],
78
+ };
79
+ const conn = connectionManager.createConnection(connConfig);
80
+ if (endpoint)
81
+ connectionEndpoints.set(conn, endpoint);
82
+ await waitForOpen(conn, peer.peerId);
83
+ debugLog(`[Node] Connected to ${peer.peerId.slice(0, 12)}... via ${conn.transportDescription}`);
84
+ peerCapabilities.set(peer.peerId, capabilities);
85
+ onOpened(conn);
86
+ return conn;
87
+ }
88
+ //# sourceMappingURL=outbound-connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outbound-connection.js","sourceRoot":"","sources":["../../src/p2p/outbound-connection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,QAA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AAI,CAAC,MAAM,CAAC,CAAC;IAErC,QAAQ,CAAC,uBAAuB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,IAAI,CAAC,oBAAoB,EAAE,CAAC,CAAC;IAChG,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAChD,QAAQ,CAAC,IAAI,CAAC,CAAC;IACf,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antseed/node",
3
- "version": "0.2.128",
3
+ "version": "0.2.129",
4
4
  "description": "Antseed Network protocol SDK — P2P inference marketplace",
5
5
  "type": "module",
6
6
  "files": [
@@ -69,8 +69,8 @@
69
69
  "ethers": "~6.16.0",
70
70
  "node-datachannel": "^0.7.0",
71
71
  "tokenx": "^1.3.0",
72
- "@antseed/buyer-core": "0.1.12",
73
- "@antseed/api-adapter": "0.1.48",
72
+ "@antseed/api-adapter": "0.1.49",
73
+ "@antseed/buyer-core": "0.1.13",
74
74
  "@antseed/protocol": "0.1.5"
75
75
  },
76
76
  "optionalDependencies": {