@libp2p/kad-dht 0.28.6
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/LICENSE +4 -0
- package/README.md +105 -0
- package/dist/src/constants.d.ts +20 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +34 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/content-fetching/index.d.ts +55 -0
- package/dist/src/content-fetching/index.d.ts.map +1 -0
- package/dist/src/content-fetching/index.js +190 -0
- package/dist/src/content-fetching/index.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +42 -0
- package/dist/src/content-routing/index.d.ts.map +1 -0
- package/dist/src/content-routing/index.js +129 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/dual-kad-dht.d.ts +65 -0
- package/dist/src/dual-kad-dht.d.ts.map +1 -0
- package/dist/src/dual-kad-dht.js +191 -0
- package/dist/src/dual-kad-dht.js.map +1 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +15 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/kad-dht.d.ts +131 -0
- package/dist/src/kad-dht.d.ts.map +1 -0
- package/dist/src/kad-dht.js +268 -0
- package/dist/src/kad-dht.js.map +1 -0
- package/dist/src/message/dht.d.ts +297 -0
- package/dist/src/message/dht.js +921 -0
- package/dist/src/message/index.d.ts +32 -0
- package/dist/src/message/index.d.ts.map +1 -0
- package/dist/src/message/index.js +81 -0
- package/dist/src/message/index.js.map +1 -0
- package/dist/src/network.d.ts +60 -0
- package/dist/src/network.d.ts.map +1 -0
- package/dist/src/network.js +124 -0
- package/dist/src/network.js.map +1 -0
- package/dist/src/peer-list/index.d.ts +29 -0
- package/dist/src/peer-list/index.d.ts.map +1 -0
- package/dist/src/peer-list/index.js +44 -0
- package/dist/src/peer-list/index.js.map +1 -0
- package/dist/src/peer-list/peer-distance-list.d.ts +34 -0
- package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -0
- package/dist/src/peer-list/peer-distance-list.js +64 -0
- package/dist/src/peer-list/peer-distance-list.js.map +1 -0
- package/dist/src/peer-routing/index.d.ts +71 -0
- package/dist/src/peer-routing/index.d.ts.map +1 -0
- package/dist/src/peer-routing/index.js +256 -0
- package/dist/src/peer-routing/index.js.map +1 -0
- package/dist/src/providers.d.ts +64 -0
- package/dist/src/providers.d.ts.map +1 -0
- package/dist/src/providers.js +208 -0
- package/dist/src/providers.js.map +1 -0
- package/dist/src/query/events.d.ts +46 -0
- package/dist/src/query/events.d.ts.map +1 -0
- package/dist/src/query/events.js +73 -0
- package/dist/src/query/events.js.map +1 -0
- package/dist/src/query/manager.d.ts +40 -0
- package/dist/src/query/manager.d.ts.map +1 -0
- package/dist/src/query/manager.js +140 -0
- package/dist/src/query/manager.js.map +1 -0
- package/dist/src/query/query-path.d.ts +58 -0
- package/dist/src/query/query-path.d.ts.map +1 -0
- package/dist/src/query/query-path.js +171 -0
- package/dist/src/query/query-path.js.map +1 -0
- package/dist/src/query/types.d.ts +16 -0
- package/dist/src/query/types.d.ts.map +1 -0
- package/dist/src/query/types.js +2 -0
- package/dist/src/query/types.js.map +1 -0
- package/dist/src/query-self.d.ts +31 -0
- package/dist/src/query-self.d.ts.map +1 -0
- package/dist/src/query-self.js +73 -0
- package/dist/src/query-self.js.map +1 -0
- package/dist/src/routing-table/generated-prefix-list-browser.d.ts +3 -0
- package/dist/src/routing-table/generated-prefix-list-browser.d.ts.map +1 -0
- package/dist/src/routing-table/generated-prefix-list-browser.js +1027 -0
- package/dist/src/routing-table/generated-prefix-list-browser.js.map +1 -0
- package/dist/src/routing-table/generated-prefix-list.d.ts +3 -0
- package/dist/src/routing-table/generated-prefix-list.d.ts.map +1 -0
- package/dist/src/routing-table/generated-prefix-list.js +4099 -0
- package/dist/src/routing-table/generated-prefix-list.js.map +1 -0
- package/dist/src/routing-table/index.d.ts +91 -0
- package/dist/src/routing-table/index.d.ts.map +1 -0
- package/dist/src/routing-table/index.js +183 -0
- package/dist/src/routing-table/index.js.map +1 -0
- package/dist/src/routing-table/refresh.d.ts +50 -0
- package/dist/src/routing-table/refresh.d.ts.map +1 -0
- package/dist/src/routing-table/refresh.js +204 -0
- package/dist/src/routing-table/refresh.js.map +1 -0
- package/dist/src/routing-table/types.d.ts +24 -0
- package/dist/src/routing-table/types.d.ts.map +1 -0
- package/dist/src/rpc/handlers/add-provider.d.ts +13 -0
- package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -0
- package/dist/src/rpc/handlers/add-provider.js +42 -0
- package/dist/src/rpc/handlers/add-provider.js.map +1 -0
- package/dist/src/rpc/handlers/find-node.d.ts +18 -0
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -0
- package/dist/src/rpc/handlers/find-node.js +32 -0
- package/dist/src/rpc/handlers/find-node.js.map +1 -0
- package/dist/src/rpc/handlers/get-providers.d.ts +24 -0
- package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -0
- package/dist/src/rpc/handlers/get-providers.js +60 -0
- package/dist/src/rpc/handlers/get-providers.js.map +1 -0
- package/dist/src/rpc/handlers/get-value.d.ts +27 -0
- package/dist/src/rpc/handlers/get-value.d.ts.map +1 -0
- package/dist/src/rpc/handlers/get-value.js +94 -0
- package/dist/src/rpc/handlers/get-value.js.map +1 -0
- package/dist/src/rpc/handlers/index.d.ts +13 -0
- package/dist/src/rpc/handlers/index.d.ts.map +1 -0
- package/dist/src/rpc/handlers/ping.d.ts +7 -0
- package/dist/src/rpc/handlers/ping.d.ts.map +1 -0
- package/dist/src/rpc/handlers/ping.js +9 -0
- package/dist/src/rpc/handlers/ping.js.map +1 -0
- package/dist/src/rpc/handlers/put-value.d.ts +18 -0
- package/dist/src/rpc/handlers/put-value.d.ts.map +1 -0
- package/dist/src/rpc/handlers/put-value.js +35 -0
- package/dist/src/rpc/handlers/put-value.js.map +1 -0
- package/dist/src/rpc/index.d.ts +38 -0
- package/dist/src/rpc/index.d.ts.map +1 -0
- package/dist/src/rpc/index.js +75 -0
- package/dist/src/rpc/index.js.map +1 -0
- package/dist/src/rpc/types.d.ts +6 -0
- package/dist/src/rpc/types.d.ts.map +1 -0
- package/dist/src/topology-listener.d.ts +33 -0
- package/dist/src/topology-listener.d.ts.map +1 -0
- package/dist/src/topology-listener.js +50 -0
- package/dist/src/topology-listener.js.map +1 -0
- package/dist/src/types.d.ts +143 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils.d.ts +33 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +89 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +200 -0
- package/src/constants.ts +50 -0
- package/src/content-fetching/index.ts +276 -0
- package/src/content-routing/index.ts +202 -0
- package/src/dual-kad-dht.ts +257 -0
- package/src/index.ts +21 -0
- package/src/kad-dht.ts +396 -0
- package/src/message/dht.d.ts +297 -0
- package/src/message/dht.js +921 -0
- package/src/message/dht.proto +75 -0
- package/src/message/index.ts +111 -0
- package/src/network.ts +185 -0
- package/src/peer-list/index.ts +54 -0
- package/src/peer-list/peer-distance-list.ts +93 -0
- package/src/peer-routing/index.ts +332 -0
- package/src/providers.ts +278 -0
- package/src/query/events.ts +126 -0
- package/src/query/manager.ts +188 -0
- package/src/query/query-path.ts +263 -0
- package/src/query/types.ts +22 -0
- package/src/query-self.ts +106 -0
- package/src/routing-table/generated-prefix-list-browser.ts +1026 -0
- package/src/routing-table/generated-prefix-list.ts +4098 -0
- package/src/routing-table/index.ts +265 -0
- package/src/routing-table/refresh.ts +263 -0
- package/src/rpc/handlers/add-provider.ts +63 -0
- package/src/rpc/handlers/find-node.ts +57 -0
- package/src/rpc/handlers/get-providers.ts +95 -0
- package/src/rpc/handlers/get-value.ts +130 -0
- package/src/rpc/handlers/ping.ts +13 -0
- package/src/rpc/handlers/put-value.ts +58 -0
- package/src/rpc/index.ts +118 -0
- package/src/topology-listener.ts +78 -0
- package/src/utils.ts +108 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Libp2pRecord } from '@libp2p/record';
|
|
2
|
+
import Proto from './dht.js';
|
|
3
|
+
import type { PeerData } from '@libp2p/interfaces/peer-data';
|
|
4
|
+
export declare const MESSAGE_TYPE: typeof Proto.Message.MessageType;
|
|
5
|
+
export declare const CONNECTION_TYPE: typeof Proto.Message.ConnectionType;
|
|
6
|
+
export declare const MESSAGE_TYPE_LOOKUP: string[];
|
|
7
|
+
/**
|
|
8
|
+
* Represents a single DHT control message.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Message {
|
|
11
|
+
type: Proto.Message.MessageType;
|
|
12
|
+
key: Uint8Array;
|
|
13
|
+
private clusterLevelRaw;
|
|
14
|
+
closerPeers: PeerData[];
|
|
15
|
+
providerPeers: PeerData[];
|
|
16
|
+
record?: Libp2pRecord;
|
|
17
|
+
constructor(type: Proto.Message.MessageType, key: Uint8Array, level: number);
|
|
18
|
+
/**
|
|
19
|
+
* @type {number}
|
|
20
|
+
*/
|
|
21
|
+
get clusterLevel(): number;
|
|
22
|
+
set clusterLevel(level: number);
|
|
23
|
+
/**
|
|
24
|
+
* Encode into protobuf
|
|
25
|
+
*/
|
|
26
|
+
serialize(): Uint8Array;
|
|
27
|
+
/**
|
|
28
|
+
* Decode from protobuf
|
|
29
|
+
*/
|
|
30
|
+
static deserialize(raw: Uint8Array): Message;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/message/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAE5D,eAAO,MAAM,YAAY,kCAA4B,CAAA;AACrD,eAAO,MAAM,eAAe,qCAA+B,CAAA;AAC3D,eAAO,MAAM,mBAAmB,UAA4B,CAAA;AAU5D;;GAEG;AACH,qBAAa,OAAO;IACX,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAA;IAC/B,GAAG,EAAE,UAAU,CAAA;IACtB,OAAO,CAAC,eAAe,CAAQ;IACxB,WAAW,EAAE,QAAQ,EAAE,CAAA;IACvB,aAAa,EAAE,QAAQ,EAAE,CAAA;IACzB,MAAM,CAAC,EAAE,YAAY,CAAA;gBAEf,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM;IAa5E;;OAEG;IACH,IAAI,YAAY,WAOf;IAED,IAAI,YAAY,CAAE,KAAK,QAAA,EAEtB;IAED;;OAEG;IACH,SAAS;IAWT;;OAEG;IACH,MAAM,CAAC,WAAW,CAAE,GAAG,EAAE,UAAU;CAapC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
2
|
+
import { Multiaddr } from '@multiformats/multiaddr';
|
|
3
|
+
import { Libp2pRecord } from '@libp2p/record';
|
|
4
|
+
import Proto from './dht.js';
|
|
5
|
+
export const MESSAGE_TYPE = Proto.Message.MessageType;
|
|
6
|
+
export const CONNECTION_TYPE = Proto.Message.ConnectionType;
|
|
7
|
+
export const MESSAGE_TYPE_LOOKUP = Object.keys(MESSAGE_TYPE);
|
|
8
|
+
/**
|
|
9
|
+
* Represents a single DHT control message.
|
|
10
|
+
*/
|
|
11
|
+
export class Message {
|
|
12
|
+
constructor(type, key, level) {
|
|
13
|
+
if (!(key instanceof Uint8Array)) {
|
|
14
|
+
throw new Error('Key must be a Uint8Array');
|
|
15
|
+
}
|
|
16
|
+
this.type = type;
|
|
17
|
+
this.key = key;
|
|
18
|
+
this.clusterLevelRaw = level;
|
|
19
|
+
this.closerPeers = [];
|
|
20
|
+
this.providerPeers = [];
|
|
21
|
+
this.record = undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @type {number}
|
|
25
|
+
*/
|
|
26
|
+
get clusterLevel() {
|
|
27
|
+
const level = this.clusterLevelRaw - 1;
|
|
28
|
+
if (level < 0) {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
return level;
|
|
32
|
+
}
|
|
33
|
+
set clusterLevel(level) {
|
|
34
|
+
this.clusterLevelRaw = level;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Encode into protobuf
|
|
38
|
+
*/
|
|
39
|
+
serialize() {
|
|
40
|
+
return Proto.Message.encode({
|
|
41
|
+
key: this.key,
|
|
42
|
+
type: this.type,
|
|
43
|
+
clusterLevelRaw: this.clusterLevelRaw,
|
|
44
|
+
closerPeers: this.closerPeers.map(toPbPeer),
|
|
45
|
+
providerPeers: this.providerPeers.map(toPbPeer),
|
|
46
|
+
record: this.record == null ? undefined : this.record.serialize()
|
|
47
|
+
}).finish();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Decode from protobuf
|
|
51
|
+
*/
|
|
52
|
+
static deserialize(raw) {
|
|
53
|
+
const dec = Proto.Message.decode(raw);
|
|
54
|
+
const msg = new Message(dec.type ?? 0, dec.key ?? Uint8Array.from([]), dec.clusterLevelRaw ?? 0);
|
|
55
|
+
msg.closerPeers = dec.closerPeers.map(fromPbPeer);
|
|
56
|
+
msg.providerPeers = dec.providerPeers.map(fromPbPeer);
|
|
57
|
+
if (dec.record?.length != null) {
|
|
58
|
+
msg.record = Libp2pRecord.deserialize(dec.record);
|
|
59
|
+
}
|
|
60
|
+
return msg;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function toPbPeer(peer) {
|
|
64
|
+
const output = {
|
|
65
|
+
id: peer.id.toBytes(),
|
|
66
|
+
addrs: (peer.multiaddrs ?? []).map((m) => m.bytes),
|
|
67
|
+
connection: CONNECTION_TYPE.CONNECTED
|
|
68
|
+
};
|
|
69
|
+
return output;
|
|
70
|
+
}
|
|
71
|
+
function fromPbPeer(peer) {
|
|
72
|
+
if (peer.id == null) {
|
|
73
|
+
throw new Error('Invalid peer in message');
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
id: peerIdFromBytes(peer.id),
|
|
77
|
+
multiaddrs: (peer.addrs ?? []).map((a) => new Multiaddr(a)),
|
|
78
|
+
protocols: []
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/message/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,MAAM,UAAU,CAAA;AAG5B,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,CAAA;AACrD,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC,OAAO,CAAC,cAAc,CAAA;AAC3D,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;AAU5D;;GAEG;AACH,MAAM,OAAO,OAAO;IAQlB,YAAa,IAA+B,EAAE,GAAe,EAAE,KAAa;QAC1E,IAAI,CAAC,CAAC,GAAG,YAAY,UAAU,CAAC,EAAE;YAChC,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;SAC5C;QAED,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,eAAe,GAAG,KAAK,CAAA;QAC5B,IAAI,CAAC,WAAW,GAAG,EAAE,CAAA;QACrB,IAAI,CAAC,aAAa,GAAG,EAAE,CAAA;QACvB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;IACzB,CA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C,CAAA;QAErD,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,IAAI,IAAI,EAAE;YAC9B,GAAG,CAAC,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;SAClD;QAED,OAAO,GAAG,CAAA;IACZ,CAAC;CACF;AAED,SAAS,QAAQ,CAAE,IAAc;IAC/B,MAAM,MAAM,GAAW;QACrB,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE;QACrB,KAAK,EAAE,CAAC,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;QAClD,UAAU,EAAE,eAAe,CAAC,SAAS;KACtC,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED,SAAS,UAAU,CAAE,IAAyB;IAC5C,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI,EAAE;QACnB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;KAC3C;IAED,OAAO;QACL,EAAE,EAAE,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5B,UAAU,EAAE,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC;QAC3D,SAAS,EAAE,EAAE;KACd,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Message } from './message/index.js';
|
|
2
|
+
import { EventEmitter } from '@libp2p/interfaces';
|
|
3
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
4
|
+
import type { AbortOptions, Dialer, Startable } from '@libp2p/interfaces';
|
|
5
|
+
import type { Duplex } from 'it-stream-types';
|
|
6
|
+
import type { PeerData } from '@libp2p/interfaces/peer-data';
|
|
7
|
+
export interface NetworkOptions {
|
|
8
|
+
dialer: Dialer;
|
|
9
|
+
protocol: string;
|
|
10
|
+
lan: boolean;
|
|
11
|
+
peerId: PeerId;
|
|
12
|
+
}
|
|
13
|
+
interface NetworkEvents {
|
|
14
|
+
'peer': CustomEvent<PeerData>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Handle network operations for the dht
|
|
18
|
+
*/
|
|
19
|
+
export declare class Network extends EventEmitter<NetworkEvents> implements Startable {
|
|
20
|
+
private readonly log;
|
|
21
|
+
dialer: Dialer;
|
|
22
|
+
private readonly protocol;
|
|
23
|
+
private running;
|
|
24
|
+
/**
|
|
25
|
+
* Create a new network
|
|
26
|
+
*/
|
|
27
|
+
constructor(options: NetworkOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Start the network
|
|
30
|
+
*/
|
|
31
|
+
start(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Stop all network activity
|
|
34
|
+
*/
|
|
35
|
+
stop(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Is the network online?
|
|
38
|
+
*/
|
|
39
|
+
isStarted(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Send a request and record RTT for latency measurements
|
|
42
|
+
*/
|
|
43
|
+
sendRequest(to: PeerId, msg: Message, options?: AbortOptions): AsyncGenerator<import("@libp2p/interfaces/dist/src/dht").SendingQueryEvent | import("@libp2p/interfaces/dist/src/dht").PeerResponseEvent | import("@libp2p/interfaces/dist/src/dht").QueryErrorEvent | import("@libp2p/interfaces/dist/src/dht").DialingPeerEvent, void, unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* Sends a message without expecting an answer
|
|
46
|
+
*/
|
|
47
|
+
sendMessage(to: PeerId, msg: Message, options?: AbortOptions): AsyncGenerator<import("@libp2p/interfaces/dist/src/dht").SendingQueryEvent | import("@libp2p/interfaces/dist/src/dht").PeerResponseEvent | import("@libp2p/interfaces/dist/src/dht").QueryErrorEvent | import("@libp2p/interfaces/dist/src/dht").DialingPeerEvent, void, unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* Write a message to the given stream
|
|
50
|
+
*/
|
|
51
|
+
_writeMessage(stream: Duplex<Uint8Array>, msg: Uint8Array): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Write a message and read its response.
|
|
54
|
+
* If no response is received after the specified timeout
|
|
55
|
+
* this will error out.
|
|
56
|
+
*/
|
|
57
|
+
_writeReadMessage(stream: Duplex<Uint8Array>, msg: Uint8Array): Promise<Message>;
|
|
58
|
+
}
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/network.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,OAAO,EAAuB,MAAM,oBAAoB,CAAA;AACjE,OAAO,EAAE,YAAY,EAAe,MAAM,oBAAoB,CAAA;AAQ9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAEzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAE5D,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,GAAG,EAAE,OAAO,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;CACf;AAED,UAAU,aAAa;IACrB,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAA;CAC9B;AAED;;GAEG;AACH,qBAAa,OAAQ,SAAQ,YAAY,CAAC,aAAa,CAAE,YAAW,SAAS;IAC3E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IACrB,MAAM,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,OAAO,CAAS;IAExB;;OAEG;gBACU,OAAO,EAAE,cAAc;IAUpC;;OAEG;IACG,KAAK;IAQX;;OAEG;IACG,IAAI;IAIV;;OAEG;IACH,SAAS;IAIT;;OAEG;IACK,WAAW,CAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,GAAE,YAAiB;IA4BzE;;OAEG;IACK,WAAW,CAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,GAAE,YAAiB;IAsBzE;;OAEG;IACG,aAAa,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,GAAG,EAAE,UAAU;IAShE;;;;OAIG;IACG,iBAAiB,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,GAAG,EAAE,UAAU;CAiCrE"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import errcode from 'err-code';
|
|
2
|
+
import { pipe } from 'it-pipe';
|
|
3
|
+
import * as lp from 'it-length-prefixed';
|
|
4
|
+
import drain from 'it-drain';
|
|
5
|
+
import first from 'it-first';
|
|
6
|
+
import { Message, MESSAGE_TYPE_LOOKUP } from './message/index.js';
|
|
7
|
+
import { EventEmitter, CustomEvent } from '@libp2p/interfaces';
|
|
8
|
+
import { dialingPeerEvent, sendingQueryEvent, peerResponseEvent, queryErrorEvent } from './query/events.js';
|
|
9
|
+
import { logger } from '@libp2p/logger';
|
|
10
|
+
/**
|
|
11
|
+
* Handle network operations for the dht
|
|
12
|
+
*/
|
|
13
|
+
export class Network extends EventEmitter {
|
|
14
|
+
/**
|
|
15
|
+
* Create a new network
|
|
16
|
+
*/
|
|
17
|
+
constructor(options) {
|
|
18
|
+
super();
|
|
19
|
+
const { dialer, protocol, lan, peerId } = options;
|
|
20
|
+
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:network:${peerId.toString()}`);
|
|
21
|
+
this.running = false;
|
|
22
|
+
this.dialer = dialer;
|
|
23
|
+
this.protocol = protocol;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Start the network
|
|
27
|
+
*/
|
|
28
|
+
async start() {
|
|
29
|
+
if (this.running) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.running = true;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Stop all network activity
|
|
36
|
+
*/
|
|
37
|
+
async stop() {
|
|
38
|
+
this.running = false;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Is the network online?
|
|
42
|
+
*/
|
|
43
|
+
isStarted() {
|
|
44
|
+
return this.running;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Send a request and record RTT for latency measurements
|
|
48
|
+
*/
|
|
49
|
+
async *sendRequest(to, msg, options = {}) {
|
|
50
|
+
if (!this.running) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.log('sending %s to %p', MESSAGE_TYPE_LOOKUP[msg.type], to);
|
|
54
|
+
try {
|
|
55
|
+
yield dialingPeerEvent({ peer: to });
|
|
56
|
+
const { stream } = await this.dialer.dialProtocol(to, this.protocol, options);
|
|
57
|
+
yield sendingQueryEvent({ to, type: msg.type });
|
|
58
|
+
const response = await this._writeReadMessage(stream, msg.serialize());
|
|
59
|
+
yield peerResponseEvent({
|
|
60
|
+
from: to,
|
|
61
|
+
messageType: response.type,
|
|
62
|
+
closer: response.closerPeers,
|
|
63
|
+
providers: response.providerPeers,
|
|
64
|
+
record: response.record
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
yield queryErrorEvent({ from: to, error: err });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Sends a message without expecting an answer
|
|
73
|
+
*/
|
|
74
|
+
async *sendMessage(to, msg, options = {}) {
|
|
75
|
+
if (!this.running) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.log('sending %s to %p', MESSAGE_TYPE_LOOKUP[msg.type], to);
|
|
79
|
+
yield dialingPeerEvent({ peer: to });
|
|
80
|
+
const { stream } = await this.dialer.dialProtocol(to, this.protocol, options);
|
|
81
|
+
yield sendingQueryEvent({ to, type: msg.type });
|
|
82
|
+
try {
|
|
83
|
+
await this._writeMessage(stream, msg.serialize());
|
|
84
|
+
yield peerResponseEvent({ from: to, messageType: msg.type });
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
yield queryErrorEvent({ from: to, error: err });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Write a message to the given stream
|
|
92
|
+
*/
|
|
93
|
+
async _writeMessage(stream, msg) {
|
|
94
|
+
await pipe([msg], lp.encode(), stream, drain);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Write a message and read its response.
|
|
98
|
+
* If no response is received after the specified timeout
|
|
99
|
+
* this will error out.
|
|
100
|
+
*/
|
|
101
|
+
async _writeReadMessage(stream, msg) {
|
|
102
|
+
const res = await pipe([msg], lp.encode(), stream, lp.decode(), async (source) => {
|
|
103
|
+
const buf = await first(source);
|
|
104
|
+
if (buf != null) {
|
|
105
|
+
return buf;
|
|
106
|
+
}
|
|
107
|
+
throw errcode(new Error('No message received'), 'ERR_NO_MESSAGE_RECEIVED');
|
|
108
|
+
});
|
|
109
|
+
const message = Message.deserialize(res);
|
|
110
|
+
// tell any listeners about new peers we've seen
|
|
111
|
+
message.closerPeers.forEach(peerData => {
|
|
112
|
+
this.dispatchEvent(new CustomEvent('peer', {
|
|
113
|
+
detail: peerData
|
|
114
|
+
}));
|
|
115
|
+
});
|
|
116
|
+
message.providerPeers.forEach(peerData => {
|
|
117
|
+
this.dispatchEvent(new CustomEvent('peer', {
|
|
118
|
+
detail: peerData
|
|
119
|
+
}));
|
|
120
|
+
});
|
|
121
|
+
return message;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=network.js.map
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AG,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC,CAAA;YAE/B,IAAI,GAAG,IAAI,IAAI,EAAE;gBACf,OAAO,GAAG,CAAA;aACX;YAED,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,qBAAqB,CAAC,EAAE,yBAAyB,CAAC,CAAA;QAC5E,CAAC,CACF,CAAA;QAED,MAAM,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAExC,gDAAgD;QAChD,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YACrC,IAAI,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,MAAM,EAAE;gBACzC,MAAM,EAAE,QAAQ;aACjB,CAAC,CAAC,CAAA;QACL,CAAC,CAAC,CAAA;QACF,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YACvC,IAAI,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,MAAM,EAAE;gBACzC,MAAM,EAAE,QAAQ;aACjB,CAAC,CAAC,CAAA;QACL,CAAC,CAAC,CAAA;QAEF,OAAO,OAAO,CAAA;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
2
|
+
/**
|
|
3
|
+
* A list of unique peers.
|
|
4
|
+
*/
|
|
5
|
+
export declare class PeerList {
|
|
6
|
+
private readonly list;
|
|
7
|
+
constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Add a new peer. Returns `true` if it was a new one
|
|
10
|
+
*/
|
|
11
|
+
push(peerId: PeerId): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Check if this PeerData is already in here
|
|
14
|
+
*/
|
|
15
|
+
has(peerId: PeerId): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Get the list as an array
|
|
18
|
+
*/
|
|
19
|
+
toArray(): PeerId[];
|
|
20
|
+
/**
|
|
21
|
+
* Remove the last element
|
|
22
|
+
*/
|
|
23
|
+
pop(): PeerId | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The length of the list
|
|
26
|
+
*/
|
|
27
|
+
get length(): number;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/peer-list/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAExD;;GAEG;AACH,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAU;;IAM/B;;OAEG;IACH,IAAI,CAAE,MAAM,EAAE,MAAM;IAUpB;;OAEG;IACH,GAAG,CAAE,MAAM,EAAE,MAAM;IAKnB;;OAEG;IACH,OAAO;IAIP;;OAEG;IACH,GAAG;IAIH;;OAEG;IACH,IAAI,MAAM,WAET;CACF"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A list of unique peers.
|
|
3
|
+
*/
|
|
4
|
+
export class PeerList {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.list = [];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Add a new peer. Returns `true` if it was a new one
|
|
10
|
+
*/
|
|
11
|
+
push(peerId) {
|
|
12
|
+
if (!this.has(peerId)) {
|
|
13
|
+
this.list.push(peerId);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Check if this PeerData is already in here
|
|
20
|
+
*/
|
|
21
|
+
has(peerId) {
|
|
22
|
+
const match = this.list.find((i) => i.equals(peerId));
|
|
23
|
+
return Boolean(match);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the list as an array
|
|
27
|
+
*/
|
|
28
|
+
toArray() {
|
|
29
|
+
return this.list.slice();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Remove the last element
|
|
33
|
+
*/
|
|
34
|
+
pop() {
|
|
35
|
+
return this.list.pop();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The length of the list
|
|
39
|
+
*/
|
|
40
|
+
get length() {
|
|
41
|
+
return this.list.length;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/peer-list/index.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,OAAO,QAAQ;IAGnB;QACE,IAAI,CAAC,IAAI,GAAG,EAAE,CAAA;IAChB,CAAC;IAED;;OAEG;IACH,IAAI,CAAE,MAAc;QAClB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;YACrB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;YAEtB,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;OAEG;IACH,GAAG,CAAE,MAAc;QACjB,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;QACrD,OAAO,OAAO,CAAC,KAAK,CAAC,CAAA;IACvB,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;IAC1B,CAAC;IAED;;OAEG;IACH,GAAG;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;IACxB,CAAC;IAED;;OAEG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAA;IACzB,CAAC;CACF"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
2
|
+
/**
|
|
3
|
+
* Maintains a list of peerIds sorted by distance from a DHT key.
|
|
4
|
+
*/
|
|
5
|
+
export declare class PeerDistanceList {
|
|
6
|
+
/**
|
|
7
|
+
* The DHT key from which distance is calculated
|
|
8
|
+
*/
|
|
9
|
+
private readonly originDhtKey;
|
|
10
|
+
/**
|
|
11
|
+
* The maximum size of the list
|
|
12
|
+
*/
|
|
13
|
+
private readonly capacity;
|
|
14
|
+
private peerDistances;
|
|
15
|
+
constructor(originDhtKey: Uint8Array, capacity: number);
|
|
16
|
+
/**
|
|
17
|
+
* The length of the list
|
|
18
|
+
*/
|
|
19
|
+
get length(): number;
|
|
20
|
+
/**
|
|
21
|
+
* The peerIds in the list, in order of distance from the origin key
|
|
22
|
+
*/
|
|
23
|
+
get peers(): PeerId[];
|
|
24
|
+
/**
|
|
25
|
+
* Add a peerId to the list.
|
|
26
|
+
*/
|
|
27
|
+
add(peerId: PeerId): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Indicates whether any of the peerIds passed as a parameter are closer
|
|
30
|
+
* to the origin key than the furthest peerId in the PeerDistanceList.
|
|
31
|
+
*/
|
|
32
|
+
anyCloser(peerIds: PeerId[]): Promise<boolean>;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=peer-distance-list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peer-distance-list.d.ts","sourceRoot":"","sources":["../../../src/peer-list/peer-distance-list.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAOxD;;GAEG;AACH,qBAAa,gBAAgB;IAC3B;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAY;IAEzC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IAEjC,OAAO,CAAC,aAAa,CAAgB;gBAExB,YAAY,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM;IAMvD;;OAEG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,KAAK,aAER;IAED;;OAEG;IACG,GAAG,CAAE,MAAM,EAAE,MAAM;IAgBzB;;;OAGG;IACG,SAAS,CAAE,OAAO,EAAE,MAAM,EAAE;CAsBnC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as utils from '../utils.js';
|
|
2
|
+
import pMap from 'p-map';
|
|
3
|
+
import { compare as uint8ArrayCompare } from 'uint8arrays/compare';
|
|
4
|
+
import { xor as uint8ArrayXor } from 'uint8arrays/xor';
|
|
5
|
+
/**
|
|
6
|
+
* Maintains a list of peerIds sorted by distance from a DHT key.
|
|
7
|
+
*/
|
|
8
|
+
export class PeerDistanceList {
|
|
9
|
+
constructor(originDhtKey, capacity) {
|
|
10
|
+
this.originDhtKey = originDhtKey;
|
|
11
|
+
this.capacity = capacity;
|
|
12
|
+
this.peerDistances = [];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The length of the list
|
|
16
|
+
*/
|
|
17
|
+
get length() {
|
|
18
|
+
return this.peerDistances.length;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The peerIds in the list, in order of distance from the origin key
|
|
22
|
+
*/
|
|
23
|
+
get peers() {
|
|
24
|
+
return this.peerDistances.map(pd => pd.peerId);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Add a peerId to the list.
|
|
28
|
+
*/
|
|
29
|
+
async add(peerId) {
|
|
30
|
+
if (this.peerDistances.find(pd => pd.peerId.equals(peerId)) != null) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const dhtKey = await utils.convertPeerId(peerId);
|
|
34
|
+
const el = {
|
|
35
|
+
peerId,
|
|
36
|
+
distance: uint8ArrayXor(this.originDhtKey, dhtKey)
|
|
37
|
+
};
|
|
38
|
+
this.peerDistances.push(el);
|
|
39
|
+
this.peerDistances.sort((a, b) => uint8ArrayCompare(a.distance, b.distance));
|
|
40
|
+
this.peerDistances = this.peerDistances.slice(0, this.capacity);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Indicates whether any of the peerIds passed as a parameter are closer
|
|
44
|
+
* to the origin key than the furthest peerId in the PeerDistanceList.
|
|
45
|
+
*/
|
|
46
|
+
async anyCloser(peerIds) {
|
|
47
|
+
if (peerIds.length === 0) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (this.length === 0) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const dhtKeys = await pMap(peerIds, async (peerId) => await utils.convertPeerId(peerId));
|
|
54
|
+
const furthestDistance = this.peerDistances[this.peerDistances.length - 1].distance;
|
|
55
|
+
for (const dhtKey of dhtKeys) {
|
|
56
|
+
const keyDistance = uint8ArrayXor(this.originDhtKey, dhtKey);
|
|
57
|
+
if (uint8ArrayCompare(keyDistance, furthestDistance) < 0) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=peer-distance-list.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peer-distance-list.js","sourceRoot":"","sources":["../../../src/peer-list/peer-distance-list.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,aAAa,CAAA;AACpC,OAAO,IAAI,MAAM,OAAO,CAAA;AACxB,OAAO,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAClE,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAQtD;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAa3B,YAAa,YAAwB,EAAE,QAAgB;QACrD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAA;QAChC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,aAAa,GAAG,EAAE,CAAA;IACzB,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K,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAA;QACxF,MAAM,gBAAgB,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAA;QAEnF,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC5B,MAAM,WAAW,GAAG,aAAa,CAAC,IAAI,CAAC,YAAY,EAAE,MAAM,CAAC,CAAA;YAE5D,IAAI,iBAAiB,CAAC,WAAW,EAAE,gBAAgB,CAAC,GAAG,CAAC,EAAE;gBACxD,OAAO,IAAI,CAAA;aACZ;SACF;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { DHTRecord, QueryOptions, Validators } from '@libp2p/interfaces/dht';
|
|
2
|
+
import type { RoutingTable } from '../routing-table/index.js';
|
|
3
|
+
import type { PeerStore } from '@libp2p/interfaces/peer-store';
|
|
4
|
+
import type { QueryManager } from '../query/manager.js';
|
|
5
|
+
import type { Network } from '../network.js';
|
|
6
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
7
|
+
import type { PeerData } from '@libp2p/interfaces/peer-data';
|
|
8
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
9
|
+
export interface PeerRoutingOptions {
|
|
10
|
+
peerId: PeerId;
|
|
11
|
+
routingTable: RoutingTable;
|
|
12
|
+
peerStore: PeerStore;
|
|
13
|
+
network: Network;
|
|
14
|
+
validators: Validators;
|
|
15
|
+
queryManager: QueryManager;
|
|
16
|
+
lan: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class PeerRouting {
|
|
19
|
+
private readonly log;
|
|
20
|
+
private readonly peerId;
|
|
21
|
+
private readonly routingTable;
|
|
22
|
+
private readonly peerStore;
|
|
23
|
+
private readonly network;
|
|
24
|
+
private readonly validators;
|
|
25
|
+
private readonly queryManager;
|
|
26
|
+
constructor(options: PeerRoutingOptions);
|
|
27
|
+
/**
|
|
28
|
+
* Look if we are connected to a peer with the given id.
|
|
29
|
+
* Returns its id and addresses, if found, otherwise `undefined`.
|
|
30
|
+
*/
|
|
31
|
+
findPeerLocal(peer: PeerId): Promise<{
|
|
32
|
+
id: PeerId;
|
|
33
|
+
multiaddrs: import("@multiformats/multiaddr").Multiaddr[];
|
|
34
|
+
protocols: never[];
|
|
35
|
+
} | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Get a value via rpc call for the given parameters
|
|
38
|
+
*/
|
|
39
|
+
_getValueSingle(peer: PeerId, key: Uint8Array, options?: AbortOptions): AsyncGenerator<import("@libp2p/interfaces/dht").SendingQueryEvent | import("@libp2p/interfaces/dht").PeerResponseEvent | import("@libp2p/interfaces/dht").QueryErrorEvent | import("@libp2p/interfaces/dht").DialingPeerEvent, void, unknown>;
|
|
40
|
+
/**
|
|
41
|
+
* Get the public key directly from a node
|
|
42
|
+
*/
|
|
43
|
+
getPublicKeyFromNode(peer: PeerId, options?: AbortOptions): AsyncGenerator<import("@libp2p/interfaces/dht").SendingQueryEvent | import("@libp2p/interfaces/dht").PeerResponseEvent | import("@libp2p/interfaces/dht").QueryErrorEvent | import("@libp2p/interfaces/dht").ValueEvent | import("@libp2p/interfaces/dht").DialingPeerEvent, void, unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* Search for a peer with the given ID
|
|
46
|
+
*/
|
|
47
|
+
findPeer(id: PeerId, options?: QueryOptions): AsyncGenerator<import("@libp2p/interfaces/dht").QueryEvent, void, unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* Kademlia 'node lookup' operation on a key, which could be a the
|
|
50
|
+
* bytes from a multihash or a peer ID
|
|
51
|
+
*/
|
|
52
|
+
getClosestPeers(key: Uint8Array, options?: QueryOptions): AsyncGenerator<import("@libp2p/interfaces/dht").QueryEvent, void, unknown>;
|
|
53
|
+
/**
|
|
54
|
+
* Query a particular peer for the value for the given key.
|
|
55
|
+
* It will either return the value or a list of closer peers.
|
|
56
|
+
*
|
|
57
|
+
* Note: The peerStore is updated with new addresses found for the given peer.
|
|
58
|
+
*/
|
|
59
|
+
getValueOrPeers(peer: PeerId, key: Uint8Array, options?: AbortOptions): AsyncGenerator<import("@libp2p/interfaces/dht").SendingQueryEvent | import("@libp2p/interfaces/dht").PeerResponseEvent | import("@libp2p/interfaces/dht").QueryErrorEvent | import("@libp2p/interfaces/dht").DialingPeerEvent, void, unknown>;
|
|
60
|
+
/**
|
|
61
|
+
* Verify a record, fetching missing public keys from the network.
|
|
62
|
+
* Throws an error if the record is invalid.
|
|
63
|
+
*/
|
|
64
|
+
_verifyRecordOnline(record: DHTRecord): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Get the nearest peers to the given query, but if closer
|
|
67
|
+
* than self
|
|
68
|
+
*/
|
|
69
|
+
getCloserPeersOffline(key: Uint8Array, closerThan: PeerId): Promise<PeerData[]>;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/peer-routing/index.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAA;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA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