@yubing744/rooch-sdk
Version:
127 lines (126 loc) • 4.38 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var binaryDeserializer_exports = {};
__export(binaryDeserializer_exports, {
BinaryDeserializer: () => BinaryDeserializer
});
module.exports = __toCommonJS(binaryDeserializer_exports);
var util = __toESM(require("@kayahr/text-encoding"));
const _BinaryDeserializer = class {
constructor(data) {
this.buffer = new ArrayBuffer(data.length);
new Uint8Array(this.buffer).set(data, 0);
this.offset = 0;
}
read(length) {
const bytes = this.buffer.slice(this.offset, this.offset + length);
this.offset += length;
return bytes;
}
deserializeStr() {
const value = this.deserializeBytes();
return _BinaryDeserializer.textDecoder.decode(value);
}
deserializeBytes() {
const len = this.deserializeLen();
if (len < 0) {
throw new Error("Length of a bytes array can't be negative");
}
return new Uint8Array(this.read(len));
}
deserializeBool() {
const bool = new Uint8Array(this.read(1))[0];
return bool == 1;
}
deserializeUnit() {
return null;
}
deserializeU8() {
return new DataView(this.read(1)).getUint8(0);
}
deserializeU16() {
return new DataView(this.read(2)).getUint16(0, true);
}
deserializeU32() {
return new DataView(this.read(4)).getUint32(0, true);
}
deserializeU64() {
const low = this.deserializeU32();
const high = this.deserializeU32();
return BigInt(
BigInt(high.toString()) << _BinaryDeserializer.BIG_32 | BigInt(low.toString())
);
}
deserializeU128() {
const low = this.deserializeU64();
const high = this.deserializeU64();
return BigInt(
BigInt(high.toString()) << _BinaryDeserializer.BIG_64 | BigInt(low.toString())
);
}
deserializeI8() {
return new DataView(this.read(1)).getInt8(0);
}
deserializeI16() {
return new DataView(this.read(2)).getInt16(0, true);
}
deserializeI32() {
return new DataView(this.read(4)).getInt32(0, true);
}
deserializeI64() {
const low = this.deserializeI32();
const high = this.deserializeI32();
return BigInt(high.toString()) << _BinaryDeserializer.BIG_32 | BigInt(low.toString());
}
deserializeI128() {
const low = this.deserializeI64();
const high = this.deserializeI64();
return BigInt(high.toString()) << _BinaryDeserializer.BIG_64 | BigInt(low.toString());
}
deserializeOptionTag() {
return this.deserializeBool();
}
getBufferOffset() {
return this.offset;
}
deserializeChar() {
throw new Error("Method deserializeChar not implemented.");
}
deserializeF32() {
return new DataView(this.read(4)).getFloat32(0, true);
}
deserializeF64() {
return new DataView(this.read(8)).getFloat64(0, true);
}
};
let BinaryDeserializer = _BinaryDeserializer;
BinaryDeserializer.BIG_32 = BigInt(32);
BinaryDeserializer.BIG_64 = BigInt(64);
BinaryDeserializer.textDecoder = typeof window === "undefined" ? new util.TextDecoder() : new TextDecoder();
//# sourceMappingURL=binaryDeserializer.js.map