@@ -18,6 +18,8 @@ import type { Logger } from '@libp2p/logger'
1818import type { Duplex } from 'it-stream-types'
1919import type { PeerInfo } from '@libp2p/interfaces/peer-info'
2020import { Components , Initializable } from '@libp2p/interfaces/components'
21+ import type { Stream } from '@libp2p/interfaces/connection'
22+ import { abortableDuplex } from 'abortable-iterator'
2123
2224export interface NetworkInit {
2325 protocol : string
@@ -87,15 +89,17 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
8789 }
8890
8991 this . log ( 'sending %s to %p' , msg . type , to )
92+ yield dialingPeerEvent ( { peer : to } )
93+ yield sendingQueryEvent ( { to, type : msg . type } )
9094
91- try {
92- yield dialingPeerEvent ( { peer : to } )
93-
94- const { stream } = await this . components . getDialer ( ) . dialProtocol ( to , this . protocol , options )
95+ let stream : Stream | undefined
9596
96- yield sendingQueryEvent ( { to, type : msg . type } )
97+ try {
98+ const connection = await this . components . getConnectionManager ( ) . openConnection ( to , options )
99+ const streamData = await connection . newStream ( this . protocol )
100+ stream = streamData . stream
97101
98- const response = await this . _writeReadMessage ( stream , msg . serialize ( ) )
102+ const response = await this . _writeReadMessage ( stream , msg . serialize ( ) , options )
99103
100104 yield peerResponseEvent ( {
101105 from : to ,
@@ -106,6 +110,10 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
106110 } )
107111 } catch ( err : any ) {
108112 yield queryErrorEvent ( { from : to , error : err } )
113+ } finally {
114+ if ( stream != null ) {
115+ stream . close ( )
116+ }
109117 }
110118 }
111119
@@ -118,26 +126,36 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
118126 }
119127
120128 this . log ( 'sending %s to %p' , msg . type , to )
121-
122129 yield dialingPeerEvent ( { peer : to } )
123-
124- const { stream } = await this . components . getDialer ( ) . dialProtocol ( to , this . protocol , options )
125-
126130 yield sendingQueryEvent ( { to, type : msg . type } )
127131
132+ let stream : Stream | undefined
133+
128134 try {
129- await this . _writeMessage ( stream , msg . serialize ( ) )
135+ const connection = await this . components . getConnectionManager ( ) . openConnection ( to , options )
136+ const data = await connection . newStream ( this . protocol )
137+ stream = data . stream
138+
139+ await this . _writeMessage ( stream , msg . serialize ( ) , options )
130140
131141 yield peerResponseEvent ( { from : to , messageType : msg . type } )
132142 } catch ( err : any ) {
133143 yield queryErrorEvent ( { from : to , error : err } )
144+ } finally {
145+ if ( stream != null ) {
146+ stream . close ( )
147+ }
134148 }
135149 }
136150
137151 /**
138152 * Write a message to the given stream
139153 */
140- async _writeMessage ( stream : Duplex < Uint8Array > , msg : Uint8Array ) {
154+ async _writeMessage ( stream : Duplex < Uint8Array > , msg : Uint8Array , options : AbortOptions ) {
155+ if ( options . signal != null ) {
156+ stream = abortableDuplex ( stream , options . signal )
157+ }
158+
141159 await pipe (
142160 [ msg ] ,
143161 lp . encode ( ) ,
@@ -151,7 +169,11 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
151169 * If no response is received after the specified timeout
152170 * this will error out.
153171 */
154- async _writeReadMessage ( stream : Duplex < Uint8Array > , msg : Uint8Array ) {
172+ async _writeReadMessage ( stream : Duplex < Uint8Array > , msg : Uint8Array , options : AbortOptions ) {
173+ if ( options . signal != null ) {
174+ stream = abortableDuplex ( stream , options . signal )
175+ }
176+
155177 const res = await pipe (
156178 [ msg ] ,
157179 lp . encode ( ) ,
0 commit comments