@libp2p/kad-dht 1.0.6 → 1.0.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.
- package/dist/src/rpc/handlers/find-node.d.ts +4 -2
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
- package/dist/src/rpc/handlers/find-node.js +20 -7
- package/dist/src/rpc/handlers/find-node.js.map +1 -1
- package/dist/src/rpc/index.d.ts +1 -1
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/rpc/handlers/find-node.ts +22 -14
- package/src/rpc/index.ts +2 -2
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Message } from '../../message/index.js';
|
|
2
|
+
import { Components } from '@libp2p/interfaces/components';
|
|
3
|
+
import type { Initializable } from '@libp2p/interfaces/components';
|
|
2
4
|
import type { DHTMessageHandler } from '../index.js';
|
|
3
5
|
import type { PeerRouting } from '../../peer-routing/index.js';
|
|
4
6
|
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
5
|
-
import type { Initializable } from '@libp2p/interfaces/components';
|
|
6
7
|
export interface FindNodeHandlerInit {
|
|
7
8
|
peerRouting: PeerRouting;
|
|
8
9
|
lan: boolean;
|
|
@@ -10,8 +11,9 @@ export interface FindNodeHandlerInit {
|
|
|
10
11
|
export declare class FindNodeHandler implements DHTMessageHandler, Initializable {
|
|
11
12
|
private readonly peerRouting;
|
|
12
13
|
private readonly lan;
|
|
14
|
+
private components;
|
|
13
15
|
constructor(init: FindNodeHandlerInit);
|
|
14
|
-
init(): void;
|
|
16
|
+
init(components: Components): void;
|
|
15
17
|
/**
|
|
16
18
|
* Process `FindNode` DHT messages
|
|
17
19
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-node.d.ts","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAOhD,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"find-node.d.ts","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAOhD,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAE1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAElE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AAC9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAIxD,MAAM,WAAW,mBAAmB;IAClC,WAAW,EAAE,WAAW,CAAA;IACxB,GAAG,EAAE,OAAO,CAAA;CACb;AAED,qBAAa,eAAgB,YAAW,iBAAiB,EAAE,aAAa;IACtE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IACzC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,UAAU,CAAmB;gBAExB,IAAI,EAAE,mBAAmB;IAMtC,IAAI,CAAE,UAAU,EAAE,UAAU,GAAG,IAAI;IAInC;;OAEG;IACG,MAAM,CAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO;CA6B3C"}
|
|
@@ -1,26 +1,39 @@
|
|
|
1
1
|
import { Message } from '../../message/index.js';
|
|
2
2
|
import { logger } from '@libp2p/logger';
|
|
3
3
|
import { removePrivateAddresses, removePublicAddresses } from '../../utils.js';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import all from 'it-all';
|
|
4
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays';
|
|
5
|
+
import { Components } from '@libp2p/interfaces/components';
|
|
6
|
+
import { protocols } from '@multiformats/multiaddr';
|
|
8
7
|
const log = logger('libp2p:kad-dht:rpc:handlers:find-node');
|
|
9
8
|
export class FindNodeHandler {
|
|
10
9
|
constructor(init) {
|
|
10
|
+
this.components = new Components();
|
|
11
11
|
const { peerRouting, lan } = init;
|
|
12
12
|
this.peerRouting = peerRouting;
|
|
13
13
|
this.lan = Boolean(lan);
|
|
14
14
|
}
|
|
15
|
-
init() {
|
|
15
|
+
init(components) {
|
|
16
|
+
this.components = components;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Process `FindNode` DHT messages
|
|
19
20
|
*/
|
|
20
21
|
async handle(peerId, msg) {
|
|
21
22
|
log('incoming request from %p for peers closer to %b', peerId, msg.key);
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
let closer = [];
|
|
24
|
+
if (uint8ArrayEquals(this.components.getPeerId().toBytes(), msg.key)) {
|
|
25
|
+
closer = [{
|
|
26
|
+
id: this.components.getPeerId(),
|
|
27
|
+
multiaddrs: this.components.getAddressManager().getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code)),
|
|
28
|
+
protocols: []
|
|
29
|
+
}];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
closer = await this.peerRouting.getCloserPeersOffline(msg.key, peerId);
|
|
33
|
+
}
|
|
34
|
+
closer = closer
|
|
35
|
+
.map(this.lan ? removePublicAddresses : removePrivateAddresses)
|
|
36
|
+
.filter(({ multiaddrs }) => multiaddrs.length);
|
|
24
37
|
const response = new Message(msg.type, new Uint8Array(0), msg.clusterLevel);
|
|
25
38
|
if (closer.length > 0) {
|
|
26
39
|
response.closerPeers = closer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-node.js","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,gBAAgB,CAAA;AACvB,OA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,EAAE,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAC,YAAY,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,eAAe,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;oBACnH,SAAS,EAAE,EAAE;iBACd,CAAC,CAAA;SACH;aAAM;YACL,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,qBAAqB,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;SACvE;QAED,MAAM,GAAG,MAAM;aACZ,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,sBAAsB,CAAC;aAC9D,MAAM,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;QAEhD,MAAM,QAAQ,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,YAAY,CAAC,CAAA;QAE3E,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,QAAQ,CAAC,WAAW,GAAG,MAAM,CAAA;SAC9B;aAAM;YACL,GAAG,CAAC,+CAA+C,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;SACtE;QAED,OAAO,QAAQ,CAAA;IACjB,CAAC;CACF"}
|
package/dist/src/rpc/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { Providers } from '../providers';
|
|
|
6
6
|
import type { PeerRouting } from '../peer-routing';
|
|
7
7
|
import type { Validators } from '@libp2p/interfaces/dht';
|
|
8
8
|
import type { Components, Initializable } from '@libp2p/interfaces/components';
|
|
9
|
-
export interface DHTMessageHandler {
|
|
9
|
+
export interface DHTMessageHandler extends Initializable {
|
|
10
10
|
handle: (peerId: PeerId, msg: Message) => Promise<Message | undefined>;
|
|
11
11
|
}
|
|
12
12
|
export interface RPCInit {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/rpc/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,OAAO,EAAgB,MAAM,qBAAqB,CAAA;AAO3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAA;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAE9E,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/rpc/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,OAAO,EAAgB,MAAM,qBAAqB,CAAA;AAO3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAA;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAE9E,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACtD,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,CAAA;CACvE;AAED,MAAM,WAAW,OAAO;IACtB,YAAY,EAAE,YAAY,CAAA;IAC1B,SAAS,EAAE,SAAS,CAAA;IACpB,WAAW,EAAE,WAAW,CAAA;IACxB,UAAU,EAAE,UAAU,CAAA;IACtB,GAAG,EAAE,OAAO,CAAA;CACb;AAED,qBAAa,GAAI,YAAW,aAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmC;IAC5D,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAc;IAC3C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,IAAI,EAAE,OAAO;IAgB1B,IAAI,CAAE,UAAU,EAAE,UAAU,GAAG,IAAI;IAMnC;;OAEG;IACG,aAAa,CAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO;IAkBjD;;OAEG;IACH,gBAAgB,CAAE,IAAI,EAAE,kBAAkB;CAqC3C"}
|
package/package.json
CHANGED
|
@@ -4,14 +4,14 @@ import {
|
|
|
4
4
|
removePrivateAddresses,
|
|
5
5
|
removePublicAddresses
|
|
6
6
|
} from '../../utils.js'
|
|
7
|
-
import {
|
|
7
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays'
|
|
8
|
+
import { Components } from '@libp2p/interfaces/components'
|
|
9
|
+
import { protocols } from '@multiformats/multiaddr'
|
|
10
|
+
import type { Initializable } from '@libp2p/interfaces/components'
|
|
11
|
+
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
8
12
|
import type { DHTMessageHandler } from '../index.js'
|
|
9
13
|
import type { PeerRouting } from '../../peer-routing/index.js'
|
|
10
14
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
11
|
-
import map from 'it-map'
|
|
12
|
-
import filter from 'it-filter'
|
|
13
|
-
import all from 'it-all'
|
|
14
|
-
import type { Initializable } from '@libp2p/interfaces/components'
|
|
15
15
|
|
|
16
16
|
const log = logger('libp2p:kad-dht:rpc:handlers:find-node')
|
|
17
17
|
|
|
@@ -23,6 +23,7 @@ export interface FindNodeHandlerInit {
|
|
|
23
23
|
export class FindNodeHandler implements DHTMessageHandler, Initializable {
|
|
24
24
|
private readonly peerRouting: PeerRouting
|
|
25
25
|
private readonly lan: boolean
|
|
26
|
+
private components = new Components()
|
|
26
27
|
|
|
27
28
|
constructor (init: FindNodeHandlerInit) {
|
|
28
29
|
const { peerRouting, lan } = init
|
|
@@ -30,8 +31,8 @@ export class FindNodeHandler implements DHTMessageHandler, Initializable {
|
|
|
30
31
|
this.lan = Boolean(lan)
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
init (): void {
|
|
34
|
-
|
|
34
|
+
init (components: Components): void {
|
|
35
|
+
this.components = components
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
/**
|
|
@@ -40,14 +41,21 @@ export class FindNodeHandler implements DHTMessageHandler, Initializable {
|
|
|
40
41
|
async handle (peerId: PeerId, msg: Message) {
|
|
41
42
|
log('incoming request from %p for peers closer to %b', peerId, msg.key)
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
let closer: PeerInfo[] = []
|
|
45
|
+
|
|
46
|
+
if (uint8ArrayEquals(this.components.getPeerId().toBytes(), msg.key)) {
|
|
47
|
+
closer = [{
|
|
48
|
+
id: this.components.getPeerId(),
|
|
49
|
+
multiaddrs: this.components.getAddressManager().getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code)),
|
|
50
|
+
protocols: []
|
|
51
|
+
}]
|
|
52
|
+
} else {
|
|
53
|
+
closer = await this.peerRouting.getCloserPeersOffline(msg.key, peerId)
|
|
54
|
+
}
|
|
44
55
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(
|
|
48
|
-
(source) => filter(source, ({ multiaddrs }) => multiaddrs.length > 0),
|
|
49
|
-
async (source) => await all(source)
|
|
50
|
-
)
|
|
56
|
+
closer = closer
|
|
57
|
+
.map(this.lan ? removePublicAddresses : removePrivateAddresses)
|
|
58
|
+
.filter(({ multiaddrs }) => multiaddrs.length)
|
|
51
59
|
|
|
52
60
|
const response = new Message(msg.type, new Uint8Array(0), msg.clusterLevel)
|
|
53
61
|
|
package/src/rpc/index.ts
CHANGED
|
@@ -16,7 +16,7 @@ import type { PeerRouting } from '../peer-routing'
|
|
|
16
16
|
import type { Validators } from '@libp2p/interfaces/dht'
|
|
17
17
|
import type { Components, Initializable } from '@libp2p/interfaces/components'
|
|
18
18
|
|
|
19
|
-
export interface DHTMessageHandler {
|
|
19
|
+
export interface DHTMessageHandler extends Initializable {
|
|
20
20
|
handle: (peerId: PeerId, msg: Message) => Promise<Message | undefined>
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -29,7 +29,7 @@ export interface RPCInit {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export class RPC implements Initializable {
|
|
32
|
-
private readonly handlers: Record<string, DHTMessageHandler
|
|
32
|
+
private readonly handlers: Record<string, DHTMessageHandler>
|
|
33
33
|
private readonly routingTable: RoutingTable
|
|
34
34
|
private readonly log: Logger
|
|
35
35
|
|