const EMPTY_BUF = Buffer . allocUnsafe ( 0 ) ; const CR = 13 ; const LF = 10 ; enum ParserState { START , CR } hidden export class DataBuffer { buffers : Buffer [ ] = [ ] ; byteLength : number = 0 ; pack ( ) { if ( this . buffers . length > 1 ) { let v = Buffer . concat ( this . buffers , this . byteLength ) ; this . buffers = [ v ] ; } } drain ( n ? ) { if ( n === undefined ) { n = this . byteLength ; } if ( this . byteLength >= n ) { this . pack ( ) ; let v = this . buffers . pop ( ) ; if ( v ) { let d = v . slice ( 0 , n ) ; if ( this . byteLength > n ) { this . buffers . push ( v . slice ( n ) ) ; } this . byteLength -= n ; return d ; } } return EMPTY_BUF ; } fill ( data ) { if ( data ) { this . buffers . push ( data ) ; this . byteLength += data . byteLength ; } } protoLen ( ) { let ps = ParserState . START ; let offset = 0 ; for ( let j = 0 ; j < this . buffers . length ; j ++ ) { let cb = this . buffers [ j ] ; for ( let i = 0 ; i < cb . byteLength ; i ++ ) { let v = cb . readUInt8 ( i ) ; switch ( ps ) { case ParserState . START : switch ( v ) { case CR : ps = ParserState . CR ; break ; default : } break ; case ParserState . CR : switch ( v ) { case LF : return offset + i + 1 ; default : ps = ParserState . START ; } break ; } } offset += cb . byteLength ; } return - 1 ; } peek ( ) { if ( this . buffers . length ) { this . pack ( ) ; return this . buffers [ 0 ] ; } return EMPTY_BUF ; } size ( ) { return this . byteLength ; } length ( ) { return this . buffers . length ; } reset ( ) { let a = this . buffers ; this . buffers = [ ] ; this . byteLength = 0 ; return a ; } }