{"version":3,"file":"SimpleDataView-DluPdyGj.cjs","names":[],"sources":["../src/util/SimpleDataView.ts"],"sourcesContent":["// A DataView wrapper to automatically handle byte offsets when reading/writing and automatically handle batching when a buffer is full.\n\nexport class SimpleDataView {\n  private _buffer: ArrayBuffer;\n  private _view: DataView;\n  private _offset: number;\n  private _maxSize: number;\n  private _littleEndian: boolean;\n  private _overflowHandler?: (filledBuffer: ArrayBuffer) => void;\n\n  constructor(\n    maxSize?: number,\n    littleEndian?: boolean,\n    overflowHandler?: (filledBuffer: ArrayBuffer) => void,\n  );\n  constructor(\n    buffer: ArrayBuffer,\n    littleEndian?: boolean,\n    overflowHandler?: (filledBuffer: ArrayBuffer) => void,\n  );\n  constructor(\n    maxSizeOrBuffer: number | ArrayBuffer = 1024 * 1000,\n    littleEndian = true,\n    overflowHandler?: (filledBuffer: ArrayBuffer) => void,\n  ) {\n    this._buffer =\n      typeof maxSizeOrBuffer === 'number' ? new ArrayBuffer(maxSizeOrBuffer) : maxSizeOrBuffer;\n    this._view = new DataView(this._buffer);\n    this._offset = 0;\n    this._maxSize = typeof maxSizeOrBuffer === 'number' ? maxSizeOrBuffer : this._buffer.byteLength;\n    this._littleEndian = littleEndian;\n    this._overflowHandler = overflowHandler;\n  }\n\n  get buffer(): ArrayBuffer {\n    return this._buffer.slice(0, this._offset);\n  }\n\n  get fullBuffer(): ArrayBuffer {\n    return this._buffer;\n  }\n\n  get dataView(): DataView<ArrayBufferLike> {\n    return this._view;\n  }\n\n  get offset(): number {\n    return this._offset;\n  }\n\n  /**\n   * Zero the write offset so subsequent writes start from the beginning of\n   * the underlying buffer. The buffer itself is reused — the `buffer`\n   * getter returns a `slice()` (an independent copy), so a previous flush\n   * that postMessaged the slice does not affect this buffer's writability.\n   *\n   * Pre-fix: this method allocated a fresh `ArrayBuffer` of `_maxSize`\n   * (10KB on the worker side, 1MB default elsewhere) every time. Combined\n   * with the per-flush `slice()` allocation in the getter, that produced\n   * two ArrayBuffers per flush — significant GC pressure during a busy\n   * UI commit. The underlying buffer never needs reallocation because\n   * we never transfer it; only the slice is transferred.\n   */\n  public reset(): void {\n    this._offset = 0;\n  }\n\n  public hasSpace(size: number): boolean {\n    const newOffset = this._offset + size;\n\n    return newOffset <= this._maxSize && newOffset >= 0;\n  }\n\n  /**\n   * Advance the write offset by `n` bytes without bounds checks. Pairs with\n   * direct `dataView` writes after a prior `hasSpace(n)` validated the run.\n   * Lets a hot writer skip the per-call `_checkOverflow` + switch dispatch\n   * inside `writeXxx` when batching a known, fixed-size record.\n   */\n  public advance(n: number): void {\n    this._offset += n;\n  }\n\n  // Shifts the offset back by the specified size.\n  public shift(size: number): void {\n    if (this._offset < size) {\n      throw new Error('Cannot shift more than current offset');\n    }\n\n    this._offset -= size;\n  }\n\n  public moveTo(offset: number): void {\n    if (offset < 0 || offset >= this._maxSize) {\n      throw new Error('Offset out of bounds');\n    }\n\n    this._offset = offset;\n  }\n\n  public moveBy(delta: number): void {\n    if (!this.hasSpace(delta)) {\n      throw new Error('Offset out of bounds');\n    }\n\n    this._offset += delta;\n  }\n\n  public readUint8 = (): number => this._readInt(1, true);\n  public readUint8At = (offset: number): number => this._readIntAt(offset, 1, true);\n  public readInt8 = (): number => this._readInt(1, false);\n  public readInt8At = (offset: number): number => this._readIntAt(offset, 1, false);\n  public writeUint8 = (value: number): void => this._writeInt(1, value, true);\n  public writeUint8At = (offset: number, value: number): void =>\n    this._writeIntAt(offset, value, 1, true);\n  public writeInt8 = (value: number): void => this._writeInt(1, value, false);\n  public writeInt8At = (offset: number, value: number): void =>\n    this._writeIntAt(offset, value, 1, false);\n\n  public readUint16 = (): number => this._readInt(2, true);\n  public readUint16At = (offset: number): number => this._readIntAt(offset, 2, true);\n  public readInt16 = (): number => this._readInt(2, false);\n  public readInt16At = (offset: number): number => this._readIntAt(offset, 2, false);\n  public writeUint16 = (value: number): void => this._writeInt(2, value, true);\n  public writeUint16At = (offset: number, value: number): void =>\n    this._writeIntAt(offset, value, 2, true);\n  public writeInt16 = (value: number): void => this._writeInt(2, value, false);\n  public writeInt16At = (offset: number, value: number): void =>\n    this._writeIntAt(offset, value, 2, false);\n\n  public readUint32 = (): number => this._readInt(4, true);\n  public readUint32At = (offset: number): number => this._readIntAt(offset, 4, true);\n  public readInt32 = (): number => this._readInt(4, false);\n  public readInt32At = (offset: number): number => this._readIntAt(offset, 4, false);\n  public writeUint32 = (value: number): void => this._writeInt(4, value, true);\n  public writeUint32At = (offset: number, value: number): void =>\n    this._writeIntAt(offset, value, 4, true);\n  public writeInt32 = (value: number): void => this._writeInt(4, value, false);\n  public writeInt32At = (offset: number, value: number): void =>\n    this._writeIntAt(offset, value, 4, false);\n\n  public readBigUint64 = (): bigint => this._readInt(8, true);\n  public readBigUint64At = (offset: number): bigint => this._readIntAt(offset, 8, true);\n  public readBigInt64 = (): bigint => this._readInt(8, false);\n  public readBigInt64At = (offset: number): bigint => this._readIntAt(offset, 8, false);\n  public writeBigUint64 = (value: bigint): void => this._writeInt(8, value, true);\n  public writeBigUint64At = (offset: number, value: bigint): void =>\n    this._writeIntAt(offset, value, 8, true);\n  public writeBigInt64 = (value: bigint): void => this._writeInt(8, value, false);\n  public writeBigInt64At = (offset: number, value: bigint): void =>\n    this._writeIntAt(offset, value, 8, false);\n\n  public readFloat32 = (): number => this._readFloat(4);\n  public readFloat32At = (offset: number): number => this._readFloatAt(offset, 4);\n  public readFloat64 = (): number => this._readFloat(8);\n  public readFloat64At = (offset: number): number => this._readFloatAt(offset, 8);\n  public writeFloat32 = (value: number): void => this._writeFloat(4, value);\n  public writeFloat32At = (offset: number, value: number): void =>\n    this._writeFloatAt(offset, value, 4);\n  public writeFloat64 = (value: number): void => this._writeFloat(8, value);\n  public writeFloat64At = (offset: number, value: number): void =>\n    this._writeFloatAt(offset, value, 8);\n\n  private _checkOverflow(size: number) {\n    if (this._offset + size > this._maxSize) {\n      if (this._overflowHandler) {\n        this._overflowHandler(this.buffer);\n        this.reset();\n      } else {\n        throw new Error('Buffer overflowed');\n      }\n    }\n  }\n\n  private _readIntAt(offset: number, bytes: 1 | 2 | 4, unsigned: boolean): number;\n  private _readIntAt(offset: number, bytes: 8, unsigned: boolean): bigint;\n  private _readIntAt(offset: number, bytes: 1 | 2 | 4 | 8, unsigned: boolean): number | bigint;\n  private _readIntAt(offset: number, bytes: 1 | 2 | 4 | 8, unsigned: boolean): number | bigint {\n    if (offset < 0 || offset + bytes > this._maxSize) {\n      throw new Error('Offset out of bounds');\n    }\n\n    switch (bytes) {\n      case 1:\n        return unsigned ? this._view.getUint8(offset) : this._view.getInt8(offset);\n      case 2:\n        return unsigned\n          ? this._view.getUint16(offset, this._littleEndian)\n          : this._view.getInt16(offset, this._littleEndian);\n      case 4:\n        return unsigned\n          ? this._view.getUint32(offset, this._littleEndian)\n          : this._view.getInt32(offset, this._littleEndian);\n      case 8:\n        return unsigned\n          ? this._view.getBigUint64(offset, this._littleEndian)\n          : this._view.getBigInt64(offset, this._littleEndian);\n      default:\n        throw new Error('Size must be 1, 2, 4 or 8 bytes');\n    }\n  }\n\n  private _readInt(size: 1 | 2 | 4, unsigned: boolean): number;\n  private _readInt(size: 8, unsigned: boolean): bigint;\n  private _readInt(size: 1 | 2 | 4 | 8, unsigned: boolean): number | bigint {\n    this._checkOverflow(size);\n    const value = this._readIntAt(this._offset, size, unsigned);\n\n    this._offset += size;\n\n    return value;\n  }\n\n  private _writeIntAt(offset: number, value: bigint, size: 8, unsigned: boolean): void;\n  private _writeIntAt(offset: number, value: number, size: 1 | 2 | 4, unsigned: boolean): void;\n  private _writeIntAt(\n    offset: number,\n    value: number | bigint,\n    size: 1 | 2 | 4 | 8,\n    unsigned: boolean,\n  ): void;\n  private _writeIntAt(\n    offset: number,\n    value: number | bigint,\n    size: 1 | 2 | 4 | 8,\n    unsigned: boolean,\n  ) {\n    if (offset < 0 || offset + size > this._maxSize) {\n      throw new Error('Offset out of bounds');\n    }\n\n    switch (size) {\n      case 1:\n        if (unsigned) {\n          this._view.setUint8(offset, value as number);\n        } else {\n          this._view.setInt8(offset, value as number);\n        }\n        break;\n      case 2:\n        if (unsigned) {\n          this._view.setUint16(offset, value as number, this._littleEndian);\n        } else {\n          this._view.setInt16(offset, value as number, this._littleEndian);\n        }\n        break;\n      case 4:\n        if (unsigned) {\n          this._view.setUint32(offset, value as number, this._littleEndian);\n        } else {\n          this._view.setInt32(offset, value as number, this._littleEndian);\n        }\n        break;\n      case 8:\n        if (unsigned) {\n          this._view.setBigUint64(offset, value as bigint, this._littleEndian);\n        } else {\n          this._view.setBigInt64(offset, value as bigint, this._littleEndian);\n        }\n        break;\n      default:\n        throw new Error('Size must be 1, 2, 4 or 8 bytes');\n    }\n  }\n\n  private _writeInt(size: 1 | 2 | 4, value: number, unsigned: boolean): void;\n  private _writeInt(size: 8, value: bigint, unsigned: boolean): void;\n  private _writeInt(size: 1 | 2 | 4 | 8, value: number | bigint, unsigned: boolean) {\n    this._checkOverflow(size);\n    this._writeIntAt(this._offset, value, size, unsigned);\n    this._offset += size;\n  }\n\n  private _readFloatAt(offset: number, size: 4 | 8): number {\n    if (offset < 0 || offset + size > this._maxSize) {\n      throw new Error('Offset out of bounds');\n    }\n\n    if (size === 4) {\n      return this._view.getFloat32(offset, this._littleEndian);\n    } else if (size === 8) {\n      return this._view.getFloat64(offset, this._littleEndian);\n    } else {\n      throw new Error('Size must be 4 or 8 bytes');\n    }\n  }\n\n  private _readFloat(size: 4 | 8): number {\n    this._checkOverflow(size);\n    const value = this._readFloatAt(this._offset, size);\n\n    this._offset += size;\n\n    return value;\n  }\n\n  private _writeFloatAt(offset: number, value: number, size: 4 | 8): void {\n    if (offset < 0 || offset + size > this._maxSize) {\n      throw new Error('Offset out of bounds');\n    }\n\n    if (size === 4) {\n      this._view.setFloat32(offset, value, this._littleEndian);\n    } else if (size === 8) {\n      this._view.setFloat64(offset, value, this._littleEndian);\n    } else {\n      throw new Error('Size must be 4 or 8 bytes');\n    }\n  }\n\n  private _writeFloat(size: 4 | 8, value: number): void {\n    this._checkOverflow(size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