11import { abortableSource } from 'abortable-iterator'
22import { encode , decode } from 'it-length-prefixed'
3- import map from 'it-map'
4- import merge from 'it-merge'
53import { pipe } from 'it-pipe'
64import { pushable , type Pushable } from 'it-pushable'
75import type { Stream } from '@libp2p/interface'
@@ -18,24 +16,17 @@ interface InboundStreamOpts {
1816}
1917
2018export class OutboundStream {
21- private readonly pushable : Pushable < Uint8Array >
22- private readonly lpPushable : Pushable < Uint8ArrayList >
19+ private readonly pushable : Pushable < Uint8Array | Uint8ArrayList >
2320 private readonly closeController : AbortController
2421 private readonly maxBufferSize : number
2522
2623 constructor ( private readonly rawStream : Stream , errCallback : ( e : Error ) => void , opts : OutboundStreamOpts ) {
27- this . pushable = pushable ( { objectMode : false } )
28- this . lpPushable = pushable ( { objectMode : false } )
24+ this . pushable = pushable ( )
2925 this . closeController = new AbortController ( )
3026 this . maxBufferSize = opts . maxBufferSize ?? Infinity
3127
3228 pipe (
33- abortableSource (
34- merge (
35- this . lpPushable ,
36- map ( this . pushable , buf => encode . single ( buf ) )
37- ) , this . closeController . signal , { returnOnAbort : true }
38- ) ,
29+ abortableSource ( this . pushable , this . closeController . signal , { returnOnAbort : true } ) ,
3930 this . rawStream
4031 ) . catch ( errCallback )
4132 }
@@ -51,24 +42,23 @@ export class OutboundStream {
5142 throw Error ( `OutboundStream buffer full, size > ${ this . maxBufferSize } ` )
5243 }
5344
54- this . pushable . push ( data )
45+ this . pushable . push ( encode . single ( data ) )
5546 }
5647
5748 /**
5849 * Same to push() but this is prefixed data so no need to encode length prefixed again
5950 */
6051 pushPrefixed ( data : Uint8ArrayList ) : void {
61- if ( this . lpPushable . readableLength > this . maxBufferSize ) {
52+ if ( this . pushable . readableLength > this . maxBufferSize ) {
6253 throw Error ( `OutboundStream buffer full, size > ${ this . maxBufferSize } ` )
6354 }
64- this . lpPushable . push ( data )
55+ this . pushable . push ( data )
6556 }
6657
6758 async close ( ) : Promise < void > {
6859 this . closeController . abort ( )
6960 // similar to pushable.end() but clear the internal buffer
7061 await this . pushable . return ( )
71- await this . lpPushable . return ( )
7262 await this . rawStream . close ( )
7363 }
7464}
0 commit comments