@iota/bcs
Version:
BCS - Canonical Binary Serialization implementation for JavaScript
257 lines (256 loc) • 8.1 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var bcs_type_exports = {};
__export(bcs_type_exports, {
BcsType: () => BcsType,
SerializedBcs: () => SerializedBcs,
bigUIntBcsType: () => bigUIntBcsType,
dynamicSizeBcsType: () => dynamicSizeBcsType,
fixedSizeBcsType: () => fixedSizeBcsType,
isSerializedBcs: () => isSerializedBcs,
lazyBcsType: () => lazyBcsType,
stringLikeBcsType: () => stringLikeBcsType,
uIntBcsType: () => uIntBcsType
});
module.exports = __toCommonJS(bcs_type_exports);
var import_b58 = require("./b58.js");
var import_b64 = require("./b64.js");
var import_hex = require("./hex.js");
var import_reader = require("./reader.js");
var import_uleb = require("./uleb.js");
var import_writer = require("./writer.js");
var _write, _serialize, _schema, _bytes;
const _BcsType = class _BcsType {
constructor(options) {
__privateAdd(this, _write);
__privateAdd(this, _serialize);
this.name = options.name;
this.read = options.read;
this.serializedSize = options.serializedSize ?? (() => null);
__privateSet(this, _write, options.write);
__privateSet(this, _serialize, options.serialize ?? ((value, options2) => {
const writer = new import_writer.BcsWriter({
initialSize: this.serializedSize(value) ?? void 0,
...options2
});
__privateGet(this, _write).call(this, value, writer);
return writer.toBytes();
}));
this.validate = options.validate ?? (() => {
});
}
write(value, writer) {
this.validate(value);
__privateGet(this, _write).call(this, value, writer);
}
serialize(value, options) {
this.validate(value);
return new SerializedBcs(this, __privateGet(this, _serialize).call(this, value, options));
}
parse(bytes) {
const reader = new import_reader.BcsReader(bytes);
return this.read(reader);
}
fromHex(hex) {
return this.parse((0, import_hex.fromHex)(hex));
}
fromBase58(b64) {
return this.parse((0, import_b58.fromBase58)(b64));
}
fromBase64(b64) {
return this.parse((0, import_b64.fromBase64)(b64));
}
transform({
name,
input,
output,
validate
}) {
return new _BcsType({
name: name ?? this.name,
read: (reader) => output ? output(this.read(reader)) : this.read(reader),
write: (value, writer) => __privateGet(this, _write).call(this, input ? input(value) : value, writer),
serializedSize: (value) => this.serializedSize(input ? input(value) : value),
serialize: (value, options) => __privateGet(this, _serialize).call(this, input ? input(value) : value, options),
validate: (value) => {
validate?.(value);
this.validate(input ? input(value) : value);
}
});
}
};
_write = new WeakMap();
_serialize = new WeakMap();
let BcsType = _BcsType;
const SERIALIZED_BCS_BRAND = Symbol.for("@iota/serialized-bcs");
function isSerializedBcs(obj) {
return !!obj && typeof obj === "object" && obj[SERIALIZED_BCS_BRAND] === true;
}
class SerializedBcs {
constructor(type, schema) {
__privateAdd(this, _schema);
__privateAdd(this, _bytes);
__privateSet(this, _schema, type);
__privateSet(this, _bytes, schema);
}
// Used to brand SerializedBcs so that they can be identified, even between multiple copies
// of the @iota/bcs package are installed
get [SERIALIZED_BCS_BRAND]() {
return true;
}
toBytes() {
return __privateGet(this, _bytes);
}
toHex() {
return (0, import_hex.toHex)(__privateGet(this, _bytes));
}
toBase64() {
return (0, import_b64.toBase64)(__privateGet(this, _bytes));
}
toBase58() {
return (0, import_b58.toBase58)(__privateGet(this, _bytes));
}
parse() {
return __privateGet(this, _schema).parse(__privateGet(this, _bytes));
}
}
_schema = new WeakMap();
_bytes = new WeakMap();
function fixedSizeBcsType({
size,
...options
}) {
return new BcsType({
...options,
serializedSize: () => size
});
}
function uIntBcsType({
readMethod,
writeMethod,
...options
}) {
return fixedSizeBcsType({
...options,
read: (reader) => reader[readMethod](),
write: (value, writer) => writer[writeMethod](value),
validate: (value) => {
if (value < 0 || value > options.maxValue) {
throw new TypeError(
`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`
);
}
options.validate?.(value);
}
});
}
function bigUIntBcsType({
readMethod,
writeMethod,
...options
}) {
return fixedSizeBcsType({
...options,
read: (reader) => reader[readMethod](),
write: (value, writer) => writer[writeMethod](BigInt(value)),
validate: (val) => {
const value = BigInt(val);
if (value < 0 || value > options.maxValue) {
throw new TypeError(
`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`
);
}
options.validate?.(value);
}
});
}
function dynamicSizeBcsType({
serialize,
...options
}) {
const type = new BcsType({
...options,
serialize,
write: (value, writer) => {
for (const byte of type.serialize(value).toBytes()) {
writer.write8(byte);
}
}
});
return type;
}
function stringLikeBcsType({
toBytes,
fromBytes,
...options
}) {
return new BcsType({
...options,
read: (reader) => {
const length = reader.readULEB();
const bytes = reader.readBytes(length);
return fromBytes(bytes);
},
write: (hex, writer) => {
const bytes = toBytes(hex);
writer.writeULEB(bytes.length);
for (let i = 0; i < bytes.length; i++) {
writer.write8(bytes[i]);
}
},
serialize: (value) => {
const bytes = toBytes(value);
const size = (0, import_uleb.ulebEncode)(bytes.length);
const result = new Uint8Array(size.length + bytes.length);
result.set(size, 0);
result.set(bytes, size.length);
return result;
},
validate: (value) => {
if (typeof value !== "string") {
throw new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);
}
options.validate?.(value);
}
});
}
function lazyBcsType(cb) {
let lazyType = null;
function getType() {
if (!lazyType) {
lazyType = cb();
}
return lazyType;
}
return new BcsType({
name: "lazy",
read: (data) => getType().read(data),
serializedSize: (value) => getType().serializedSize(value),
write: (value, writer) => getType().write(value, writer),
serialize: (value, options) => getType().serialize(value, options).toBytes()
});
}
//# sourceMappingURL=bcs-type.js.map