{"version":3,"file":"encoder.mjs","names":[],"sources":["../../src/tools/encoder.ts"],"sourcesContent":["import type { Uint8ArrayBuffer } from './utils/byteUtils'\nimport { computeBytesCount } from './utils/byteUtils'\n\nexport interface Encoder<Output extends string | Uint8ArrayBuffer = string | Uint8ArrayBuffer> {\n  /**\n   * Whether this encoder might call the provided callbacks asynchronously\n   */\n  isAsync: boolean\n\n  /**\n   * Whether some data has been written since the last finish() or finishSync() call\n   */\n  isEmpty: boolean\n\n  /**\n   * Write a string to be encoded.\n   *\n   * This operation can be synchronous or asynchronous depending on the encoder implementation.\n   *\n   * If specified, the callback will be invoked when the operation finishes, unless the operation is\n   * asynchronous and finish() or finishSync() is called in the meantime.\n   */\n  write(data: string, callback?: (additionalEncodedBytesCount: number) => void): void\n\n  /**\n   * Waits for pending data to be encoded and resets the encoder state.\n   *\n   * This operation can be synchronous or asynchronous depending on the encoder implementation.\n   *\n   * The callback will be invoked when the operation finishes, unless the operation is asynchronous\n   * and another call to finish() or finishSync() occurs in the meantime.\n   */\n  finish(callback: (result: EncoderResult<Output>) => void): void\n\n  /**\n   * Resets the encoder state then returns the encoded data and any potential pending data directly,\n   * discarding all pending write operations and finish() callbacks.\n   */\n  finishSync(): EncoderResult<Output> & { pendingData: string }\n\n  /**\n   * Returns a rough estimation of the bytes count if the data was encoded.\n   */\n  estimateEncodedBytesCount(data: string): number\n}\n\nexport interface EncoderResult<Output extends string | Uint8ArrayBuffer = string | Uint8ArrayBuffer> {\n  output: Output\n  outputBytesCount: number\n\n  /**\n   * An encoding type supported by HTTP Content-Encoding, if applicable.\n   * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#directives\n   */\n  encoding?: 'deflate'\n\n  /**\n   * Total bytes count of the input strings encoded to UTF-8.\n   */\n  rawBytesCount: number\n}\n\nexport function createIdentityEncoder(): Encoder<string> {\n  let output = ''\n  let outputBytesCount = 0\n\n  return {\n    isAsync: false,\n\n    get isEmpty() {\n      return !output\n    },\n\n    write(data, callback) {\n      const additionalEncodedBytesCount = computeBytesCount(data)\n      outputBytesCount += additionalEncodedBytesCount\n      output += data\n      if (callback) {\n        callback(additionalEncodedBytesCount)\n      }\n    },\n\n    finish(callback) {\n      callback(this.finishSync())\n    },\n\n    finishSync() {\n      const result = {\n        output,\n        outputBytesCount,\n        rawBytesCount: outputBytesCount,\n        pendingData: '',\n      }\n      output = ''\n      outputBytesCount = 0\n      return result\n    },\n\n    estimateEncodedBytesCount(data) {\n      return data.length\n    },\n  }\n}\n"],"mappings":";;AA8DA,SAAgB,wBAAyC;CACvD,IAAI,SAAS;CACb,IAAI,mBAAmB;CAEvB,OAAO;EACL,SAAS;EAET,IAAI,UAAU;GACZ,OAAO,CAAC;EACV;EAEA,MAAM,MAAM,UAAU;GACpB,MAAM,8BAA8B,kBAAkB,IAAI;GAC1D,oBAAoB;GACpB,UAAU;GACV,IAAI,UACF,SAAS,2BAA2B;EAExC;EAEA,OAAO,UAAU;GACf,SAAS,KAAK,WAAW,CAAC;EAC5B;EAEA,aAAa;GACX,MAAM,SAAS;IACb;IACA;IACA,eAAe;IACf,aAAa;GACf;GACA,SAAS;GACT,mBAAmB;GACnB,OAAO;EACT;EAEA,0BAA0B,MAAM;GAC9B,OAAO,KAAK;EACd;CACF;AACF"}