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H,CAAC;CACF;AA9FD,wCA8FC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/**\n * Minimal binary protobuf reader.\n * Only implements the wire-types that we currently need; this is not intended\n * to be a general-purpose protobuf reader.\n *\n * Since the values we parse are generally small and not very nested, it's public\n * interface does not enforce the same low-allocation philosophy that ProtobufWriter does.\n * If this is needed in the future, we should refactor this to fit the use-case.\n */\nexport class ProtobufReader {\n  pos: number = 0;\n  private readonly _buf: Uint8Array;\n  private readonly _textDecoder: {\n    decode: (input?: Uint8Array | null) => string;\n  };\n\n  constructor(buf: Uint8Array) {\n    this._buf = buf;\n    this._textDecoder = new TextDecoder();\n  }\n\n  isAtEnd(): boolean {\n    return this.pos >= this._buf.length;\n  }\n\n  /** Read a varint and decode it as a tag, returning field number and wire type. */\n  readTag(): { fieldNumber: number; wireType: number } {\n    const raw = this.readVarint();\n    return { fieldNumber: raw >>> 3, wireType: raw & 0x7 };\n  }\n\n  /**\n   * Read a base-128 varint.\n   * Returns a JS `number`; precision above 2^53 is silently lost.\n   */\n  readVarint(): number {\n    let result = 0;\n    let shift = 0;\n    while (this.pos < this._buf.length) {\n      const b = this._buf[this.pos++];\n      result += (b & 0x7f) * Math.pow(2, shift);\n      shift += 7;\n      if ((b & 0x80) === 0) break;\n    }\n    return result;\n  }\n\n  /** Read a length-delimited byte sequence (bytes field or embedded message). */\n  readBytes(): Uint8Array {\n    const len = this.readVarint();\n    const slice = this._buf.subarray(this.pos, this.pos + len);\n    this.pos += len;\n    return slice;\n  }\n\n  /** Read a length-delimited UTF-8 string. */\n  readString(): string {\n    return this._textDecoder.decode(this.readBytes());\n  }\n\n  /**\n   * Skip an unknown field.\n   * Handles wire types 0 (varint), 1 (64-bit), 2 (length-delimited),\n   * 3 (start-group), 4 (end-group), and 5 (32-bit).\n   */\n  skip(wireType: number): void {\n    switch (wireType) {\n      case 0: // varint\n        this.readVarint();\n        break;\n      case 1: // 64-bit fixed\n        this.pos += 8;\n        break;\n      case 2: // length-delimited\n        this.readBytes();\n        break;\n      case 3: // start group (deprecated)\n        // We should never encounter this, but let's handle it gracefully in case we do:\n        // Read nested tags until matching end-group (wire type 4) is found.\n        // Groups can be nested, so continue until the end-group for this\n        // start-group is encountered.\n        while (!this.isAtEnd()) {\n          const { wireType: nestedWireType } = this.readTag();\n          if (nestedWireType === 4) {\n            // matched end-group for this start-group\n            break;\n          }\n          // recursive skip also handles nested groups\n          this.skip(nestedWireType);\n        }\n        break;\n      case 4: // end group\n        // End-group should be handled by the start-group logic above.\n        // When encountered directly in skip, treat it as a no-op (it signals\n        // termination of the enclosing group).\n        break;\n      case 5: // 32-bit fixed\n        this.pos += 4;\n        break;\n      default:\n        throw new Error(`Unknown wire type ${wireType}, cannot safely skip`);\n    }\n  }\n}\n"]}