const textDecoder = new TextDecoder();
class BinaryReader {
  #p = 0;
  #b;
  constructor(b) {
    this.#b = b;
  }
  readUint8() {
    this.checkSize(1);
    return this.#b[this.#p++];
  }
  readInt16() {
    this.checkSize(2);
    return this.#b[this.#p++] << 8 | this.#b[this.#p++];
  }
  readInt32() {
    this.checkSize(4);
    return this.#b[this.#p++] << 24 | this.#b[this.#p++] << 16 | this.#b[this.#p++] << 8 | this.#b[this.#p++];
  }
  readString() {
    const endIdx = this.#b.indexOf(0, this.#p);
    if (endIdx < 0) {
      throw Error("unexpected end of message");
    }
    const strBuf = this.#b.subarray(this.#p, endIdx);
    this.#p = endIdx + 1;
    return this.decodeText(strBuf);
  }
  decodeText(strBuf) {
    return textDecoder.decode(strBuf);
  }
  read(n) {
    this.checkSize(n);
    return this.#b.subarray(this.#p, this.#p += n);
  }
  checkSize(n) {
    if (this.#b.length < this.#p + n) {
      throw Error("unexpected end of message");
    }
  }
  array(length, fn) {
    return Array.from({ length }, fn, this);
  }
  // replication helpers
  readLsn() {
    const h = this.readUint32();
    const l = this.readUint32();
    if (h === 0 && l === 0) {
      return null;
    }
    return `${h.toString(16).padStart(8, "0")}/${l.toString(16).padStart(8, "0")}`.toUpperCase();
  }
  readTime() {
    return this.readUint64() + BigInt("946684800000000");
  }
  readUint64() {
    return BigInt(this.readUint32()) << BigInt(32) | BigInt(this.readUint32());
  }
  readUint32() {
    return this.readInt32() >>> 0;
  }
}
export {
  BinaryReader
};
//# sourceMappingURL=binary-reader.js.map
