@decentnetwork/peer 0.1.0 → 0.1.1
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/peer.d.ts +31 -0
- package/dist/peer.js +46 -0
- package/package.json +1 -1
package/dist/peer.d.ts
CHANGED
|
@@ -41,5 +41,36 @@ export declare class Peer {
|
|
|
41
41
|
onFriendConnection(cb: (ev: FriendConnectionEvent) => void): void;
|
|
42
42
|
onFriendInfo(cb: (ev: FriendInfoEvent) => void): void;
|
|
43
43
|
friends(): FriendRecord[];
|
|
44
|
+
/**
|
|
45
|
+
* Read-only snapshot of the live net_crypto session state for a
|
|
46
|
+
* friend, or null if no session has been established yet. Lets
|
|
47
|
+
* callers see whether a peer is reachable via direct UDP, only via
|
|
48
|
+
* TCP relay, or not at all — the answer is the difference between
|
|
49
|
+
* ~80ms RTT (UDP) and ~500ms+ RTT (relay) in practice, so the
|
|
50
|
+
* caller (e.g. `agentnet diag`) can show an operator where their
|
|
51
|
+
* latency is coming from. Returns:
|
|
52
|
+
*
|
|
53
|
+
* established — handshake complete on this side
|
|
54
|
+
* udpRemote — direct UDP endpoint we've seen the peer at,
|
|
55
|
+
* if any; null means UDP holepunch hasn't
|
|
56
|
+
* succeeded (yet) so traffic falls back to TCP
|
|
57
|
+
* relay
|
|
58
|
+
* hasTcpRoute — true when a TCP relay has reported a route to
|
|
59
|
+
* the peer
|
|
60
|
+
* transport — convenience derived field: which path the next
|
|
61
|
+
* outbound packet will actually use
|
|
62
|
+
* lastPingRecvMs — for staleness; if older than ~32s the session
|
|
63
|
+
* is on its way to timeout
|
|
64
|
+
*/
|
|
65
|
+
sessionStatus(pubkey: string): {
|
|
66
|
+
established: boolean;
|
|
67
|
+
udpRemote: {
|
|
68
|
+
host: string;
|
|
69
|
+
port: number;
|
|
70
|
+
} | null;
|
|
71
|
+
hasTcpRoute: boolean;
|
|
72
|
+
transport: "udp" | "tcp-relay" | "both" | "none";
|
|
73
|
+
lastPingRecvMs: number | null;
|
|
74
|
+
} | null;
|
|
44
75
|
waitForFriendRequest(timeoutMs?: number): Promise<FriendRequest>;
|
|
45
76
|
}
|
package/dist/peer.js
CHANGED
|
@@ -747,6 +747,52 @@ export class Peer {
|
|
|
747
747
|
friends() {
|
|
748
748
|
return [...this.#friends.values()];
|
|
749
749
|
}
|
|
750
|
+
/**
|
|
751
|
+
* Read-only snapshot of the live net_crypto session state for a
|
|
752
|
+
* friend, or null if no session has been established yet. Lets
|
|
753
|
+
* callers see whether a peer is reachable via direct UDP, only via
|
|
754
|
+
* TCP relay, or not at all — the answer is the difference between
|
|
755
|
+
* ~80ms RTT (UDP) and ~500ms+ RTT (relay) in practice, so the
|
|
756
|
+
* caller (e.g. `agentnet diag`) can show an operator where their
|
|
757
|
+
* latency is coming from. Returns:
|
|
758
|
+
*
|
|
759
|
+
* established — handshake complete on this side
|
|
760
|
+
* udpRemote — direct UDP endpoint we've seen the peer at,
|
|
761
|
+
* if any; null means UDP holepunch hasn't
|
|
762
|
+
* succeeded (yet) so traffic falls back to TCP
|
|
763
|
+
* relay
|
|
764
|
+
* hasTcpRoute — true when a TCP relay has reported a route to
|
|
765
|
+
* the peer
|
|
766
|
+
* transport — convenience derived field: which path the next
|
|
767
|
+
* outbound packet will actually use
|
|
768
|
+
* lastPingRecvMs — for staleness; if older than ~32s the session
|
|
769
|
+
* is on its way to timeout
|
|
770
|
+
*/
|
|
771
|
+
sessionStatus(pubkey) {
|
|
772
|
+
const s = this.#friendSessions.get(pubkey);
|
|
773
|
+
if (!s)
|
|
774
|
+
return null;
|
|
775
|
+
// session.remote can be a synthetic `tcp:<dhtpk>:0` placeholder
|
|
776
|
+
// when the TCP relay path reports an endpoint with no real UDP
|
|
777
|
+
// address; treat those as no-UDP for reporting.
|
|
778
|
+
const realUdp = s.remote && !s.remote.host?.startsWith("tcp:") && s.remote.port !== 0
|
|
779
|
+
? { host: s.remote.host, port: s.remote.port }
|
|
780
|
+
: null;
|
|
781
|
+
const transport = realUdp
|
|
782
|
+
? s.hasTcpRoute
|
|
783
|
+
? "both"
|
|
784
|
+
: "udp"
|
|
785
|
+
: s.hasTcpRoute
|
|
786
|
+
? "tcp-relay"
|
|
787
|
+
: "none";
|
|
788
|
+
return {
|
|
789
|
+
established: s.established === true,
|
|
790
|
+
udpRemote: realUdp,
|
|
791
|
+
hasTcpRoute: s.hasTcpRoute === true,
|
|
792
|
+
transport,
|
|
793
|
+
lastPingRecvMs: s.lastPingRecvMs ?? null,
|
|
794
|
+
};
|
|
795
|
+
}
|
|
750
796
|
waitForFriendRequest(timeoutMs = 30000) {
|
|
751
797
|
return new Promise((resolve, reject) => {
|
|
752
798
|
const timer = setTimeout(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decentnetwork/peer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Pure TypeScript port of Elastos Carrier (toxcore-derived) P2P messaging. DHT, onion routing, TCP relay, FlatBuffers app payloads, Express offline relay. Wire-compatible with iOS Beagle and the Carrier C SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|