@@ -5,15 +5,20 @@ import { pushable, Pushable } from 'it-pushable'
55import { encode , decode } from 'it-length-prefixed'
66import { Uint8ArrayList } from 'uint8arraylist'
77
8+ type OutboundStreamOpts = {
9+ /** Max size in bytes for pushable buffer. If full, will throw on .push */
10+ maxBufferSize ?: number
11+ }
12+
813export class OutboundStream {
9- private readonly rawStream : Stream
1014 private readonly pushable : Pushable < Uint8Array >
1115 private readonly closeController : AbortController
16+ private readonly maxBufferSize : number
1217
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 } )
1620 this . closeController = new AbortController ( )
21+ this . maxBufferSize = opts . maxBufferSize ?? Infinity
1722
1823 pipe (
1924 abortableSource ( this . pushable , this . closeController . signal , { returnOnAbort : true } ) ,
@@ -28,6 +33,10 @@ export class OutboundStream {
2833 }
2934
3035 push ( data : Uint8Array ) : void {
36+ if ( this . pushable . readableLength > this . maxBufferSize ) {
37+ throw Error ( `OutboundStream buffer full, size > ${ this . maxBufferSize } ` )
38+ }
39+
3140 this . pushable . push ( data )
3241 }
3342
0 commit comments