@@ -5,15 +5,20 @@ import { pushable, Pushable } from 'it-pushable'
5
5
import { encode , decode } from 'it-length-prefixed'
6
6
import { Uint8ArrayList } from 'uint8arraylist'
7
7
8
+ type OutboundStreamOpts = {
9
+ /** Max size in bytes for pushable buffer. If full, will throw on .push */
10
+ maxBufferSize ?: number
11
+ }
12
+
8
13
export class OutboundStream {
9
- private readonly rawStream : Stream
10
14
private readonly pushable : Pushable < Uint8Array >
11
15
private readonly closeController : AbortController
16
+ private readonly maxBufferSize : number
12
17
13
- constructor ( rawStream : Stream , errCallback : ( e : Error ) => void ) {
14
- this . rawStream = rawStream
15
- this . pushable = pushable ( )
18
+ constructor ( private readonly rawStream : Stream , errCallback : ( e : Error ) => void , opts : OutboundStreamOpts ) {
19
+ this . pushable = pushable ( { objectMode : false } )
16
20
this . closeController = new AbortController ( )
21
+ this . maxBufferSize = opts . maxBufferSize ?? Infinity
17
22
18
23
pipe (
19
24
abortableSource ( this . pushable , this . closeController . signal , { returnOnAbort : true } ) ,
@@ -28,6 +33,10 @@ export class OutboundStream {
28
33
}
29
34
30
35
push ( data : Uint8Array ) : void {
36
+ if ( this . pushable . readableLength > this . maxBufferSize ) {
37
+ throw Error ( `OutboundStream buffer full, size > ${ this . maxBufferSize } ` )
38
+ }
39
+
31
40
this . pushable . push ( data )
32
41
}
33
42
0 commit comments