{"version":3,"file":"ProxyExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":";;;AACA,mCAAgC;AAMhC,8DAAuD;AACvD,0EAAmE;AAMnE,MAAa,qBAAsB,SAAQ,mCAAwB;IACxD,OAAO,CAAwB;IAExC;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC8B;QACrC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,IAAA,eAAM,GAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,MAAc;QAC1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,+CAAsB,CAAC;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,KAAK,EAAE,MAAM;SACd,CAAC,CAAC;QAEH,0DAA0D;QAC1D,iFAAiF;QACjF,2DAA2D;QAC3D,sFAAsF;QACtF,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAC7B,MAAM,CAAC,KAAK,CAAC;gBACX,IAAI,EAAE,SAAS;gBACf,IAAI,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,IAAA,eAAM,GAAE,EAAE;aACvD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IACpC,CAAC;CACF;AA7ED,sDA6EC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport type {\n  ExecutionServiceArgs,\n  TerminateJobArgs,\n} from '../ExecutionService';\nimport { ExecutionService } from '../ExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype ProxyExecutionEnvironmentServiceArgs = {\n  stream: BasePostMessageStream;\n} & ExecutionServiceArgs;\n\nexport class ProxyExecutionService extends ExecutionService<string> {\n  readonly #stream: BasePostMessageStream;\n\n  /**\n   * Create a new proxy execution service.\n   *\n   * @param args - The constructor arguments.\n   * @param args.messenger - The messenger to use for communication with the\n   * `SnapController`.\n   * @param args.setupSnapProvider - The function to use to set up the snap\n   * provider.\n   * @param args.stream - The stream to use for communicating with the proxy\n   * executor.\n   */\n  constructor({\n    stream,\n    messenger,\n    setupSnapProvider,\n    ...args\n  }: ProxyExecutionEnvironmentServiceArgs) {\n    super({\n      ...args,\n      messenger,\n      setupSnapProvider,\n      usePing: false,\n    });\n\n    this.#stream = stream;\n  }\n\n  /**\n   * Send a termination command to the proxy stream.\n   *\n   * @param job - The job to terminate.\n   */\n  protected async terminateJob(job: TerminateJobArgs<string>) {\n    // The `AbstractExecutionService` will have already closed the job stream,\n    // so we write to the runtime stream directly.\n    this.#stream.write({\n      jobId: job.id,\n      data: {\n        jsonrpc: '2.0',\n        method: 'terminateJob',\n        id: nanoid(),\n      },\n    });\n  }\n\n  /**\n   * Create a new stream for the specified Snap. This wraps the root stream\n   * in a stream specific to the Snap.\n   *\n   * @param snapId - The Snap ID.\n   * @returns An object with the worker ID and stream.\n   */\n  protected async initEnvStream(snapId: string) {\n    this.setSnapStatus(snapId, 'initializing');\n\n    const stream = new ProxyPostMessageStream({\n      stream: this.#stream,\n      jobId: snapId,\n    });\n\n    // Send a request and await any response before continuing\n    // This simulates the behaviour of non-proxy environments by effectively awaiting\n    // the load of the environment inside the proxy environment\n    // This assumes the proxy environment is already loaded before this function is called\n    await new Promise((resolve) => {\n      stream.once('data', resolve);\n      stream.write({\n        name: 'command',\n        data: { jsonrpc: '2.0', method: 'ping', id: nanoid() },\n      });\n    });\n\n    return { worker: snapId, stream };\n  }\n}\n"]}