@@ -5,22 +5,22 @@ import * as lp from 'it-length-prefixed'
55import map from 'it-map'
66import { pushable } from 'it-pushable'
77import take from 'it-take'
8+ import { CID } from 'multiformats/cid'
89import { CustomProgressEvent } from 'progress-events'
910import { 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'
11+ 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'
1112import { BitswapMessage } from './pb/message.ts'
1213import { mergeMessages } from './utils/merge-messages.ts'
1314import { splitMessage } from './utils/split-message.ts'
1415import type { WantOptions } from './bitswap.ts'
15- import type { MultihashHasherLoader } from './index.ts'
1616import type { Block } from './pb/message.ts'
1717import type { QueuedBitswapMessage } from './utils/bitswap-message.ts'
18+ import type { BlockBrokerGetBlockProgressEvents } from '@helia/interface'
1819import type { Provider , Routing , RoutingFindProvidersProgressEvents } from '@helia/interface/routing'
19- import type { Libp2p , AbortOptions , Connection , PeerId , Topology , ComponentLogger , IdentifyResult , Counter , Metrics , Stream } from '@libp2p/interface'
20+ import type { Libp2p , AbortOptions , Connection , PeerId , Topology , ComponentLogger , IdentifyResult , Counter , Metrics , Stream , OpenConnectionProgressEvents , NewStreamProgressEvents } from '@libp2p/interface'
2021import type { Logger } from '@libp2p/logger'
2122import type { PeerQueueJobOptions } from '@libp2p/utils'
2223import type { Multiaddr } from '@multiformats/multiaddr'
23- import type { CID } from 'multiformats/cid'
2424import type { ProgressEvent , ProgressOptions } from 'progress-events'
2525import type { Uint8ArrayList } from 'uint8arraylist'
2626
@@ -47,25 +47,28 @@ export interface BitswapProvider {
4747}
4848
4949export type BitswapNetworkProgressEvents =
50- ProgressEvent < 'bitswap:dial' , PeerId | Multiaddr | Multiaddr [ ] >
50+ ProgressEvent < 'bitswap:dial' , PeerId | Multiaddr | Multiaddr [ ] > |
51+ OpenConnectionProgressEvents |
52+ NewStreamProgressEvents
5153
5254export type BitswapNetworkWantProgressEvents =
5355 ProgressEvent < 'bitswap:send-wantlist' , PeerId > |
5456 ProgressEvent < 'bitswap:send-wantlist:error' , { peer : PeerId , error : Error } > |
5557 ProgressEvent < 'bitswap:find-providers' , CID > |
5658 ProgressEvent < 'bitswap:found-provider' , BitswapProvider > |
5759 BitswapNetworkProgressEvents |
58- RoutingFindProvidersProgressEvents
60+ RoutingFindProvidersProgressEvents |
61+ BlockBrokerGetBlockProgressEvents
5962
6063export type BitswapNetworkNotifyProgressEvents =
6164 BitswapNetworkProgressEvents |
6265 ProgressEvent < 'bitswap:send-block' , PeerId >
6366
6467export interface NetworkInit {
65- hashLoader ?: MultihashHasherLoader
6668 maxInboundStreams ?: number
6769 maxOutboundStreams ?: number
6870 messageReceiveTimeout ?: number
71+ messageSendTimeout ?: number
6972 messageSendConcurrency ?: number
7073 protocols ?: string [ ]
7174 runOnLimitedConnections ?: boolean
@@ -81,12 +84,16 @@ export interface NetworkComponents {
8184}
8285
8386export interface BitswapMessageEventDetail {
87+ /**
88+ * @deprecated access the peer via connection.remotePeer instead
89+ */
8490 peer : PeerId
8591 message : BitswapMessage
92+ connection : Connection
8693}
8794
8895export interface NetworkEvents {
89- 'bitswap:message' : CustomEvent < { peer : PeerId , message : BitswapMessage } >
96+ 'bitswap:message' : CustomEvent < BitswapMessageEventDetail >
9097 'peer:connected' : CustomEvent < PeerId >
9198 'peer:disconnected' : CustomEvent < PeerId >
9299}
@@ -104,6 +111,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
104111 private readonly maxInboundStreams : number
105112 private readonly maxOutboundStreams : number
106113 private readonly messageReceiveTimeout : number
114+ private readonly messageSendTimeout : number
107115 private registrarIds : string [ ]
108116 private readonly metrics : { blocksSent ?: Counter , dataSent ?: Counter }
109117 private readonly sendQueue : PeerQueue < void , SendMessageJobOptions >
@@ -126,6 +134,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
126134 this . maxInboundStreams = init . maxInboundStreams ?? DEFAULT_MAX_INBOUND_STREAMS
127135 this . maxOutboundStreams = init . maxOutboundStreams ?? DEFAULT_MAX_OUTBOUND_STREAMS
128136 this . messageReceiveTimeout = init . messageReceiveTimeout ?? DEFAULT_MESSAGE_RECEIVE_TIMEOUT
137+ this . messageSendTimeout = init . messageSendTimeout ?? DEFAULT_MESSAGE_SEND_TIMEOUT
129138 this . runOnLimitedConnections = init . runOnLimitedConnections ?? DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS
130139 this . maxIncomingMessageSize = init . maxIncomingMessageSize ?? DEFAULT_MAX_OUTGOING_MESSAGE_SIZE
131140 this . maxOutgoingMessageSize = init . maxOutgoingMessageSize ?? init . maxIncomingMessageSize ?? DEFAULT_MAX_INCOMING_MESSAGE_SIZE
@@ -245,10 +254,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
245254 const message = BitswapMessage . decode ( data )
246255 this . log ( 'incoming new bitswap %s message from %p on stream' , stream . protocol , connection . remotePeer , stream . id )
247256
248- this . safeDispatchEvent ( 'bitswap:message' , {
257+ this . safeDispatchEvent < BitswapMessageEventDetail > ( 'bitswap:message' , {
249258 detail : {
250259 peer : connection . remotePeer ,
251- message
260+ message,
261+ connection
252262 }
253263 } )
254264
@@ -287,7 +297,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
287297 continue
288298 }
289299
290- options ?. onProgress ?.( new CustomProgressEvent ( 'bitswap:found-provider' , {
300+ options ?. onProgress ?.( new CustomProgressEvent < BitswapProvider > ( 'bitswap:found-provider' , {
291301 type : 'bitswap' ,
292302 cid,
293303 provider,
@@ -325,8 +335,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
325335 }
326336
327337 /**
328- * Connect to the given peer
329- * Send the given msg (instance of Message) to the given peer
338+ * Connect to the specified peer and send the given message
330339 */
331340 async sendMessage ( peerId : PeerId , message : QueuedBitswapMessage , options ?: AbortOptions & ProgressOptions < BitswapNetworkWantProgressEvents > ) : Promise < void > {
332341 if ( ! this . running ) {
@@ -359,7 +368,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
359368 options ?. onProgress ?.( new CustomProgressEvent < PeerId > ( 'bitswap:network:send-wantlist' , peerId ) )
360369
361370 const stream = await this . libp2p . dialProtocol ( peerId , BITSWAP_120 , options )
362- await stream . closeRead ( )
371+ await stream . closeRead ( options )
363372
364373 try {
365374 for ( const buf of splitMessage ( message , this . maxOutgoingMessageSize ) ) {
@@ -377,8 +386,9 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
377386
378387 this . _updateSentStats ( message . blocks )
379388 } , {
389+ onProgress : options ?. onProgress ,
380390 peerId,
381- signal : options ?. signal ,
391+ signal : options ?. signal ?? AbortSignal . timeout ( this . messageSendTimeout ) ,
382392 message
383393 } )
384394 }
0 commit comments