the-forest/client/node_modules/.cache/babel-loader/55e9aec5d91712ca960cd535e51b0c34dd403424d106cc763ee2e29a29eefe6d.json

1 line
13 KiB
JSON
Raw Normal View History

2024-09-17 20:35:18 -04:00
{"ast":null,"code":"import { ProtoError } from \"../../errors.js\";\nimport { VARINT, FIXED_64, LENGTH_DELIMITED, FIXED_32 } from \"./util.js\";\nclass MessageReader {\n #array;\n #view;\n #pos;\n constructor(array) {\n this.#array = array;\n this.#view = new DataView(array.buffer, array.byteOffset, array.byteLength);\n this.#pos = 0;\n }\n varint() {\n let value = 0;\n for (let shift = 0;; shift += 7) {\n const byte = this.#array[this.#pos++];\n value |= (byte & 0x7f) << shift;\n if (!(byte & 0x80)) {\n break;\n }\n }\n return value;\n }\n varintBig() {\n let value = 0n;\n for (let shift = 0n;; shift += 7n) {\n const byte = this.#array[this.#pos++];\n value |= BigInt(byte & 0x7f) << shift;\n if (!(byte & 0x80)) {\n break;\n }\n }\n return value;\n }\n bytes(length) {\n const array = new Uint8Array(this.#array.buffer, this.#array.byteOffset + this.#pos, length);\n this.#pos += length;\n return array;\n }\n double() {\n const value = this.#view.getFloat64(this.#pos, true);\n this.#pos += 8;\n return value;\n }\n skipVarint() {\n for (;;) {\n const byte = this.#array[this.#pos++];\n if (!(byte & 0x80)) {\n break;\n }\n }\n }\n skip(count) {\n this.#pos += count;\n }\n eof() {\n return this.#pos >= this.#array.byteLength;\n }\n}\nexport class FieldReader {\n #reader;\n #wireType;\n constructor(reader) {\n this.#reader = reader;\n this.#wireType = -1;\n }\n setup(wireType) {\n this.#wireType = wireType;\n }\n #expect(expectedWireType) {\n if (this.#wireType !== expectedWireType) {\n throw new ProtoError(`Expected wire type ${expectedWireType}, got ${this.#wireType}`);\n }\n this.#wireType = -1;\n }\n bytes() {\n this.#expect(LENGTH_DELIMITED);\n const length = this.#reader.varint();\n return this.#reader.bytes(length);\n }\n string() {\n return new TextDecoder().decode(this.bytes());\n }\n message(def) {\n return readProtobufMessage(this.bytes(), def);\n }\n int32() {\n this.#expect(VARINT);\n return this.#reader.varint();\n }\n uint32() {\n return this.int32();\n }\n bool() {\n return this.int32() !== 0;\n }\n uint64() {\n this.#expect(VARINT);\n return this.#reader.varintBig();\n }\n sint64() {\n const value = this.uint64();\n return value >> 1n ^ -(value & 1n);\n }\n double() {\n this.#expect(FIXED_64);\n return this.#reader.double();\n }\n maybeSkip() {\n if (this.#wireType < 0) {\n return;\n } else if (this.#wireType === VARINT) {\n this.#reader.skipVarint();\n } else if (this.#wireType === FIXED_64) {\n this.#reader.skip(8);\n } else if (this.#wireType === LENGTH_DELIMITED) {\n const length = this.#reader.varint();\n this.#reader.skip(length);\n } else if (this.#wireType === FIXED_32) {\n this.#reader.skip(4);\n } else {\n throw new ProtoError(`Unexpected wire type ${this.#wireType}`);\n }\n this.#wireType = -1;\n }\n}\nexport function readProtobufMessage(data, def) {\n const msgReader = new MessageReader(data);\n const fieldReader = new FieldReader(msgReader);\n let value = def.default();\n while (!msgReader.eof()) {\n const key = msgReader.varint();\n const tag = key >> 3;\n const wireType = key & 0x7;\n fieldReader.setup(wireType);\n const tagFun = def[tag];\n if (tagFun !== undefined) {\n const returnedValue = tagFun(fieldReader, value);\n if (returnedValue !== undefined) {\n value = returnedValue;\n }\n }\n fieldReader.maybeSkip();\n }\n return value;\n}","map":{"version":3,"names":["ProtoError","VARINT","FIXED_64","LENGTH_DELIMITED","FIXED_32","MessageReader","array","view","pos","constructor","DataView","buffer","byteOffset","byteLength","varint","value","shift","byte","varintBig","BigInt","bytes","length","Uint8Array","double","getFloat64","skipVarint","skip","count","eof","FieldReader","reader","wireType","setup","expect","#expect","expectedWireType",