@@ -4,47 +4,73 @@ import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events'
4
4
import { type PeerDiscovery , peerDiscovery , type PeerDiscoveryEvents } from '@libp2p/interface/peer-discovery'
5
5
import { type PeerRouting , peerRouting } from '@libp2p/interface/peer-routing'
6
6
import { logger } from '@libp2p/logger'
7
- import drain from 'it-drain'
8
7
import merge from 'it-merge'
9
8
import isPrivate from 'private-ip'
9
+ import { CustomProgressEvent } from 'progress-events'
10
10
import { DefaultKadDHT } from './kad-dht.js'
11
11
import { queryErrorEvent } from './query/events.js'
12
12
import type { DualKadDHT , KadDHT , KadDHTComponents , KadDHTInit , QueryEvent , QueryOptions } from './index.js'
13
13
import type { PeerId } from '@libp2p/interface/peer-id'
14
14
import type { PeerInfo } from '@libp2p/interface/peer-info'
15
15
import type { Multiaddr } from '@multiformats/multiaddr'
16
16
import type { CID } from 'multiformats/cid'
17
+ import type { ProgressEvent , ProgressOptions } from 'progress-events'
18
+
19
+ export type ProvideProgressEvents =
20
+ ProgressEvent < 'libp2p:content-routing:provide:dht:event' , QueryEvent >
21
+
22
+ export type FindProvidersProgressEvents =
23
+ ProgressEvent < 'libp2p:content-routing:find-providers:dht:event' , QueryEvent >
24
+
25
+ export type PutProgressEvents =
26
+ ProgressEvent < 'libp2p:content-routing:put:dht:event' , QueryEvent >
27
+
28
+ export type GetProgressEvents =
29
+ ProgressEvent < 'libp2p:content-routing:get:dht:event' , QueryEvent >
17
30
18
31
const log = logger ( 'libp2p:kad-dht' )
19
32
20
33
/**
21
34
* Wrapper class to convert events into returned values
22
35
*/
23
- class DHTContentRouting implements ContentRouting {
36
+ class DHTContentRouting implements ContentRouting <
37
+ ProvideProgressEvents ,
38
+ FindProvidersProgressEvents ,
39
+ PutProgressEvents ,
40
+ GetProgressEvents
41
+ > {
24
42
private readonly dht : KadDHT
25
43
26
44
constructor ( dht : KadDHT ) {
27
45
this . dht = dht
28
46
}
29
47
30
- async provide ( cid : CID , options : QueryOptions = { } ) : Promise < void > {
31
- await drain ( this . dht . provide ( cid , options ) )
48
+ async provide ( cid : CID , options : QueryOptions & ProgressOptions < ProvideProgressEvents > = { } ) : Promise < void > {
49
+ for await ( const event of this . dht . provide ( cid , options ) ) {
50
+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:content-routing:provide:dht:event' , event ) )
51
+ }
32
52
}
33
53
34
- async * findProviders ( cid : CID , options : QueryOptions = { } ) : AsyncGenerator < PeerInfo , void , undefined > {
54
+ async * findProviders ( cid : CID , options : QueryOptions & ProgressOptions < FindProvidersProgressEvents > = { } ) : AsyncGenerator < PeerInfo , void , undefined > {
35
55
for await ( const event of this . dht . findProviders ( cid , options ) ) {
56
+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:content-routing:find-providers:dht:event' , event ) )
57
+
36
58
if ( event . name === 'PROVIDER' ) {
37
59
yield * event . providers
38
60
}
39
61
}
40
62
}
41
63
42
- async put ( key : Uint8Array , value : Uint8Array , options ?: QueryOptions ) : Promise < void > {
43
- await drain ( this . dht . put ( key , value , options ) )
64
+ async put ( key : Uint8Array , value : Uint8Array , options : QueryOptions & ProgressOptions < PutProgressEvents > = { } ) : Promise < void > {
65
+ for await ( const event of this . dht . put ( key , value , options ) ) {
66
+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:content-routing:put:dht:event' , event ) )
67
+ }
44
68
}
45
69
46
- async get ( key : Uint8Array , options ? : QueryOptions ) : Promise < Uint8Array > {
70
+ async get ( key : Uint8Array , options : QueryOptions & ProgressOptions < GetProgressEvents > = { } ) : Promise < Uint8Array > {
47
71
for await ( const event of this . dht . get ( key , options ) ) {
72
+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:content-routing:get:dht:event' , event ) )
73
+
48
74
if ( event . name === 'VALUE' ) {
49
75
return event . value
50
76
}
@@ -54,18 +80,29 @@ class DHTContentRouting implements ContentRouting {
54
80
}
55
81
}
56
82
83
+ export type FindPeerProgressEvents =
84
+ ProgressEvent < 'libp2p:peer-routing:find-peer:dht:event' , QueryEvent >
85
+
86
+ export type GetClosestPeersProgressEvents =
87
+ ProgressEvent < 'libp2p:peer-routing:get-closest-peers:dht:event' , QueryEvent >
88
+
57
89
/**
58
90
* Wrapper class to convert events into returned values
59
91
*/
60
- class DHTPeerRouting implements PeerRouting {
92
+ class DHTPeerRouting implements PeerRouting <
93
+ FindPeerProgressEvents ,
94
+ GetClosestPeersProgressEvents
95
+ > {
61
96
private readonly dht : KadDHT
62
97
63
98
constructor ( dht : KadDHT ) {
64
99
this . dht = dht
65
100
}
66
101
67
- async findPeer ( peerId : PeerId , options : QueryOptions = { } ) : Promise < PeerInfo > {
102
+ async findPeer ( peerId : PeerId , options : QueryOptions & ProgressOptions < FindPeerProgressEvents > = { } ) : Promise < PeerInfo > {
68
103
for await ( const event of this . dht . findPeer ( peerId , options ) ) {
104
+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:peer-routing:find-peer:dht:event' , event ) )
105
+
69
106
if ( event . name === 'FINAL_PEER' ) {
70
107
return event . peer
71
108
}
@@ -74,8 +111,10 @@ class DHTPeerRouting implements PeerRouting {
74
111
throw new CodeError ( 'Not found' , 'ERR_NOT_FOUND' )
75
112
}
76
113
77
- async * getClosestPeers ( key : Uint8Array , options : QueryOptions = { } ) : AsyncIterable < PeerInfo > {
114
+ async * getClosestPeers ( key : Uint8Array , options : QueryOptions & ProgressOptions < GetClosestPeersProgressEvents > = { } ) : AsyncIterable < PeerInfo > {
78
115
for await ( const event of this . dht . getClosestPeers ( key , options ) ) {
116
+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:peer-routing:get-closest-peers:dht:event' , event ) )
117
+
79
118
if ( event . name === 'FINAL_PEER' ) {
80
119
yield event . peer
81
120
}
0 commit comments