{"version":3,"file":"ProxyPostMessageStream.mjs","sourceRoot":"","sources":["../../src/services/ProxyPostMessageStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AAetE;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,qBAAqB;IACtD,OAAO,CAAwB;IAE/B,MAAM,CAAS;IAExB;;;;;;OAMG;IACH,YAAY,EAAE,MAAM,EAAE,KAAK,EAA8B;QACvD,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QAEpB,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnD,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,IAAsB;QAC5B,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YAC/B,OAAO;QACT,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACH,YAAY,CAAC,IAAsB;QACjC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,IAAI;SACL,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { BasePostMessageStream } from '@metamask/post-message-stream';\nimport type { JsonRpcRequest } from '@metamask/utils';\n\nexport type ProxyPostMessageStreamArgs = {\n  stream: BasePostMessageStream;\n  jobId: string;\n  extra?: Record<string, unknown>;\n};\n\nexport type ProxyPostMessage = {\n  jobId: string;\n  data: JsonRpcRequest;\n  extra?: Record<string, unknown>;\n};\n\n/**\n * A post message stream that wraps messages in a job ID, before sending them\n * over the underlying stream.\n */\nexport class ProxyPostMessageStream extends BasePostMessageStream {\n  readonly #stream: BasePostMessageStream;\n\n  readonly #jobId: string;\n\n  /**\n   * Initializes a new `ProxyPostMessageStream` instance.\n   *\n   * @param args - The constructor arguments.\n   * @param args.stream - The underlying stream to use for communication.\n   * @param args.jobId - The ID of the job this stream is associated with.\n   */\n  constructor({ stream, jobId }: ProxyPostMessageStreamArgs) {\n    super();\n\n    this.#stream = stream;\n    this.#jobId = jobId;\n\n    this.#stream.on('data', this.#onData.bind(this));\n  }\n\n  /**\n   * Handle incoming data from the underlying stream. This checks that the job\n   * ID matches the expected job ID, and pushes the data to the stream if so.\n   *\n   * @param data - The data to handle.\n   */\n  #onData(data: ProxyPostMessage) {\n    if (data.jobId !== this.#jobId) {\n      return;\n    }\n\n    this.push(data.data);\n  }\n\n  /**\n   * Write data to the underlying stream. This wraps the data in an object with\n   * the job ID.\n   *\n   * @param data - The data to write.\n   */\n  _postMessage(data: ProxyPostMessage) {\n    this.#stream.write({\n      jobId: this.#jobId,\n      data,\n    });\n  }\n}\n"]}