import * as binary from '../binary' ; const MAX_PACKETS = 10 ; export class ThriftFrameCodec { public encode ( dateToSend ) { const msg = Buffer . alloc ( dateToSend . length + 4 ) ; binary . writeI32 ( msg , dateToSend . length ) ; dateToSend . copy ( msg , 4 , 0 , dateToSend . length ) ; return msg ; } public decode ( dataToRead ) { const dataToReturn = Buffer . alloc ( dataToRead . length - 4 ) ; let writeCursor = 0 ; let count = 0 ; while ( dataToRead . length > 0 ) { if ( dataToRead . length < 4 ) { return Buffer . alloc ( 0 ) ; } else { const frameSize = binary . readI32 ( dataToRead , 0 ) ; if ( dataToRead . length < 4 + frameSize ) { return Buffer . alloc ( 0 ) ; } const frame = dataToRead . slice ( 4 , 4 + frameSize ) ; const remaining = dataToRead . slice ( 4 + frameSize ) ; frame . copy ( dataToReturn , 0 , writeCursor ) ; writeCursor += frame . length ; dataToRead = remaining ; count ++ ; if ( count >= MAX_PACKETS ) { dataToRead = Buffer . alloc ( 0 ) ; } } } return dataToReturn ; } }