Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes VarInt #234

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/varint.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
}
}
}
Expand Down