@@ -4,47 +4,73 @@ import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events'
44import { type PeerDiscovery , peerDiscovery , type PeerDiscoveryEvents } from '@libp2p/interface/peer-discovery'
55import { type PeerRouting , peerRouting } from '@libp2p/interface/peer-routing'
66import { logger } from '@libp2p/logger'
7- import drain from 'it-drain'
87import merge from 'it-merge'
98import isPrivate from 'private-ip'
9+ import { CustomProgressEvent } from 'progress-events'
1010import { DefaultKadDHT } from './kad-dht.js'
1111import { queryErrorEvent } from './query/events.js'
1212import type { DualKadDHT , KadDHT , KadDHTComponents , KadDHTInit , QueryEvent , QueryOptions } from './index.js'
1313import type { PeerId } from '@libp2p/interface/peer-id'
1414import type { PeerInfo } from '@libp2p/interface/peer-info'
1515import type { Multiaddr } from '@multiformats/multiaddr'
1616import 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 >
1730
1831const log = logger ( 'libp2p:kad-dht' )
1932
2033/**
2134 * Wrapper class to convert events into returned values
2235 */
23- class DHTContentRouting implements ContentRouting {
36+ class DHTContentRouting implements ContentRouting <
37+ ProvideProgressEvents ,
38+ FindProvidersProgressEvents ,
39+ PutProgressEvents ,
40+ GetProgressEvents
41+ > {
2442 private readonly dht : KadDHT
2543
2644 constructor ( dht : KadDHT ) {
2745 this . dht = dht
2846 }
2947
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+ }
3252 }
3353
34- async * findProviders ( cid : CID , options : QueryOptions = { } ) : AsyncGenerator < PeerInfo , void , undefined > {
54+ async * findProviders ( cid : CID , options : QueryOptions & ProgressOptions < FindProvidersProgressEvents > = { } ) : AsyncGenerator < PeerInfo , void , undefined > {
3555 for await ( const event of this . dht . findProviders ( cid , options ) ) {
56+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:content-routing:find-providers:dht:event' , event ) )
57+
3658 if ( event . name === 'PROVIDER' ) {
3759 yield * event . providers
3860 }
3961 }
4062 }
4163
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+ }
4468 }
4569
46- async get ( key : Uint8Array , options ? : QueryOptions ) : Promise < Uint8Array > {
70+ async get ( key : Uint8Array , options : QueryOptions & ProgressOptions < GetProgressEvents > = { } ) : Promise < Uint8Array > {
4771 for await ( const event of this . dht . get ( key , options ) ) {
72+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:content-routing:get:dht:event' , event ) )
73+
4874 if ( event . name === 'VALUE' ) {
4975 return event . value
5076 }
@@ -54,18 +80,29 @@ class DHTContentRouting implements ContentRouting {
5480 }
5581}
5682
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+
5789/**
5890 * Wrapper class to convert events into returned values
5991 */
60- class DHTPeerRouting implements PeerRouting {
92+ class DHTPeerRouting implements PeerRouting <
93+ FindPeerProgressEvents ,
94+ GetClosestPeersProgressEvents
95+ > {
6196 private readonly dht : KadDHT
6297
6398 constructor ( dht : KadDHT ) {
6499 this . dht = dht
65100 }
66101
67- async findPeer ( peerId : PeerId , options : QueryOptions = { } ) : Promise < PeerInfo > {
102+ async findPeer ( peerId : PeerId , options : QueryOptions & ProgressOptions < FindPeerProgressEvents > = { } ) : Promise < PeerInfo > {
68103 for await ( const event of this . dht . findPeer ( peerId , options ) ) {
104+ options . onProgress ?.( new CustomProgressEvent ( 'libp2p:peer-routing:find-peer:dht:event' , event ) )
105+
69106 if ( event . name === 'FINAL_PEER' ) {
70107 return event . peer
71108 }
@@ -74,8 +111,10 @@ class DHTPeerRouting implements PeerRouting {
74111 throw new CodeError ( 'Not found' , 'ERR_NOT_FOUND' )
75112 }
76113
77- async * getClosestPeers ( key : Uint8Array , options : QueryOptions = { } ) : AsyncIterable < PeerInfo > {
114+ async * getClosestPeers ( key : Uint8Array , options : QueryOptions & ProgressOptions < GetClosestPeersProgressEvents > = { } ) : AsyncIterable < PeerInfo > {
78115 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+
79118 if ( event . name === 'FINAL_PEER' ) {
80119 yield event . peer
81120 }
0 commit comments