Skip to content

Commit

Permalink
Merge pull request #3 from UnfamiliarLegacy/fix/hpacket
Browse files Browse the repository at this point in the history
Optimize packet reader
  • Loading branch information
WiredSpast authored Feb 1, 2024
2 parents 3dc0a6d + 7228c56 commit 629974d
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/protocol/hpacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { PacketInfoManager } from "../services/packetinfo/packetinfomanager.js";
import util from "util";

export class HPacket {
static TEXT_DECODER = new TextDecoder('utf8');

#isEdited = false;
#packetInBytes;
#readIndex = 6;
Expand Down Expand Up @@ -257,7 +259,7 @@ export class HPacket {
}

if(Number.isInteger(index) && index < this.#packetInBytes.length - 1) {
return Buffer.from(this.#packetInBytes).readInt16BE(index);
return (this.#packetInBytes[index + 1]) | (this.#packetInBytes[index] << 8);
}

throw new Error("Invalid argument(s) passed or read index out of bounds");
Expand All @@ -270,7 +272,7 @@ export class HPacket {
}

if(Number.isInteger(index) && index < this.#packetInBytes.length - 1) {
return Buffer.from(this.#packetInBytes).readUInt16BE(index);
return (this.#packetInBytes[index + 1]) | (this.#packetInBytes[index] << 8);
}

throw new Error("Invalid argument(s) passed or read index out of bounds");
Expand All @@ -287,7 +289,10 @@ export class HPacket {
}

if(Number.isInteger(index) && index < this.#packetInBytes.length - 3) {
return Buffer.from(this.#packetInBytes).readInt32BE(index);
return (this.#packetInBytes[index + 3]) |
(this.#packetInBytes[index + 2] << 8) |
(this.#packetInBytes[index + 1] << 16) |
(this.#packetInBytes[index] << 24);
}

throw new Error("Invalid argument(s) passed or read index out of bounds");
Expand Down Expand Up @@ -335,11 +340,7 @@ export class HPacket {
}

if(Number.isInteger(index) && index < this.#packetInBytes.length + 1 - length) {
let newBytes = new Uint8Array(length);
for(let i = 0; i < length; i++) {
newBytes[i] = this.#packetInBytes[i + index];
}
return newBytes;
return new Uint8Array(length, index, length);
}
}

Expand All @@ -353,7 +354,14 @@ export class HPacket {
}

if(Number.isInteger(index) && index < this.#packetInBytes.length - 7) {
return Number(Buffer.from(this.#packetInBytes).readBigInt64BE(index));
return (this.#packetInBytes[index + 7]) |
(this.#packetInBytes[index + 6] << 8) |
(this.#packetInBytes[index + 5] << 16) |
(this.#packetInBytes[index + 4] << 24)
(this.#packetInBytes[index + 3] << 32) |
(this.#packetInBytes[index + 2] << 40) |
(this.#packetInBytes[index + 1] << 48) |
(this.#packetInBytes[index] << 52);
}

throw new Error("Invalid argument(s) passed or read index out of bounds");
Expand All @@ -378,7 +386,7 @@ export class HPacket {
let length = this.readUShort(index);
index += 2;
if(index < this.#packetInBytes.length + 1 - length) {
return Buffer.from(this.#packetInBytes).toString(charset, index, index + length);
return HPacket.TEXT_DECODER.decode(new Uint8Array(this.#packetInBytes.buffer, index, length));
}
}

Expand All @@ -404,7 +412,7 @@ export class HPacket {
let length = this.readInteger(index);
index += 4;
if(index < this.#packetInBytes.length + 1 - length) {
return Buffer.from(this.#packetInBytes).toString(charset, index, index + length);
return HPacket.TEXT_DECODER.decode(new Uint8Array(this.#packetInBytes.buffer, index, length));
}
}

Expand Down

0 comments on commit 629974d

Please sign in to comment.