This commit is contained in:
みけCAT 2025-04-05 19:06:34 +01:00 committed by GitHub
commit d6804b9e52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 5 deletions

View file

@ -24,7 +24,7 @@ class VarIntDecode extends Operation {
this.description = "Decodes a VarInt encoded integer. VarInt is an efficient way of encoding variable length integers and is commonly used with Protobuf."; this.description = "Decodes a VarInt encoded integer. VarInt is an efficient way of encoding variable length integers and is commonly used with Protobuf.";
this.infoURL = "https://developers.google.com/protocol-buffers/docs/encoding#varints"; this.infoURL = "https://developers.google.com/protocol-buffers/docs/encoding#varints";
this.inputType = "byteArray"; this.inputType = "byteArray";
this.outputType = "number"; this.outputType = "string";
this.args = []; this.args = [];
} }
@ -35,7 +35,18 @@ class VarIntDecode extends Operation {
*/ */
run(input, args) { run(input, args) {
try { try {
return Protobuf.varIntDecode(input); if (typeof BigInt === "function") {
let result = BigInt(0);
let offset = BigInt(0);
for (let i = 0; i < input.length; i++) {
result |= BigInt(input[i] & 0x7f) << offset;
if (!(input[i] & 0x80)) break;
offset += BigInt(7);
}
return result.toString();
} else {
return Protobuf.varIntDecode(input).toString();
}
} catch (err) { } catch (err) {
throw new OperationError(err); throw new OperationError(err);
} }

View file

@ -23,19 +23,31 @@ class VarIntEncode extends Operation {
this.module = "Default"; this.module = "Default";
this.description = "Encodes a Vn integer as a VarInt. VarInt is an efficient way of encoding variable length integers and is commonly used with Protobuf."; this.description = "Encodes a Vn integer as a VarInt. VarInt is an efficient way of encoding variable length integers and is commonly used with Protobuf.";
this.infoURL = "https://developers.google.com/protocol-buffers/docs/encoding#varints"; this.infoURL = "https://developers.google.com/protocol-buffers/docs/encoding#varints";
this.inputType = "number"; this.inputType = "string";
this.outputType = "byteArray"; this.outputType = "byteArray";
this.args = []; this.args = [];
} }
/** /**
* @param {number} input * @param {string} input
* @param {Object[]} args * @param {Object[]} args
* @returns {byteArray} * @returns {byteArray}
*/ */
run(input, args) { run(input, args) {
try { try {
return Protobuf.varIntEncode(input); if (typeof BigInt === "function") {
let value = BigInt(input);
if (value < 0) throw new OperationError("Negative values cannot be represented as VarInt");
const result = [];
while (value >= 0x80) {
result.push(Number(value & BigInt(0x7f)) | 0x80);
value >>= BigInt(7);
}
result.push(Number(value));
return result;
} else {
return Protobuf.varIntEncode(Number(input));
}
} catch (err) { } catch (err) {
throw new OperationError(err); throw new OperationError(err);
} }