flexbuf
Version:
225 lines • 7.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataEncoder = void 0;
exports.BE = BE;
exports.BEncoder = BE;
const low_level_1 = require("low-level");
const low_level_2 = require("low-level");
const utils_js_1 = require("./utils.js");
class DataEncoder {
key;
hashRemove;
constructor(key, hashRemove = false) {
this.key = key;
this.hashRemove = hashRemove;
}
}
exports.DataEncoder = DataEncoder;
class ArrayEncoder extends DataEncoder {
prefixLength;
targetObject;
constructor(key, prefixLength, targetObject, hashRemove = false) {
super(key, hashRemove);
this.prefixLength = prefixLength;
this.targetObject = targetObject;
}
encode(array) {
const result = [];
// length check implemeting later
if (this.prefixLength === "unlimited") {
result.push(utils_js_1.Utils.encodeLengthForUnlimited(array.length));
}
else {
result.push(low_level_2.Uint.from(array.length, this.prefixLength));
}
for (let item of array) {
result.push(this.targetObject.prototype.encodeToHex.call(item, false));
}
return result;
}
decode(arrayDataWithPrefix) {
const final_array = [];
let arrayCount, prefixLength;
if (this.prefixLength === "unlimited") {
[arrayCount, prefixLength] = utils_js_1.Utils.decodeLengthFromUnlimited(arrayDataWithPrefix);
}
else {
prefixLength = this.prefixLength;
arrayCount = arrayDataWithPrefix.slice(0, prefixLength).toInt();
}
let arrayData = arrayDataWithPrefix.slice(prefixLength);
let total_arrayLength = prefixLength;
for (let i = 0; i < arrayCount; i++) {
const array_item = this.targetObject.fromDecodedHex(arrayData, true);
final_array.push(array_item.data);
arrayData = arrayData.slice(array_item.length);
total_arrayLength += array_item.length;
}
return {
data: final_array,
length: total_arrayLength
};
}
}
class ObjectEncoder extends DataEncoder {
targetObject;
constructor(key, targetObject, hashRemove = false) {
super(key, hashRemove);
this.targetObject = targetObject;
}
encode(object) {
return [this.targetObject.prototype.encodeToHex.call(object, false)];
}
decode(hexData) {
return this.targetObject.fromDecodedHex(hexData, true);
}
}
class BigIntEncoder extends DataEncoder {
prefixLength = 1;
encode(value) {
const hexValue = value.toShortUint();
const hexValueLength = low_level_2.Uint.from(hexValue.getLen(), this.prefixLength);
return [hexValueLength, hexValue];
}
decode(hexDataWithPrefix) {
const dataLength = hexDataWithPrefix.slice(0, this.prefixLength).toInt();
const totalLength = this.prefixLength + dataLength;
const hexValue = hexDataWithPrefix.slice(this.prefixLength, totalLength);
if (hexValue.getLen() !== dataLength)
return null;
return {
data: low_level_1.Uint64.create(hexValue),
length: totalLength
};
}
}
class BoolEncoder extends DataEncoder {
fixedLength = 1;
encode(value) {
return [value ? low_level_1.Uint8.from(1) : low_level_1.Uint8.from(0)];
}
decode(hexData) {
const hexValue = hexData.slice(0, this.fixedLength);
if (hexValue.getLen() !== this.fixedLength) {
return null;
}
return {
data: hexValue.eq(1),
length: this.fixedLength
};
}
}
class FixedUintEncoder extends DataEncoder {
CLS;
constructor(CLS, key, hashRemove = false) {
super(key, hashRemove);
this.CLS = CLS;
}
encode(v) {
return [v];
}
decode(hexData) {
const hexValue = hexData.slice(0, this.CLS.byteLength);
if (hexValue.getLen() !== this.CLS.byteLength) {
return null;
}
return {
data: this.CLS.create(hexValue),
length: hexValue.getLen()
};
}
}
class CustomEncoder extends DataEncoder {
encodeFN;
decodeFN;
prefixLength;
fixedLength;
constructor(key, length, hashRemove = false, encodeFN, decodeFN) {
super(key, hashRemove);
this.encodeFN = encodeFN;
this.decodeFN = decodeFN;
if (length.type === "prefix") {
this.prefixLength = length.val;
}
else if (length.type === "fixed") {
this.fixedLength = length.val;
}
}
encode(value) {
const hexValue = this.encodeFN ? this.encodeFN(value) : value;
if (this.prefixLength) {
if (this.prefixLength === "unlimited") {
const hexValueLength = utils_js_1.Utils.encodeLengthForUnlimited(hexValue.getLen());
return [hexValueLength, hexValue];
}
const hexValueLength = low_level_2.Uint.from(hexValue.getLen(), this.prefixLength);
return [hexValueLength, hexValue];
}
else if (this.fixedLength && hexValue.getLen() !== this.fixedLength) {
return null;
}
return [hexValue];
}
decode(hexData) {
let hexValueLength = 0;
let totalLength = 0;
if (this.fixedLength) {
hexValueLength = this.fixedLength;
totalLength = this.fixedLength;
}
else if (this.prefixLength) {
let prefixLength = 0;
if (this.prefixLength === "unlimited") {
const lengthPrefixData = utils_js_1.Utils.decodeLengthFromUnlimited(hexData);
hexValueLength = lengthPrefixData[0];
prefixLength = lengthPrefixData[1];
}
else {
prefixLength = this.prefixLength;
hexValueLength = hexData.slice(0, this.prefixLength).toInt();
}
totalLength = prefixLength + hexValueLength;
hexData = hexData.slice(prefixLength);
}
let hexValue = hexData.slice(0, 0 + hexValueLength);
if (hexValue.getLen() !== hexValueLength) {
return null;
}
const value = this.decodeFN ? this.decodeFN(hexValue) : hexValue;
return {
data: value,
length: totalLength
};
}
}
function createEncoderConstructor(CLS) {
return (...args) => new CLS(...args);
}
function BE(CLS, key, hashRemove = false) {
return new FixedUintEncoder(CLS, key, hashRemove);
}
/**
* Basic Types
*/
(function (BE) {
BE.BigInt = createEncoderConstructor(BigIntEncoder);
BE.Bool = createEncoderConstructor(BoolEncoder);
BE.Array = createEncoderConstructor(ArrayEncoder);
BE.Object = createEncoderConstructor(ObjectEncoder);
function Custom(key, length, hashRemove = false, encodeFN, decodeFN) {
return new CustomEncoder(key, length, hashRemove, encodeFN, decodeFN);
}
BE.Custom = Custom;
;
})(BE || (exports.BEncoder = exports.BE = BE = {}));
/**
* More Advanced Types
*/
(function (BE) {
function Str(key, hashRemove = false) {
return new CustomEncoder(key, { type: "prefix", val: "unlimited" }, hashRemove, (v) => low_level_2.Uint.from(v, "utf8"), (v) => v.toString("utf8"));
}
BE.Str = Str;
;
})(BE || (exports.BEncoder = exports.BE = BE = {}));
//# sourceMappingURL=encoders.js.map