import { Writable } from 'stream' ; export class MaxSizeStringBuilder { private size = 0 ; private pristine = true ; constructor ( private stream , private maxSizeBytes , private bom = '' ) { } tryAppend ( chunk ) { const byteLength = Buffer . byteLength ( chunk ) ; if ( this . size + byteLength > this . maxSizeBytes ) { return false ; } if ( this . pristine ) { this . stream . write ( this . bom ) ; this . pristine = false ; } this . stream . write ( chunk ) ; this . size += byteLength ; return true ; } getSizeInBytes ( ) { return this . size ; } }