{"version":3,"file":"dataPacketBuffer.mjs","sources":["../../../src/utils/dataPacketBuffer.ts"],"sourcesContent":["import type { NonSharedUint8Array } from '../type-polyfills/non-shared-typed-arrays';\n\nexport interface DataPacketItem {\n  data: NonSharedUint8Array;\n  sequence: number;\n  /**\n   * Whether the packet has been handed to the data channel. Unsent packets are queued for the\n   * resume replay (e.g. sends that landed in a reconnect window) and must never be trimmed by\n   * {@link DataPacketBuffer.alignBufferedAmount}, which reasons about the channel's buffered\n   * bytes — those only ever contain sent packets.\n   */\n  sent: boolean;\n}\n\nexport class DataPacketBuffer {\n  private buffer: DataPacketItem[] = [];\n\n  private _totalSize = 0;\n\n  private _sentSize = 0;\n\n  push(item: DataPacketItem) {\n    this.buffer.push(item);\n    this._totalSize += item.data.byteLength;\n    if (item.sent) {\n      this._sentSize += item.data.byteLength;\n    }\n  }\n\n  pop(): DataPacketItem | undefined {\n    const item = this.buffer.shift();\n    if (item) {\n      this._totalSize -= item.data.byteLength;\n      if (item.sent) {\n        this._sentSize -= item.data.byteLength;\n      }\n    }\n    return item;\n  }\n\n  getAll(): DataPacketItem[] {\n    return this.buffer.slice();\n  }\n\n  /** Every queued packet not yet handed to the channel, in sequence order. */\n  getUnsent(): DataPacketItem[] {\n    return this.buffer.filter((item) => !item.sent);\n  }\n\n  /** Marks a single queued packet as handed to the channel. */\n  markSent(item: DataPacketItem) {\n    if (!item.sent) {\n      item.sent = true;\n      this._sentSize += item.data.byteLength;\n    }\n  }\n\n  /**\n   * Marks every queued packet as not-yet-sent. Used at the start of a resume replay: whatever is\n   * still buffered was sent on the previous channel (or deferred) and must be re-handed to the\n   * current one, so none of it counts as sent until the replay actually transmits it.\n   */\n  markAllUnsent() {\n    for (const item of this.buffer) {\n      item.sent = false;\n    }\n    this._sentSize = 0;\n  }\n\n  popToSequence(sequence: number) {\n    while (this.buffer.length > 0) {\n      const first = this.buffer[0];\n      if (first.sequence <= sequence) {\n        this.pop();\n      } else {\n        break;\n      }\n    }\n  }\n\n  alignBufferedAmount(bufferedAmount: number) {\n    while (this.buffer.length > 0) {\n      const first = this.buffer[0];\n      // Unsent packets aren't part of the channel's bufferedAmount and are still awaiting the\n      // resume replay — trimming them would silently lose them.\n      if (!first.sent) {\n        break;\n      }\n      if (this._sentSize - first.data.byteLength <= bufferedAmount) {\n        break;\n      }\n      this.pop();\n    }\n  }\n\n  get length(): number {\n    return this.buffer.length;\n  }\n}\n"],"names":["DataPacketBuffer","constructor","buffer","_totalSize","_sentSize","push","item","data","byteLength","sent","pop","shift","getAll","slice","getUnsent","filter","markSent","markAllUnsent","popToSequence","sequence","length","first","alignBufferedAmount","bufferedAmount"],"mappings":"MAcaA,gBAAgB,CAAA;AAA7BC,EAAAA,WAAAA,GAAA;IACU,IAAA,CAAAC,MAAM,GAAqB,EAAE;IAE7B,IAAA,CAAAC,UAAU,GAAG,CAAC;IAEd,IAAA,CAAAC,SAAS,GAAG,CAAC;AA+EvB,EAAA;EA7EEC,IAAIA,CAACC,IAAoB,EAAA;AACvB,IAAA,IAAI,CAACJ,MAAM,CAACG,IAAI,CAACC,IAAI,CAAC;AACtB,IAAA,IAAI,CAACH,UAAU,IAAIG,IAAI,CAACC,IAAI,CAACC,UAAU;IACvC,IAAIF,IAAI,CAACG,IAAI,EAAE;AACb,MAAA,IAAI,CAACL,SAAS,IAAIE,IAAI,CAACC,IAAI,CAACC,UAAU;AACxC,IAAA;AACF,EAAA;AAEAE,EAAAA,GAAGA,GAAA;IACD,MAAMJ,IAAI,GAAG,IAAI,CAACJ,MAAM,CAACS,KAAK,EAAE;AAChC,IAAA,IAAIL,IAAI,EAAE;AACR,MAAA,IAAI,CAACH,UAAU,IAAIG,IAAI,CAACC,IAAI,CAACC,UAAU;MACvC,IAAIF,IAAI,CAACG,IAAI,EAAE;AACb,QAAA,IAAI,CAACL,SAAS,IAAIE,IAAI,CAACC,IAAI,CAACC,UAAU;AACxC,MAAA;AACF,IAAA;AACA,IAAA,OAAOF,IAAI;AACb,EAAA;AAEAM,EA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