1 line
14 KiB
JSON
1 line
14 KiB
JSON
|
{"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.readProtobufMessage = exports.FieldReader = void 0;\nconst errors_js_1 = require(\"../../errors.js\");\nconst util_js_1 = require(\"./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}\nclass 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 errors_js_1.ProtoError(`Expected wire type ${expectedWireType}, got ${this.#wireType}`);\n }\n this.#wireType = -1;\n }\n bytes() {\n this.#expect(util_js_1.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(util_js_1.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(util_js_1.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(util_js_1.FIXED_64);\n return this.#reader.double();\n }\n maybeSkip() {\n if (this.#wireType < 0) {\n return;\n } else if (this.#wireType === util_js_1.VARINT) {\n this.#reader.skipVarint();\n } else if (this.#wireType === util_js_1.FIXED_64) {\n this.#reader.skip(8);\n } else if (this.#wireType === util_js_1.LENGTH_DELIMITED) {\n const length = this.#reader.varint();\n this.#reader.skip(length);\n } else if (this.#wireType === util_js_1.FIXED_32) {\n this.#reader.skip(4);\n } else {\n throw new errors_js_1.ProtoError(`Unexpected wire type ${this.#wireType}`);\n }\n this.#wireType = -1;\n }\n}\nexports.FieldReader = FieldReader;\nfunction 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}\nexports.readProtobufMessage = readProtobufMessage;","map":{"version":3,"names":["Object","defineProperty","exports","value","readProtobufMessage","FieldReader","er
|