From 597815f6121a809cc09e75c922a6d6300749b61d Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Tue, 25 Feb 2025 14:28:11 -0400 Subject: [PATCH] Fixes VarInt --- lib/varint.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/varint.js b/lib/varint.js index 36c5600e..0d115de5 100644 --- a/lib/varint.js +++ b/lib/varint.js @@ -1,10 +1,10 @@ class VarInt { - constructor(value) { + constructor(value, offset = 0) { if (typeof value === 'number' || typeof value === 'bigint') { this.value = BigInt(value); this.originallyEncodedSize = this.getSizeInBytes(); } else if (Buffer.isBuffer(value)) { - this.value = this.decode(value); + this.value = this.decode(value, offset); this.originallyEncodedSize = this.getSizeInBytes(); } else { throw new Error('Invalid input: value should be a number or buffer'); @@ -49,16 +49,16 @@ class VarInt { } } - decode(buffer) { - const first = buffer[0]; + decode(buffer, offset = 0) { + const first = buffer[offset]; if (first < 253) { return BigInt(first); } else if (first === 253) { - return BigInt(buffer.readUInt16LE(1)); + return BigInt(buffer.readUInt16LE(offset + 1)); } else if (first === 254) { - return BigInt(buffer.readUInt32LE(1)); + return BigInt(buffer.readUInt32LE(offset + 1)); } else { - return buffer.readBigUInt64LE(1); + return buffer.readBigUInt64LE(offset + 1); } } }