{"version":3,"file":"serializable.mjs","sources":["../../../../../src/room/data-track/packet/serializable.ts"],"sourcesContent":["import type { NonSharedUint8Array } from '../../../type-polyfills/non-shared-typed-arrays';\nimport { type Throws } from '../../../utils/throws';\nimport { DataTrackSerializeError } from './errors';\n\n/** An abstract class implementing common behavior related to data track binary serialization. */\nexport default abstract class Serializable {\n  /** Returns the expected length of the serialized output in bytes */\n  abstract toBinaryLengthBytes(): number;\n\n  /** Given a DataView, serialize the instance inside and return the number of bytes written. */\n  abstract toBinaryInto(dataView: DataView): Throws<number, DataTrackSerializeError>;\n\n  /** Encodes the instance as binary and returns the data as a Uint8Array. */\n  toBinary(): Throws<NonSharedUint8Array, DataTrackSerializeError> {\n    const lengthBytes = this.toBinaryLengthBytes();\n    const output = new ArrayBuffer(lengthBytes);\n    const view = new DataView(output);\n\n    const writtenBytes = this.toBinaryInto(view);\n\n    if (lengthBytes !== writtenBytes) {\n      // @throws-transformer ignore - this should be treated as a \"panic\" and not be caught\n      throw new Error(\n        `${this.constructor.name}.toBinary: written bytes (${writtenBytes} bytes) not equal to allocated array buffer length (${lengthBytes} bytes).`,\n      );\n    }\n\n    return new Uint8Array(output);\n  }\n}\n"],"names":["Serializable","toBinary","lengthBytes","toBinaryLengthBytes","output","ArrayBuffer","view","DataView","writtenBytes","toBinaryInto","Error","concat","constructor","name","Uint8Array"],"mappings":"qBAIA;AACc,MAAgBA,YAAY,CAAA;AAOxC;AACAC,EAAAA,QAAQA,GAAA;AACN,IAAA,MAAMC,WAAW,GAAG,IAAI,CAACC,mBAAmB,EAAE;AAC9C,IAAA,MAAMC,MAAM,GAAG,IAAIC,WAAW,CAACH,WAAW,CAAC;AAC3C,IAAA,MAAMI,IAAI,GAAG,IAAIC,QAAQ,CAACH,MAAM,CAAC;AAEjC,IAAA,MAAMI,YAAY,GAAG,IAAI,CAACC,YAAY,CAACH,IAAI,CAAC;IAE5C,IAAIJ,WAAW,KAAKM,YAAY,EAAE;AAChC;AACA,MAAA,MAAM,IAAIE,KAAK,CAAA,EAAA,CAAAC,MAAA,CACV,IAAI,CAACC,WAAW,CAACC,IAAI,EAAA,4BAAA,CAAA,CAAAF,MAAA,CAA6BH,YAAY,EAAA,sDAAA,CAAA,CAAAG,MAAA,CAAuDT,WAAW,aAAU,CAC9I;AACH,IAAA;AAEA,IAAA,OAAO,IAAIY,UAAU,CAACV,MAAM,CAAC;AAC/B,EAAA;AACD"}