File tree Expand file tree Collapse file tree 1 file changed +19
-19
lines changed Expand file tree Collapse file tree 1 file changed +19
-19
lines changed Original file line number Diff line number Diff line change @@ -239,23 +239,7 @@ export class Network {
239
239
const connection = await this . _libp2p . dial ( peer )
240
240
const stream = await connection . newStream ( [ BITSWAP120 , BITSWAP110 , BITSWAP100 ] )
241
241
242
- /** @type {Uint8Array } */
243
- let serialized
244
- switch ( stream . stat . protocol ) {
245
- case BITSWAP100 :
246
- serialized = msg . serializeToBitswap100 ( )
247
- break
248
- case BITSWAP110 :
249
- case BITSWAP120 :
250
- serialized = msg . serializeToBitswap110 ( )
251
- break
252
- default :
253
- throw new Error ( 'Unknown protocol: ' + stream . stat . protocol )
254
- }
255
-
256
- await writeMessage ( stream , serialized , this . _log )
257
-
258
- stream . close ( )
242
+ await writeMessage ( stream , msg , this . _log )
259
243
260
244
this . _updateSentStats ( peer , msg . blocks )
261
245
}
@@ -297,17 +281,33 @@ export class Network {
297
281
/**
298
282
*
299
283
* @param {Stream } stream
300
- * @param {Uint8Array } msg
284
+ * @param {Message } msg
301
285
* @param {* } log
302
286
*/
303
287
async function writeMessage ( stream , msg , log ) {
304
288
try {
289
+ /** @type {Uint8Array } */
290
+ let serialized
291
+ switch ( stream . stat . protocol ) {
292
+ case BITSWAP100 :
293
+ serialized = msg . serializeToBitswap100 ( )
294
+ break
295
+ case BITSWAP110 :
296
+ case BITSWAP120 :
297
+ serialized = msg . serializeToBitswap110 ( )
298
+ break
299
+ default :
300
+ throw new Error ( 'Unknown protocol: ' + stream . stat . protocol )
301
+ }
302
+
305
303
await pipe (
306
- [ msg ] ,
304
+ [ serialized ] ,
307
305
lp . encode ( ) ,
308
306
stream
309
307
)
310
308
} catch ( err ) {
311
309
log ( err )
310
+ } finally {
311
+ stream . close ( )
312
312
}
313
313
}
You can’t perform that action at this time.
0 commit comments