@@ -7,7 +7,7 @@ import { pushable } from 'it-pushable'
77import take from 'it-take'
88import { CustomProgressEvent } from 'progress-events'
99import { raceEvent } from 'race-event'
10- import { BITSWAP_120 , DEFAULT_MAX_INBOUND_STREAMS , DEFAULT_MAX_INCOMING_MESSAGE_SIZE , DEFAULT_MAX_OUTBOUND_STREAMS , DEFAULT_MAX_OUTGOING_MESSAGE_SIZE , DEFAULT_MAX_PROVIDERS_PER_REQUEST , DEFAULT_MESSAGE_RECEIVE_TIMEOUT , DEFAULT_MESSAGE_SEND_CONCURRENCY , DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS } from './constants.ts'
10+ import { BITSWAP_120 , DEFAULT_MAX_INBOUND_STREAMS , DEFAULT_MAX_INCOMING_MESSAGE_SIZE , DEFAULT_MAX_OUTBOUND_STREAMS , DEFAULT_MAX_OUTGOING_MESSAGE_SIZE , DEFAULT_MAX_PROVIDERS_PER_REQUEST , DEFAULT_MESSAGE_RECEIVE_TIMEOUT , DEFAULT_MESSAGE_SEND_CONCURRENCY , DEFAULT_MESSAGE_SEND_TIMEOUT , DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS } from './constants.ts'
1111import { BitswapMessage } from './pb/message.ts'
1212import { mergeMessages } from './utils/merge-messages.ts'
1313import { splitMessage } from './utils/split-message.ts'
@@ -16,13 +16,14 @@ import type { MultihashHasherLoader } from './index.ts'
1616import type { Block } from './pb/message.ts'
1717import type { QueuedBitswapMessage } from './utils/bitswap-message.ts'
1818import type { Provider , Routing , RoutingFindProvidersProgressEvents } from '@helia/interface/routing'
19- import type { Libp2p , AbortOptions , Connection , PeerId , Topology , ComponentLogger , IdentifyResult , Counter , Metrics , Stream } from '@libp2p/interface'
19+ import type { Libp2p , AbortOptions , Connection , PeerId , Topology , ComponentLogger , IdentifyResult , Counter , Metrics , Stream , OpenConnectionProgressEvents , NewStreamProgressEvents } from '@libp2p/interface'
2020import type { Logger } from '@libp2p/logger'
2121import type { PeerQueueJobOptions } from '@libp2p/utils'
2222import type { Multiaddr } from '@multiformats/multiaddr'
23- import type { CID } from 'multiformats/cid'
23+ import { CID } from 'multiformats/cid'
2424import type { ProgressEvent , ProgressOptions } from 'progress-events'
2525import type { Uint8ArrayList } from 'uint8arraylist'
26+ import type { BlockBrokerGetBlockProgressEvents } from '@helia/interface'
2627
2728export interface BitswapProvider {
2829 /**
@@ -47,15 +48,18 @@ export interface BitswapProvider {
4748}
4849
4950export type BitswapNetworkProgressEvents =
50- ProgressEvent < 'bitswap:dial' , PeerId | Multiaddr | Multiaddr [ ] >
51+ ProgressEvent < 'bitswap:dial' , PeerId | Multiaddr | Multiaddr [ ] > |
52+ OpenConnectionProgressEvents |
53+ NewStreamProgressEvents
5154
5255export type BitswapNetworkWantProgressEvents =
5356 ProgressEvent < 'bitswap:send-wantlist' , PeerId > |
5457 ProgressEvent < 'bitswap:send-wantlist:error' , { peer : PeerId , error : Error } > |
5558 ProgressEvent < 'bitswap:find-providers' , CID > |
5659 ProgressEvent < 'bitswap:found-provider' , BitswapProvider > |
5760 BitswapNetworkProgressEvents |
58- RoutingFindProvidersProgressEvents
61+ RoutingFindProvidersProgressEvents |
62+ BlockBrokerGetBlockProgressEvents
5963
6064export type BitswapNetworkNotifyProgressEvents =
6165 BitswapNetworkProgressEvents |
@@ -66,6 +70,7 @@ export interface NetworkInit {
6670 maxInboundStreams ?: number
6771 maxOutboundStreams ?: number
6872 messageReceiveTimeout ?: number
73+ messageSendTimeout ?: number
6974 messageSendConcurrency ?: number
7075 protocols ?: string [ ]
7176 runOnLimitedConnections ?: boolean
@@ -81,12 +86,16 @@ export interface NetworkComponents {
8186}
8287
8388export interface BitswapMessageEventDetail {
89+ /**
90+ * @deprecated access the peer via connection.remotePeer instead
91+ */
8492 peer : PeerId
8593 message : BitswapMessage
94+ connection : Connection
8695}
8796
8897export interface NetworkEvents {
89- 'bitswap:message' : CustomEvent < { peer : PeerId , message : BitswapMessage } >
98+ 'bitswap:message' : CustomEvent < BitswapMessageEventDetail >
9099 'peer:connected' : CustomEvent < PeerId >
91100 'peer:disconnected' : CustomEvent < PeerId >
92101}
@@ -104,6 +113,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
104113 private readonly maxInboundStreams : number
105114 private readonly maxOutboundStreams : number
106115 private readonly messageReceiveTimeout : number
116+ private readonly messageSendTimeout : number
107117 private registrarIds : string [ ]
108118 private readonly metrics : { blocksSent ?: Counter , dataSent ?: Counter }
109119 private readonly sendQueue : PeerQueue < void , SendMessageJobOptions >
@@ -126,6 +136,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
126136 this . maxInboundStreams = init . maxInboundStreams ?? DEFAULT_MAX_INBOUND_STREAMS
127137 this . maxOutboundStreams = init . maxOutboundStreams ?? DEFAULT_MAX_OUTBOUND_STREAMS
128138 this . messageReceiveTimeout = init . messageReceiveTimeout ?? DEFAULT_MESSAGE_RECEIVE_TIMEOUT
139+ this . messageSendTimeout = init . messageSendTimeout ?? DEFAULT_MESSAGE_SEND_TIMEOUT
129140 this . runOnLimitedConnections = init . runOnLimitedConnections ?? DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS
130141 this . maxIncomingMessageSize = init . maxIncomingMessageSize ?? DEFAULT_MAX_OUTGOING_MESSAGE_SIZE
131142 this . maxOutgoingMessageSize = init . maxOutgoingMessageSize ?? init . maxIncomingMessageSize ?? DEFAULT_MAX_INCOMING_MESSAGE_SIZE
@@ -245,10 +256,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
245256 const message = BitswapMessage . decode ( data )
246257 this . log ( 'incoming new bitswap %s message from %p on stream' , stream . protocol , connection . remotePeer , stream . id )
247258
248- this . safeDispatchEvent ( 'bitswap:message' , {
259+ this . safeDispatchEvent < BitswapMessageEventDetail > ( 'bitswap:message' , {
249260 detail : {
250261 peer : connection . remotePeer ,
251- message
262+ message,
263+ connection
252264 }
253265 } )
254266
@@ -287,7 +299,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
287299 continue
288300 }
289301
290- options ?. onProgress ?.( new CustomProgressEvent ( 'bitswap:found-provider' , {
302+ options ?. onProgress ?.( new CustomProgressEvent < BitswapProvider > ( 'bitswap:found-provider' , {
291303 type : 'bitswap' ,
292304 cid,
293305 provider,
@@ -325,8 +337,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
325337 }
326338
327339 /**
328- * Connect to the given peer
329- * Send the given msg (instance of Message) to the given peer
340+ * Connect to the specified peer and send the given message
330341 */
331342 async sendMessage ( peerId : PeerId , message : QueuedBitswapMessage , options ?: AbortOptions & ProgressOptions < BitswapNetworkWantProgressEvents > ) : Promise < void > {
332343 if ( ! this . running ) {
@@ -359,7 +370,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
359370 options ?. onProgress ?.( new CustomProgressEvent < PeerId > ( 'bitswap:network:send-wantlist' , peerId ) )
360371
361372 const stream = await this . libp2p . dialProtocol ( peerId , BITSWAP_120 , options )
362- await stream . closeRead ( )
373+ await stream . closeRead ( options )
363374
364375 try {
365376 for ( const buf of splitMessage ( message , this . maxOutgoingMessageSize ) ) {
@@ -377,8 +388,9 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
377388
378389 this . _updateSentStats ( message . blocks )
379390 } , {
391+ onProgress : options ?. onProgress ,
380392 peerId,
381- signal : options ?. signal ,
393+ signal : options ?. signal ?? AbortSignal . timeout ( this . messageSendTimeout ) ,
382394 message
383395 } )
384396 }
0 commit comments