import * as ByteBuffer from 'bytebuffer' ; import { MESSAGE_HEADER_BYTE } from '../../config' ; /**
 * Reads meta information from buffer
 */ const readHeader = ( buffer ) => { const typeId = buffer . readUint16 ( ) ; const length = buffer . readUint32 ( ) ; return { typeId , length } ; } ; /**
 * Reads meta information from chunked buffer
 */ const readHeaderChunked = ( buffer ) => { const sharp1 = buffer . readByte ( ) ; const sharp2 = buffer . readByte ( ) ; const typeId = buffer . readUint16 ( ) ; const length = buffer . readUint32 ( ) ; return { sharp1 , sharp2 , typeId , length } ; } ; export const decode = ( byteBuffer ) => { const { typeId } = readHeader ( byteBuffer ) ; return { typeId , buffer : byteBuffer , } ; } ; export const decodeChunked = ( bytes ) => { const byteBuffer = ByteBuffer . wrap ( bytes , undefined , undefined , true ) ; const { sharp1 , sharp2 , typeId , length } = readHeaderChunked ( byteBuffer ) ; if ( sharp1 !== MESSAGE_HEADER_BYTE || sharp2 !== MESSAGE_HEADER_BYTE ) { throw new Error ( "Didn't receive expected header signature." ) ; } return { length , typeId , restBuffer : byteBuffer } ; } ;