1
1
import { abortableSource } from 'abortable-iterator'
2
2
import { encode , decode } from 'it-length-prefixed'
3
- import map from 'it-map'
4
- import merge from 'it-merge'
5
3
import { pipe } from 'it-pipe'
6
4
import { pushable , type Pushable } from 'it-pushable'
7
5
import type { Stream } from '@libp2p/interface'
@@ -18,24 +16,17 @@ interface InboundStreamOpts {
18
16
}
19
17
20
18
export class OutboundStream {
21
- private readonly pushable : Pushable < Uint8Array >
22
- private readonly lpPushable : Pushable < Uint8ArrayList >
19
+ private readonly pushable : Pushable < Uint8Array | Uint8ArrayList >
23
20
private readonly closeController : AbortController
24
21
private readonly maxBufferSize : number
25
22
26
23
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 ( )
29
25
this . closeController = new AbortController ( )
30
26
this . maxBufferSize = opts . maxBufferSize ?? Infinity
31
27
32
28
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 } ) ,
39
30
this . rawStream
40
31
) . catch ( errCallback )
41
32
}
@@ -51,24 +42,23 @@ export class OutboundStream {
51
42
throw Error ( `OutboundStream buffer full, size > ${ this . maxBufferSize } ` )
52
43
}
53
44
54
- this . pushable . push ( data )
45
+ this . pushable . push ( encode . single ( data ) )
55
46
}
56
47
57
48
/**
58
49
* Same to push() but this is prefixed data so no need to encode length prefixed again
59
50
*/
60
51
pushPrefixed ( data : Uint8ArrayList ) : void {
61
- if ( this . lpPushable . readableLength > this . maxBufferSize ) {
52
+ if ( this . pushable . readableLength > this . maxBufferSize ) {
62
53
throw Error ( `OutboundStream buffer full, size > ${ this . maxBufferSize } ` )
63
54
}
64
- this . lpPushable . push ( data )
55
+ this . pushable . push ( data )
65
56
}
66
57
67
58
async close ( ) : Promise < void > {
68
59
this . closeController . abort ( )
69
60
// similar to pushable.end() but clear the internal buffer
70
61
await this . pushable . return ( )
71
- await this . lpPushable . return ( )
72
62
await this . rawStream . close ( )
73
63
}
74
64
}
0 commit comments