@decentnetwork/peer 0.1.32 → 0.1.34
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/index.d.ts +1 -1
- package/dist/peer.d.ts +17 -1
- package/dist/peer.js +47 -1
- package/dist/types/peer.d.ts +27 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export { LegacyProtocolNotImplementedError } from "./runtime/errors.js";
|
|
|
7
7
|
export type { CarrierPacket, FriendMessagePacket, FriendRequestPacket } from "./compat/packet.js";
|
|
8
8
|
export type { ToxDhtCryptoRequest } from "./compat/tox-dht-crypto.js";
|
|
9
9
|
export type { CarrierAddressParts } from "./compat/address.js";
|
|
10
|
-
export type { CompatibilityMode, FriendConnectionEvent, FriendConnectionStatus, FriendInfoEvent, FriendRequest, LookupResult, NetworkNode, PeerOptions, TextMessage } from "./types/peer.js";
|
|
10
|
+
export type { CompatibilityMode, CustomPacketEvent, FriendConnectionEvent, FriendConnectionStatus, FriendInfoEvent, FriendRequest, LookupResult, NetworkNode, PeerOptions, TextMessage } from "./types/peer.js";
|
package/dist/peer.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type BootstrapResult } from "./compat/bootstrap.js";
|
|
2
2
|
import type { FriendRecord } from "./store/friends.js";
|
|
3
|
-
import type { FriendConnectionEvent, FriendRequest, FriendInfoEvent, LookupResult, NetworkNode, PeerOptions, TextMessage } from "./types/peer.js";
|
|
3
|
+
import type { CustomPacketEvent, FriendConnectionEvent, FriendRequest, FriendInfoEvent, LookupResult, NetworkNode, PeerOptions, TextMessage } from "./types/peer.js";
|
|
4
4
|
export declare class Peer {
|
|
5
5
|
#private;
|
|
6
6
|
private constructor();
|
|
@@ -74,6 +74,22 @@ export declare class Peer {
|
|
|
74
74
|
waitForFriendConnected(pubkey: string, timeoutMs?: number): Promise<boolean>;
|
|
75
75
|
onFriendRequest(cb: (req: FriendRequest) => void): void;
|
|
76
76
|
onText(cb: (msg: TextMessage) => void): void;
|
|
77
|
+
/**
|
|
78
|
+
* Send an application-defined custom packet to a friend. `id` selects the
|
|
79
|
+
* channel and its delivery class by toxcore range: **160–191 lossless**
|
|
80
|
+
* (reliable, ordered) or **192–254 lossy** (best-effort). Apps layered on
|
|
81
|
+
* the peer use this to run their own protocols alongside chat without
|
|
82
|
+
* collision — e.g. decentlan sends IP traffic on `192`. Throws if `pubkey`
|
|
83
|
+
* is not a friend or the session has no transport (fail-fast; callers that
|
|
84
|
+
* want offline delivery should use a message instead).
|
|
85
|
+
*/
|
|
86
|
+
sendCustomPacket(pubkey: string, id: number, data: Uint8Array): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Receive application custom packets (toxcore lossless 160–191 / lossy
|
|
89
|
+
* 192–254). SDK-internal IDs are never delivered here. See
|
|
90
|
+
* {@link sendCustomPacket}.
|
|
91
|
+
*/
|
|
92
|
+
onCustomPacket(cb: (evt: CustomPacketEvent) => void): void;
|
|
77
93
|
onFriendConnection(cb: (ev: FriendConnectionEvent) => void): void;
|
|
78
94
|
onFriendInfo(cb: (ev: FriendInfoEvent) => void): void;
|
|
79
95
|
friends(): FriendRecord[];
|
package/dist/peer.js
CHANGED
|
@@ -891,6 +891,32 @@ export class Peer {
|
|
|
891
891
|
onText(cb) {
|
|
892
892
|
this.#events.on("text", cb);
|
|
893
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* Send an application-defined custom packet to a friend. `id` selects the
|
|
896
|
+
* channel and its delivery class by toxcore range: **160–191 lossless**
|
|
897
|
+
* (reliable, ordered) or **192–254 lossy** (best-effort). Apps layered on
|
|
898
|
+
* the peer use this to run their own protocols alongside chat without
|
|
899
|
+
* collision — e.g. decentlan sends IP traffic on `192`. Throws if `pubkey`
|
|
900
|
+
* is not a friend or the session has no transport (fail-fast; callers that
|
|
901
|
+
* want offline delivery should use a message instead).
|
|
902
|
+
*/
|
|
903
|
+
async sendCustomPacket(pubkey, id, data) {
|
|
904
|
+
if (!Number.isInteger(id) || id < 160 || id > 254) {
|
|
905
|
+
throw new Error(`custom packet id must be 160-254 (lossless 160-191 / lossy 192-254), got ${id}`);
|
|
906
|
+
}
|
|
907
|
+
if (!this.#friends.get(pubkey)) {
|
|
908
|
+
throw new Error(`Not a friend: ${pubkey}`);
|
|
909
|
+
}
|
|
910
|
+
await this.#sendMessengerPacket(pubkey, id, data);
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Receive application custom packets (toxcore lossless 160–191 / lossy
|
|
914
|
+
* 192–254). SDK-internal IDs are never delivered here. See
|
|
915
|
+
* {@link sendCustomPacket}.
|
|
916
|
+
*/
|
|
917
|
+
onCustomPacket(cb) {
|
|
918
|
+
this.#events.on("customPacket", cb);
|
|
919
|
+
}
|
|
894
920
|
onFriendConnection(cb) {
|
|
895
921
|
this.#events.on("friendConnection", cb);
|
|
896
922
|
}
|
|
@@ -1600,6 +1626,21 @@ export class Peer {
|
|
|
1600
1626
|
this.#debugLog(`crypto ${kind === PACKET_ID_ACTION ? "action" : "message"} from ${friendId}: "${text}"`);
|
|
1601
1627
|
return;
|
|
1602
1628
|
}
|
|
1629
|
+
if (kind >= 160 && kind <= 254) {
|
|
1630
|
+
// Application custom packet (toxcore lossless 160-191 / lossy
|
|
1631
|
+
// 192-254). SDK-reserved IDs (160 UDP_ENDPOINT, 254 NAT_PING, …)
|
|
1632
|
+
// are handled by earlier branches and never reach here; anything
|
|
1633
|
+
// else belongs to the application layer (e.g. decentlan IP traffic
|
|
1634
|
+
// on 192, control on 161/162). Receiving one proves the friend is
|
|
1635
|
+
// reachable, so mark online.
|
|
1636
|
+
this.#setFriendOnline(friendId, remote.address, remote.port);
|
|
1637
|
+
this.#events.emit("customPacket", {
|
|
1638
|
+
pubkey: friendId,
|
|
1639
|
+
id: kind,
|
|
1640
|
+
data: inner
|
|
1641
|
+
});
|
|
1642
|
+
return;
|
|
1643
|
+
}
|
|
1603
1644
|
this.#debugVerboseLog(`unknown messenger packet kind ${kind} from ${friendId}`);
|
|
1604
1645
|
return;
|
|
1605
1646
|
}
|
|
@@ -2630,7 +2671,12 @@ export class Peer {
|
|
|
2630
2671
|
// re-punch survive a UDP outage. Brief UDP gaps just drop a few data
|
|
2631
2672
|
// packets (the proxied TCP stream retransmits) and the re-punch loop
|
|
2632
2673
|
// restores the direct path within a few seconds.
|
|
2633
|
-
|
|
2674
|
+
// Native chat (64) is always bulk; an app can register one extra custom
|
|
2675
|
+
// id (decentlan's IP channel = 163) via bulkDataPacketId so its
|
|
2676
|
+
// high-volume traffic gets the same single-transport routing instead of
|
|
2677
|
+
// fanning out over UDP + relay + TCP relay (which delivers 3-4 duplicates).
|
|
2678
|
+
const isBulkData = kind === PACKET_ID_MESSAGE ||
|
|
2679
|
+
(this.#opts.bulkDataPacketId !== undefined && kind === this.#opts.bulkDataPacketId);
|
|
2634
2680
|
await this.#sendToFriend(friendId, encrypted, session, isBulkData);
|
|
2635
2681
|
}
|
|
2636
2682
|
/**
|
package/dist/types/peer.d.ts
CHANGED
|
@@ -28,6 +28,18 @@ export type PeerOptions = {
|
|
|
28
28
|
* apps that genuinely want offline message delivery leave it false.
|
|
29
29
|
*/
|
|
30
30
|
expressControlPlaneOnly?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Custom packet id carrying the app's high-volume "bulk data" stream
|
|
33
|
+
* (e.g. decentlan's IP-forwarding channel). Packets sent on this id get
|
|
34
|
+
* the same single-transport routing as native chat (PACKET_ID_MESSAGE):
|
|
35
|
+
* they ride the fresh direct UDP path exclusively, or bridge over the
|
|
36
|
+
* relay when it's stale — instead of fanning out over UDP + relay + TCP
|
|
37
|
+
* relay at once (which delivers 3-4 duplicates of every packet and backs
|
|
38
|
+
* up the relay). Apps that put bulk traffic on a custom id (decentlan
|
|
39
|
+
* uses 163) MUST set this so the SDK recognises it; otherwise the bulk
|
|
40
|
+
* optimisation only applies to packet 64 and custom-id data is duplicated.
|
|
41
|
+
*/
|
|
42
|
+
bulkDataPacketId?: number;
|
|
31
43
|
compatibilityMode?: CompatibilityMode;
|
|
32
44
|
debugLabel?: string;
|
|
33
45
|
};
|
|
@@ -55,6 +67,21 @@ export type TextMessage = {
|
|
|
55
67
|
pubkey: string;
|
|
56
68
|
text: string;
|
|
57
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* An application-defined custom packet received from a friend, over the
|
|
72
|
+
* toxcore custom packet ranges: lossless `160–191` (reliable, ordered) or
|
|
73
|
+
* lossy `192–254` (best-effort). Apps layered on the peer (e.g. decentlan's
|
|
74
|
+
* IP traffic on `192`) use these to multiplex their own channels without
|
|
75
|
+
* colliding with chat (`PACKET_ID_MESSAGE = 64`). Packet IDs reserved by the
|
|
76
|
+
* SDK itself (e.g. `160` UDP-endpoint, `254` NAT-ping) are handled internally
|
|
77
|
+
* and never surface here.
|
|
78
|
+
*/
|
|
79
|
+
export type CustomPacketEvent = {
|
|
80
|
+
pubkey: string;
|
|
81
|
+
/** toxcore packet ID: 160–191 lossless, 192–254 lossy. */
|
|
82
|
+
id: number;
|
|
83
|
+
data: Uint8Array;
|
|
84
|
+
};
|
|
58
85
|
export type LookupResult = {
|
|
59
86
|
pubkey: string;
|
|
60
87
|
status: "unknown";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decentnetwork/peer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.34",
|
|
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",
|