lowstorage
Version:
Simple, micro-dependency, pseudo-database using Apache Avro serialization on S3-compatible storages, inspired by lowdb.
1,248 lines • 137 kB
JavaScript
// <stdin>
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 __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
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 require_index_no_eval = __commonJS({
"node_modules/msgpackr/dist/index-no-eval.cjs"(exports, module) {
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.msgpackr = {}));
})(exports, function(exports2) {
"use strict";
var decoder;
try {
decoder = new TextDecoder();
} catch (error) {
}
var src;
var srcEnd;
var position$1 = 0;
var currentUnpackr = {};
var currentStructures;
var srcString;
var srcStringStart = 0;
var srcStringEnd = 0;
var bundledStrings$1;
var referenceMap;
var currentExtensions = [];
var dataView;
var defaultOptions = {
useRecords: false,
mapsAsObjects: true
};
class C1Type {
}
const C1 = new C1Type();
C1.name = "MessagePack 0xC1";
var sequentialMode = false;
var inlineObjectReadThreshold = 2;
var readStruct;
var BlockedFunction;
try {
new BlockedFunction("");
} catch (error) {
inlineObjectReadThreshold = Infinity;
}
class Unpackr {
constructor(options) {
if (options) {
if (options.useRecords === false && options.mapsAsObjects === void 0)
options.mapsAsObjects = true;
if (options.sequential && options.trusted !== false) {
options.trusted = true;
if (!options.structures && options.useRecords != false) {
options.structures = [];
if (!options.maxSharedStructures)
options.maxSharedStructures = 0;
}
}
if (options.structures)
options.structures.sharedLength = options.structures.length;
else if (options.getStructures) {
(options.structures = []).uninitialized = true;
options.structures.sharedLength = 0;
}
if (options.int64AsNumber) {
options.int64AsType = "number";
}
}
Object.assign(this, options);
}
unpack(source, options) {
if (src) {
return saveState(() => {
clearSource();
return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options);
});
}
if (!source.buffer && source.constructor === ArrayBuffer)
source = typeof Buffer !== "undefined" ? Buffer.from(source) : new Uint8Array(source);
if (typeof options === "object") {
srcEnd = options.end || source.length;
position$1 = options.start || 0;
} else {
position$1 = 0;
srcEnd = options > -1 ? options : source.length;
}
srcStringEnd = 0;
srcString = null;
bundledStrings$1 = null;
src = source;
try {
dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
} catch (error) {
src = null;
if (source instanceof Uint8Array)
throw error;
throw new Error("Source must be a Uint8Array or Buffer but was a " + (source && typeof source == "object" ? source.constructor.name : typeof source));
}
if (this instanceof Unpackr) {
currentUnpackr = this;
if (this.structures) {
currentStructures = this.structures;
return checkedRead(options);
} else if (!currentStructures || currentStructures.length > 0) {
currentStructures = [];
}
} else {
currentUnpackr = defaultOptions;
if (!currentStructures || currentStructures.length > 0)
currentStructures = [];
}
return checkedRead(options);
}
unpackMultiple(source, forEach) {
let values, lastPosition = 0;
try {
sequentialMode = true;
let size = source.length;
let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
if (forEach) {
if (forEach(value, lastPosition, position$1) === false) return;
while (position$1 < size) {
lastPosition = position$1;
if (forEach(checkedRead(), lastPosition, position$1) === false) {
return;
}
}
} else {
values = [value];
while (position$1 < size) {
lastPosition = position$1;
values.push(checkedRead());
}
return values;
}
} catch (error) {
error.lastPosition = lastPosition;
error.values = values;
throw error;
} finally {
sequentialMode = false;
clearSource();
}
}
_mergeStructures(loadedStructures, existingStructures) {
loadedStructures = loadedStructures || [];
if (Object.isFrozen(loadedStructures))
loadedStructures = loadedStructures.map((structure) => structure.slice(0));
for (let i = 0, l = loadedStructures.length; i < l; i++) {
let structure = loadedStructures[i];
if (structure) {
structure.isShared = true;
if (i >= 32)
structure.highByte = i - 32 >> 5;
}
}
loadedStructures.sharedLength = loadedStructures.length;
for (let id in existingStructures || []) {
if (id >= 0) {
let structure = loadedStructures[id];
let existing = existingStructures[id];
if (existing) {
if (structure)
(loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
loadedStructures[id] = existing;
}
}
}
return this.structures = loadedStructures;
}
decode(source, options) {
return this.unpack(source, options);
}
}
function checkedRead(options) {
try {
if (!currentUnpackr.trusted && !sequentialMode) {
let sharedLength = currentStructures.sharedLength || 0;
if (sharedLength < currentStructures.length)
currentStructures.length = sharedLength;
}
let result;
if (currentUnpackr.randomAccessStructure && src[position$1] < 64 && src[position$1] >= 32 && readStruct) {
result = readStruct(src, position$1, srcEnd, currentUnpackr);
src = null;
if (!(options && options.lazy) && result)
result = result.toJSON();
position$1 = srcEnd;
} else
result = read();
if (bundledStrings$1) {
position$1 = bundledStrings$1.postBundlePosition;
bundledStrings$1 = null;
}
if (sequentialMode)
currentStructures.restoreStructures = null;
if (position$1 == srcEnd) {
if (currentStructures && currentStructures.restoreStructures)
restoreStructures();
currentStructures = null;
src = null;
if (referenceMap)
referenceMap = null;
} else if (position$1 > srcEnd) {
throw new Error("Unexpected end of MessagePack data");
} else if (!sequentialMode) {
let jsonView;
try {
jsonView = JSON.stringify(result, (_2, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
} catch (error) {
jsonView = "(JSON view not available " + error + ")";
}
throw new Error("Data read, but end of buffer not reached " + jsonView);
}
return result;
} catch (error) {
if (currentStructures && currentStructures.restoreStructures)
restoreStructures();
clearSource();
if (error instanceof RangeError || error.message.startsWith("Unexpected end of buffer") || position$1 > srcEnd) {
error.incomplete = true;
}
throw error;
}
}
function restoreStructures() {
for (let id in currentStructures.restoreStructures) {
currentStructures[id] = currentStructures.restoreStructures[id];
}
currentStructures.restoreStructures = null;
}
function read() {
let token = src[position$1++];
if (token < 160) {
if (token < 128) {
if (token < 64)
return token;
else {
let structure = currentStructures[token & 63] || currentUnpackr.getStructures && loadStructures()[token & 63];
if (structure) {
if (!structure.read) {
structure.read = createStructureReader(structure, token & 63);
}
return structure.read();
} else
return token;
}
} else if (token < 144) {
token -= 128;
if (currentUnpackr.mapsAsObjects) {
let object = {};
for (let i = 0; i < token; i++) {
let key = readKey();
if (key === "__proto__")
key = "__proto_";
object[key] = read();
}
return object;
} else {
let map = /* @__PURE__ */ new Map();
for (let i = 0; i < token; i++) {
map.set(read(), read());
}
return map;
}
} else {
token -= 144;
let array = new Array(token);
for (let i = 0; i < token; i++) {
array[i] = read();
}
if (currentUnpackr.freezeData)
return Object.freeze(array);
return array;
}
} else if (token < 192) {
let length = token - 160;
if (srcStringEnd >= position$1) {
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
}
if (srcStringEnd == 0 && srcEnd < 140) {
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
if (string != null)
return string;
}
return readFixedString(length);
} else {
let value;
switch (token) {
case 192:
return null;
case 193:
if (bundledStrings$1) {
value = read();
if (value > 0)
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value);
else
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value);
}
return C1;
// "never-used", return special object to denote that
case 194:
return false;
case 195:
return true;
case 196:
value = src[position$1++];
if (value === void 0)
throw new Error("Unexpected end of buffer");
return readBin(value);
case 197:
value = dataView.getUint16(position$1);
position$1 += 2;
return readBin(value);
case 198:
value = dataView.getUint32(position$1);
position$1 += 4;
return readBin(value);
case 199:
return readExt(src[position$1++]);
case 200:
value = dataView.getUint16(position$1);
position$1 += 2;
return readExt(value);
case 201:
value = dataView.getUint32(position$1);
position$1 += 4;
return readExt(value);
case 202:
value = dataView.getFloat32(position$1);
if (currentUnpackr.useFloat32 > 2) {
let multiplier = mult10[(src[position$1] & 127) << 1 | src[position$1 + 1] >> 7];
position$1 += 4;
return (multiplier * value + (value > 0 ? 0.5 : -0.5) >> 0) / multiplier;
}
position$1 += 4;
return value;
case 203:
value = dataView.getFloat64(position$1);
position$1 += 8;
return value;
// uint handlers
case 204:
return src[position$1++];
case 205:
value = dataView.getUint16(position$1);
position$1 += 2;
return value;
case 206:
value = dataView.getUint32(position$1);
position$1 += 4;
return value;
case 207:
if (currentUnpackr.int64AsType === "number") {
value = dataView.getUint32(position$1) * 4294967296;
value += dataView.getUint32(position$1 + 4);
} else if (currentUnpackr.int64AsType === "string") {
value = dataView.getBigUint64(position$1).toString();
} else if (currentUnpackr.int64AsType === "auto") {
value = dataView.getBigUint64(position$1);
if (value <= BigInt(2) << BigInt(52)) value = Number(value);
} else
value = dataView.getBigUint64(position$1);
position$1 += 8;
return value;
// int handlers
case 208:
return dataView.getInt8(position$1++);
case 209:
value = dataView.getInt16(position$1);
position$1 += 2;
return value;
case 210:
value = dataView.getInt32(position$1);
position$1 += 4;
return value;
case 211:
if (currentUnpackr.int64AsType === "number") {
value = dataView.getInt32(position$1) * 4294967296;
value += dataView.getUint32(position$1 + 4);
} else if (currentUnpackr.int64AsType === "string") {
value = dataView.getBigInt64(position$1).toString();
} else if (currentUnpackr.int64AsType === "auto") {
value = dataView.getBigInt64(position$1);
if (value >= BigInt(-2) << BigInt(52) && value <= BigInt(2) << BigInt(52)) value = Number(value);
} else
value = dataView.getBigInt64(position$1);
position$1 += 8;
return value;
case 212:
value = src[position$1++];
if (value == 114) {
return recordDefinition(src[position$1++] & 63);
} else {
let extension = currentExtensions[value];
if (extension) {
if (extension.read) {
position$1++;
return extension.read(read());
} else if (extension.noBuffer) {
position$1++;
return extension();
} else
return extension(src.subarray(position$1, ++position$1));
} else
throw new Error("Unknown extension " + value);
}
case 213:
value = src[position$1];
if (value == 114) {
position$1++;
return recordDefinition(src[position$1++] & 63, src[position$1++]);
} else
return readExt(2);
case 214:
return readExt(4);
case 215:
return readExt(8);
case 216:
return readExt(16);
case 217:
value = src[position$1++];
if (srcStringEnd >= position$1) {
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
}
return readString8(value);
case 218:
value = dataView.getUint16(position$1);
position$1 += 2;
if (srcStringEnd >= position$1) {
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
}
return readString16(value);
case 219:
value = dataView.getUint32(position$1);
position$1 += 4;
if (srcStringEnd >= position$1) {
return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
}
return readString32(value);
case 220:
value = dataView.getUint16(position$1);
position$1 += 2;
return readArray(value);
case 221:
value = dataView.getUint32(position$1);
position$1 += 4;
return readArray(value);
case 222:
value = dataView.getUint16(position$1);
position$1 += 2;
return readMap(value);
case 223:
value = dataView.getUint32(position$1);
position$1 += 4;
return readMap(value);
default:
if (token >= 224)
return token - 256;
if (token === void 0) {
let error = new Error("Unexpected end of MessagePack data");
error.incomplete = true;
throw error;
}
throw new Error("Unknown MessagePack token " + token);
}
}
}
const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
function createStructureReader(structure, firstId) {
function readObject() {
if (readObject.count++ > inlineObjectReadThreshold) {
let readObject2 = structure.read = new BlockedFunction("r", "return function(){return " + (currentUnpackr.freezeData ? "Object.freeze" : "") + "({" + structure.map((key) => key === "__proto__" ? "__proto_:r()" : validName.test(key) ? key + ":r()" : "[" + JSON.stringify(key) + "]:r()").join(",") + "})}")(read);
if (structure.highByte === 0)
structure.read = createSecondByteReader(firstId, structure.read);
return readObject2();
}
let object = {};
for (let i = 0, l = structure.length; i < l; i++) {
let key = structure[i];
if (key === "__proto__")
key = "__proto_";
object[key] = read();
}
if (currentUnpackr.freezeData)
return Object.freeze(object);
return object;
}
readObject.count = 0;
if (structure.highByte === 0) {
return createSecondByteReader(firstId, readObject);
}
return readObject;
}
const createSecondByteReader = (firstId, read0) => {
return function() {
let highByte = src[position$1++];
if (highByte === 0)
return read0();
let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
let structure = currentStructures[id] || loadStructures()[id];
if (!structure) {
throw new Error("Record id is not defined for " + id);
}
if (!structure.read)
structure.read = createStructureReader(structure, firstId);
return structure.read();
};
};
function loadStructures() {
let loadedStructures = saveState(() => {
src = null;
return currentUnpackr.getStructures();
});
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures);
}
var readFixedString = readStringJS;
var readString8 = readStringJS;
var readString16 = readStringJS;
var readString32 = readStringJS;
let isNativeAccelerationEnabled = false;
function readStringJS(length) {
let result;
if (length < 16) {
if (result = shortStringInJS(length))
return result;
}
if (length > 64 && decoder)
return decoder.decode(src.subarray(position$1, position$1 += length));
const end = position$1 + length;
const units = [];
result = "";
while (position$1 < end) {
const byte1 = src[position$1++];
if ((byte1 & 128) === 0) {
units.push(byte1);
} else if ((byte1 & 224) === 192) {
const byte2 = src[position$1++] & 63;
units.push((byte1 & 31) << 6 | byte2);
} else if ((byte1 & 240) === 224) {
const byte2 = src[position$1++] & 63;
const byte3 = src[position$1++] & 63;
units.push((byte1 & 31) << 12 | byte2 << 6 | byte3);
} else if ((byte1 & 248) === 240) {
const byte2 = src[position$1++] & 63;
const byte3 = src[position$1++] & 63;
const byte4 = src[position$1++] & 63;
let unit = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
if (unit > 65535) {
unit -= 65536;
units.push(unit >>> 10 & 1023 | 55296);
unit = 56320 | unit & 1023;
}
units.push(unit);
} else {
units.push(byte1);
}
if (units.length >= 4096) {
result += fromCharCode.apply(String, units);
units.length = 0;
}
}
if (units.length > 0) {
result += fromCharCode.apply(String, units);
}
return result;
}
function readArray(length) {
let array = new Array(length);
for (let i = 0; i < length; i++) {
array[i] = read();
}
if (currentUnpackr.freezeData)
return Object.freeze(array);
return array;
}
function readMap(length) {
if (currentUnpackr.mapsAsObjects) {
let object = {};
for (let i = 0; i < length; i++) {
let key = readKey();
if (key === "__proto__")
key = "__proto_";
object[key] = read();
}
return object;
} else {
let map = /* @__PURE__ */ new Map();
for (let i = 0; i < length; i++) {
map.set(read(), read());
}
return map;
}
}
var fromCharCode = String.fromCharCode;
function longStringInJS(length) {
let start = position$1;
let bytes = new Array(length);
for (let i = 0; i < length; i++) {
const byte = src[position$1++];
if ((byte & 128) > 0) {
position$1 = start;
return;
}
bytes[i] = byte;
}
return fromCharCode.apply(String, bytes);
}
function shortStringInJS(length) {
if (length < 4) {
if (length < 2) {
if (length === 0)
return "";
else {
let a = src[position$1++];
if ((a & 128) > 1) {
position$1 -= 1;
return;
}
return fromCharCode(a);
}
} else {
let a = src[position$1++];
let b2 = src[position$1++];
if ((a & 128) > 0 || (b2 & 128) > 0) {
position$1 -= 2;
return;
}
if (length < 3)
return fromCharCode(a, b2);
let c = src[position$1++];
if ((c & 128) > 0) {
position$1 -= 3;
return;
}
return fromCharCode(a, b2, c);
}
} else {
let a = src[position$1++];
let b2 = src[position$1++];
let c = src[position$1++];
let d = src[position$1++];
if ((a & 128) > 0 || (b2 & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
position$1 -= 4;
return;
}
if (length < 6) {
if (length === 4)
return fromCharCode(a, b2, c, d);
else {
let e = src[position$1++];
if ((e & 128) > 0) {
position$1 -= 5;
return;
}
return fromCharCode(a, b2, c, d, e);
}
} else if (length < 8) {
let e = src[position$1++];
let f2 = src[position$1++];
if ((e & 128) > 0 || (f2 & 128) > 0) {
position$1 -= 6;
return;
}
if (length < 7)
return fromCharCode(a, b2, c, d, e, f2);
let g = src[position$1++];
if ((g & 128) > 0) {
position$1 -= 7;
return;
}
return fromCharCode(a, b2, c, d, e, f2, g);
} else {
let e = src[position$1++];
let f2 = src[position$1++];
let g = src[position$1++];
let h2 = src[position$1++];
if ((e & 128) > 0 || (f2 & 128) > 0 || (g & 128) > 0 || (h2 & 128) > 0) {
position$1 -= 8;
return;
}
if (length < 10) {
if (length === 8)
return fromCharCode(a, b2, c, d, e, f2, g, h2);
else {
let i = src[position$1++];
if ((i & 128) > 0) {
position$1 -= 9;
return;
}
return fromCharCode(a, b2, c, d, e, f2, g, h2, i);
}
} else if (length < 12) {
let i = src[position$1++];
let j2 = src[position$1++];
if ((i & 128) > 0 || (j2 & 128) > 0) {
position$1 -= 10;
return;
}
if (length < 11)
return fromCharCode(a, b2, c, d, e, f2, g, h2, i, j2);
let k2 = src[position$1++];
if ((k2 & 128) > 0) {
position$1 -= 11;
return;
}
return fromCharCode(a, b2, c, d, e, f2, g, h2, i, j2, k2);
} else {
let i = src[position$1++];
let j2 = src[position$1++];
let k2 = src[position$1++];
let l = src[position$1++];
if ((i & 128) > 0 || (j2 & 128) > 0 || (k2 & 128) > 0 || (l & 128) > 0) {
position$1 -= 12;
return;
}
if (length < 14) {
if (length === 12)
return fromCharCode(a, b2, c, d, e, f2, g, h2, i, j2, k2, l);
else {
let m2 = src[position$1++];
if ((m2 & 128) > 0) {
position$1 -= 13;
return;
}
return fromCharCode(a, b2, c, d, e, f2, g, h2, i, j2, k2, l, m2);
}
} else {
let m2 = src[position$1++];
let n = src[position$1++];
if ((m2 & 128) > 0 || (n & 128) > 0) {
position$1 -= 14;
return;
}
if (length < 15)
return fromCharCode(a, b2, c, d, e, f2, g, h2, i, j2, k2, l, m2, n);
let o = src[position$1++];
if ((o & 128) > 0) {
position$1 -= 15;
return;
}
return fromCharCode(a, b2, c, d, e, f2, g, h2, i, j2, k2, l, m2, n, o);
}
}
}
}
}
function readOnlyJSString() {
let token = src[position$1++];
let length;
if (token < 192) {
length = token - 160;
} else {
switch (token) {
case 217:
length = src[position$1++];
break;
case 218:
length = dataView.getUint16(position$1);
position$1 += 2;
break;
case 219:
length = dataView.getUint32(position$1);
position$1 += 4;
break;
default:
throw new Error("Expected string");
}
}
return readStringJS(length);
}
function readBin(length) {
return currentUnpackr.copyBuffers ? (
// specifically use the copying slice (not the node one)
Uint8Array.prototype.slice.call(src, position$1, position$1 += length)
) : src.subarray(position$1, position$1 += length);
}
function readExt(length) {
let type = src[position$1++];
if (currentExtensions[type]) {
let end;
return currentExtensions[type](src.subarray(position$1, end = position$1 += length), (readPosition) => {
position$1 = readPosition;
try {
return read();
} finally {
position$1 = end;
}
});
} else
throw new Error("Unknown extension type " + type);
}
var keyCache = new Array(4096);
function readKey() {
let length = src[position$1++];
if (length >= 160 && length < 192) {
length = length - 160;
if (srcStringEnd >= position$1)
return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
else if (!(srcStringEnd == 0 && srcEnd < 180))
return readFixedString(length);
} else {
position$1--;
return asSafeString(read());
}
let key = (length << 5 ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 4095;
let entry = keyCache[key];
let checkPosition = position$1;
let end = position$1 + length - 3;
let chunk;
let i = 0;
if (entry && entry.bytes == length) {
while (checkPosition < end) {
chunk = dataView.getUint32(checkPosition);
if (chunk != entry[i++]) {
checkPosition = 1879048192;
break;
}
checkPosition += 4;
}
end += 3;
while (checkPosition < end) {
chunk = src[checkPosition++];
if (chunk != entry[i++]) {
checkPosition = 1879048192;
break;
}
}
if (checkPosition === end) {
position$1 = checkPosition;
return entry.string;
}
end -= 3;
checkPosition = position$1;
}
entry = [];
keyCache[key] = entry;
entry.bytes = length;
while (checkPosition < end) {
chunk = dataView.getUint32(checkPosition);
entry.push(chunk);
checkPosition += 4;
}
end += 3;
while (checkPosition < end) {
chunk = src[checkPosition++];
entry.push(chunk);
}
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
if (string != null)
return entry.string = string;
return entry.string = readFixedString(length);
}
function asSafeString(property) {
if (typeof property === "string") return property;
if (typeof property === "number" || typeof property === "boolean" || typeof property === "bigint") return property.toString();
if (property == null) return property + "";
throw new Error("Invalid property type for record", typeof property);
}
const recordDefinition = (id, highByte) => {
let structure = read().map(asSafeString);
let firstByte = id;
if (highByte !== void 0) {
id = id < 32 ? -((highByte << 5) + id) : (highByte << 5) + id;
structure.highByte = highByte;
}
let existingStructure = currentStructures[id];
if (existingStructure && (existingStructure.isShared || sequentialMode)) {
(currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
}
currentStructures[id] = structure;
structure.read = createStructureReader(structure, firstByte);
return structure.read();
};
currentExtensions[0] = () => {
};
currentExtensions[0].noBuffer = true;
currentExtensions[66] = (data) => {
let length = data.length;
let value = BigInt(data[0] & 128 ? data[0] - 256 : data[0]);
for (let i = 1; i < length; i++) {
value <<= BigInt(8);
value += BigInt(data[i]);
}
return value;
};
let errors = { Error, TypeError, ReferenceError };
currentExtensions[101] = () => {
let data = read();
return (errors[data[0]] || Error)(data[1], { cause: data[2] });
};
currentExtensions[105] = (data) => {
if (currentUnpackr.structuredClone === false) throw new Error("Structured clone extension is disabled");
let id = dataView.getUint32(position$1 - 4);
if (!referenceMap)
referenceMap = /* @__PURE__ */ new Map();
let token = src[position$1];
let target2;
if (token >= 144 && token < 160 || token == 220 || token == 221)
target2 = [];
else
target2 = {};
let refEntry = { target: target2 };
referenceMap.set(id, refEntry);
let targetProperties = read();
if (refEntry.used)
return Object.assign(target2, targetProperties);
refEntry.target = targetProperties;
return targetProperties;
};
currentExtensions[112] = (data) => {
if (currentUnpackr.structuredClone === false) throw new Error("Structured clone extension is disabled");
let id = dataView.getUint32(position$1 - 4);
let refEntry = referenceMap.get(id);
refEntry.used = true;
return refEntry.target;
};
currentExtensions[115] = () => new Set(read());
const typedArrays = ["Int8", "Uint8", "Uint8Clamped", "Int16", "Uint16", "Int32", "Uint32", "Float32", "Float64", "BigInt64", "BigUint64"].map((type) => type + "Array");
let glbl = typeof globalThis === "object" ? globalThis : window;
currentExtensions[116] = (data) => {
let typeCode = data[0];
let typedArrayName = typedArrays[typeCode];
if (!typedArrayName) {
if (typeCode === 16) {
let ab = new ArrayBuffer(data.length - 1);
let u8 = new Uint8Array(ab);
u8.set(data.subarray(1));
return ab;
}
throw new Error("Could not find typed array for code " + typeCode);
}
return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer);
};
currentExtensions[120] = () => {
let data = read();
return new RegExp(data[0], data[1]);
};
const TEMP_BUNDLE = [];
currentExtensions[98] = (data) => {
let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
let dataPosition = position$1;
position$1 += dataSize - data.length;
bundledStrings$1 = TEMP_BUNDLE;
bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
bundledStrings$1.position0 = 0;
bundledStrings$1.position1 = 0;
bundledStrings$1.postBundlePosition = position$1;
position$1 = dataPosition;
return read();
};
currentExtensions[255] = (data) => {
if (data.length == 4)
return new Date((data[0] * 16777216 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1e3);
else if (data.length == 8)
return new Date(
((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1e6 + ((data[3] & 3) * 4294967296 + data[4] * 16777216 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1e3
);
else if (data.length == 12)
return new Date(
((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1e6 + ((data[4] & 128 ? -281474976710656 : 0) + data[6] * 1099511627776 + data[7] * 4294967296 + data[8] * 16777216 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1e3
);
else
return /* @__PURE__ */ new Date("invalid");
};
function saveState(callback) {
let savedSrcEnd = srcEnd;
let savedPosition = position$1;
let savedSrcStringStart = srcStringStart;
let savedSrcStringEnd = srcStringEnd;
let savedSrcString = srcString;
let savedReferenceMap = referenceMap;
let savedBundledStrings = bundledStrings$1;
let savedSrc = new Uint8Array(src.slice(0, srcEnd));
let savedStructures = currentStructures;
let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
let savedPackr = currentUnpackr;
let savedSequentialMode = sequentialMode;
let value = callback();
srcEnd = savedSrcEnd;
position$1 = savedPosition;
srcStringStart = savedSrcStringStart;
srcStringEnd = savedSrcStringEnd;
srcString = savedSrcString;
referenceMap = savedReferenceMap;
bundledStrings$1 = savedBundledStrings;
src = savedSrc;
sequentialMode = savedSequentialMode;
currentStructures = savedStructures;
currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
currentUnpackr = savedPackr;
dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
return value;
}
function clearSource() {
src = null;
referenceMap = null;
currentStructures = null;
}
function addExtension$1(extension) {
if (extension.unpack)
currentExtensions[extension.type] = extension.unpack;
else
currentExtensions[extension.type] = extension;
}
const mult10 = new Array(147);
for (let i = 0; i < 256; i++) {
mult10[i] = +("1e" + Math.floor(45.15 - i * 0.30103));
}
const Decoder = Unpackr;
var defaultUnpackr = new Unpackr({ useRecords: false });
const unpack = defaultUnpackr.unpack;
const unpackMultiple = defaultUnpackr.unpackMultiple;
const decode = defaultUnpackr.unpack;
const FLOAT32_OPTIONS = {
NEVER: 0,
ALWAYS: 1,
DECIMAL_ROUND: 3,
DECIMAL_FIT: 4
};
let f32Array = new Float32Array(1);
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
function roundFloat32(float32Number) {
f32Array[0] = float32Number;
let multiplier = mult10[(u8Array[3] & 127) << 1 | u8Array[2] >> 7];
return (multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5) >> 0) / multiplier;
}
let textEncoder;
try {
textEncoder = new TextEncoder();
} catch (error) {
}
let extensions, extensionClasses;
const hasNodeBuffer = typeof Buffer !== "undefined";
const ByteArrayAllocate = hasNodeBuffer ? function(length) {
return Buffer.allocUnsafeSlow(length);
} : Uint8Array;
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
const MAX_BUFFER_SIZE = hasNodeBuffer ? 4294967296 : 2144337920;
let target, keysTarget;
let targetView;
let position = 0;
let safeEnd;
let bundledStrings = null;
let writeStructSlots;
const MAX_BUNDLE_SIZE = 21760;
const hasNonLatin = /[\u0080-\uFFFF]/;
const RECORD_SYMBOL = Symbol("record-id");
class Packr2 extends Unpackr {
constructor(options) {
super(options);
this.offset = 0;
let start;
let hasSharedUpdate;
let structures;
let referenceMap2;
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position2) {
return target.utf8Write(string, position2, target.byteLength - position2);
} : textEncoder && textEncoder.encodeInto ? function(string, position2) {
return textEncoder.encodeInto(string, target.subarray(position2)).written;
} : false;
let packr = this;
if (!options)
options = {};
let isSequential = options && options.sequential;
let hasSharedStructures = options.structures || options.saveStructures;
let maxSharedStructures = options.maxSharedStructures;
if (maxSharedStructures == null)
maxSharedStructures = hasSharedStructures ? 32 : 0;
if (maxSharedStructures > 8160)
throw new Error("Maximum maxSharedStructure is 8160");
if (options.structuredClone && options.moreTypes == void 0) {
this.moreTypes = true;
}
let maxOwnStructures = options.maxOwnStructures;
if (maxOwnStructures == null)
maxOwnStructures = hasSharedStructures ? 32 : 64;
if (!this.structures && options.useRecords != false)
this.structures = [];
let useTwoByteRecords = maxSharedStructures > 32 || maxOwnStructures + maxSharedStructures > 64;
let sharedLimitId = maxSharedStructures + 64;
let maxStructureId = maxSharedStructures + maxOwnStructures + 64;
if (maxStructureId > 8256) {
throw new Error("Maximum maxSharedStructure + maxOwnStructure is 8192");
}
let recordIdsToRemove = [];
let transitionsCount = 0;
let serializationsSinceTransitionRebuild = 0;
this.pack = this.encode = function(value, encodeOptions) {
if (!target) {
target = new ByteArrayAllocate(8192);
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
position = 0;
}
safeEnd = target.length - 10;
if (safeEnd - position < 2048) {
target = new ByteArrayAllocate(target.length);
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
safeEnd = target.length - 10;
position = 0;
} else
position = position + 7 & 2147483640;
start = position;
if (encodeOptions & RESERVE_START_SPACE) position += encodeOptions & 255;
referenceMap2 = packr.structuredClone ? /* @__PURE__ */ new Map() : null;
if (packr.bundleStrings && typeof value !== "string") {
bundledStrings = [];
bundledStrings.size = Infinity;
} else
bundledStrings = null;
structures = packr.structures;
if (structures) {
if (structures.uninitialized)
structures = packr._mergeStructures(packr.getStructures());
let sharedLength = structures.sharedLength || 0;
if (sharedLength > maxSharedStructures) {
throw new Error("Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to " + structures.sharedLength);
}
if (!structures.transitions) {
structures.transitions = /* @__PURE__ */ Object.create(null);
for (let i = 0; i < sharedLength; i++) {
let keys = structures[i];
if (!keys)
continue;
let nextTransition, transition = structures.transitions;
for (let j2 = 0, l = keys.length; j2 < l; j2++) {
let key = keys[j2];
nextTransition = transition[key];
if (!nextTransition) {
nextTransition = transition[key] = /* @__PURE__ */ Object.create(null);
}
transition = nextTransition;
}
transition[RECORD_SYMBOL] = i + 64;
}
this.lastNamedStructuresLength = sharedLength;
}
if (!isSequential) {
structures.nextId = sharedLength + 64;
}
}
if (hasSharedUpdate)
hasSharedUpdate = false;
let encodingError;
try {
if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
writeStruct(value);
else
pack2(value);
let lastBundle = bundledStrings;
if (bundledStrings)
writeBundles(start, pack2, 0);
if (referenceMap2 && referenceMap2.idsToInsert) {
let idsToInsert = referenceMap2.idsToInsert.sort((a, b2) => a.offset > b2.offset ? 1 : -1);
let i = idsToInsert.length;
let incrementPosition = -1;
while (lastBundle && i > 0) {
let insertionPoint = idsToInsert[--i].offset + start;
if (insertionPoint < lastBundle.stringsPosition + start && incrementPosition === -1)
incrementPosition = 0;
if (insertionPoint > lastBundle.position + start) {
if (incrementPosition >= 0)
incrementPosition += 6;
} else {
if (incrementPosition >= 0) {
targetView.setUint32(
lastBundle.position + start,
targetView.getUint32(lastBundle.position + start) + incrementPosition
);
incrementPosition = -1;
}
lastBundle = lastBundle.previous;
i++;
}
}
if (incrementPosition >= 0 && lastBundle) {
targetView.setUint32(
lastBundle.position + start,
targetView.getUint32(lastBundle.position + start) + incrementPosition
);
}
position += idsToInsert.length * 6;
if (position > safeEnd)
makeRoom(position);
packr.offset = position;
let serialized = insertIds(target.subarray(start, position), idsToInsert);
referenceMap2 = null;
return serialized;
}
packr.offset = position;
if (encodeOptions & REUSE_BUFFER_MODE) {
target.start = start;
target.end = position;
return target;
}
return target.subarray(start, position);
} catch (error) {
encodingError = error;
throw error;
} finally {
if (structures) {
resetStructures();
if (hasSharedUpdate && packr.saveStructures) {
let sharedLength = structures.sharedLength || 0;
let returnBuffer = target.subarray(start, position);
let newSharedData = prepareStructures(structures, packr);
if (!encodingError) {
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
return packr.pack(value, encodeOptions);
}
packr.lastNamedStructuresLength = sharedLength;
if (target.length > 1073741824) target = null;
return returnBuffer;
}
}
}
if (target.length > 1073741824) target = null;
if (encodeOptions & RESET_BUFFER_MODE)
position = start;
}
};
const resetStructures = () => {
if (serializationsSinceTransitionRebuild < 10)
serializationsSinceTransitionRebuild++;
let sharedLength = structures.sharedLength || 0;
if (structures.length > sharedLength && !isSequential)
structures.length = sharedLen