@surrealdb/cbor
Version:
CBOR encoder and decoder for SurrealDB.
8 lines (7 loc) • 32.7 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../src/constants.ts", "../src/encoded.ts", "../src/error.ts", "../src/gap.ts", "../src/writer.ts", "../src/partial.ts", "../src/tagged.ts", "../src/encoder.ts", "../src/reader.ts", "../src/util.ts", "../src/decoder.ts"],
"sourcesContent": ["// biome-ignore lint/suspicious/noExplicitAny: We don't know what it will return\nexport type Replacer = (v: any) => unknown;\nexport type Major = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;\nexport const POW_2_53: number = 2 ** 53;\nexport const POW_2_64: bigint = BigInt(2 ** 64);\n", "export class Encoded {\n\tconstructor(readonly encoded: Uint8Array) {}\n}\n", "export abstract class CborError {\n\tabstract readonly name: string;\n\treadonly message: string;\n\n\tconstructor(message: string) {\n\t\tthis.message = message;\n\t}\n}\n\nexport class CborNumberError extends CborError {\n\tname = \"CborNumberError\";\n}\n\nexport class CborRangeError extends CborError {\n\tname = \"CborRangeError\";\n}\n\nexport class CborInvalidMajorError extends CborError {\n\tname = \"CborInvalidMajorError\";\n}\n\nexport class CborBreak extends CborError {\n\tname = \"CborBreak\";\n\tconstructor() {\n\t\tsuper(\"Came across a break which was not intercepted by the decoder\");\n\t}\n}\n\nexport class CborPartialDisabled extends CborError {\n\tname = \"CborPartialDisabled\";\n\tconstructor() {\n\t\tsuper(\n\t\t\t\"Tried to insert a Gap into a CBOR value, while partial mode is not enabled\",\n\t\t);\n\t}\n}\n\nexport class CborFillMissing extends CborError {\n\tname = \"CborFillMissing\";\n\tconstructor() {\n\t\tsuper(\"Fill for a gap is missing, and gap has no default\");\n\t}\n}\n", "// Why is the default value being stored in an array? undefined, null, false, etc... are all valid defaults,\n// and specifying a field on a class as optional will make it undefined by default.\n\nexport type Fill<T = unknown> = [Gap<T>, T];\nexport class Gap<T = unknown> {\n\treadonly args: [T?] = [];\n\tconstructor(...args: [T?]) {\n\t\tthis.args = args;\n\t}\n\n\tfill(value: T): Fill<T> {\n\t\treturn [this, value];\n\t}\n\n\thasDefault(): boolean {\n\t\treturn this.args.length === 1;\n\t}\n\n\tget default(): T | undefined {\n\t\treturn this.args[0];\n\t}\n}\n", "import type { Major, Replacer } from \"./constants\";\nimport type { Gap } from \"./gap\";\nimport { PartiallyEncoded } from \"./partial\";\n\nexport class Writer {\n\tprivate _chunks: [Uint8Array, Gap][] = [];\n\tprivate _pos = 0;\n\tprivate _buf: ArrayBuffer;\n\tprivate _view: DataView;\n\tprivate _byte: Uint8Array;\n\n\tconstructor(readonly byteLength = 256) {\n\t\tthis._buf = new ArrayBuffer(this.byteLength);\n\t\tthis._view = new DataView(this._buf);\n\t\tthis._byte = new Uint8Array(this._buf);\n\t}\n\n\tchunk(gap: Gap): void {\n\t\tthis._chunks.push([this._byte.subarray(0, this._pos), gap]);\n\t\tthis._buf = new ArrayBuffer(this.byteLength);\n\t\tthis._view = new DataView(this._buf);\n\t\tthis._byte = new Uint8Array(this._buf);\n\t\tthis._pos = 0;\n\t}\n\n\tget chunks(): [Uint8Array, Gap][] {\n\t\treturn this._chunks;\n\t}\n\n\tget buffer(): Uint8Array {\n\t\treturn this._byte.subarray(0, this._pos);\n\t}\n\n\tprivate claim(length: number): number {\n\t\tconst pos = this._pos;\n\t\tthis._pos += length;\n\n\t\tif (this._pos <= this._buf.byteLength) return pos;\n\n\t\t// Resize with exponential growth\n\t\tlet newLen = this._buf.byteLength << 1;\n\t\twhile (newLen < this._pos) newLen <<= 1;\n\n\t\tconst oldb = this._byte;\n\t\tthis._buf = new ArrayBuffer(newLen);\n\t\tthis._view = new DataView(this._buf);\n\t\tthis._byte = new Uint8Array(this._buf);\n\t\tthis._byte.set(oldb);\n\n\t\treturn pos;\n\t}\n\n\twriteUint8(value: number): void {\n\t\tconst pos = this.claim(1);\n\t\tthis._byte[pos] = value;\n\t}\n\n\twriteUint16(value: number): void {\n\t\tconst pos = this.claim(2);\n\t\tthis._view.setUint16(pos, value, false);\n\t}\n\n\twriteUint32(value: number): void {\n\t\tconst pos = this.claim(4);\n\t\tthis._view.setUint32(pos, value, false);\n\t}\n\n\twriteUint64(value: bigint): void {\n\t\tconst pos = this.claim(8);\n\t\tthis._view.setBigUint64(pos, value, false);\n\t}\n\n\twriteFloat32(value: number): void {\n\t\tconst pos = this.claim(4);\n\t\tthis._view.setFloat32(pos, value, false);\n\t}\n\n\twriteFloat64(value: number): void {\n\t\tconst pos = this.claim(8);\n\t\tthis._view.setFloat64(pos, value, false);\n\t}\n\n\twriteUint8Array(data: Uint8Array): void {\n\t\tif (data.byteLength === 0) return;\n\t\tconst pos = this.claim(data.byteLength);\n\t\tthis._byte.set(data, pos);\n\t}\n\n\twritePartiallyEncoded(data: PartiallyEncoded): void {\n\t\tfor (const [buf, gap] of data.chunks) {\n\t\t\tthis.writeUint8Array(buf);\n\t\t\tthis.chunk(gap);\n\t\t}\n\n\t\tthis.writeUint8Array(data.end);\n\t}\n\n\twriteMajor(type: Major, length: number | bigint): void {\n\t\tconst base = type << 5;\n\t\tif (typeof length === \"number\") {\n\t\t\tif (length < 24) {\n\t\t\t\tthis.writeUint8(base + length);\n\t\t\t} else if (length < 0x100) {\n\t\t\t\tthis.writeUint8(base + 24);\n\t\t\t\tthis.writeUint8(length);\n\t\t\t} else if (length < 0x10000) {\n\t\t\t\tthis.writeUint8(base + 25);\n\t\t\t\tthis.writeUint16(length);\n\t\t\t} else if (length < 0x100000000) {\n\t\t\t\tthis.writeUint8(base + 26);\n\t\t\t\tthis.writeUint32(length);\n\t\t\t} else {\n\t\t\t\tthis.writeUint8(base + 27);\n\t\t\t\tthis.writeUint64(BigInt(length));\n\t\t\t}\n\t\t} else {\n\t\t\t// bigint path\n\t\t\tif (length < 24n) {\n\t\t\t\tthis.writeUint8(base + Number(length));\n\t\t\t} else if (length < 0x100n) {\n\t\t\t\tthis.writeUint8(base + 24);\n\t\t\t\tthis.writeUint8(Number(length));\n\t\t\t} else if (length < 0x10000n) {\n\t\t\t\tthis.writeUint8(base + 25);\n\t\t\t\tthis.writeUint16(Number(length));\n\t\t\t} else if (length < 0x100000000n) {\n\t\t\t\tthis.writeUint8(base + 26);\n\t\t\t\tthis.writeUint32(Number(length));\n\t\t\t} else {\n\t\t\t\tthis.writeUint8(base + 27);\n\t\t\t\tthis.writeUint64(length);\n\t\t\t}\n\t\t}\n\t}\n\n\toutput(partial?: false, replacer?: Replacer): Uint8Array;\n\toutput(partial: true, replacer?: Replacer): PartiallyEncoded;\n\toutput(partial = false, replacer?: Replacer): Uint8Array | PartiallyEncoded {\n\t\tif (partial) {\n\t\t\treturn new PartiallyEncoded(this._chunks, this.buffer, replacer);\n\t\t}\n\t\treturn this.buffer;\n\t}\n}\n", "import type { Replacer } from \"./constants\";\nimport { type EncoderOptions, encode } from \"./encoder\";\nimport { CborFillMissing } from \"./error\";\nimport type { Fill, Gap } from \"./gap\";\nimport { Writer } from \"./writer\";\n\nexport class PartiallyEncoded {\n\tconstructor(\n\t\treadonly chunks: [Uint8Array, Gap][],\n\t\treadonly end: Uint8Array,\n\t\treadonly replacer: Replacer | undefined,\n\t) {}\n\n\tbuild(fills: Fill[], partial?: false): Uint8Array;\n\tbuild(fills: Fill[], partial: true): PartiallyEncoded;\n\tbuild(fills: Fill[], partial?: boolean): PartiallyEncoded | Uint8Array {\n\t\tconst writer = new Writer();\n\t\tconst map = new Map(fills);\n\n\t\tfor (const [buffer, gap] of this.chunks) {\n\t\t\tconst hasValue = map.has(gap) || gap.hasDefault();\n\t\t\tif (!partial && !hasValue) throw new CborFillMissing();\n\t\t\twriter.writeUint8Array(buffer);\n\n\t\t\tif (hasValue) {\n\t\t\t\tconst data = map.get(gap) ?? gap.default;\n\t\t\t\tencode(data, {\n\t\t\t\t\twriter,\n\t\t\t\t\treplacer: this.replacer,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\twriter.chunk(gap);\n\t\t\t}\n\t\t}\n\n\t\twriter.writeUint8Array(this.end);\n\t\tif (partial) {\n\t\t\treturn writer.output(true, this.replacer);\n\t\t}\n\n\t\treturn writer.output(false, this.replacer);\n\t}\n}\n\nexport function partiallyEncodeObject(\n\tobject: Record<string, unknown>,\n\toptions?: EncoderOptions<true>,\n): Record<string, PartiallyEncoded> {\n\treturn Object.fromEntries(\n\t\tObject.entries(object).map(([k, v]) => [\n\t\t\tk,\n\t\t\tencode(v, { ...options, partial: true }),\n\t\t]),\n\t);\n}\n", "export class Tagged<T = unknown> {\n\tconstructor(\n\t\treadonly tag: number | bigint,\n\t\treadonly value: T,\n\t) {}\n}\n", "import { POW_2_53, POW_2_64, type Replacer } from \"./constants\";\nimport { Encoded } from \"./encoded\";\nimport { CborNumberError, CborPartialDisabled } from \"./error\";\nimport { type Fill, Gap } from \"./gap\";\nimport { PartiallyEncoded } from \"./partial\";\nimport { Tagged } from \"./tagged\";\nimport { Writer } from \"./writer\";\n\nconst textEncoder = new TextEncoder();\n\nexport interface EncoderOptions<Partial extends boolean = boolean> {\n\treplacer?: Replacer;\n\twriter?: Writer;\n\tpartial?: Partial;\n\tfills?: Fill[];\n}\n\nfunction encode(input: unknown, options?: EncoderOptions<false>): Uint8Array;\nfunction encode(\n\tinput: unknown,\n\toptions?: EncoderOptions<true>,\n): PartiallyEncoded;\nfunction encode(\n\tinput: unknown,\n\toptions: EncoderOptions = {},\n): PartiallyEncoded | Uint8Array {\n\tconst w = options.writer ?? new Writer();\n\tconst fillsMap = new Map(options.fills ?? []);\n\n\tfunction inner(input: unknown) {\n\t\t// biome-ignore lint/style/noParameterAssign:\n\t\tinput = options.replacer?.(input) ?? input;\n\n\t\tif (input === undefined) return w.writeUint8(0xf7);\n\t\tif (input === null) return w.writeUint8(0xf6);\n\t\tif (input === true) return w.writeUint8(0xf5);\n\t\tif (input === false) return w.writeUint8(0xf4);\n\n\t\tswitch (typeof input) {\n\t\t\tcase \"number\": {\n\t\t\t\tif (Number.isInteger(input)) {\n\t\t\t\t\tif (input >= 0 && input <= POW_2_53) {\n\t\t\t\t\t\tw.writeMajor(0, input);\n\t\t\t\t\t} else if (input < 0 && input >= -POW_2_53) {\n\t\t\t\t\t\tw.writeMajor(1, -(input + 1));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new CborNumberError(\"Number too big to be encoded\");\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Better precision when encoded as 64-bit\n\t\t\t\t\tw.writeUint8(0xfb);\n\t\t\t\t\tw.writeFloat64(input);\n\t\t\t\t}\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcase \"bigint\": {\n\t\t\t\tif (input >= 0 && input < POW_2_64) {\n\t\t\t\t\tw.writeMajor(0, input);\n\t\t\t\t} else if (input <= 0 && input >= -POW_2_64) {\n\t\t\t\t\tw.writeMajor(1, -(input + 1n));\n\t\t\t\t} else {\n\t\t\t\t\tthrow new CborNumberError(\"BigInt too big to be encoded\");\n\t\t\t\t}\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tcase \"string\": {\n\t\t\t\tconst encoded = textEncoder.encode(input);\n\t\t\t\tw.writeMajor(3, encoded.byteLength);\n\t\t\t\tw.writeUint8Array(encoded);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tif (Array.isArray(input)) {\n\t\t\t\t\tw.writeMajor(4, input.length);\n\t\t\t\t\tfor (const v of input) {\n\t\t\t\t\t\tinner(v);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (input instanceof Tagged) {\n\t\t\t\t\tw.writeMajor(6, input.tag);\n\t\t\t\t\tinner(input.value);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (input instanceof Encoded) {\n\t\t\t\t\tw.writeUint8Array(input.encoded);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (input instanceof Gap) {\n\t\t\t\t\tif (fillsMap.has(input)) {\n\t\t\t\t\t\tinner(fillsMap.get(input));\n\t\t\t\t\t} else {\n\t\t\t\t\t\tif (options.partial) {\n\t\t\t\t\t\t\tw.chunk(input);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthrow new CborPartialDisabled();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (input instanceof PartiallyEncoded) {\n\t\t\t\t\tconst res = options.partial\n\t\t\t\t\t\t? input.build(options.fills ?? [], true)\n\t\t\t\t\t\t: input.build(options.fills ?? [], false);\n\n\t\t\t\t\tif (res instanceof PartiallyEncoded) {\n\t\t\t\t\t\tw.writePartiallyEncoded(res);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tw.writeUint8Array(res);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (\n\t\t\t\t\tinput instanceof Uint8Array ||\n\t\t\t\t\tinput instanceof Uint16Array ||\n\t\t\t\t\tinput instanceof Uint32Array ||\n\t\t\t\t\tinput instanceof Int8Array ||\n\t\t\t\t\tinput instanceof Int16Array ||\n\t\t\t\t\tinput instanceof Int32Array ||\n\t\t\t\t\tinput instanceof Float32Array ||\n\t\t\t\t\tinput instanceof Float64Array ||\n\t\t\t\t\tinput instanceof ArrayBuffer\n\t\t\t\t) {\n\t\t\t\t\tconst v = input instanceof Uint8Array ? input : new Uint8Array(input);\n\t\t\t\t\tw.writeMajor(2, v.byteLength);\n\t\t\t\t\tw.writeUint8Array(v);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (input instanceof Map) {\n\t\t\t\t\tw.writeMajor(5, input.size);\n\t\t\t\t\tfor (const [k, v] of input) {\n\t\t\t\t\t\tinner(k);\n\t\t\t\t\t\tinner(v);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tconst entries = Object.entries(input);\n\t\t\t\t\tw.writeMajor(5, entries.length);\n\t\t\t\t\tfor (const [k, v] of entries) {\n\t\t\t\t\t\tinner(k);\n\t\t\t\t\t\tinner(v);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tinner(input);\n\n\tif (options.partial) {\n\t\treturn w.output(true, options.replacer);\n\t}\n\n\treturn w.output(false, options.replacer);\n}\n\nexport { encode };\n", "import { type Major, POW_2_53 } from \"./constants\";\nimport { CborInvalidMajorError, CborRangeError } from \"./error\";\n\n// Float16 constants\nconst F16_SIGN_MASK = 0x8000;\nconst F16_EXP_MASK = 0x7c00;\nconst F16_FRAC_MASK = 0x03ff;\nconst F16_EXP_SHIFT = 10;\nconst F16_EXP_BIAS = 15;\nconst F16_EXP_INF_NAN = 0x1f;\nconst F16_FRAC_SCALE = 1024;\nconst F16_SUBNORMAL_SCALE = 2 ** -14;\n\n// Optional: Precomputed exponent powers for float16\nconst FLOAT16_EXP_LUT = Array.from(\n\t{ length: 31 },\n\t(_, e) => 2 ** (e - F16_EXP_BIAS),\n);\n\nexport class Reader {\n\tprivate _view: DataView;\n\tprivate _byte: Uint8Array;\n\tprivate _pos = 0;\n\n\tconstructor(buffer: Uint8Array) {\n\t\tthis._byte = buffer;\n\t\tthis._view = new DataView(\n\t\t\tbuffer.buffer,\n\t\t\tbuffer.byteOffset,\n\t\t\tbuffer.byteLength,\n\t\t);\n\t}\n\n\tskip(amount = 1): void {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < amount)\n\t\t\tthrow new CborRangeError(\"Tried to read 1 byte beyond buffer bounds\");\n\t\tthis._pos = pos + amount;\n\t}\n\n\tpeekUint8(): number {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 1)\n\t\t\tthrow new CborRangeError(\"Tried to read 1 byte beyond buffer bounds\");\n\t\treturn this._view.getUint8(pos);\n\t}\n\n\treadUint8(): number {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 1)\n\t\t\tthrow new CborRangeError(\"Tried to read 1 byte beyond buffer bounds\");\n\t\tconst val = this._view.getUint8(pos);\n\t\tthis._pos = pos + 1;\n\t\treturn val;\n\t}\n\n\treadUint16(): number {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 2)\n\t\t\tthrow new CborRangeError(\"Tried to read 2 bytes beyond buffer bounds\");\n\t\tconst val = this._view.getUint16(pos);\n\t\tthis._pos = pos + 2;\n\t\treturn val;\n\t}\n\n\treadUint32(): number {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 4)\n\t\t\tthrow new CborRangeError(\"Tried to read 4 bytes beyond buffer bounds\");\n\t\tconst val = this._view.getUint32(pos);\n\t\tthis._pos = pos + 4;\n\t\treturn val;\n\t}\n\n\treadUint64(): bigint {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 8)\n\t\t\tthrow new CborRangeError(\"Tried to read 8 bytes beyond buffer bounds\");\n\t\tconst val = this._view.getBigUint64(pos);\n\t\tthis._pos = pos + 8;\n\t\treturn val;\n\t}\n\n\treadFloat16(): number {\n\t\tconst val = this.readUint16();\n\n\t\tconst sign = val & F16_SIGN_MASK ? -1 : 1;\n\t\tconst exp = (val & F16_EXP_MASK) >> F16_EXP_SHIFT;\n\t\tconst frac = val & F16_FRAC_MASK;\n\n\t\tif (exp === 0) {\n\t\t\treturn sign * (frac / F16_FRAC_SCALE) * F16_SUBNORMAL_SCALE;\n\t\t}\n\t\tif (exp === F16_EXP_INF_NAN) {\n\t\t\treturn frac ? Number.NaN : sign * Number.POSITIVE_INFINITY;\n\t\t}\n\t\treturn sign * (1 + frac / F16_FRAC_SCALE) * FLOAT16_EXP_LUT[exp];\n\t}\n\n\treadFloat32(): number {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 4)\n\t\t\tthrow new CborRangeError(\n\t\t\t\t\"Tried to read 4 bytes for float32 beyond buffer bounds\",\n\t\t\t);\n\t\tconst val = this._view.getFloat32(pos);\n\t\tthis._pos = pos + 4;\n\t\treturn val;\n\t}\n\n\treadFloat64(): number {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < 8)\n\t\t\tthrow new CborRangeError(\n\t\t\t\t\"Tried to read 8 bytes for float64 beyond buffer bounds\",\n\t\t\t);\n\t\tconst val = this._view.getFloat64(pos);\n\t\tthis._pos = pos + 8;\n\t\treturn val;\n\t}\n\n\treadBytes(amount: number): Uint8Array {\n\t\tconst pos = this._pos;\n\t\tif (this._byte.length - pos < amount)\n\t\t\tthrow new CborRangeError(`Tried to read ${amount} bytes beyond buffer`);\n\t\tthis._pos = pos + amount;\n\t\treturn this._byte.subarray(pos, this._pos);\n\t}\n\n\treadMajor(): [Major, number] {\n\t\tconst byte = this.readUint8();\n\t\tconst major = (byte >> 5) as Major;\n\t\tif (major < 0 || major > 7)\n\t\t\tthrow new CborInvalidMajorError(\"Received invalid major type\");\n\t\treturn [major, byte & 0x1f];\n\t}\n\n\treadMajorLength(length: number): number | bigint {\n\t\tif (length <= 23) return length;\n\t\tif (length === 24) return this.readUint8();\n\t\tif (length === 25) return this.readUint16();\n\t\tif (length === 26) return this.readUint32();\n\t\tif (length === 27) {\n\t\t\tconst read = this.readUint64();\n\t\t\treturn read > POW_2_53 ? read : Number(read);\n\t\t}\n\t\tthrow new CborRangeError(\"Expected a final length\");\n\t}\n}\n", "import type { Major } from \"./constants\";\nimport { CborInvalidMajorError, CborRangeError } from \"./error\";\nimport type { Reader } from \"./reader\";\nimport { Writer } from \"./writer\";\n\nexport function infiniteBytes(r: Reader, forMajor: Major): ArrayBuffer {\n\tconst w = new Writer();\n\twhile (true) {\n\t\tconst [major, len] = r.readMajor();\n\n\t\t// Received break signal\n\t\tif (major === 7 && len === 31) break;\n\n\t\t// Resource type has to match\n\t\tif (major !== forMajor)\n\t\t\tthrow new CborInvalidMajorError(\n\t\t\t\t`Expected a resource of the same major (${forMajor}) while processing an infinite resource`,\n\t\t\t);\n\n\t\t// Cannot have an infinite resource in an infinite resource\n\t\tif (len === 31)\n\t\t\tthrow new CborRangeError(\n\t\t\t\t\"Expected a finite resource while processing an infinite resource\",\n\t\t\t);\n\n\t\tw.writeUint8Array(r.readBytes(Number(r.readMajorLength(len))));\n\t}\n\n\treturn w.buffer.buffer as ArrayBuffer;\n}\n", "import type { Replacer } from \"./constants\";\nimport { CborBreak, CborInvalidMajorError } from \"./error\";\nimport { Reader } from \"./reader\";\nimport { Tagged } from \"./tagged\";\nimport { infiniteBytes } from \"./util\";\n\nconst textDecoder = new TextDecoder();\n\nexport interface DecodeOptions {\n\tmap?: \"object\" | \"map\";\n\ttagged?: Record<number, Replacer>;\n}\n\nexport function decode(\n\tinput: Uint8Array | Reader,\n\toptions: DecodeOptions = {},\n\t// biome-ignore lint/suspicious/noExplicitAny: We don't know what it will return\n): any {\n\tconst r = input instanceof Reader ? input : new Reader(input);\n\treturn decodeValue(r, options);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: We don't know what it will return\nfunction decodeValue(r: Reader, options: DecodeOptions): any {\n\tconst [major, len] = r.readMajor();\n\tswitch (major) {\n\t\tcase 0:\n\t\t\treturn r.readMajorLength(len);\n\t\tcase 1: {\n\t\t\tconst l = r.readMajorLength(len);\n\t\t\treturn typeof l === \"bigint\" ? -(l + 1n) : -(l + 1);\n\t\t}\n\t\tcase 2: {\n\t\t\tif (len !== 31)\n\t\t\t\treturn new Uint8Array(r.readBytes(Number(r.readMajorLength(len))))\n\t\t\t\t\t.buffer;\n\t\t\treturn infiniteBytes(r, 2);\n\t\t}\n\t\tcase 3: {\n\t\t\tconst encoded =\n\t\t\t\tlen !== 31\n\t\t\t\t\t? r.readBytes(Number(r.readMajorLength(len)))\n\t\t\t\t\t: infiniteBytes(r, 3);\n\n\t\t\treturn textDecoder.decode(encoded);\n\t\t}\n\n\t\tcase 4: {\n\t\t\tif (len !== 31) {\n\t\t\t\tconst l = r.readMajorLength(len);\n\t\t\t\tconst arr = Array(l);\n\t\t\t\tfor (let i = 0; i < l; i++) arr[i] = decodeValue(r, options);\n\t\t\t\treturn arr;\n\t\t\t}\n\n\t\t\tconst arr = [];\n\t\t\tfor (;;) {\n\t\t\t\tconst byte = r.peekUint8();\n\t\t\t\tif (byte === 0xff) {\n\t\t\t\t\tr.skip();\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tarr.push(decodeValue(r, options));\n\t\t\t}\n\n\t\t\treturn arr;\n\t\t}\n\n\t\tcase 5: {\n\t\t\tif (options.map === \"map\") {\n\t\t\t\tconst map = new Map<string, unknown>();\n\t\t\t\tif (len !== 31) {\n\t\t\t\t\tconst l = r.readMajorLength(len);\n\t\t\t\t\tfor (let i = 0; i < l; i++) {\n\t\t\t\t\t\tmap.set(decodeValue(r, options), decodeValue(r, options));\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tfor (;;) {\n\t\t\t\t\t\tconst byte = r.peekUint8();\n\t\t\t\t\t\tif (byte === 0xff) {\n\t\t\t\t\t\t\tr.skip();\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tmap.set(decodeValue(r, options), decodeValue(r, options));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn map;\n\t\t\t}\n\n\t\t\tconst obj: Record<string, unknown> = {};\n\t\t\tif (len !== 31) {\n\t\t\t\tconst l = r.readMajorLength(len);\n\t\t\t\tfor (let i = 0; i < l; i++) {\n\t\t\t\t\tobj[decodeValue(r, options)] = decodeValue(r, options);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst byte = r.peekUint8();\n\t\t\t\t\tif (byte === 0xff) {\n\t\t\t\t\t\tr.skip();\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tobj[decodeValue(r, options)] = decodeValue(r, options);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn obj;\n\t\t}\n\n\t\tcase 6: {\n\t\t\tconst tag = Number(r.readMajorLength(len));\n\t\t\tconst value = decodeValue(r, options);\n\t\t\tconst replacer = options.tagged?.[tag];\n\t\t\tif (replacer) return replacer(value);\n\t\t\treturn new Tagged(tag, value);\n\t\t}\n\n\t\tcase 7: {\n\t\t\tswitch (len) {\n\t\t\t\tcase 20:\n\t\t\t\t\treturn false;\n\t\t\t\tcase 21:\n\t\t\t\t\treturn true;\n\t\t\t\tcase 22:\n\t\t\t\t\treturn null;\n\t\t\t\tcase 23:\n\t\t\t\t\treturn undefined;\n\t\t\t\tcase 25:\n\t\t\t\t\treturn r.readFloat16();\n\t\t\t\tcase 26:\n\t\t\t\t\treturn r.readFloat32();\n\t\t\t\tcase 27:\n\t\t\t\t\treturn r.readFloat64();\n\t\t\t\tcase 31:\n\t\t\t\t\tthrow new CborBreak();\n\t\t\t}\n\t\t}\n\t}\n\n\tthrow new CborInvalidMajorError(\n\t\t`Unable to decode value with major tag ${major}`,\n\t);\n}\n"],
"mappings": "AAGO,IAAM,SAAmB,iBACnB,SAAmB,OAAO,mBAAO,ECJvC,IAAM,QAAN,KAAc,CACpB,YAAqB,QAAqB,CAArB,oBAAsB,CAC5C,ECFO,IAAe,UAAf,KAAyB,CAEtB,QAET,YAAY,QAAiB,CAC5B,KAAK,QAAU,OAChB,CACD,EAEa,gBAAN,cAA8B,SAAU,CAC9C,KAAO,iBACR,EAEa,eAAN,cAA6B,SAAU,CAC7C,KAAO,gBACR,EAEa,sBAAN,cAAoC,SAAU,CACpD,KAAO,uBACR,EAEa,UAAN,cAAwB,SAAU,CACxC,KAAO,YACP,aAAc,CACb,MAAM,8DAA8D,CACrE,CACD,EAEa,oBAAN,cAAkC,SAAU,CAClD,KAAO,sBACP,aAAc,CACb,MACC,4EACD,CACD,CACD,EAEa,gBAAN,cAA8B,SAAU,CAC9C,KAAO,kBACP,aAAc,CACb,MAAM,mDAAmD,CAC1D,CACD,ECtCO,IAAM,IAAN,KAAuB,CACpB,KAAa,CAAC,EACvB,eAAe,KAAY,CAC1B,KAAK,KAAO,IACb,CAEA,KAAK,MAAmB,CACvB,MAAO,CAAC,KAAM,KAAK,CACpB,CAEA,YAAsB,CACrB,OAAO,KAAK,KAAK,SAAW,CAC7B,CAEA,I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