UNPKG

@animo-id/mdoc

Version:
1,414 lines (1,412 loc) 163 kB
var __typeError = (msg) => { throw TypeError(msg); }; 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); // src/cbor/cbor-x/decode.js var decoder; try { decoder = new TextDecoder(); } catch (error) { } var src; var srcEnd; var position = 0; var EMPTY_ARRAY = []; var LEGACY_RECORD_INLINE_ID = 105; var RECORD_DEFINITIONS_ID = 57342; var RECORD_INLINE_ID = 57343; var BUNDLED_STRINGS_ID = 57337; var PACKED_REFERENCE_TAG_ID = 6; var STOP_CODE = {}; var maxArraySize = 11281e4; var maxMapSize = 1681e4; var strings = EMPTY_ARRAY; var stringPosition = 0; var currentDecoder = {}; var currentStructures; var srcString; var srcStringStart = 0; var srcStringEnd = 0; var bundledStrings; var referenceMap; var currentExtensions = []; var currentExtensionRanges = []; var packedValues; var dataView; var restoreMapsAsObject; var defaultOptions = { useRecords: false, mapsAsObjects: true }; var sequentialMode = false; var inlineObjectReadThreshold = 2; try { new Function(""); } catch (error) { inlineObjectReadThreshold = Number.POSITIVE_INFINITY; } var Decoder = class _Decoder { constructor(options) { if (options) { if ((options.keyMap || options._keyMap) && !options.useRecords) { options.useRecords = false; options.mapsAsObjects = true; } if (options.useRecords === false && options.mapsAsObjects === void 0) options.mapsAsObjects = true; if (options.getStructures) options.getShared = options.getStructures; if (options.getShared && !options.structures) (options.structures = []).uninitialized = true; if (options.keyMap) { this.mapKey = /* @__PURE__ */ new Map(); for (const [k, v] of Object.entries(options.keyMap)) this.mapKey.set(v, k); } } Object.assign(this, options); } /* decodeKey(key) { return this.keyMap ? Object.keys(this.keyMap)[Object.values(this.keyMap).indexOf(key)] || key : key } */ decodeKey(key) { return this.keyMap ? this.mapKey.get(key) || key : key; } encodeKey(key) { return this.keyMap?.hasOwnProperty(key) ? this.keyMap[key] : key; } encodeKeys(rec) { if (!this._keyMap) return rec; const map = /* @__PURE__ */ new Map(); for (const [k, v] of Object.entries(rec)) map.set(this._keyMap.hasOwnProperty(k) ? this._keyMap[k] : k, v); return map; } decodeKeys(map) { if (!this._keyMap || map.constructor.name !== "Map") return map; if (!this._mapKey) { this._mapKey = /* @__PURE__ */ new Map(); for (const [k, v] of Object.entries(this._keyMap)) this._mapKey.set(v, k); } const res = {}; map.forEach((v, k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v); return res; } mapDecode(source, end) { const res = this.decode(source); if (this._keyMap) { switch (res.constructor.name) { case "Array": return res.map((r) => this.decodeKeys(r)); } } return res; } decode(source, end) { if (src) { return saveState(() => { clearSource(); return this ? this.decode(source, end) : _Decoder.prototype.decode.call(defaultOptions, source, end); }); } srcEnd = end > -1 ? end : source.length; position = 0; stringPosition = 0; srcStringEnd = 0; srcString = null; strings = EMPTY_ARRAY; bundledStrings = 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 _Decoder) { currentDecoder = this; packedValues = this.sharedValues && (this.pack ? new Array(this.maxPrivatePackedValues || 16).concat(this.sharedValues) : this.sharedValues); if (this.structures) { currentStructures = this.structures; return checkedRead(); } if (!currentStructures || currentStructures.length > 0) { currentStructures = []; } } else { currentDecoder = defaultOptions; if (!currentStructures || currentStructures.length > 0) currentStructures = []; packedValues = null; } return checkedRead(); } decodeMultiple(source, forEach) { let values; let lastPosition = 0; try { const size = source.length; sequentialMode = true; const value = this ? this.decode(source, size) : defaultDecoder.decode(source, size); if (forEach) { if (forEach(value) === false) { return; } while (position < size) { lastPosition = position; if (forEach(checkedRead()) === false) { return; } } } else { values = [value]; while (position < size) { lastPosition = position; values.push(checkedRead()); } return values; } } catch (error) { error.lastPosition = lastPosition; error.values = values; throw error; } finally { sequentialMode = false; clearSource(); } } }; function checkedRead() { try { const result = read(); if (bundledStrings) { if (position >= bundledStrings.postBundlePosition) { const error = new Error("Unexpected bundle position"); error.incomplete = true; throw error; } position = bundledStrings.postBundlePosition; bundledStrings = null; } if (position === srcEnd) { currentStructures = null; src = null; if (referenceMap) referenceMap = null; } else if (position > srcEnd) { const error = new Error("Unexpected end of CBOR data"); error.incomplete = true; throw error; } else if (!sequentialMode) { throw new Error("Data read, but end of buffer not reached"); } return result; } catch (error) { clearSource(); if (error instanceof RangeError || error.message.startsWith("Unexpected end of buffer")) { error.incomplete = true; } throw error; } } function read() { let token = src[position++]; const majorType = token >> 5; token = token & 31; if (token > 23) { switch (token) { case 24: token = src[position++]; break; case 25: if (majorType === 7) { return getFloat16(); } token = dataView.getUint16(position); position += 2; break; case 26: if (majorType === 7) { const value = dataView.getFloat32(position); if (currentDecoder.useFloat32 > 2) { const multiplier = mult10[(src[position] & 127) << 1 | src[position + 1] >> 7]; position += 4; return (multiplier * value + (value > 0 ? 0.5 : -0.5) >> 0) / multiplier; } position += 4; return value; } token = dataView.getUint32(position); position += 4; break; case 27: if (majorType === 7) { const value = dataView.getFloat64(position); position += 8; return value; } if (majorType > 1) { if (dataView.getUint32(position) > 0) throw new Error("JavaScript does not support arrays, maps, or strings with length over 4294967295"); token = dataView.getUint32(position + 4); } else if (currentDecoder.int64AsNumber) { token = dataView.getUint32(position) * 4294967296; token += dataView.getUint32(position + 4); } else token = dataView.getBigUint64(position); position += 8; break; case 31: switch (majorType) { case 2: // byte string case 3: throw new Error("Indefinite length not supported for byte or text strings"); case 4: { const array = []; let value; let i = 0; while ((value = read()) !== STOP_CODE) { if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`); array[i++] = value; } return majorType === 4 ? array : majorType === 3 ? array.join("") : Buffer.concat(array); } case 5: { let key; if (currentDecoder.mapsAsObjects) { const object = {}; let i = 0; if (currentDecoder.keyMap) { while ((key = read()) !== STOP_CODE) { if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`); object[safeKey(currentDecoder.decodeKey(key))] = read(); } } else { while ((key = read()) !== STOP_CODE) { if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`); object[safeKey(key)] = read(); } } return object; } if (restoreMapsAsObject) { currentDecoder.mapsAsObjects = true; restoreMapsAsObject = false; } const map = /* @__PURE__ */ new Map(); if (currentDecoder.keyMap) { let i = 0; while ((key = read()) !== STOP_CODE) { if (i++ >= maxMapSize) { throw new Error(`Map size exceeds ${maxMapSize}`); } map.set(currentDecoder.decodeKey(key), read()); } } else { let i = 0; while ((key = read()) !== STOP_CODE) { if (i++ >= maxMapSize) { throw new Error(`Map size exceeds ${maxMapSize}`); } map.set(key, read()); } } return map; } case 7: return STOP_CODE; default: throw new Error(`Invalid major type for indefinite length ${majorType}`); } default: throw new Error(`Unknown token ${token}`); } } switch (majorType) { case 0: return token; case 1: return ~token; case 2: return readBin(token); case 3: if (srcStringEnd >= position) { return srcString.slice(position - srcStringStart, (position += token) - srcStringStart); } if (srcStringEnd === 0 && srcEnd < 140 && token < 32) { const string = token < 16 ? shortStringInJS(token) : longStringInJS(token); if (string != null) return string; } return readFixedString(token); case 4: { if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`); const array = new Array(token); for (let i = 0; i < token; i++) array[i] = read(); return array; } case 5: { if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`); if (currentDecoder.mapsAsObjects) { const object = {}; if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read(); else for (let i = 0; i < token; i++) object[safeKey(read())] = read(); return object; } if (restoreMapsAsObject) { currentDecoder.mapsAsObjects = true; restoreMapsAsObject = false; } const map = /* @__PURE__ */ new Map(); if (currentDecoder.keyMap) for (let i = 0; i < token; i++) map.set(currentDecoder.decodeKey(read()), read()); else for (let i = 0; i < token; i++) map.set(read(), read()); return map; } case 6: { if (token >= BUNDLED_STRINGS_ID) { let structure = currentStructures[token & 8191]; if (structure) { if (!structure.read) structure.read = createStructureReader(structure); return structure.read(); } if (token < 65536) { if (token === RECORD_INLINE_ID) { const length = readJustLength(); const id = read(); const structure2 = read(); recordDefinition(id, structure2); const object = {}; if (currentDecoder.keyMap) for (let i = 2; i < length; i++) { const key = currentDecoder.decodeKey(structure2[i - 2]); object[safeKey(key)] = read(); } else for (let i = 2; i < length; i++) { const key = structure2[i - 2]; object[safeKey(key)] = read(); } return object; } if (token === RECORD_DEFINITIONS_ID) { const length = readJustLength(); let id = read(); for (let i = 2; i < length; i++) { recordDefinition(id++, read()); } return read(); } if (token === BUNDLED_STRINGS_ID) { return readBundleExt(); } if (currentDecoder.getShared) { loadShared(); structure = currentStructures[token & 8191]; if (structure) { if (!structure.read) structure.read = createStructureReader(structure); return structure.read(); } } } } const extension = currentExtensions[token]; if (extension) { if (extension.handlesRead) return extension(read); return extension(read()); } const input = read(); for (let i = 0; i < currentExtensionRanges.length; i++) { const value = currentExtensionRanges[i](token, input); if (value !== void 0) return value; } return new Tag(input, token); } case 7: switch (token) { case 20: return false; case 21: return true; case 22: return null; case 23: return; // undefined default: { const packedValue = (packedValues || getPackedValues())[token]; if (packedValue !== void 0) return packedValue; throw new Error(`Unknown token ${token}`); } } default: if (Number.isNaN(token)) { const error = new Error("Unexpected end of CBOR data"); error.incomplete = true; throw error; } throw new Error(`Unknown CBOR token ${token}`); } } var validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/; function createStructureReader(structure) { if (!structure) throw new Error("Structure is required in record definition"); function readObject() { let length = src[position++]; length = length & 31; if (length > 23) { switch (length) { case 24: length = src[position++]; break; case 25: length = dataView.getUint16(position); position += 2; break; case 26: length = dataView.getUint32(position); position += 4; break; default: throw new Error(`Expected array header, but got ${src[position - 1]}`); } } let compiledReader = this.compiledReader; while (compiledReader) { if (compiledReader.propertyCount === length) return compiledReader(read); compiledReader = compiledReader.next; } if (this.slowReads++ >= inlineObjectReadThreshold) { const array = this.length === length ? this : this.slice(0, length); compiledReader = currentDecoder.keyMap ? new Function( "r", `return {${array.map((k) => currentDecoder.decodeKey(k)).map((k) => validName.test(k) ? `${safeKey(k)}:r()` : `[${JSON.stringify(k)}]:r()`).join(",")}}` ) : new Function( "r", `return {${array.map((key) => validName.test(key) ? `${safeKey(key)}:r()` : `[${JSON.stringify(key)}]:r()`).join(",")}}` ); if (this.compiledReader) compiledReader.next = this.compiledReader; compiledReader.propertyCount = length; this.compiledReader = compiledReader; return compiledReader(read); } const object = {}; if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read(); else for (let i = 0; i < length; i++) { object[safeKey(this[i])] = read(); } return object; } structure.slowReads = 0; return readObject; } function safeKey(key) { if (typeof key === "string") return key === "__proto__" ? "__proto_" : key; if (typeof key === "number" || typeof key === "boolean" || typeof key === "bigint") return key.toString(); if (key == null) return `${key}`; throw new Error(`Invalid property name type ${typeof key}`); } var readFixedString = readStringJS; function readStringJS(length) { let result; if (length < 16) { if (result = shortStringInJS(length)) return result; } if (length > 64 && decoder) return decoder.decode(src.subarray(position, position += length)); const end = position + length; const units = []; result = ""; while (position < end) { const byte1 = src[position++]; if ((byte1 & 128) === 0) { units.push(byte1); } else if ((byte1 & 224) === 192) { const byte2 = src[position++] & 63; units.push((byte1 & 31) << 6 | byte2); } else if ((byte1 & 240) === 224) { const byte2 = src[position++] & 63; const byte3 = src[position++] & 63; units.push((byte1 & 31) << 12 | byte2 << 6 | byte3); } else if ((byte1 & 248) === 240) { const byte2 = src[position++] & 63; const byte3 = src[position++] & 63; const byte4 = src[position++] & 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; } var fromCharCode = String.fromCharCode; function longStringInJS(length) { const start = position; const bytes = new Array(length); for (let i = 0; i < length; i++) { const byte = src[position++]; if ((byte & 128) > 0) { position = start; return; } bytes[i] = byte; } return fromCharCode.apply(String, bytes); } function shortStringInJS(length) { if (length < 4) { if (length < 2) { if (length === 0) return ""; const a3 = src[position++]; if ((a3 & 128) > 1) { position -= 1; return; } return fromCharCode(a3); } const a2 = src[position++]; const b2 = src[position++]; if ((a2 & 128) > 0 || (b2 & 128) > 0) { position -= 2; return; } if (length < 3) return fromCharCode(a2, b2); const c2 = src[position++]; if ((c2 & 128) > 0) { position -= 3; return; } return fromCharCode(a2, b2, c2); } const a = src[position++]; const b = src[position++]; const c = src[position++]; const d = src[position++]; if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) { position -= 4; return; } if (length < 6) { if (length === 4) return fromCharCode(a, b, c, d); const e2 = src[position++]; if ((e2 & 128) > 0) { position -= 5; return; } return fromCharCode(a, b, c, d, e2); } if (length < 8) { const e2 = src[position++]; const f2 = src[position++]; if ((e2 & 128) > 0 || (f2 & 128) > 0) { position -= 6; return; } if (length < 7) return fromCharCode(a, b, c, d, e2, f2); const g2 = src[position++]; if ((g2 & 128) > 0) { position -= 7; return; } return fromCharCode(a, b, c, d, e2, f2, g2); } const e = src[position++]; const f = src[position++]; const g = src[position++]; const h = src[position++]; if ((e & 128) > 0 || (f & 128) > 0 || (g & 128) > 0 || (h & 128) > 0) { position -= 8; return; } if (length < 10) { if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h); const i2 = src[position++]; if ((i2 & 128) > 0) { position -= 9; return; } return fromCharCode(a, b, c, d, e, f, g, h, i2); } if (length < 12) { const i2 = src[position++]; const j2 = src[position++]; if ((i2 & 128) > 0 || (j2 & 128) > 0) { position -= 10; return; } if (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i2, j2); const k2 = src[position++]; if ((k2 & 128) > 0) { position -= 11; return; } return fromCharCode(a, b, c, d, e, f, g, h, i2, j2, k2); } const i = src[position++]; const j = src[position++]; const k = src[position++]; const l = src[position++]; if ((i & 128) > 0 || (j & 128) > 0 || (k & 128) > 0 || (l & 128) > 0) { position -= 12; return; } if (length < 14) { if (length === 12) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l); const m2 = src[position++]; if ((m2 & 128) > 0) { position -= 13; return; } return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m2); } const m = src[position++]; const n = src[position++]; if ((m & 128) > 0 || (n & 128) > 0) { position -= 14; return; } if (length < 15) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n); const o = src[position++]; if ((o & 128) > 0) { position -= 15; return; } return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); } function readBin(length) { return currentDecoder.copyBuffers ? ( // specifically use the copying slice (not the node one) Uint8Array.prototype.slice.call(src, position, position += length) ) : src.subarray(position, position += length); } var f32Array = new Float32Array(1); var u8Array = new Uint8Array(f32Array.buffer, 0, 4); function getFloat16() { const byte0 = src[position++]; const byte1 = src[position++]; const exponent = (byte0 & 127) >> 2; if (exponent === 31) { if (byte1 || byte0 & 3) return Number.NaN; return byte0 & 128 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; } if (exponent === 0) { const abs = ((byte0 & 3) << 8 | byte1) / (1 << 24); return byte0 & 128 ? -abs : abs; } u8Array[3] = byte0 & 128 | // sign bit (exponent >> 1) + 56; u8Array[2] = (byte0 & 7) << 5 | // last exponent bit and first two mantissa bits byte1 >> 3; u8Array[1] = byte1 << 5; u8Array[0] = 0; return f32Array[0]; } var keyCache = new Array(4096); var Tag = class { constructor(value, tag) { this.value = value; this.tag = tag; } }; currentExtensions[1] = (epochSec) => { return new Date(Math.round(epochSec * 1e3)); }; currentExtensions[2] = (buffer) => { let value = BigInt(0); for (let i = 0, l = buffer.byteLength; i < l; i++) { value = BigInt(buffer[i]) + (value << BigInt(8)); } return value; }; currentExtensions[3] = (buffer) => { return BigInt(-1) - currentExtensions[2](buffer); }; currentExtensions[4] = (fraction) => { return +`${fraction[1]}e${fraction[0]}`; }; currentExtensions[5] = (fraction) => { return fraction[1] * Math.exp(fraction[0] * Math.log(2)); }; var recordDefinition = (id, structure) => { id = id - 57344; const existingStructure = currentStructures[id]; if (existingStructure?.isShared) { ; (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure; } currentStructures[id] = structure; structure.read = createStructureReader(structure); }; currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => { const length = data.length; const structure = data[1]; recordDefinition(data[0], structure); const object = {}; for (let i = 2; i < length; i++) { const key = structure[i - 2]; object[safeKey(key)] = data[i]; } return object; }; currentExtensions[14] = (value) => { if (bundledStrings) return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value); return new Tag(value, 14); }; currentExtensions[15] = (value) => { if (bundledStrings) return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value); return new Tag(value, 15); }; var glbl = { Error, RegExp }; currentExtensions[27] = (data) => { return (glbl[data[0]] || Error)(data[1], data[2]); }; var packedTable = (read2) => { if (src[position++] !== 132) { const error = new Error("Packed values structure must be followed by a 4 element array"); if (src.length < position) error.incomplete = true; throw error; } const newPackedValues = read2(); if (!newPackedValues || !newPackedValues.length) { const error = new Error("Packed values structure must be followed by a 4 element array"); error.incomplete = true; throw error; } packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues; packedValues.prefixes = read2(); packedValues.suffixes = read2(); return read2(); }; packedTable.handlesRead = true; currentExtensions[51] = packedTable; currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { if (!packedValues) { if (currentDecoder.getShared) loadShared(); else return new Tag(data, PACKED_REFERENCE_TAG_ID); } if (typeof data === "number") return packedValues[16 + (data >= 0 ? 2 * data : -2 * data - 1)]; const error = new Error("No support for non-integer packed references yet"); if (data === void 0) error.incomplete = true; throw error; }; currentExtensions[28] = (read2) => { if (!referenceMap) { referenceMap = /* @__PURE__ */ new Map(); referenceMap.id = 0; } const id = referenceMap.id++; const startingPosition = position; const token = src[position]; let target2; if (token >> 5 === 4) target2 = []; else target2 = {}; const refEntry = { target: target2 }; referenceMap.set(id, refEntry); let targetProperties = read2(); if (refEntry.used) { if (Object.getPrototypeOf(target2) !== Object.getPrototypeOf(targetProperties)) { position = startingPosition; target2 = targetProperties; referenceMap.set(id, { target: target2 }); targetProperties = read2(); } return Object.assign(target2, targetProperties); } refEntry.target = targetProperties; return targetProperties; }; currentExtensions[28].handlesRead = true; currentExtensions[29] = (id) => { const refEntry = referenceMap.get(id); refEntry.used = true; return refEntry.target; }; currentExtensions[258] = (array) => new Set(array); (currentExtensions[259] = (read2) => { if (currentDecoder.mapsAsObjects) { currentDecoder.mapsAsObjects = false; restoreMapsAsObject = true; } return read2(); }).handlesRead = true; function combine(a, b) { if (typeof a === "string") return a + b; if (Array.isArray(a)) return a.concat(b); return Object.assign({}, a, b); } function getPackedValues() { if (!packedValues) { if (currentDecoder.getShared) loadShared(); else throw new Error("No packed values available"); } return packedValues; } var SHARED_DATA_TAG_ID = 1399353956; currentExtensionRanges.push((tag, input) => { if (tag >= 225 && tag <= 255) return combine(getPackedValues().prefixes[tag - 224], input); if (tag >= 28704 && tag <= 32767) return combine(getPackedValues().prefixes[tag - 28672], input); if (tag >= 1879052288 && tag <= 2147483647) return combine(getPackedValues().prefixes[tag - 1879048192], input); if (tag >= 216 && tag <= 223) return combine(input, getPackedValues().suffixes[tag - 216]); if (tag >= 27647 && tag <= 28671) return combine(input, getPackedValues().suffixes[tag - 27639]); if (tag >= 1811940352 && tag <= 1879048191) return combine(input, getPackedValues().suffixes[tag - 1811939328]); if (tag === SHARED_DATA_TAG_ID) { return { packedValues, structures: currentStructures.slice(0), version: input }; } if (tag === 55799) return input; }); var isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1; var typedArrays = [ Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, typeof BigUint64Array === "undefined" ? { name: "BigUint64Array" } : BigUint64Array, Int8Array, Int16Array, Int32Array, typeof BigInt64Array === "undefined" ? { name: "BigInt64Array" } : BigInt64Array, Float32Array, Float64Array ]; var typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 85, 86]; for (let i = 0; i < typedArrays.length; i++) { registerTypedArray(typedArrays[i], typedArrayTags[i]); } function registerTypedArray(TypedArray, tag) { const dvMethod = `get${TypedArray.name.slice(0, -5)}`; let bytesPerElement; if (typeof TypedArray === "function") bytesPerElement = TypedArray.BYTES_PER_ELEMENT; else TypedArray = null; for (let littleEndian = 0; littleEndian < 2; littleEndian++) { if (!littleEndian && bytesPerElement === 1) continue; const sizeShift = bytesPerElement === 2 ? 1 : bytesPerElement === 4 ? 2 : bytesPerElement === 8 ? 3 : 0; currentExtensions[littleEndian ? tag : tag - 4] = bytesPerElement === 1 || littleEndian === isLittleEndianMachine ? (buffer) => { if (!TypedArray) throw new Error(`Could not find typed array for code ${tag}`); if (!currentDecoder.copyBuffers) { if (bytesPerElement === 1 || bytesPerElement === 2 && !(buffer.byteOffset & 1) || bytesPerElement === 4 && !(buffer.byteOffset & 3) || bytesPerElement === 8 && !(buffer.byteOffset & 7)) return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength >> sizeShift); } return new TypedArray(Uint8Array.prototype.slice.call(buffer, 0).buffer); } : (buffer) => { if (!TypedArray) throw new Error(`Could not find typed array for code ${tag}`); const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); const elements = buffer.length >> sizeShift; const ta = new TypedArray(elements); const method = dv[dvMethod]; for (let i = 0; i < elements; i++) { ta[i] = method.call(dv, i << sizeShift, littleEndian); } return ta; }; } } function readBundleExt() { const length = readJustLength(); const bundlePosition = position + read(); for (let i = 2; i < length; i++) { const bundleLength = readJustLength(); position += bundleLength; } const dataPosition = position; position = bundlePosition; bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())]; bundledStrings.position0 = 0; bundledStrings.position1 = 0; bundledStrings.postBundlePosition = position; position = dataPosition; return read(); } function readJustLength() { let token = src[position++] & 31; if (token > 23) { switch (token) { case 24: token = src[position++]; break; case 25: token = dataView.getUint16(position); position += 2; break; case 26: token = dataView.getUint32(position); position += 4; break; } } return token; } function loadShared() { if (currentDecoder.getShared) { const sharedData = saveState(() => { src = null; return currentDecoder.getShared(); }) || {}; const updatedStructures = sharedData.structures || []; currentDecoder.sharedVersion = sharedData.version; packedValues = currentDecoder.sharedValues = sharedData.packedValues; if (currentStructures === true) currentDecoder.structures = currentStructures = updatedStructures; else currentStructures.splice.apply(currentStructures, [0, updatedStructures.length].concat(updatedStructures)); } } function saveState(callback) { const savedSrcEnd = srcEnd; const savedPosition = position; const savedStringPosition = stringPosition; const savedSrcStringStart = srcStringStart; const savedSrcStringEnd = srcStringEnd; const savedSrcString = srcString; const savedStrings = strings; const savedReferenceMap = referenceMap; const savedBundledStrings = bundledStrings; const savedSrc = new Uint8Array(src.slice(0, srcEnd)); const savedStructures = currentStructures; const savedDecoder = currentDecoder; const savedSequentialMode = sequentialMode; const value = callback(); srcEnd = savedSrcEnd; position = savedPosition; stringPosition = savedStringPosition; srcStringStart = savedSrcStringStart; srcStringEnd = savedSrcStringEnd; srcString = savedSrcString; strings = savedStrings; referenceMap = savedReferenceMap; bundledStrings = savedBundledStrings; src = savedSrc; sequentialMode = savedSequentialMode; currentStructures = savedStructures; currentDecoder = savedDecoder; dataView = new DataView(src.buffer, src.byteOffset, src.byteLength); return value; } function clearSource() { src = null; referenceMap = null; currentStructures = null; } function addExtension(extension) { currentExtensions[extension.tag] = extension.decode; } var mult10 = new Array(147); for (let i = 0; i < 256; i++) { mult10[i] = +`1e${Math.floor(45.15 - i * 0.30103)}`; } var defaultDecoder = new Decoder({ useRecords: false }); var decode = defaultDecoder.decode; var decodeMultiple = defaultDecoder.decodeMultiple; var FLOAT32_OPTIONS = { NEVER: 0, ALWAYS: 1, DECIMAL_ROUND: 3, DECIMAL_FIT: 4 }; // src/cbor/cbor-x/encode.js var textEncoder; try { textEncoder = new TextEncoder(); } catch (error) { } var extensions; var extensionClasses; var Buffer2 = typeof globalThis === "object" && globalThis.Buffer; var hasNodeBuffer = typeof Buffer2 !== "undefined"; var ByteArrayAllocate = hasNodeBuffer ? Buffer2.allocUnsafeSlow : Uint8Array; var ByteArray = hasNodeBuffer ? Buffer2 : Uint8Array; var MAX_STRUCTURES = 256; var MAX_BUFFER_SIZE = hasNodeBuffer ? 4294967296 : 2144337920; var throwOnIterable; var target; var targetView; var position2 = 0; var safeEnd; var bundledStrings2 = null; var MAX_BUNDLE_SIZE = 61440; var hasNonLatin = /[\u0080-\uFFFF]/; var RECORD_SYMBOL = Symbol("record-id"); var Encoder = class extends Decoder { constructor(options) { super(options); this.offset = 0; let typeBuffer; let start; let sharedStructures; let hasSharedUpdate; let structures; let referenceMap2; options = options || {}; const encodeUtf8 = ByteArray.prototype.utf8Write ? (string, position3, maxBytes) => target.utf8Write(string, position3, maxBytes) : textEncoder?.encodeInto ? (string, position3) => textEncoder.encodeInto(string, target.subarray(position3)).written : false; const encoder = this; const hasSharedStructures = options.structures || options.saveStructures; let maxSharedStructures = options.maxSharedStructures; if (maxSharedStructures == null) maxSharedStructures = hasSharedStructures ? 128 : 0; if (maxSharedStructures > 8190) throw new Error("Maximum maxSharedStructure is 8190"); const isSequential = options.sequential; if (isSequential) { maxSharedStructures = 0; } if (!this.structures) this.structures = []; if (this.saveStructures) this.saveShared = this.saveStructures; let samplingPackedValues; let packedObjectMap2; let sharedValues = options.sharedValues; let sharedPackedObjectMap2; if (sharedValues) { sharedPackedObjectMap2 = /* @__PURE__ */ Object.create(null); for (let i = 0, l = sharedValues.length; i < l; i++) { sharedPackedObjectMap2[sharedValues[i]] = i; } } let recordIdsToRemove = []; let transitionsCount = 0; let serializationsSinceTransitionRebuild = 0; this.mapEncode = function(value, encodeOptions) { if (this._keyMap && !this._mapped) { switch (value.constructor.name) { case "Array": value = value.map((r) => this.encodeKeys(r)); break; } } return this.encode(value, encodeOptions); }; this.encode = (value, encodeOptions) => { if (!target) { target = new ByteArrayAllocate(8192); targetView = new DataView(target.buffer, 0, 8192); position2 = 0; } safeEnd = target.length - 10; if (safeEnd - position2 < 2048) { target = new ByteArrayAllocate(target.length); targetView = new DataView(target.buffer, 0, target.length); safeEnd = target.length - 10; position2 = 0; } else if (encodeOptions === REUSE_BUFFER_MODE) position2 = position2 + 7 & 2147483640; start = position2; if (encoder.useSelfDescribedHeader) { targetView.setUint32(position2, 3654940416); position2 += 3; } referenceMap2 = encoder.structuredClone ? /* @__PURE__ */ new Map() : null; if (encoder.bundleStrings && typeof value !== "string") { bundledStrings2 = []; bundledStrings2.size = Number.POSITIVE_INFINITY; } else bundledStrings2 = null; sharedStructures = encoder.structures; if (sharedStructures) { if (sharedStructures.uninitialized) { const sharedData = encoder.getShared() || {}; encoder.structures = sharedStructures = sharedData.structures || []; encoder.sharedVersion = sharedData.version; const sharedValues2 = encoder.sharedValues = sharedData.packedValues; if (sharedValues2) { sharedPackedObjectMap2 = {}; for (let i = 0, l = sharedValues2.length; i < l; i++) sharedPackedObjectMap2[sharedValues2[i]] = i; } } let sharedStructuresLength = sharedStructures.length; if (sharedStructuresLength > maxSharedStructures && !isSequential) sharedStructuresLength = maxSharedStructures; if (!sharedStructures.transitions) { sharedStructures.transitions = /* @__PURE__ */ Object.create(null); for (let i = 0; i < sharedStructuresLength; i++) { const keys = sharedStructures[i]; if (!keys) continue; let nextTransition; let transition = sharedStructures.transitions; for (let j = 0, l = keys.length; j < l; j++) { if (transition[RECORD_SYMBOL] === void 0) transition[RECORD_SYMBOL] = i; const key = keys[j]; nextTransition = transition[key]; if (!nextTransition) { nextTransition = transition[key] = /* @__PURE__ */ Object.create(null); } transition = nextTransition; } transition[RECORD_SYMBOL] = i | 1048576; } } if (!isSequential) sharedStructures.nextId = sharedStructuresLength; } if (hasSharedUpdate) hasSharedUpdate = false; structures = sharedStructures || []; packedObjectMap2 = sharedPackedObjectMap2; if (options.pack) { const packedValues2 = /* @__PURE__ */ new Map(); packedValues2.values = []; packedValues2.encoder = encoder; packedValues2.maxValues = options.maxPrivatePackedValues || (sharedPackedObjectMap2 ? 16 : Number.POSITIVE_INFINITY); packedValues2.objectMap = sharedPackedObjectMap2 || false; packedValues2.samplingPackedValues = samplingPackedValues; findRepetitiveStrings(value, packedValues2); if (packedValues2.values.length > 0) { target[position2++] = 216; target[position2++] = 51; writeArrayHeader(4); const valuesArray = packedValues2.values; encode2(valuesArray); writeArrayHeader(0); writeArrayHeader(0); packedObjectMap2 = Object.create(sharedPackedObjectMap2 || null); for (let i = 0, l = valuesArray.length; i < l; i++) { packedObjectMap2[valuesArray[i]] = i; } } } throwOnIterable = encodeOptions & THROW_ON_ITERABLE; try { if (throwOnIterable) return; encode2(value); if (bundledStrings2) { writeBundles(start, encode2); } encoder.offset = position2; if (referenceMap2?.idsToInsert) { position2 += referenceMap2.idsToInsert.length * 2; if (position2 > safeEnd) makeRoom(position2); encoder.offset = position2; const serialized = insertIds(target.subarray(start, position2), referenceMap2.idsToInsert); referenceMap2 = null; return serialized; } if (encodeOptions & REUSE_BUFFER_MODE) { target.start = start; target.end = position2; return target; } return target.subarray(start, position2); } finally { if (sharedStructures) { if (serializationsSinceTransitionRebuild < 10) serializationsSinceTransitionRebuild++; if (sharedStructures.length > maxSharedStructures) sharedStructures.length = maxSharedStructures; if (transitionsCount > 1e4) { sharedStructures.transitions = null; serializationsSinceTransitionRebuild = 0; transitionsCount = 0; if (recordIdsToRemove.length > 0) recordIdsToRemove = []; } else if (recordIdsToRemove.length > 0 && !isSequential) { for (let i = 0, l = recordIdsToRemove.length; i < l; i++) { recordIdsToRemove[i][RECORD_SYMBOL] = void 0; } recordIdsToRemove = []; } } if (hasSharedUpdate && encoder.saveShared) { if (encoder.structures.length > maxSharedStructures) { encoder.structures = encoder.structures.slice(0, maxSharedStructures); } const returnBuffer = target.subarray(start, position2); if (encoder.updateSharedData() === false) return encoder.encode(value); return returnBuffer; } if (encodeOptions & RESET_BUFFER_MODE) position2 = start; } }; this.findCommonStringsToPack = () => { samplingPackedValues = /* @__PURE__ */ new Map(); if (!sharedPackedObjectMap2) sharedPackedObjectMap2 = /* @__PURE__ */ Object.create(null); return (options2) => { const threshold = options2?.threshold || 4; let position3 = this.pack ? options2.maxPrivatePackedValues || 16 : 0; if (!sharedValues) sharedValues = this.sharedValues = []; for (const [key, status] of samplingPackedValues) { if (status.count > threshold) { sharedPackedObjectMap2[key] = position3++; sharedValues.push(key); hasSharedUpdate = true; } } while (this.saveShared && this.updateSharedData() === false) { } samplingPackedValues = null; }; }; const encode2 = (value) => { if (position2 > safeEnd) target = makeRoom(position2); const type = typeof value; let length; if (type === "string") { if (packedObjectMap2) { const packedPosition = packedObjectMap2[value]; if (packedPosition >= 0) { if (packedPosition < 16) target[position2++] = packedPosition + 224; else { target[position2++] = 198; if (packedPosition & 1) encode2(15 - packedPosition >> 1); else encode2(packedPosition - 16 >> 1); } return; } if (samplingPackedValues && !options.pack) { const status = samplingPackedValues.get(value); if (status) status.count++; else samplingPackedValues.set(value, { count: 1 }); } } const strLength = value.length; if (bundledStrings2 && strLength >= 4 && strLength < 1024) { if ((bundledStrings2.size += strLength) > MAX_BUNDLE_SIZE) { let extStart; const maxBytes2 = (bundledStrings2[0] ? bundledStrings2[0].length * 3 + bundledStrings2[1].length : 0) + 10; if (position2 + maxBytes2 > safeEnd) target = makeRoom(position2 + maxBytes2); target[position2++] = 217; target[position2++] = 223; target[position2++] = 249; target[position2++] = bundledStrings2.position ? 132 : 130; target[position2++] = 26; extStart = position2 - start; position2 += 4; if (bundledStrings2.position) { writeBundles(start, encode2); } bundledStrings2 = ["", ""]; bundledStrings2.size = 0; bundledStrings2.position = extStart; } const twoByte = hasNonLatin.test(value); bundledStrings2[twoByte ? 0 : 1] += value; target[position2++] = twoByte ? 206 : 207; encode2(strLength); return; } let headerSize; if (strLength < 32) { headerSize = 1; } else if (strLength < 256) { headerSize = 2; } else if (strLength < 65536) { headerSize = 3; } else { headerSize = 5; } const maxBytes = strLength * 3; if (position2 + maxBytes > safeEnd) target = makeRoom(position2 + maxBytes); if (strLength < 64 || !encodeUtf8) { let i; let c1; let c2; let strPosition = position2 + headerSize; for (i = 0; i < strLength; i++) { c1 = value.charCodeAt(i); if (c1 < 128) { target[strPosition++] = c1; } else if (c1 < 2048) { target[strPosition++] = c1 >> 6 | 192; target[strPosition++] = c1 & 63 | 128; } else if ((c1 & 64512) === 55296 && ((c2 = value.charCodeAt(i + 1)) & 64512) === 56320) { c1 = 65536 + ((c1 & 1023) << 10) + (c2 & 1023); i++; target[strPosition++] = c1 >> 18 | 240; target[strPosition++] = c1 >> 12 & 63 | 128; target[strPosition++] = c1 >> 6 & 63 | 128; target[strPosition++] = c1 & 63 | 128; } else { target[strPosition++] = c1 >> 12 | 224; target[strPosition++] = c1 >> 6 & 63 | 128; target[strPosition++] = c1 & 63 | 128; } } length = strPosition - position2 - headerSize; } else { length = encodeUtf8(value, position2 + headerSize, maxBytes); } if (length < 24) { target[position2++] = 96 | length; } else if (length < 256) { if (headerSize < 2) { target.copyWithin(position2 + 2, position2 + 1, position2 + 1 + length); } target[position2++] = 120; target[position2++] = length; } else if (length < 65536) { if (headerSize < 3) { target.copyWithin(position2 + 3, position2 + 2, position2 + 2 + length); } target[position2++] = 121; target[position2++] = length >> 8; target[position2++] = length & 255; } else { if (headerSize < 5) { target.copyWithin(position2 + 5, position2 + 3, position2 + 3 + length); } target[position2++] = 122; targetView.setUint32(position2, length); position2 += 4; } position2 += length; } else if (type === "number") { if (!this.alwaysUseFloat && value >>> 0 === value) { if (value < 24) { target[position2++] = value; } else if (value < 256) { target[position2++] = 24; target[position2++] = value; } else if (value < 65536) { target[position2++] = 25; target[position2++] = value >> 8; target[position2++] = value & 255; } else { target[position2++] = 26; targetView.setUint32(position2, value); position2 += 4; } } else if (!this.alwaysUseFloat && value >> 0 === value) { if (value >= -24) { target[position2++] = 31 - value; } else if (value >= -256) { target[position2++] = 56; target[position2++] = ~value; } else if (value >= -65536) { target[position2++] = 57; targetView.setUint16(position2, ~value); position2 += 2; } else { target[position2++] = 58; targetView.setUint32(position2, ~value); position2 += 4; } } else { let useFloat32; if ((useFloat32 = this.useFloat32) > 0 && value < 4294967296 && value >= -2147483648) { target[position2++] = 250; targetView.setFloat32(position2, value); let xShifted; if (useFloat32 < 4 || // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved (xShifted = value * mult10[(target[position2] & 127) << 1 | target[position2 + 1] >> 7]) >> 0 === xShifted) { position2 += 4; return; } position2--; } target[position2++] = 251; targetView.setFloat64(position2, value); position2 += 8; } } else if (type === "object") { if (!value) target[position2++] = 246; else { i