@libp2p/kad-dht 9.1.2 → 9.1.4
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.min.js +11 -11
- package/dist/src/content-fetching/index.d.ts +1 -2
- package/dist/src/content-fetching/index.d.ts.map +1 -1
- package/dist/src/content-fetching/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +1 -2
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/dual-kad-dht.d.ts +15 -11
- package/dist/src/dual-kad-dht.d.ts.map +1 -1
- package/dist/src/dual-kad-dht.js +85 -6
- package/dist/src/dual-kad-dht.js.map +1 -1
- package/dist/src/index.d.ts +181 -24
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +24 -85
- package/dist/src/index.js.map +1 -1
- package/dist/src/kad-dht.d.ts +2 -6
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +1 -4
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/network.d.ts +1 -2
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-routing/index.d.ts +1 -2
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/query/events.d.ts +1 -1
- package/dist/src/query/events.d.ts.map +1 -1
- package/dist/src/query/manager.d.ts +1 -1
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/query-path.d.ts +1 -1
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query/types.d.ts +1 -1
- package/dist/src/query/types.d.ts.map +1 -1
- package/dist/src/rpc/handlers/put-value.d.ts +1 -1
- package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
- package/dist/src/rpc/index.d.ts +1 -1
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/typedoc-urls.json +23 -3
- package/package.json +4 -5
- package/src/content-fetching/index.ts +1 -2
- package/src/content-routing/index.ts +1 -2
- package/src/dual-kad-dht.ts +107 -14
- package/src/index.ts +209 -110
- package/src/kad-dht.ts +2 -8
- package/src/network.ts +1 -2
- package/src/peer-routing/index.ts +1 -2
- package/src/query/events.ts +1 -1
- package/src/query/manager.ts +1 -1
- package/src/query/query-path.ts +1 -1
- package/src/query/types.ts +1 -1
- package/src/rpc/handlers/put-value.ts +1 -1
- package/src/rpc/index.ts +1 -1
package/src/dual-kad-dht.ts
CHANGED
|
@@ -1,34 +1,117 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ContentRouting, contentRouting } from '@libp2p/interface-content-routing'
|
|
2
|
+
import { type PeerDiscovery, peerDiscovery, type PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
|
|
3
|
+
import { type PeerRouting, peerRouting } from '@libp2p/interface-peer-routing'
|
|
2
4
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
5
|
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
|
|
4
6
|
import { logger } from '@libp2p/logger'
|
|
7
|
+
import drain from 'it-drain'
|
|
5
8
|
import merge from 'it-merge'
|
|
9
|
+
import { DefaultKadDHT } from './kad-dht.js'
|
|
6
10
|
import { queryErrorEvent } from './query/events.js'
|
|
7
|
-
import type { KadDHTComponents } from './index.js'
|
|
8
|
-
import type { KadDHT } from './kad-dht.js'
|
|
9
|
-
import type { DualDHT, QueryEvent, QueryOptions } from '@libp2p/interface-dht'
|
|
10
|
-
import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
|
|
11
|
+
import type { DualKadDHT, KadDHT, KadDHTComponents, KadDHTInit, QueryEvent, QueryOptions } from './index.js'
|
|
11
12
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
13
|
+
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
12
14
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
13
|
-
import type { CID } from 'multiformats'
|
|
15
|
+
import type { CID } from 'multiformats/cid'
|
|
14
16
|
|
|
15
17
|
const log = logger('libp2p:kad-dht')
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Wrapper class to convert events into returned values
|
|
21
|
+
*/
|
|
22
|
+
class DHTContentRouting implements ContentRouting {
|
|
23
|
+
private readonly dht: KadDHT
|
|
24
|
+
|
|
25
|
+
constructor (dht: KadDHT) {
|
|
26
|
+
this.dht = dht
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async provide (cid: CID): Promise<void> {
|
|
30
|
+
await drain(this.dht.provide(cid))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async * findProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<PeerInfo, void, undefined> {
|
|
34
|
+
for await (const event of this.dht.findProviders(cid, options)) {
|
|
35
|
+
if (event.name === 'PROVIDER') {
|
|
36
|
+
yield * event.providers
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> {
|
|
42
|
+
await drain(this.dht.put(key, value, options))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> {
|
|
46
|
+
for await (const event of this.dht.get(key, options)) {
|
|
47
|
+
if (event.name === 'VALUE') {
|
|
48
|
+
return event.value
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw new CodeError('Not found', 'ERR_NOT_FOUND')
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wrapper class to convert events into returned values
|
|
58
|
+
*/
|
|
59
|
+
class DHTPeerRouting implements PeerRouting {
|
|
60
|
+
private readonly dht: KadDHT
|
|
61
|
+
|
|
62
|
+
constructor (dht: KadDHT) {
|
|
63
|
+
this.dht = dht
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async findPeer (peerId: PeerId, options: AbortOptions = {}): Promise<PeerInfo> {
|
|
67
|
+
for await (const event of this.dht.findPeer(peerId, options)) {
|
|
68
|
+
if (event.name === 'FINAL_PEER') {
|
|
69
|
+
return event.peer
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw new CodeError('Not found', 'ERR_NOT_FOUND')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async * getClosestPeers (key: Uint8Array, options: AbortOptions = {}): AsyncIterable<PeerInfo> {
|
|
77
|
+
for await (const event of this.dht.getClosestPeers(key, options)) {
|
|
78
|
+
if (event.name === 'FINAL_PEER') {
|
|
79
|
+
yield event.peer
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
17
85
|
/**
|
|
18
86
|
* A DHT implementation modelled after Kademlia with S/Kademlia modifications.
|
|
19
87
|
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
|
|
20
88
|
*/
|
|
21
|
-
export class
|
|
22
|
-
public readonly wan:
|
|
23
|
-
public readonly lan:
|
|
89
|
+
export class DefaultDualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DualKadDHT, PeerDiscovery {
|
|
90
|
+
public readonly wan: DefaultKadDHT
|
|
91
|
+
public readonly lan: DefaultKadDHT
|
|
24
92
|
public readonly components: KadDHTComponents
|
|
93
|
+
private readonly contentRouting: ContentRouting
|
|
94
|
+
private readonly peerRouting: PeerRouting
|
|
25
95
|
|
|
26
|
-
constructor (components: KadDHTComponents,
|
|
96
|
+
constructor (components: KadDHTComponents, init: KadDHTInit = {}) {
|
|
27
97
|
super()
|
|
28
98
|
|
|
29
99
|
this.components = components
|
|
30
|
-
|
|
31
|
-
this.
|
|
100
|
+
|
|
101
|
+
this.wan = new DefaultKadDHT(components, {
|
|
102
|
+
protocolPrefix: '/ipfs',
|
|
103
|
+
...init,
|
|
104
|
+
lan: false
|
|
105
|
+
})
|
|
106
|
+
this.lan = new DefaultKadDHT(components, {
|
|
107
|
+
protocolPrefix: '/ipfs',
|
|
108
|
+
...init,
|
|
109
|
+
clientMode: false,
|
|
110
|
+
lan: true
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
this.contentRouting = new DHTContentRouting(this)
|
|
114
|
+
this.peerRouting = new DHTPeerRouting(this)
|
|
32
115
|
|
|
33
116
|
// handle peers being discovered during processing of DHT messages
|
|
34
117
|
this.wan.addEventListener('peer', (evt) => {
|
|
@@ -43,10 +126,20 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
|
|
|
43
126
|
})
|
|
44
127
|
}
|
|
45
128
|
|
|
46
|
-
readonly [symbol] = true
|
|
47
|
-
|
|
48
129
|
readonly [Symbol.toStringTag] = '@libp2p/dual-kad-dht'
|
|
49
130
|
|
|
131
|
+
get [contentRouting] (): ContentRouting {
|
|
132
|
+
return this.contentRouting
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
get [peerRouting] (): PeerRouting {
|
|
136
|
+
return this.peerRouting
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
get [peerDiscovery] (): PeerDiscovery {
|
|
140
|
+
return this
|
|
141
|
+
}
|
|
142
|
+
|
|
50
143
|
/**
|
|
51
144
|
* Is this DHT running.
|
|
52
145
|
*/
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type PeerDiscovery, peerDiscovery } from '@libp2p/interface-peer-discovery'
|
|
3
|
-
import { type PeerRouting, peerRouting } from '@libp2p/interface-peer-routing'
|
|
4
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
5
|
-
import drain from 'it-drain'
|
|
6
|
-
import { DualKadDHT } from './dual-kad-dht.js'
|
|
7
|
-
import { KadDHT as SingleKadDHT } from './kad-dht.js'
|
|
1
|
+
import { DefaultDualKadDHT } from './dual-kad-dht.js'
|
|
8
2
|
import type { ProvidersInit } from './providers.js'
|
|
9
3
|
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
10
4
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
11
|
-
import type { DHT, DualDHT, Selectors, Validators } from '@libp2p/interface-dht'
|
|
12
5
|
import type { Metrics } from '@libp2p/interface-metrics'
|
|
13
6
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
14
7
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
@@ -18,6 +11,212 @@ import type { AbortOptions } from '@libp2p/interfaces'
|
|
|
18
11
|
import type { Datastore } from 'interface-datastore'
|
|
19
12
|
import type { CID } from 'multiformats/cid'
|
|
20
13
|
|
|
14
|
+
/**
|
|
15
|
+
* The types of events emitted during DHT queries
|
|
16
|
+
*/
|
|
17
|
+
export enum EventTypes {
|
|
18
|
+
SENDING_QUERY = 0,
|
|
19
|
+
PEER_RESPONSE,
|
|
20
|
+
FINAL_PEER,
|
|
21
|
+
QUERY_ERROR,
|
|
22
|
+
PROVIDER,
|
|
23
|
+
VALUE,
|
|
24
|
+
ADDING_PEER,
|
|
25
|
+
DIALING_PEER
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The types of messages sent to peers during DHT queries
|
|
30
|
+
*/
|
|
31
|
+
export enum MessageType {
|
|
32
|
+
PUT_VALUE = 0,
|
|
33
|
+
GET_VALUE,
|
|
34
|
+
ADD_PROVIDER,
|
|
35
|
+
GET_PROVIDERS,
|
|
36
|
+
FIND_NODE,
|
|
37
|
+
PING
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type MessageName = keyof typeof MessageType
|
|
41
|
+
|
|
42
|
+
export interface DHTRecord {
|
|
43
|
+
key: Uint8Array
|
|
44
|
+
value: Uint8Array
|
|
45
|
+
timeReceived?: Date
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface QueryOptions extends AbortOptions {
|
|
49
|
+
queryFuncTimeout?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Emitted when sending queries to remote peers
|
|
54
|
+
*/
|
|
55
|
+
export interface SendingQueryEvent {
|
|
56
|
+
to: PeerId
|
|
57
|
+
type: EventTypes.SENDING_QUERY
|
|
58
|
+
name: 'SENDING_QUERY'
|
|
59
|
+
messageName: keyof typeof MessageType
|
|
60
|
+
messageType: MessageType
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Emitted when query responses are received form remote peers. Depending on the query
|
|
65
|
+
* these events may be followed by a `FinalPeerEvent`, a `ValueEvent` or a `ProviderEvent`.
|
|
66
|
+
*/
|
|
67
|
+
export interface PeerResponseEvent {
|
|
68
|
+
from: PeerId
|
|
69
|
+
type: EventTypes.PEER_RESPONSE
|
|
70
|
+
name: 'PEER_RESPONSE'
|
|
71
|
+
messageName: keyof typeof MessageType
|
|
72
|
+
messageType: MessageType
|
|
73
|
+
closer: PeerInfo[]
|
|
74
|
+
providers: PeerInfo[]
|
|
75
|
+
record?: DHTRecord
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Emitted at the end of a `findPeer` query
|
|
80
|
+
*/
|
|
81
|
+
export interface FinalPeerEvent {
|
|
82
|
+
from: PeerId
|
|
83
|
+
peer: PeerInfo
|
|
84
|
+
type: EventTypes.FINAL_PEER
|
|
85
|
+
name: 'FINAL_PEER'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Something went wrong with the query
|
|
90
|
+
*/
|
|
91
|
+
export interface QueryErrorEvent {
|
|
92
|
+
from: PeerId
|
|
93
|
+
type: EventTypes.QUERY_ERROR
|
|
94
|
+
name: 'QUERY_ERROR'
|
|
95
|
+
error: Error
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Emitted when providers are found
|
|
100
|
+
*/
|
|
101
|
+
export interface ProviderEvent {
|
|
102
|
+
from: PeerId
|
|
103
|
+
type: EventTypes.PROVIDER
|
|
104
|
+
name: 'PROVIDER'
|
|
105
|
+
providers: PeerInfo[]
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Emitted when values are found
|
|
110
|
+
*/
|
|
111
|
+
export interface ValueEvent {
|
|
112
|
+
from: PeerId
|
|
113
|
+
type: EventTypes.VALUE
|
|
114
|
+
name: 'VALUE'
|
|
115
|
+
value: Uint8Array
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Emitted when peers are added to a query
|
|
120
|
+
*/
|
|
121
|
+
export interface AddingPeerEvent {
|
|
122
|
+
type: EventTypes.ADDING_PEER
|
|
123
|
+
name: 'ADDING_PEER'
|
|
124
|
+
peer: PeerId
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Emitted when peers are dialled as part of a query
|
|
129
|
+
*/
|
|
130
|
+
export interface DialingPeerEvent {
|
|
131
|
+
peer: PeerId
|
|
132
|
+
type: EventTypes.DIALING_PEER
|
|
133
|
+
name: 'DIALING_PEER'
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export type QueryEvent = SendingQueryEvent | PeerResponseEvent | FinalPeerEvent | QueryErrorEvent | ProviderEvent | ValueEvent | AddingPeerEvent | DialingPeerEvent
|
|
137
|
+
|
|
138
|
+
export interface RoutingTable {
|
|
139
|
+
size: number
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface KadDHT {
|
|
143
|
+
/**
|
|
144
|
+
* Get a value from the DHT, the final ValueEvent will be the best value
|
|
145
|
+
*/
|
|
146
|
+
get: (key: Uint8Array, options?: QueryOptions) => AsyncIterable<QueryEvent>
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Find providers of a given CID
|
|
150
|
+
*/
|
|
151
|
+
findProviders: (key: CID, options?: QueryOptions) => AsyncIterable<QueryEvent>
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Find a peer on the DHT
|
|
155
|
+
*/
|
|
156
|
+
findPeer: (id: PeerId, options?: QueryOptions) => AsyncIterable<QueryEvent>
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Find the closest peers to the passed key
|
|
160
|
+
*/
|
|
161
|
+
getClosestPeers: (key: Uint8Array, options?: QueryOptions) => AsyncIterable<QueryEvent>
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Store provider records for the passed CID on the DHT pointing to us
|
|
165
|
+
*/
|
|
166
|
+
provide: (key: CID, options?: QueryOptions) => AsyncIterable<QueryEvent>
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Store the passed value under the passed key on the DHT
|
|
170
|
+
*/
|
|
171
|
+
put: (key: Uint8Array, value: Uint8Array, options?: QueryOptions) => AsyncIterable<QueryEvent>
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Returns the mode this node is in
|
|
175
|
+
*/
|
|
176
|
+
getMode: () => Promise<'client' | 'server'>
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* If 'server' this node will respond to DHT queries, if 'client' this node will not
|
|
180
|
+
*/
|
|
181
|
+
setMode: (mode: 'client' | 'server') => Promise<void>
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Force a routing table refresh
|
|
185
|
+
*/
|
|
186
|
+
refreshRoutingTable: () => Promise<void>
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface SingleKadDHT extends KadDHT {
|
|
190
|
+
routingTable: RoutingTable
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface DualKadDHT extends KadDHT {
|
|
194
|
+
wan: SingleKadDHT
|
|
195
|
+
lan: SingleKadDHT
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* A selector function takes a DHT key and a list of records and returns the
|
|
200
|
+
* index of the best record in the list
|
|
201
|
+
*/
|
|
202
|
+
export interface SelectFn { (key: Uint8Array, records: Uint8Array[]): number }
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* A validator function takes a DHT key and the value of the record for that key
|
|
206
|
+
* and throws if the record is invalid
|
|
207
|
+
*/
|
|
208
|
+
export interface ValidateFn { (key: Uint8Array, value: Uint8Array): Promise<void> }
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Selectors are a map of key prefixes to selector functions
|
|
212
|
+
*/
|
|
213
|
+
export type Selectors = Record<string, SelectFn>
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Validators are a map of key prefixes to validator functions
|
|
217
|
+
*/
|
|
218
|
+
export type Validators = Record<string, ValidateFn>
|
|
219
|
+
|
|
21
220
|
export interface KadDHTInit {
|
|
22
221
|
/**
|
|
23
222
|
* How many peers to store in each kBucket (default 20)
|
|
@@ -103,106 +302,6 @@ export interface KadDHTComponents {
|
|
|
103
302
|
datastore: Datastore
|
|
104
303
|
}
|
|
105
304
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
*/
|
|
109
|
-
export class DHTContentRouting implements ContentRouting {
|
|
110
|
-
private readonly dht: DHT
|
|
111
|
-
|
|
112
|
-
constructor (dht: DHT) {
|
|
113
|
-
this.dht = dht
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async provide (cid: CID): Promise<void> {
|
|
117
|
-
await drain(this.dht.provide(cid))
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async * findProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<PeerInfo, void, undefined> {
|
|
121
|
-
for await (const event of this.dht.findProviders(cid, options)) {
|
|
122
|
-
if (event.name === 'PROVIDER') {
|
|
123
|
-
yield * event.providers
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> {
|
|
129
|
-
await drain(this.dht.put(key, value, options))
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> {
|
|
133
|
-
for await (const event of this.dht.get(key, options)) {
|
|
134
|
-
if (event.name === 'VALUE') {
|
|
135
|
-
return event.value
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
throw new CodeError('Not found', 'ERR_NOT_FOUND')
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Wrapper class to convert events into returned values
|
|
145
|
-
*/
|
|
146
|
-
export class DHTPeerRouting implements PeerRouting {
|
|
147
|
-
private readonly dht: DHT
|
|
148
|
-
|
|
149
|
-
constructor (dht: DHT) {
|
|
150
|
-
this.dht = dht
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async findPeer (peerId: PeerId, options: AbortOptions = {}): Promise<PeerInfo> {
|
|
154
|
-
for await (const event of this.dht.findPeer(peerId, options)) {
|
|
155
|
-
if (event.name === 'FINAL_PEER') {
|
|
156
|
-
return event.peer
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
throw new CodeError('Not found', 'ERR_NOT_FOUND')
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async * getClosestPeers (key: Uint8Array, options: AbortOptions = {}): AsyncIterable<PeerInfo> {
|
|
164
|
-
for await (const event of this.dht.getClosestPeers(key, options)) {
|
|
165
|
-
if (event.name === 'FINAL_PEER') {
|
|
166
|
-
yield event.peer
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
class KadDHT extends DualKadDHT {
|
|
173
|
-
private readonly contentRouting: ContentRouting
|
|
174
|
-
private readonly peerRouting: PeerRouting
|
|
175
|
-
|
|
176
|
-
constructor (components: KadDHTComponents, init?: KadDHTInit) {
|
|
177
|
-
super(components, new SingleKadDHT(components, {
|
|
178
|
-
protocolPrefix: '/ipfs',
|
|
179
|
-
...init,
|
|
180
|
-
lan: false
|
|
181
|
-
}),
|
|
182
|
-
new SingleKadDHT(components, {
|
|
183
|
-
protocolPrefix: '/ipfs',
|
|
184
|
-
...init,
|
|
185
|
-
clientMode: false,
|
|
186
|
-
lan: true
|
|
187
|
-
}))
|
|
188
|
-
|
|
189
|
-
this.contentRouting = new DHTContentRouting(this)
|
|
190
|
-
this.peerRouting = new DHTPeerRouting(this)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
get [contentRouting] (): ContentRouting {
|
|
194
|
-
return this.contentRouting
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
get [peerRouting] (): PeerRouting {
|
|
198
|
-
return this.peerRouting
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
get [peerDiscovery] (): PeerDiscovery {
|
|
202
|
-
return this
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export function kadDHT (init?: KadDHTInit): (components: KadDHTComponents) => DualDHT {
|
|
207
|
-
return (components: KadDHTComponents) => new KadDHT(components, init)
|
|
305
|
+
export function kadDHT (init?: KadDHTInit): (components: KadDHTComponents) => DualKadDHT {
|
|
306
|
+
return (components: KadDHTComponents) => new DefaultDualKadDHT(components, init)
|
|
208
307
|
}
|
package/src/kad-dht.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { symbol } from '@libp2p/interface-peer-discovery'
|
|
2
1
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
3
2
|
import { type Logger, logger } from '@libp2p/logger'
|
|
4
3
|
import { selectors as recordSelectors } from '@libp2p/record/selectors'
|
|
@@ -20,8 +19,7 @@ import {
|
|
|
20
19
|
removePrivateAddresses,
|
|
21
20
|
removePublicAddresses
|
|
22
21
|
} from './utils.js'
|
|
23
|
-
import type { KadDHTComponents, KadDHTInit } from './index.js'
|
|
24
|
-
import type { QueryOptions, Validators, Selectors, DHT, QueryEvent } from '@libp2p/interface-dht'
|
|
22
|
+
import type { KadDHTComponents, KadDHTInit, QueryOptions, Validators, Selectors, KadDHT, QueryEvent } from './index.js'
|
|
25
23
|
import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
|
|
26
24
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
27
25
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
@@ -41,7 +39,7 @@ export interface SingleKadDHTInit extends KadDHTInit {
|
|
|
41
39
|
* A DHT implementation modelled after Kademlia with S/Kademlia modifications.
|
|
42
40
|
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
|
|
43
41
|
*/
|
|
44
|
-
export class
|
|
42
|
+
export class DefaultKadDHT extends EventEmitter<PeerDiscoveryEvents> implements KadDHT {
|
|
45
43
|
public protocol: string
|
|
46
44
|
public routingTable: RoutingTable
|
|
47
45
|
public providers: Providers
|
|
@@ -219,10 +217,6 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
|
|
|
219
217
|
})
|
|
220
218
|
}
|
|
221
219
|
|
|
222
|
-
readonly [symbol] = true
|
|
223
|
-
|
|
224
|
-
readonly [Symbol.toStringTag] = '@libp2p/kad-dht'
|
|
225
|
-
|
|
226
220
|
async onPeerConnect (peerData: PeerInfo): Promise<void> {
|
|
227
221
|
this.log('peer %p connected with protocols', peerData.id, peerData.protocols)
|
|
228
222
|
|
package/src/network.ts
CHANGED
|
@@ -13,9 +13,8 @@ import {
|
|
|
13
13
|
peerResponseEvent,
|
|
14
14
|
queryErrorEvent
|
|
15
15
|
} from './query/events.js'
|
|
16
|
-
import type { KadDHTComponents } from './index.js'
|
|
16
|
+
import type { KadDHTComponents, QueryEvent } from './index.js'
|
|
17
17
|
import type { Stream } from '@libp2p/interface-connection'
|
|
18
|
-
import type { QueryEvent } from '@libp2p/interface-dht'
|
|
19
18
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
20
19
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
21
20
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
@@ -13,12 +13,11 @@ import {
|
|
|
13
13
|
valueEvent
|
|
14
14
|
} from '../query/events.js'
|
|
15
15
|
import * as utils from '../utils.js'
|
|
16
|
-
import type { KadDHTComponents } from '../index.js'
|
|
16
|
+
import type { KadDHTComponents, DHTRecord, DialingPeerEvent, FinalPeerEvent, QueryEvent, Validators } from '../index.js'
|
|
17
17
|
import type { Network } from '../network.js'
|
|
18
18
|
import type { QueryManager, QueryOptions } from '../query/manager.js'
|
|
19
19
|
import type { QueryFunc } from '../query/types.js'
|
|
20
20
|
import type { RoutingTable } from '../routing-table/index.js'
|
|
21
|
-
import type { DHTRecord, DialingPeerEvent, FinalPeerEvent, QueryEvent, Validators } from '@libp2p/interface-dht'
|
|
22
21
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
23
22
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
24
23
|
import type { AbortOptions } from '@libp2p/interfaces'
|
package/src/query/events.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MESSAGE_TYPE_LOOKUP } from '../message/index.js'
|
|
2
|
+
import type { SendingQueryEvent, PeerResponseEvent, DialingPeerEvent, AddingPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent } from '../index.js'
|
|
2
3
|
import type { Message } from '../message/dht.js'
|
|
3
|
-
import type { SendingQueryEvent, PeerResponseEvent, DialingPeerEvent, AddingPeerEvent, ValueEvent, ProviderEvent, QueryErrorEvent, FinalPeerEvent } from '@libp2p/interface-dht'
|
|
4
4
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
5
5
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
6
6
|
import type { Libp2pRecord } from '@libp2p/record'
|
package/src/query/manager.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from '../constants.js'
|
|
12
12
|
import { queryPath } from './query-path.js'
|
|
13
13
|
import type { QueryFunc } from './types.js'
|
|
14
|
-
import type { QueryEvent } from '
|
|
14
|
+
import type { QueryEvent } from '../index.js'
|
|
15
15
|
import type { Metric, Metrics } from '@libp2p/interface-metrics'
|
|
16
16
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
17
17
|
import type { AbortOptions } from '@libp2p/interfaces'
|
package/src/query/query-path.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { xor } from 'uint8arrays/xor'
|
|
|
7
7
|
import { convertPeerId, convertBuffer } from '../utils.js'
|
|
8
8
|
import { queryErrorEvent } from './events.js'
|
|
9
9
|
import type { CleanUpEvents } from './manager.js'
|
|
10
|
+
import type { QueryEvent } from '../index.js'
|
|
10
11
|
import type { QueryFunc } from '../query/types.js'
|
|
11
|
-
import type { QueryEvent } from '@libp2p/interface-dht'
|
|
12
12
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
13
13
|
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
14
14
|
import type { Logger } from '@libp2p/logger'
|
package/src/query/types.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { CodeError } from '@libp2p/interfaces/errors'
|
|
|
2
2
|
import { type Logger, logger } from '@libp2p/logger'
|
|
3
3
|
import { verifyRecord } from '@libp2p/record/validators'
|
|
4
4
|
import { bufferToRecordKey } from '../../utils.js'
|
|
5
|
+
import type { Validators } from '../../index.js'
|
|
5
6
|
import type { Message } from '../../message/index.js'
|
|
6
7
|
import type { DHTMessageHandler } from '../index.js'
|
|
7
|
-
import type { Validators } from '@libp2p/interface-dht'
|
|
8
8
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
9
9
|
import type { Datastore } from 'interface-datastore'
|
|
10
10
|
|
package/src/rpc/index.ts
CHANGED
|
@@ -8,10 +8,10 @@ import { GetProvidersHandler, type GetProvidersHandlerComponents } from './handl
|
|
|
8
8
|
import { GetValueHandler, type GetValueHandlerComponents } from './handlers/get-value.js'
|
|
9
9
|
import { PingHandler } from './handlers/ping.js'
|
|
10
10
|
import { PutValueHandler, type PutValueHandlerComponents } from './handlers/put-value.js'
|
|
11
|
+
import type { Validators } from '../index.js'
|
|
11
12
|
import type { PeerRouting } from '../peer-routing'
|
|
12
13
|
import type { Providers } from '../providers'
|
|
13
14
|
import type { RoutingTable } from '../routing-table'
|
|
14
|
-
import type { Validators } from '@libp2p/interface-dht'
|
|
15
15
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
16
16
|
import type { IncomingStreamData } from '@libp2p/interface-registrar'
|
|
17
17
|
|