lakutata
Version:
An IoC-based universal application framework.
1,491 lines (1,430 loc) • 40.8 kB
JavaScript
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */
"use strict";
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module"
});
function e(e) {
const t = e.length;
let s = 0;
let i = 0;
while (i < t) {
let n = e.charCodeAt(i++);
if ((n & 4294967168) === 0) {
s++;
continue;
} else if ((n & 4294965248) === 0) {
s += 2;
} else {
if (n >= 55296 && n <= 56319) {
if (i < t) {
const t = e.charCodeAt(i);
if ((t & 64512) === 56320) {
++i;
n = ((n & 1023) << 10) + (t & 1023) + 65536;
}
}
}
if ((n & 4294901760) === 0) {
s += 3;
} else {
s += 4;
}
}
}
return s;
}
function t(e, t, s) {
const i = e.length;
let n = s;
let r = 0;
while (r < i) {
let s = e.charCodeAt(r++);
if ((s & 4294967168) === 0) {
t[n++] = s;
continue;
} else if ((s & 4294965248) === 0) {
t[n++] = s >> 6 & 31 | 192;
} else {
if (s >= 55296 && s <= 56319) {
if (r < i) {
const t = e.charCodeAt(r);
if ((t & 64512) === 56320) {
++r;
s = ((s & 1023) << 10) + (t & 1023) + 65536;
}
}
}
if ((s & 4294901760) === 0) {
t[n++] = s >> 12 & 15 | 224;
t[n++] = s >> 6 & 63 | 128;
} else {
t[n++] = s >> 18 & 7 | 240;
t[n++] = s >> 12 & 63 | 128;
t[n++] = s >> 6 & 63 | 128;
}
}
t[n++] = s & 63 | 128;
}
}
const s = new TextEncoder;
const i = 50;
function n(e, t, i) {
s.encodeInto(e, t.subarray(i));
}
function r(e, s, r) {
if (e.length > i) {
n(e, s, r);
} else {
t(e, s, r);
}
}
const o = 4096;
function h(e, t, s) {
let i = t;
const n = i + s;
const r = [];
let h = "";
while (i < n) {
const t = e[i++];
if ((t & 128) === 0) {
r.push(t);
} else if ((t & 224) === 192) {
const s = e[i++] & 63;
r.push((t & 31) << 6 | s);
} else if ((t & 240) === 224) {
const s = e[i++] & 63;
const n = e[i++] & 63;
r.push((t & 31) << 12 | s << 6 | n);
} else if ((t & 248) === 240) {
const s = e[i++] & 63;
const n = e[i++] & 63;
const o = e[i++] & 63;
let h = (t & 7) << 18 | s << 12 | n << 6 | o;
if (h > 65535) {
h -= 65536;
r.push(h >>> 10 & 1023 | 55296);
h = 56320 | h & 1023;
}
r.push(h);
} else {
r.push(t);
}
if (r.length >= o) {
h += String.fromCharCode(...r);
r.length = 0;
}
}
if (r.length > 0) {
h += String.fromCharCode(...r);
}
return h;
}
const a = new TextDecoder;
const c = 200;
function f(e, t, s) {
const i = e.subarray(t, t + s);
return a.decode(i);
}
function d(e, t, s) {
if (s > c) {
return f(e, t, s);
} else {
return h(e, t, s);
}
}
class ExtData {
type;
data;
constructor(e, t) {
this.type = e;
this.data = t;
}
}
class DecodeError extends Error {
constructor(e) {
super(e);
const t = Object.create(DecodeError.prototype);
Object.setPrototypeOf(this, t);
Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
value: DecodeError.name
});
}
}
const l = 4294967295;
function u(e, t, s) {
const i = s / 4294967296;
const n = s;
e.setUint32(t, i);
e.setUint32(t + 4, n);
}
function w(e, t, s) {
const i = Math.floor(s / 4294967296);
const n = s;
e.setUint32(t, i);
e.setUint32(t + 4, n);
}
function g(e, t) {
const s = e.getInt32(t);
const i = e.getUint32(t + 4);
return s * 4294967296 + i;
}
function y(e, t) {
const s = e.getUint32(t);
const i = e.getUint32(t + 4);
return s * 4294967296 + i;
}
const p = -1;
const U = 4294967296 - 1;
const x = 17179869184 - 1;
function m({sec: e, nsec: t}) {
if (e >= 0 && t >= 0 && e <= x) {
if (t === 0 && e <= U) {
const t = new Uint8Array(4);
const s = new DataView(t.buffer);
s.setUint32(0, e);
return t;
} else {
const s = e / 4294967296;
const i = e & 4294967295;
const n = new Uint8Array(8);
const r = new DataView(n.buffer);
r.setUint32(0, t << 2 | s & 3);
r.setUint32(4, i);
return n;
}
} else {
const s = new Uint8Array(12);
const i = new DataView(s.buffer);
i.setUint32(0, t);
w(i, 4, e);
return s;
}
}
function B(e) {
const t = e.getTime();
const s = Math.floor(t / 1e3);
const i = (t - s * 1e3) * 1e6;
const n = Math.floor(i / 1e9);
return {
sec: s + n,
nsec: i - n * 1e9
};
}
function b(e) {
if (e instanceof Date) {
const t = B(e);
return m(t);
} else {
return null;
}
}
function S(e) {
const t = new DataView(e.buffer, e.byteOffset, e.byteLength);
switch (e.byteLength) {
case 4:
{
const e = t.getUint32(0);
const s = 0;
return {
sec: e,
nsec: s
};
}
case 8:
{
const e = t.getUint32(0);
const s = t.getUint32(4);
const i = (e & 3) * 4294967296 + s;
const n = e >>> 2;
return {
sec: i,
nsec: n
};
}
case 12:
{
const e = g(t, 4);
const s = t.getUint32(0);
return {
sec: e,
nsec: s
};
}
default:
throw new DecodeError(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${e.length}`);
}
}
function E(e) {
const t = S(e);
return new Date(t.sec * 1e3 + t.nsec / 1e6);
}
const I = {
type: p,
encode: b,
decode: E
};
class ExtensionCodec {
static defaultCodec=new ExtensionCodec;
__brand;
builtInEncoders=[];
builtInDecoders=[];
encoders=[];
decoders=[];
constructor() {
this.register(I);
}
register({type: e, encode: t, decode: s}) {
if (e >= 0) {
this.encoders[e] = t;
this.decoders[e] = s;
} else {
const i = -1 - e;
this.builtInEncoders[i] = t;
this.builtInDecoders[i] = s;
}
}
tryToEncode(e, t) {
for (let s = 0; s < this.builtInEncoders.length; s++) {
const i = this.builtInEncoders[s];
if (i != null) {
const n = i(e, t);
if (n != null) {
const e = -1 - s;
return new ExtData(e, n);
}
}
}
for (let s = 0; s < this.encoders.length; s++) {
const i = this.encoders[s];
if (i != null) {
const n = i(e, t);
if (n != null) {
const e = s;
return new ExtData(e, n);
}
}
}
if (e instanceof ExtData) {
return e;
}
return null;
}
decode(e, t, s) {
const i = t < 0 ? this.builtInDecoders[-1 - t] : this.decoders[t];
if (i) {
return i(e, t, s);
} else {
return new ExtData(t, e);
}
}
}
function A(e) {
return e instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && e instanceof SharedArrayBuffer;
}
function k(e) {
if (e instanceof Uint8Array) {
return e;
} else if (ArrayBuffer.isView(e)) {
return new Uint8Array(e.buffer, e.byteOffset, e.byteLength);
} else if (A(e)) {
return new Uint8Array(e);
} else {
return Uint8Array.from(e);
}
}
const L = 100;
const D = 2048;
class Encoder {
extensionCodec;
context;
useBigInt64;
maxDepth;
initialBufferSize;
sortKeys;
forceFloat32;
ignoreUndefined;
forceIntegerToFloat;
pos;
view;
bytes;
entered=false;
constructor(e) {
this.extensionCodec = e?.extensionCodec ?? ExtensionCodec.defaultCodec;
this.context = e?.context;
this.useBigInt64 = e?.useBigInt64 ?? false;
this.maxDepth = e?.maxDepth ?? L;
this.initialBufferSize = e?.initialBufferSize ?? D;
this.sortKeys = e?.sortKeys ?? false;
this.forceFloat32 = e?.forceFloat32 ?? false;
this.ignoreUndefined = e?.ignoreUndefined ?? false;
this.forceIntegerToFloat = e?.forceIntegerToFloat ?? false;
this.pos = 0;
this.view = new DataView(new ArrayBuffer(this.initialBufferSize));
this.bytes = new Uint8Array(this.view.buffer);
}
clone() {
return new Encoder({
extensionCodec: this.extensionCodec,
context: this.context,
useBigInt64: this.useBigInt64,
maxDepth: this.maxDepth,
initialBufferSize: this.initialBufferSize,
sortKeys: this.sortKeys,
forceFloat32: this.forceFloat32,
ignoreUndefined: this.ignoreUndefined,
forceIntegerToFloat: this.forceIntegerToFloat
});
}
reinitializeState() {
this.pos = 0;
}
encodeSharedRef(e) {
if (this.entered) {
const t = this.clone();
return t.encodeSharedRef(e);
}
try {
this.entered = true;
this.reinitializeState();
this.doEncode(e, 1);
return this.bytes.subarray(0, this.pos);
} finally {
this.entered = false;
}
}
encode(e) {
if (this.entered) {
const t = this.clone();
return t.encode(e);
}
try {
this.entered = true;
this.reinitializeState();
this.doEncode(e, 1);
return this.bytes.slice(0, this.pos);
} finally {
this.entered = false;
}
}
doEncode(e, t) {
if (t > this.maxDepth) {
throw new Error(`Too deep objects in depth ${t}`);
}
if (e == null) {
this.encodeNil();
} else if (typeof e === "boolean") {
this.encodeBoolean(e);
} else if (typeof e === "number") {
if (!this.forceIntegerToFloat) {
this.encodeNumber(e);
} else {
this.encodeNumberAsFloat(e);
}
} else if (typeof e === "string") {
this.encodeString(e);
} else if (this.useBigInt64 && typeof e === "bigint") {
this.encodeBigInt64(e);
} else {
this.encodeObject(e, t);
}
}
ensureBufferSizeToWrite(e) {
const t = this.pos + e;
if (this.view.byteLength < t) {
this.resizeBuffer(t * 2);
}
}
resizeBuffer(e) {
const t = new ArrayBuffer(e);
const s = new Uint8Array(t);
const i = new DataView(t);
s.set(this.bytes);
this.view = i;
this.bytes = s;
}
encodeNil() {
this.writeU8(192);
}
encodeBoolean(e) {
if (e === false) {
this.writeU8(194);
} else {
this.writeU8(195);
}
}
encodeNumber(e) {
if (!this.forceIntegerToFloat && Number.isSafeInteger(e)) {
if (e >= 0) {
if (e < 128) {
this.writeU8(e);
} else if (e < 256) {
this.writeU8(204);
this.writeU8(e);
} else if (e < 65536) {
this.writeU8(205);
this.writeU16(e);
} else if (e < 4294967296) {
this.writeU8(206);
this.writeU32(e);
} else if (!this.useBigInt64) {
this.writeU8(207);
this.writeU64(e);
} else {
this.encodeNumberAsFloat(e);
}
} else {
if (e >= -32) {
this.writeU8(224 | e + 32);
} else if (e >= -128) {
this.writeU8(208);
this.writeI8(e);
} else if (e >= -32768) {
this.writeU8(209);
this.writeI16(e);
} else if (e >= -2147483648) {
this.writeU8(210);
this.writeI32(e);
} else if (!this.useBigInt64) {
this.writeU8(211);
this.writeI64(e);
} else {
this.encodeNumberAsFloat(e);
}
}
} else {
this.encodeNumberAsFloat(e);
}
}
encodeNumberAsFloat(e) {
if (this.forceFloat32) {
this.writeU8(202);
this.writeF32(e);
} else {
this.writeU8(203);
this.writeF64(e);
}
}
encodeBigInt64(e) {
if (e >= BigInt(0)) {
this.writeU8(207);
this.writeBigUint64(e);
} else {
this.writeU8(211);
this.writeBigInt64(e);
}
}
writeStringHeader(e) {
if (e < 32) {
this.writeU8(160 + e);
} else if (e < 256) {
this.writeU8(217);
this.writeU8(e);
} else if (e < 65536) {
this.writeU8(218);
this.writeU16(e);
} else if (e < 4294967296) {
this.writeU8(219);
this.writeU32(e);
} else {
throw new Error(`Too long string: ${e} bytes in UTF-8`);
}
}
encodeString(t) {
const s = 1 + 4;
const i = e(t);
this.ensureBufferSizeToWrite(s + i);
this.writeStringHeader(i);
r(t, this.bytes, this.pos);
this.pos += i;
}
encodeObject(e, t) {
const s = this.extensionCodec.tryToEncode(e, this.context);
if (s != null) {
this.encodeExtension(s);
} else if (Array.isArray(e)) {
this.encodeArray(e, t);
} else if (ArrayBuffer.isView(e)) {
this.encodeBinary(e);
} else if (typeof e === "object") {
this.encodeMap(e, t);
} else {
throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(e)}`);
}
}
encodeBinary(e) {
const t = e.byteLength;
if (t < 256) {
this.writeU8(196);
this.writeU8(t);
} else if (t < 65536) {
this.writeU8(197);
this.writeU16(t);
} else if (t < 4294967296) {
this.writeU8(198);
this.writeU32(t);
} else {
throw new Error(`Too large binary: ${t}`);
}
const s = k(e);
this.writeU8a(s);
}
encodeArray(e, t) {
const s = e.length;
if (s < 16) {
this.writeU8(144 + s);
} else if (s < 65536) {
this.writeU8(220);
this.writeU16(s);
} else if (s < 4294967296) {
this.writeU8(221);
this.writeU32(s);
} else {
throw new Error(`Too large array: ${s}`);
}
for (const s of e) {
this.doEncode(s, t + 1);
}
}
countWithoutUndefined(e, t) {
let s = 0;
for (const i of t) {
if (e[i] !== undefined) {
s++;
}
}
return s;
}
encodeMap(e, t) {
const s = Object.keys(e);
if (this.sortKeys) {
s.sort();
}
const i = this.ignoreUndefined ? this.countWithoutUndefined(e, s) : s.length;
if (i < 16) {
this.writeU8(128 + i);
} else if (i < 65536) {
this.writeU8(222);
this.writeU16(i);
} else if (i < 4294967296) {
this.writeU8(223);
this.writeU32(i);
} else {
throw new Error(`Too large map object: ${i}`);
}
for (const i of s) {
const s = e[i];
if (!(this.ignoreUndefined && s === undefined)) {
this.encodeString(i);
this.doEncode(s, t + 1);
}
}
}
encodeExtension(e) {
if (typeof e.data === "function") {
const t = e.data(this.pos + 6);
const s = t.length;
if (s >= 4294967296) {
throw new Error(`Too large extension object: ${s}`);
}
this.writeU8(201);
this.writeU32(s);
this.writeI8(e.type);
this.writeU8a(t);
return;
}
const t = e.data.length;
if (t === 1) {
this.writeU8(212);
} else if (t === 2) {
this.writeU8(213);
} else if (t === 4) {
this.writeU8(214);
} else if (t === 8) {
this.writeU8(215);
} else if (t === 16) {
this.writeU8(216);
} else if (t < 256) {
this.writeU8(199);
this.writeU8(t);
} else if (t < 65536) {
this.writeU8(200);
this.writeU16(t);
} else if (t < 4294967296) {
this.writeU8(201);
this.writeU32(t);
} else {
throw new Error(`Too large extension object: ${t}`);
}
this.writeI8(e.type);
this.writeU8a(e.data);
}
writeU8(e) {
this.ensureBufferSizeToWrite(1);
this.view.setUint8(this.pos, e);
this.pos++;
}
writeU8a(e) {
const t = e.length;
this.ensureBufferSizeToWrite(t);
this.bytes.set(e, this.pos);
this.pos += t;
}
writeI8(e) {
this.ensureBufferSizeToWrite(1);
this.view.setInt8(this.pos, e);
this.pos++;
}
writeU16(e) {
this.ensureBufferSizeToWrite(2);
this.view.setUint16(this.pos, e);
this.pos += 2;
}
writeI16(e) {
this.ensureBufferSizeToWrite(2);
this.view.setInt16(this.pos, e);
this.pos += 2;
}
writeU32(e) {
this.ensureBufferSizeToWrite(4);
this.view.setUint32(this.pos, e);
this.pos += 4;
}
writeI32(e) {
this.ensureBufferSizeToWrite(4);
this.view.setInt32(this.pos, e);
this.pos += 4;
}
writeF32(e) {
this.ensureBufferSizeToWrite(4);
this.view.setFloat32(this.pos, e);
this.pos += 4;
}
writeF64(e) {
this.ensureBufferSizeToWrite(8);
this.view.setFloat64(this.pos, e);
this.pos += 8;
}
writeU64(e) {
this.ensureBufferSizeToWrite(8);
u(this.view, this.pos, e);
this.pos += 8;
}
writeI64(e) {
this.ensureBufferSizeToWrite(8);
w(this.view, this.pos, e);
this.pos += 8;
}
writeBigUint64(e) {
this.ensureBufferSizeToWrite(8);
this.view.setBigUint64(this.pos, e);
this.pos += 8;
}
writeBigInt64(e) {
this.ensureBufferSizeToWrite(8);
this.view.setBigInt64(this.pos, e);
this.pos += 8;
}
}
function v(e, t) {
const s = new Encoder(t);
return s.encodeSharedRef(e);
}
function z(e) {
return `${e < 0 ? "-" : ""}0x${Math.abs(e).toString(16).padStart(2, "0")}`;
}
const C = 16;
const T = 16;
class CachedKeyDecoder {
hit=0;
miss=0;
caches;
maxKeyLength;
maxLengthPerKey;
constructor(e = C, t = T) {
this.maxKeyLength = e;
this.maxLengthPerKey = t;
this.caches = [];
for (let e = 0; e < this.maxKeyLength; e++) {
this.caches.push([]);
}
}
canBeCached(e) {
return e > 0 && e <= this.maxKeyLength;
}
find(e, t, s) {
const i = this.caches[s - 1];
e: for (const n of i) {
const i = n.bytes;
for (let n = 0; n < s; n++) {
if (i[n] !== e[t + n]) {
continue e;
}
}
return n.str;
}
return null;
}
store(e, t) {
const s = this.caches[e.length - 1];
const i = {
bytes: e,
str: t
};
if (s.length >= this.maxLengthPerKey) {
s[Math.random() * s.length | 0] = i;
} else {
s.push(i);
}
}
decode(e, t, s) {
const i = this.find(e, t, s);
if (i != null) {
this.hit++;
return i;
}
this.miss++;
const n = h(e, t, s);
const r = Uint8Array.prototype.slice.call(e, t, t + s);
this.store(r, n);
return n;
}
}
const M = "array";
const F = "map_key";
const P = "map_value";
const $ = e => {
if (typeof e === "string" || typeof e === "number") {
return e;
}
throw new DecodeError("The type of key must be string or number but " + typeof e);
};
class StackPool {
stack=[];
stackHeadPosition=-1;
get length() {
return this.stackHeadPosition + 1;
}
top() {
return this.stack[this.stackHeadPosition];
}
pushArrayState(e) {
const t = this.getUninitializedStateFromPool();
t.type = M;
t.position = 0;
t.size = e;
t.array = new Array(e);
}
pushMapState(e) {
const t = this.getUninitializedStateFromPool();
t.type = F;
t.readCount = 0;
t.size = e;
t.map = {};
}
getUninitializedStateFromPool() {
this.stackHeadPosition++;
if (this.stackHeadPosition === this.stack.length) {
const e = {
type: undefined,
size: 0,
array: undefined,
position: 0,
readCount: 0,
map: undefined,
key: null
};
this.stack.push(e);
}
return this.stack[this.stackHeadPosition];
}
release(e) {
const t = this.stack[this.stackHeadPosition];
if (t !== e) {
throw new Error("Invalid stack state. Released state is not on top of the stack.");
}
if (e.type === M) {
const t = e;
t.size = 0;
t.array = undefined;
t.position = 0;
t.type = undefined;
}
if (e.type === F || e.type === P) {
const t = e;
t.size = 0;
t.map = undefined;
t.readCount = 0;
t.type = undefined;
}
this.stackHeadPosition--;
}
reset() {
this.stack.length = 0;
this.stackHeadPosition = -1;
}
}
const K = -1;
const R = new DataView(new ArrayBuffer(0));
const W = new Uint8Array(R.buffer);
try {
R.getInt8(0);
} catch (e) {
if (!(e instanceof RangeError)) {
throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access");
}
}
const j = new RangeError("Insufficient data");
const H = new CachedKeyDecoder;
class Decoder {
extensionCodec;
context;
useBigInt64;
rawStrings;
maxStrLength;
maxBinLength;
maxArrayLength;
maxMapLength;
maxExtLength;
keyDecoder;
mapKeyConverter;
totalPos=0;
pos=0;
view=R;
bytes=W;
headByte=K;
stack=new StackPool;
entered=false;
constructor(e) {
this.extensionCodec = e?.extensionCodec ?? ExtensionCodec.defaultCodec;
this.context = e?.context;
this.useBigInt64 = e?.useBigInt64 ?? false;
this.rawStrings = e?.rawStrings ?? false;
this.maxStrLength = e?.maxStrLength ?? l;
this.maxBinLength = e?.maxBinLength ?? l;
this.maxArrayLength = e?.maxArrayLength ?? l;
this.maxMapLength = e?.maxMapLength ?? l;
this.maxExtLength = e?.maxExtLength ?? l;
this.keyDecoder = e?.keyDecoder !== undefined ? e.keyDecoder : H;
this.mapKeyConverter = e?.mapKeyConverter ?? $;
}
clone() {
return new Decoder({
extensionCodec: this.extensionCodec,
context: this.context,
useBigInt64: this.useBigInt64,
rawStrings: this.rawStrings,
maxStrLength: this.maxStrLength,
maxBinLength: this.maxBinLength,
maxArrayLength: this.maxArrayLength,
maxMapLength: this.maxMapLength,
maxExtLength: this.maxExtLength,
keyDecoder: this.keyDecoder
});
}
reinitializeState() {
this.totalPos = 0;
this.headByte = K;
this.stack.reset();
}
setBuffer(e) {
const t = k(e);
this.bytes = t;
this.view = new DataView(t.buffer, t.byteOffset, t.byteLength);
this.pos = 0;
}
appendBuffer(e) {
if (this.headByte === K && !this.hasRemaining(1)) {
this.setBuffer(e);
} else {
const t = this.bytes.subarray(this.pos);
const s = k(e);
const i = new Uint8Array(t.length + s.length);
i.set(t);
i.set(s, t.length);
this.setBuffer(i);
}
}
hasRemaining(e) {
return this.view.byteLength - this.pos >= e;
}
createExtraByteError(e) {
const {view: t, pos: s} = this;
return new RangeError(`Extra ${t.byteLength - s} of ${t.byteLength} byte(s) found at buffer[${e}]`);
}
decode(e) {
if (this.entered) {
const t = this.clone();
return t.decode(e);
}
try {
this.entered = true;
this.reinitializeState();
this.setBuffer(e);
const t = this.doDecodeSync();
if (this.hasRemaining(1)) {
throw this.createExtraByteError(this.pos);
}
return t;
} finally {
this.entered = false;
}
}
* decodeMulti(e) {
if (this.entered) {
const t = this.clone();
yield* t.decodeMulti(e);
return;
}
try {
this.entered = true;
this.reinitializeState();
this.setBuffer(e);
while (this.hasRemaining(1)) {
yield this.doDecodeSync();
}
} finally {
this.entered = false;
}
}
async decodeAsync(e) {
if (this.entered) {
const t = this.clone();
return t.decodeAsync(e);
}
try {
this.entered = true;
let t = false;
let s;
for await (const i of e) {
if (t) {
this.entered = false;
throw this.createExtraByteError(this.totalPos);
}
this.appendBuffer(i);
try {
s = this.doDecodeSync();
t = true;
} catch (e) {
if (!(e instanceof RangeError)) {
throw e;
}
}
this.totalPos += this.pos;
}
if (t) {
if (this.hasRemaining(1)) {
throw this.createExtraByteError(this.totalPos);
}
return s;
}
const {headByte: i, pos: n, totalPos: r} = this;
throw new RangeError(`Insufficient data in parsing ${z(i)} at ${r} (${n} in the current buffer)`);
} finally {
this.entered = false;
}
}
decodeArrayStream(e) {
return this.decodeMultiAsync(e, true);
}
decodeStream(e) {
return this.decodeMultiAsync(e, false);
}
async* decodeMultiAsync(e, t) {
if (this.entered) {
const s = this.clone();
yield* s.decodeMultiAsync(e, t);
return;
}
try {
this.entered = true;
let s = t;
let i = -1;
for await (const n of e) {
if (t && i === 0) {
throw this.createExtraByteError(this.totalPos);
}
this.appendBuffer(n);
if (s) {
i = this.readArraySize();
s = false;
this.complete();
}
try {
while (true) {
yield this.doDecodeSync();
if (--i === 0) {
break;
}
}
} catch (e) {
if (!(e instanceof RangeError)) {
throw e;
}
}
this.totalPos += this.pos;
}
} finally {
this.entered = false;
}
}
doDecodeSync() {
e: while (true) {
const e = this.readHeadByte();
let t;
if (e >= 224) {
t = e - 256;
} else if (e < 192) {
if (e < 128) {
t = e;
} else if (e < 144) {
const s = e - 128;
if (s !== 0) {
this.pushMapState(s);
this.complete();
continue e;
} else {
t = {};
}
} else if (e < 160) {
const s = e - 144;
if (s !== 0) {
this.pushArrayState(s);
this.complete();
continue e;
} else {
t = [];
}
} else {
const s = e - 160;
t = this.decodeString(s, 0);
}
} else if (e === 192) {
t = null;
} else if (e === 194) {
t = false;
} else if (e === 195) {
t = true;
} else if (e === 202) {
t = this.readF32();
} else if (e === 203) {
t = this.readF64();
} else if (e === 204) {
t = this.readU8();
} else if (e === 205) {
t = this.readU16();
} else if (e === 206) {
t = this.readU32();
} else if (e === 207) {
if (this.useBigInt64) {
t = this.readU64AsBigInt();
} else {
t = this.readU64();
}
} else if (e === 208) {
t = this.readI8();
} else if (e === 209) {
t = this.readI16();
} else if (e === 210) {
t = this.readI32();
} else if (e === 211) {
if (this.useBigInt64) {
t = this.readI64AsBigInt();
} else {
t = this.readI64();
}
} else if (e === 217) {
const e = this.lookU8();
t = this.decodeString(e, 1);
} else if (e === 218) {
const e = this.lookU16();
t = this.decodeString(e, 2);
} else if (e === 219) {
const e = this.lookU32();
t = this.decodeString(e, 4);
} else if (e === 220) {
const e = this.readU16();
if (e !== 0) {
this.pushArrayState(e);
this.complete();
continue e;
} else {
t = [];
}
} else if (e === 221) {
const e = this.readU32();
if (e !== 0) {
this.pushArrayState(e);
this.complete();
continue e;
} else {
t = [];
}
} else if (e === 222) {
const e = this.readU16();
if (e !== 0) {
this.pushMapState(e);
this.complete();
continue e;
} else {
t = {};
}
} else if (e === 223) {
const e = this.readU32();
if (e !== 0) {
this.pushMapState(e);
this.complete();
continue e;
} else {
t = {};
}
} else if (e === 196) {
const e = this.lookU8();
t = this.decodeBinary(e, 1);
} else if (e === 197) {
const e = this.lookU16();
t = this.decodeBinary(e, 2);
} else if (e === 198) {
const e = this.lookU32();
t = this.decodeBinary(e, 4);
} else if (e === 212) {
t = this.decodeExtension(1, 0);
} else if (e === 213) {
t = this.decodeExtension(2, 0);
} else if (e === 214) {
t = this.decodeExtension(4, 0);
} else if (e === 215) {
t = this.decodeExtension(8, 0);
} else if (e === 216) {
t = this.decodeExtension(16, 0);
} else if (e === 199) {
const e = this.lookU8();
t = this.decodeExtension(e, 1);
} else if (e === 200) {
const e = this.lookU16();
t = this.decodeExtension(e, 2);
} else if (e === 201) {
const e = this.lookU32();
t = this.decodeExtension(e, 4);
} else {
throw new DecodeError(`Unrecognized type byte: ${z(e)}`);
}
this.complete();
const s = this.stack;
while (s.length > 0) {
const e = s.top();
if (e.type === M) {
e.array[e.position] = t;
e.position++;
if (e.position === e.size) {
t = e.array;
s.release(e);
} else {
continue e;
}
} else if (e.type === F) {
if (t === "__proto__") {
throw new DecodeError("The key __proto__ is not allowed");
}
e.key = this.mapKeyConverter(t);
e.type = P;
continue e;
} else {
e.map[e.key] = t;
e.readCount++;
if (e.readCount === e.size) {
t = e.map;
s.release(e);
} else {
e.key = null;
e.type = F;
continue e;
}
}
}
return t;
}
}
readHeadByte() {
if (this.headByte === K) {
this.headByte = this.readU8();
}
return this.headByte;
}
complete() {
this.headByte = K;
}
readArraySize() {
const e = this.readHeadByte();
switch (e) {
case 220:
return this.readU16();
case 221:
return this.readU32();
default:
{
if (e < 160) {
return e - 144;
} else {
throw new DecodeError(`Unrecognized array type byte: ${z(e)}`);
}
}
}
}
pushMapState(e) {
if (e > this.maxMapLength) {
throw new DecodeError(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);
}
this.stack.pushMapState(e);
}
pushArrayState(e) {
if (e > this.maxArrayLength) {
throw new DecodeError(`Max length exceeded: array length (${e}) > maxArrayLength (${this.maxArrayLength})`);
}
this.stack.pushArrayState(e);
}
decodeString(e, t) {
if (!this.rawStrings || this.stateIsMapKey()) {
return this.decodeUtf8String(e, t);
}
return this.decodeBinary(e, t);
}
decodeUtf8String(e, t) {
if (e > this.maxStrLength) {
throw new DecodeError(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);
}
if (this.bytes.byteLength < this.pos + t + e) {
throw j;
}
const s = this.pos + t;
let i;
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(e)) {
i = this.keyDecoder.decode(this.bytes, s, e);
} else {
i = d(this.bytes, s, e);
}
this.pos += t + e;
return i;
}
stateIsMapKey() {
if (this.stack.length > 0) {
const e = this.stack.top();
return e.type === F;
}
return false;
}
decodeBinary(e, t) {
if (e > this.maxBinLength) {
throw new DecodeError(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);
}
if (!this.hasRemaining(e + t)) {
throw j;
}
const s = this.pos + t;
const i = this.bytes.subarray(s, s + e);
this.pos += t + e;
return i;
}
decodeExtension(e, t) {
if (e > this.maxExtLength) {
throw new DecodeError(`Max length exceeded: ext length (${e}) > maxExtLength (${this.maxExtLength})`);
}
const s = this.view.getInt8(this.pos + t);
const i = this.decodeBinary(e, t + 1);
return this.extensionCodec.decode(i, s, this.context);
}
lookU8() {
return this.view.getUint8(this.pos);
}
lookU16() {
return this.view.getUint16(this.pos);
}
lookU32() {
return this.view.getUint32(this.pos);
}
readU8() {
const e = this.view.getUint8(this.pos);
this.pos++;
return e;
}
readI8() {
const e = this.view.getInt8(this.pos);
this.pos++;
return e;
}
readU16() {
const e = this.view.getUint16(this.pos);
this.pos += 2;
return e;
}
readI16() {
const e = this.view.getInt16(this.pos);
this.pos += 2;
return e;
}
readU32() {
const e = this.view.getUint32(this.pos);
this.pos += 4;
return e;
}
readI32() {
const e = this.view.getInt32(this.pos);
this.pos += 4;
return e;
}
readU64() {
const e = y(this.view, this.pos);
this.pos += 8;
return e;
}
readI64() {
const e = g(this.view, this.pos);
this.pos += 8;
return e;
}
readU64AsBigInt() {
const e = this.view.getBigUint64(this.pos);
this.pos += 8;
return e;
}
readI64AsBigInt() {
const e = this.view.getBigInt64(this.pos);
this.pos += 8;
return e;
}
readF32() {
const e = this.view.getFloat32(this.pos);
this.pos += 4;
return e;
}
readF64() {
const e = this.view.getFloat64(this.pos);
this.pos += 8;
return e;
}
}
function O(e, t) {
const s = new Decoder(t);
return s.decode(e);
}
class MessagePack {
static encode(e) {
return v(e, {
useBigInt64: true,
ignoreUndefined: true
});
}
static decode(e) {
return O(e, {
useBigInt64: true
});
}
static stringify(e, t = "base64") {
return Buffer.from(this.encode(e)).toString(t);
}
static parse(e, t = "base64") {
return this.decode(Buffer.from(e, t));
}
}
exports.MessagePack = MessagePack;