import { Int64BE , Int64LE , Uint64BE , Uint64LE } from 'int64-buffer' ; import { PacketBaseShared } from 'packets/packetbaseshared' ; import { PacketLongString } from 'packets/packetlongstring' ; import { PacketString } from 'packets/packetstring' ; class export class InPacketBase extends PacketBaseShared { protected packetData : Buffer ; private curOffset : number ; private parsedSuccessfully : boolean ; constructor ( data ) { super ( ) ; this . packetData = data ; this . curOffset = 0 ; try { this . parse ( ) ; this . parsedSuccessfully = true ; } catch ( error ) { console . warn ( 'Error parsing packet: %s' , error ) ; this . parsedSuccessfully = false ; } } returns public getData ( ) { return this . packetData ; } returns public isParsed ( ) { return this . parsedSuccessfully ; } returns public readInt8 ( ) { if ( this . canReadBytes ( 1 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } return this . packetData . readInt8 ( this . curOffset ++ ) ; } returns public readInt16 ( bigEndian = false ) { if ( this . canReadBytes ( 2 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? this . packetData . readInt16BE ( this . curOffset ) : this . packetData . readInt16LE ( this . curOffset ) ; this . curOffset += 2 ; return res ; } returns public readInt32 ( bigEndian = false ) { if ( this . canReadBytes ( 4 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? this . packetData . readInt32BE ( this . curOffset ) : this . packetData . readInt32LE ( this . curOffset ) ; this . curOffset += 4 ; return res ; } returns public readInt64 ( bigEndian = false ) { if ( this . canReadBytes ( 8 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? new Int64BE ( this . packetData , this . curOffset ) : new Int64LE ( this . packetData , this . curOffset ) ; this . curOffset += 8 ; return res ; } returns public readUInt8 ( ) { if ( this . canReadBytes ( 1 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } return this . packetData . readUInt8 ( this . curOffset ++ ) ; } returns public readUInt16 ( bigEndian = false ) { if ( this . canReadBytes ( 2 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? this . packetData . readUInt16BE ( this . curOffset ) : this . packetData . readUInt16LE ( this . curOffset ) ; this . curOffset += 2 ; return res ; } returns public readUInt32 ( bigEndian = false ) { if ( this . canReadBytes ( 4 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? this . packetData . readUInt32BE ( this . curOffset ) : this . packetData . readUInt32LE ( this . curOffset ) ; this . curOffset += 4 ; return res ; } returns public readUInt64 ( bigEndian = false ) { if ( this . canReadBytes ( 8 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? new Uint64BE ( this . packetData , this . curOffset ) : new Uint64LE ( this . packetData , this . curOffset ) ; this . curOffset += 8 ; return res ; } returns public readFloat ( bigEndian = false ) { if ( this . canReadBytes ( 4 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? this . packetData . readFloatBE ( this . curOffset ) : this . packetData . readFloatLE ( this . curOffset ) ; this . curOffset += 4 ; return res ; } returns public readDouble ( bigEndian = false ) { if ( this . canReadBytes ( 8 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = bigEndian ? this . packetData . readDoubleBE ( this . curOffset ) : this . packetData . readDoubleLE ( this . curOffset ) ; this . curOffset += 8 ; return res ; } returns public readString ( ) { if ( this . canReadBytes ( 1 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = PacketString . from ( this . packetData . slice ( this . curOffset , this . packetData . length ) ) ; this . curOffset += res . totalLen ; return res . str ; } returns public readLongString ( ) { if ( this . canReadBytes ( 2 ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = PacketLongString . from ( this . packetData . slice ( this . curOffset , this . packetData . length ) ) ; this . curOffset += res . totalLen ; return res . str ; } param length returns public readArray ( length ) { if ( this . canReadBytes ( length ) === false ) { throw new Error ( 'Data buffer is too small' ) ; } const res = this . packetData . slice ( this . curOffset , this . curOffset + length ) ; this . curOffset += length ; return res ; } /**
     * parses the packet's data
     */ protected parse ( ) { this . signature = this . readUInt8 ( ) ; if ( this . isValid ( ) === false ) { throw new Error ( `This is not a packet. Signature: ${ this . signature }` ) ; } this . sequence = this . readUInt8 ( ) ; this . length = this . readUInt16 ( ) ; this . id = this . readUInt8 ( ) ; } private canReadBytes ( bytes ) { return this . packetData . byteLength >= this . curOffset + bytes ; } }