@metamask/snaps-controllers 9.19.1 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/dist/multichain/MultichainRouter.cjs +9 -1
  3. package/dist/multichain/MultichainRouter.cjs.map +1 -1
  4. package/dist/multichain/MultichainRouter.d.cts +1 -1
  5. package/dist/multichain/MultichainRouter.d.cts.map +1 -1
  6. package/dist/multichain/MultichainRouter.d.mts +1 -1
  7. package/dist/multichain/MultichainRouter.d.mts.map +1 -1
  8. package/dist/multichain/MultichainRouter.mjs +9 -1
  9. package/dist/multichain/MultichainRouter.mjs.map +1 -1
  10. package/dist/services/iframe/IframeExecutionService.cjs +2 -1
  11. package/dist/services/iframe/IframeExecutionService.cjs.map +1 -1
  12. package/dist/services/iframe/IframeExecutionService.d.cts +1 -1
  13. package/dist/services/iframe/IframeExecutionService.d.cts.map +1 -1
  14. package/dist/services/iframe/IframeExecutionService.d.mts +1 -1
  15. package/dist/services/iframe/IframeExecutionService.d.mts.map +1 -1
  16. package/dist/services/iframe/IframeExecutionService.mjs +2 -1
  17. package/dist/services/iframe/IframeExecutionService.mjs.map +1 -1
  18. package/dist/services/offscreen/OffscreenExecutionService.cjs +2 -1
  19. package/dist/services/offscreen/OffscreenExecutionService.cjs.map +1 -1
  20. package/dist/services/offscreen/OffscreenExecutionService.d.cts +1 -1
  21. package/dist/services/offscreen/OffscreenExecutionService.d.cts.map +1 -1
  22. package/dist/services/offscreen/OffscreenExecutionService.d.mts +1 -1
  23. package/dist/services/offscreen/OffscreenExecutionService.d.mts.map +1 -1
  24. package/dist/services/offscreen/OffscreenExecutionService.mjs +2 -1
  25. package/dist/services/offscreen/OffscreenExecutionService.mjs.map +1 -1
  26. package/dist/services/proxy/ProxyExecutionService.cjs +2 -1
  27. package/dist/services/proxy/ProxyExecutionService.cjs.map +1 -1
  28. package/dist/services/proxy/ProxyExecutionService.d.cts +1 -1
  29. package/dist/services/proxy/ProxyExecutionService.d.cts.map +1 -1
  30. package/dist/services/proxy/ProxyExecutionService.d.mts +1 -1
  31. package/dist/services/proxy/ProxyExecutionService.d.mts.map +1 -1
  32. package/dist/services/proxy/ProxyExecutionService.mjs +2 -1
  33. package/dist/services/proxy/ProxyExecutionService.mjs.map +1 -1
  34. package/dist/services/webview/WebViewExecutionService.cjs +20 -22
  35. package/dist/services/webview/WebViewExecutionService.cjs.map +1 -1
  36. package/dist/services/webview/WebViewExecutionService.d.cts +10 -7
  37. package/dist/services/webview/WebViewExecutionService.d.cts.map +1 -1
  38. package/dist/services/webview/WebViewExecutionService.d.mts +10 -7
  39. package/dist/services/webview/WebViewExecutionService.d.mts.map +1 -1
  40. package/dist/services/webview/WebViewExecutionService.mjs +20 -22
  41. package/dist/services/webview/WebViewExecutionService.mjs.map +1 -1
  42. package/dist/services/webview/WebViewMessageStream.cjs +11 -22
  43. package/dist/services/webview/WebViewMessageStream.cjs.map +1 -1
  44. package/dist/services/webview/WebViewMessageStream.d.cts +3 -3
  45. package/dist/services/webview/WebViewMessageStream.d.cts.map +1 -1
  46. package/dist/services/webview/WebViewMessageStream.d.mts +3 -3
  47. package/dist/services/webview/WebViewMessageStream.d.mts.map +1 -1
  48. package/dist/services/webview/WebViewMessageStream.mjs +12 -23
  49. package/dist/services/webview/WebViewMessageStream.mjs.map +1 -1
  50. package/dist/services/webworker/WebWorkerExecutionService.cjs +2 -1
  51. package/dist/services/webworker/WebWorkerExecutionService.cjs.map +1 -1
  52. package/dist/services/webworker/WebWorkerExecutionService.d.cts +1 -1
  53. package/dist/services/webworker/WebWorkerExecutionService.d.cts.map +1 -1
  54. package/dist/services/webworker/WebWorkerExecutionService.d.mts +1 -1
  55. package/dist/services/webworker/WebWorkerExecutionService.d.mts.map +1 -1
  56. package/dist/services/webworker/WebWorkerExecutionService.mjs +2 -1
  57. package/dist/services/webworker/WebWorkerExecutionService.mjs.map +1 -1
  58. package/package.json +11 -11
@@ -1 +1 @@
1
- {"version":3,"file":"ProxyExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,MAAM,EAAE,eAAe;AAMhC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAMnE,MAAM,OAAO,qBAAsB,SAAQ,wBAAgC;IAGzE;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,GACoB;QACrC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAtBI,gDAA+B;QAwBtC,uBAAA,IAAI,iCAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,uBAAA,IAAI,qCAAQ,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,MAAM,EAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,MAAM,MAAM,GAAG,IAAI,sBAAsB,CAAC;YACxC,MAAM,EAAE,uBAAA,IAAI,qCAAQ;YACpB,KAAK;SACN,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,MAAM,EAAE,EAAE;aACvD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype ProxyExecutionEnvironmentServiceArgs = {\n stream: BasePostMessageStream;\n} & ExecutionServiceArgs;\n\nexport class ProxyExecutionService extends AbstractExecutionService<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 }: ProxyExecutionEnvironmentServiceArgs) {\n super({\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 job. This wraps the root stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId,\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: jobId, stream };\n }\n}\n"]}
1
+ {"version":3,"file":"ProxyExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,MAAM,EAAE,eAAe;AAMhC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAMnE,MAAM,OAAO,qBAAsB,SAAQ,wBAAgC;IAGzE;;;;;;;;;;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;QAxBI,gDAA+B;QA0BtC,uBAAA,IAAI,iCAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,uBAAA,IAAI,qCAAQ,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,MAAM,EAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,MAAM,MAAM,GAAG,IAAI,sBAAsB,CAAC;YACxC,MAAM,EAAE,uBAAA,IAAI,qCAAQ;YACpB,KAAK;SACN,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,MAAM,EAAE,EAAE;aACvD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype ProxyExecutionEnvironmentServiceArgs = {\n stream: BasePostMessageStream;\n} & ExecutionServiceArgs;\n\nexport class ProxyExecutionService extends AbstractExecutionService<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 job. This wraps the root stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId,\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: jobId, stream };\n }\n}\n"]}
@@ -10,25 +10,22 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _WebViewExecutionService_instances, _WebViewExecutionService_getWebView, _WebViewExecutionService_ensureWebViewLoaded;
13
+ var _WebViewExecutionService_createWebView, _WebViewExecutionService_removeWebView;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.WebViewExecutionService = void 0;
16
- const ProxyExecutionService_1 = require("../proxy/ProxyExecutionService.cjs");
16
+ const AbstractExecutionService_1 = require("../AbstractExecutionService.cjs");
17
17
  const WebViewMessageStream_1 = require("./WebViewMessageStream.cjs");
18
- class WebViewExecutionService extends ProxyExecutionService_1.ProxyExecutionService {
19
- constructor({ messenger, setupSnapProvider, getWebView, }) {
18
+ class WebViewExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
19
+ constructor({ messenger, setupSnapProvider, createWebView, removeWebView, ...args }) {
20
20
  super({
21
+ ...args,
21
22
  messenger,
22
23
  setupSnapProvider,
23
- stream: new WebViewMessageStream_1.WebViewMessageStream({
24
- name: 'parent',
25
- target: 'child',
26
- getWebView,
27
- }),
28
24
  });
29
- _WebViewExecutionService_instances.add(this);
30
- _WebViewExecutionService_getWebView.set(this, void 0);
31
- __classPrivateFieldSet(this, _WebViewExecutionService_getWebView, getWebView, "f");
25
+ _WebViewExecutionService_createWebView.set(this, void 0);
26
+ _WebViewExecutionService_removeWebView.set(this, void 0);
27
+ __classPrivateFieldSet(this, _WebViewExecutionService_createWebView, createWebView, "f");
28
+ __classPrivateFieldSet(this, _WebViewExecutionService_removeWebView, removeWebView, "f");
32
29
  }
33
30
  /**
34
31
  * Create a new stream for the specified job. This wraps the runtime stream
@@ -38,17 +35,18 @@ class WebViewExecutionService extends ProxyExecutionService_1.ProxyExecutionServ
38
35
  * @returns An object with the worker ID and stream.
39
36
  */
40
37
  async initEnvStream(jobId) {
41
- // Ensure that the WebView has been loaded before we proceed.
42
- await __classPrivateFieldGet(this, _WebViewExecutionService_instances, "m", _WebViewExecutionService_ensureWebViewLoaded).call(this);
43
- return super.initEnvStream(jobId);
38
+ const webView = await __classPrivateFieldGet(this, _WebViewExecutionService_createWebView, "f").call(this, jobId);
39
+ const stream = new WebViewMessageStream_1.WebViewMessageStream({
40
+ name: 'parent',
41
+ target: 'child',
42
+ webView,
43
+ });
44
+ return { worker: webView, stream };
45
+ }
46
+ terminateJob(jobWrapper) {
47
+ __classPrivateFieldGet(this, _WebViewExecutionService_removeWebView, "f").call(this, jobWrapper.id);
44
48
  }
45
49
  }
46
50
  exports.WebViewExecutionService = WebViewExecutionService;
47
- _WebViewExecutionService_getWebView = new WeakMap(), _WebViewExecutionService_instances = new WeakSet(), _WebViewExecutionService_ensureWebViewLoaded =
48
- /**
49
- * Ensure that the WebView has been loaded by awaiting the getWebView promise.
50
- */
51
- async function _WebViewExecutionService_ensureWebViewLoaded() {
52
- await __classPrivateFieldGet(this, _WebViewExecutionService_getWebView, "f").call(this);
53
- };
51
+ _WebViewExecutionService_createWebView = new WeakMap(), _WebViewExecutionService_removeWebView = new WeakMap();
54
52
  //# sourceMappingURL=WebViewExecutionService.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,8EAAuE;AAEvE,qEAA8D;AAM9D,MAAa,uBAAwB,SAAQ,6CAAqB;IAGhE,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,UAAU,GACkB;QAC5B,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;YACjB,MAAM,EAAE,IAAI,2CAAoB,CAAC;gBAC/B,IAAI,EAAE,QAAQ;gBACd,MAAM,EAAE,OAAO;gBACf,UAAU;aACX,CAAC;SACH,CAAC,CAAC;;QAfL,sDAAY;QAgBV,uBAAA,IAAI,uCAAe,UAAU,MAAA,CAAC;IAChC,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,6DAA6D;QAC7D,MAAM,uBAAA,IAAI,wFAAqB,MAAzB,IAAI,CAAuB,CAAC;QAElC,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CAQF;AAxCD,0DAwCC;;AANC;;GAEG;AACH,KAAK;IACH,MAAM,uBAAA,IAAI,2CAAY,MAAhB,IAAI,CAAc,CAAC;AAC3B,CAAC","sourcesContent":["import type { ExecutionServiceArgs } from '../AbstractExecutionService';\nimport { ProxyExecutionService } from '../proxy/ProxyExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n getWebView: () => Promise<WebViewInterface>;\n};\n\nexport class WebViewExecutionService extends ProxyExecutionService {\n #getWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n getWebView,\n }: WebViewExecutionServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n stream: new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n getWebView,\n }),\n });\n this.#getWebView = getWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Ensure that the WebView has been loaded before we proceed.\n await this.#ensureWebViewLoaded();\n\n return super.initEnvStream(jobId);\n }\n\n /**\n * Ensure that the WebView has been loaded by awaiting the getWebView promise.\n */\n async #ensureWebViewLoaded() {\n await this.#getWebView();\n }\n}\n"]}
1
+ {"version":3,"file":"WebViewExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,8EAGqC;AAErC,qEAA8D;AAO9D,MAAa,uBAAwB,SAAQ,mDAA0C;IAKrF,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,GAAG,IAAI,EACqB;QAC5B,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAfI,yDAAe;QAEf,yDAAe;QActB,uBAAA,IAAI,0CAAkB,aAAa,MAAA,CAAC;QACpC,uBAAA,IAAI,0CAAkB,aAAa,MAAA,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,MAAM,OAAO,GAAG,MAAM,uBAAA,IAAI,8CAAe,MAAnB,IAAI,EAAgB,KAAK,CAAC,CAAC;QAEjD,MAAM,MAAM,GAAG,IAAI,2CAAoB,CAAC;YACtC,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,OAAO;SACR,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IACrC,CAAC;IAES,YAAY,CAAC,UAA8C;QACnE,uBAAA,IAAI,8CAAe,MAAnB,IAAI,EAAgB,UAAU,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;CACF;AA3CD,0DA2CC","sourcesContent":["import type { TerminateJobArgs } from '../AbstractExecutionService';\nimport {\n AbstractExecutionService,\n type ExecutionServiceArgs,\n} from '../AbstractExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n createWebView: (jobId: string) => Promise<WebViewInterface>;\n removeWebView: (jobId: string) => void;\n};\n\nexport class WebViewExecutionService extends AbstractExecutionService<WebViewInterface> {\n readonly #createWebView;\n\n readonly #removeWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n createWebView,\n removeWebView,\n ...args\n }: WebViewExecutionServiceArgs) {\n super({\n ...args,\n messenger,\n setupSnapProvider,\n });\n this.#createWebView = createWebView;\n this.#removeWebView = removeWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n const webView = await this.#createWebView(jobId);\n\n const stream = new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n webView,\n });\n\n return { worker: webView, stream };\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<WebViewInterface>): void {\n this.#removeWebView(jobWrapper.id);\n }\n}\n"]}
@@ -1,12 +1,14 @@
1
- import type { ExecutionServiceArgs } from "../AbstractExecutionService.cjs";
2
- import { ProxyExecutionService } from "../proxy/ProxyExecutionService.cjs";
1
+ import type { TerminateJobArgs } from "../AbstractExecutionService.cjs";
2
+ import { AbstractExecutionService, type ExecutionServiceArgs } from "../AbstractExecutionService.cjs";
3
3
  import type { WebViewInterface } from "./WebViewMessageStream.cjs";
4
+ import { WebViewMessageStream } from "./WebViewMessageStream.cjs";
4
5
  export type WebViewExecutionServiceArgs = ExecutionServiceArgs & {
5
- getWebView: () => Promise<WebViewInterface>;
6
+ createWebView: (jobId: string) => Promise<WebViewInterface>;
7
+ removeWebView: (jobId: string) => void;
6
8
  };
7
- export declare class WebViewExecutionService extends ProxyExecutionService {
9
+ export declare class WebViewExecutionService extends AbstractExecutionService<WebViewInterface> {
8
10
  #private;
9
- constructor({ messenger, setupSnapProvider, getWebView, }: WebViewExecutionServiceArgs);
11
+ constructor({ messenger, setupSnapProvider, createWebView, removeWebView, ...args }: WebViewExecutionServiceArgs);
10
12
  /**
11
13
  * Create a new stream for the specified job. This wraps the runtime stream
12
14
  * in a stream specific to the job.
@@ -15,8 +17,9 @@ export declare class WebViewExecutionService extends ProxyExecutionService {
15
17
  * @returns An object with the worker ID and stream.
16
18
  */
17
19
  protected initEnvStream(jobId: string): Promise<{
18
- worker: string;
19
- stream: import("../index.cjs").ProxyPostMessageStream;
20
+ worker: WebViewInterface;
21
+ stream: WebViewMessageStream;
20
22
  }>;
23
+ protected terminateJob(jobWrapper: TerminateJobArgs<WebViewInterface>): void;
21
24
  }
22
25
  //# sourceMappingURL=WebViewExecutionService.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,wCAAoC;AACxE,OAAO,EAAE,qBAAqB,EAAE,2CAAuC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,mCAA+B;AAG/D,MAAM,MAAM,2BAA2B,GAAG,oBAAoB,GAAG;IAC/D,UAAU,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,CAAC;CAC7C,CAAC;AAEF,qBAAa,uBAAwB,SAAQ,qBAAqB;;gBAGpD,EACV,SAAS,EACT,iBAAiB,EACjB,UAAU,GACX,EAAE,2BAA2B;IAa9B;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;CAa5C"}
1
+ {"version":3,"file":"WebViewExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAoC;AACpE,OAAO,EACL,wBAAwB,EACxB,KAAK,oBAAoB,EAC1B,wCAAoC;AACrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,mCAA+B;AAC/D,OAAO,EAAE,oBAAoB,EAAE,mCAA+B;AAE9D,MAAM,MAAM,2BAA2B,GAAG,oBAAoB,GAAG;IAC/D,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC5D,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACxC,CAAC;AAEF,qBAAa,uBAAwB,SAAQ,wBAAwB,CAAC,gBAAgB,CAAC;;gBAKzE,EACV,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,GAAG,IAAI,EACR,EAAE,2BAA2B;IAU9B;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;IAY3C,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,GAAG,IAAI;CAG7E"}
@@ -1,12 +1,14 @@
1
- import type { ExecutionServiceArgs } from "../AbstractExecutionService.mjs";
2
- import { ProxyExecutionService } from "../proxy/ProxyExecutionService.mjs";
1
+ import type { TerminateJobArgs } from "../AbstractExecutionService.mjs";
2
+ import { AbstractExecutionService, type ExecutionServiceArgs } from "../AbstractExecutionService.mjs";
3
3
  import type { WebViewInterface } from "./WebViewMessageStream.mjs";
4
+ import { WebViewMessageStream } from "./WebViewMessageStream.mjs";
4
5
  export type WebViewExecutionServiceArgs = ExecutionServiceArgs & {
5
- getWebView: () => Promise<WebViewInterface>;
6
+ createWebView: (jobId: string) => Promise<WebViewInterface>;
7
+ removeWebView: (jobId: string) => void;
6
8
  };
7
- export declare class WebViewExecutionService extends ProxyExecutionService {
9
+ export declare class WebViewExecutionService extends AbstractExecutionService<WebViewInterface> {
8
10
  #private;
9
- constructor({ messenger, setupSnapProvider, getWebView, }: WebViewExecutionServiceArgs);
11
+ constructor({ messenger, setupSnapProvider, createWebView, removeWebView, ...args }: WebViewExecutionServiceArgs);
10
12
  /**
11
13
  * Create a new stream for the specified job. This wraps the runtime stream
12
14
  * in a stream specific to the job.
@@ -15,8 +17,9 @@ export declare class WebViewExecutionService extends ProxyExecutionService {
15
17
  * @returns An object with the worker ID and stream.
16
18
  */
17
19
  protected initEnvStream(jobId: string): Promise<{
18
- worker: string;
19
- stream: import("../index.mjs").ProxyPostMessageStream;
20
+ worker: WebViewInterface;
21
+ stream: WebViewMessageStream;
20
22
  }>;
23
+ protected terminateJob(jobWrapper: TerminateJobArgs<WebViewInterface>): void;
21
24
  }
22
25
  //# sourceMappingURL=WebViewExecutionService.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,wCAAoC;AACxE,OAAO,EAAE,qBAAqB,EAAE,2CAAuC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,mCAA+B;AAG/D,MAAM,MAAM,2BAA2B,GAAG,oBAAoB,GAAG;IAC/D,UAAU,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,CAAC;CAC7C,CAAC;AAEF,qBAAa,uBAAwB,SAAQ,qBAAqB;;gBAGpD,EACV,SAAS,EACT,iBAAiB,EACjB,UAAU,GACX,EAAE,2BAA2B;IAa9B;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;CAa5C"}
1
+ {"version":3,"file":"WebViewExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAoC;AACpE,OAAO,EACL,wBAAwB,EACxB,KAAK,oBAAoB,EAC1B,wCAAoC;AACrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,mCAA+B;AAC/D,OAAO,EAAE,oBAAoB,EAAE,mCAA+B;AAE9D,MAAM,MAAM,2BAA2B,GAAG,oBAAoB,GAAG;IAC/D,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC5D,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACxC,CAAC;AAEF,qBAAa,uBAAwB,SAAQ,wBAAwB,CAAC,gBAAgB,CAAC;;gBAKzE,EACV,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,GAAG,IAAI,EACR,EAAE,2BAA2B;IAU9B;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;IAY3C,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,GAAG,IAAI;CAG7E"}
@@ -9,23 +9,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _WebViewExecutionService_instances, _WebViewExecutionService_getWebView, _WebViewExecutionService_ensureWebViewLoaded;
13
- import { ProxyExecutionService } from "../proxy/ProxyExecutionService.mjs";
12
+ var _WebViewExecutionService_createWebView, _WebViewExecutionService_removeWebView;
13
+ import { AbstractExecutionService } from "../AbstractExecutionService.mjs";
14
14
  import { WebViewMessageStream } from "./WebViewMessageStream.mjs";
15
- export class WebViewExecutionService extends ProxyExecutionService {
16
- constructor({ messenger, setupSnapProvider, getWebView, }) {
15
+ export class WebViewExecutionService extends AbstractExecutionService {
16
+ constructor({ messenger, setupSnapProvider, createWebView, removeWebView, ...args }) {
17
17
  super({
18
+ ...args,
18
19
  messenger,
19
20
  setupSnapProvider,
20
- stream: new WebViewMessageStream({
21
- name: 'parent',
22
- target: 'child',
23
- getWebView,
24
- }),
25
21
  });
26
- _WebViewExecutionService_instances.add(this);
27
- _WebViewExecutionService_getWebView.set(this, void 0);
28
- __classPrivateFieldSet(this, _WebViewExecutionService_getWebView, getWebView, "f");
22
+ _WebViewExecutionService_createWebView.set(this, void 0);
23
+ _WebViewExecutionService_removeWebView.set(this, void 0);
24
+ __classPrivateFieldSet(this, _WebViewExecutionService_createWebView, createWebView, "f");
25
+ __classPrivateFieldSet(this, _WebViewExecutionService_removeWebView, removeWebView, "f");
29
26
  }
30
27
  /**
31
28
  * Create a new stream for the specified job. This wraps the runtime stream
@@ -35,16 +32,17 @@ export class WebViewExecutionService extends ProxyExecutionService {
35
32
  * @returns An object with the worker ID and stream.
36
33
  */
37
34
  async initEnvStream(jobId) {
38
- // Ensure that the WebView has been loaded before we proceed.
39
- await __classPrivateFieldGet(this, _WebViewExecutionService_instances, "m", _WebViewExecutionService_ensureWebViewLoaded).call(this);
40
- return super.initEnvStream(jobId);
35
+ const webView = await __classPrivateFieldGet(this, _WebViewExecutionService_createWebView, "f").call(this, jobId);
36
+ const stream = new WebViewMessageStream({
37
+ name: 'parent',
38
+ target: 'child',
39
+ webView,
40
+ });
41
+ return { worker: webView, stream };
42
+ }
43
+ terminateJob(jobWrapper) {
44
+ __classPrivateFieldGet(this, _WebViewExecutionService_removeWebView, "f").call(this, jobWrapper.id);
41
45
  }
42
46
  }
43
- _WebViewExecutionService_getWebView = new WeakMap(), _WebViewExecutionService_instances = new WeakSet(), _WebViewExecutionService_ensureWebViewLoaded =
44
- /**
45
- * Ensure that the WebView has been loaded by awaiting the getWebView promise.
46
- */
47
- async function _WebViewExecutionService_ensureWebViewLoaded() {
48
- await __classPrivateFieldGet(this, _WebViewExecutionService_getWebView, "f").call(this);
49
- };
47
+ _WebViewExecutionService_createWebView = new WeakMap(), _WebViewExecutionService_removeWebView = new WeakMap();
50
48
  //# sourceMappingURL=WebViewExecutionService.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,qBAAqB,EAAE,2CAAuC;AAEvE,OAAO,EAAE,oBAAoB,EAAE,mCAA+B;AAM9D,MAAM,OAAO,uBAAwB,SAAQ,qBAAqB;IAGhE,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,UAAU,GACkB;QAC5B,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;YACjB,MAAM,EAAE,IAAI,oBAAoB,CAAC;gBAC/B,IAAI,EAAE,QAAQ;gBACd,MAAM,EAAE,OAAO;gBACf,UAAU;aACX,CAAC;SACH,CAAC,CAAC;;QAfL,sDAAY;QAgBV,uBAAA,IAAI,uCAAe,UAAU,MAAA,CAAC;IAChC,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,6DAA6D;QAC7D,MAAM,uBAAA,IAAI,wFAAqB,MAAzB,IAAI,CAAuB,CAAC;QAElC,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CAQF;;AANC;;GAEG;AACH,KAAK;IACH,MAAM,uBAAA,IAAI,2CAAY,MAAhB,IAAI,CAAc,CAAC;AAC3B,CAAC","sourcesContent":["import type { ExecutionServiceArgs } from '../AbstractExecutionService';\nimport { ProxyExecutionService } from '../proxy/ProxyExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n getWebView: () => Promise<WebViewInterface>;\n};\n\nexport class WebViewExecutionService extends ProxyExecutionService {\n #getWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n getWebView,\n }: WebViewExecutionServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n stream: new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n getWebView,\n }),\n });\n this.#getWebView = getWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Ensure that the WebView has been loaded before we proceed.\n await this.#ensureWebViewLoaded();\n\n return super.initEnvStream(jobId);\n }\n\n /**\n * Ensure that the WebView has been loaded by awaiting the getWebView promise.\n */\n async #ensureWebViewLoaded() {\n await this.#getWebView();\n }\n}\n"]}
1
+ {"version":3,"file":"WebViewExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,wBAAwB,EAEzB,wCAAoC;AAErC,OAAO,EAAE,oBAAoB,EAAE,mCAA+B;AAO9D,MAAM,OAAO,uBAAwB,SAAQ,wBAA0C;IAKrF,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,GAAG,IAAI,EACqB;QAC5B,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAfI,yDAAe;QAEf,yDAAe;QActB,uBAAA,IAAI,0CAAkB,aAAa,MAAA,CAAC;QACpC,uBAAA,IAAI,0CAAkB,aAAa,MAAA,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,MAAM,OAAO,GAAG,MAAM,uBAAA,IAAI,8CAAe,MAAnB,IAAI,EAAgB,KAAK,CAAC,CAAC;QAEjD,MAAM,MAAM,GAAG,IAAI,oBAAoB,CAAC;YACtC,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,OAAO;SACR,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IACrC,CAAC;IAES,YAAY,CAAC,UAA8C;QACnE,uBAAA,IAAI,8CAAe,MAAnB,IAAI,EAAgB,UAAU,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;CACF","sourcesContent":["import type { TerminateJobArgs } from '../AbstractExecutionService';\nimport {\n AbstractExecutionService,\n type ExecutionServiceArgs,\n} from '../AbstractExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n createWebView: (jobId: string) => Promise<WebViewInterface>;\n removeWebView: (jobId: string) => void;\n};\n\nexport class WebViewExecutionService extends AbstractExecutionService<WebViewInterface> {\n readonly #createWebView;\n\n readonly #removeWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n createWebView,\n removeWebView,\n ...args\n }: WebViewExecutionServiceArgs) {\n super({\n ...args,\n messenger,\n setupSnapProvider,\n });\n this.#createWebView = createWebView;\n this.#removeWebView = removeWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n const webView = await this.#createWebView(jobId);\n\n const stream = new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n webView,\n });\n\n return { worker: webView, stream };\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<WebViewInterface>): void {\n this.#removeWebView(jobWrapper.id);\n }\n}\n"]}
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.WebViewMessageStream = void 0;
16
16
  const post_message_stream_1 = require("@metamask/post-message-stream");
17
17
  const utils_1 = require("@metamask/post-message-stream/dist/utils.js");
18
- const snaps_utils_1 = require("@metamask/snaps-utils");
19
18
  const utils_2 = require("@metamask/utils");
20
19
  /**
21
20
  * A special postMessage stream used to interface with a WebView.
@@ -28,9 +27,9 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
28
27
  * @param args.name - The name of the stream. Used to differentiate between
29
28
  * multiple streams sharing the same window object.
30
29
  * @param args.target - The name of the stream to exchange messages with.
31
- * @param args.getWebView - A asynchronous getter for the webview.
30
+ * @param args.webView - A reference to the WebView.
32
31
  */
33
- constructor({ name, target, getWebView }) {
32
+ constructor({ name, target, webView }) {
34
33
  super();
35
34
  _WebViewMessageStream_name.set(this, void 0);
36
35
  _WebViewMessageStream_target.set(this, void 0);
@@ -38,19 +37,11 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
38
37
  __classPrivateFieldSet(this, _WebViewMessageStream_name, name, "f");
39
38
  __classPrivateFieldSet(this, _WebViewMessageStream_target, target, "f");
40
39
  this._onMessage = this._onMessage.bind(this);
41
- // This is a bit atypical from other post-message streams.
42
- // We have to wait for the WebView to fully load before we can continue using the stream.
43
- getWebView()
44
- .then((webView) => {
45
- __classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
46
- // This method is already bound.
47
- // eslint-disable-next-line @typescript-eslint/unbound-method
48
- webView.registerMessageListener(this._onMessage);
49
- this._handshake();
50
- })
51
- .catch((error) => {
52
- (0, snaps_utils_1.logError)(error);
53
- });
40
+ __classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
41
+ // This method is already bound.
42
+ // eslint-disable-next-line @typescript-eslint/unbound-method
43
+ __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").registerMessageListener(this._onMessage);
44
+ this._handshake();
54
45
  }
55
46
  _postMessage(data) {
56
47
  (0, utils_2.assert)(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
@@ -58,12 +49,10 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
58
49
  target: __classPrivateFieldGet(this, _WebViewMessageStream_target, "f"),
59
50
  data,
60
51
  });
61
- // To prevent XSS, we base64 encode the message before injecting it.
62
- // This adds significant performance overhead.
63
- // TODO: Should we use mobile native base64 here?
64
- const bytes = (0, utils_2.stringToBytes)(json);
65
- const base64 = (0, utils_2.bytesToBase64)(bytes);
66
- __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage('${base64}')`);
52
+ // To prevent XSS, we encode the message before injecting it.
53
+ // This adds significant performance overhead for larger messages.
54
+ const bytes = new Uint8Array((0, utils_2.stringToBytes)(json));
55
+ __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage([${bytes.toString()}])`);
67
56
  }
68
57
  _onMessage(event) {
69
58
  if (typeof event.data !== 'string') {
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewMessageStream.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAAsE;AACtE,uEAAgF;AAChF,uDAAiD;AACjD,2CAAuE;AAcvE;;GAEG;AACH,MAAa,oBAAqB,SAAQ,2CAAqB;IAO7D;;;;;;;;OAQG;IACH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAqB;QACzD,KAAK,EAAE,CAAC;QAhBV,6CAAM;QAEN,+CAAQ;QAER,gDAAuC;QAcrC,uBAAA,IAAI,8BAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QAEtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE7C,0DAA0D;QAC1D,yFAAyF;QACzF,UAAU,EAAE;aACT,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAChB,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;YACxB,gCAAgC;YAChC,6DAA6D;YAC7D,OAAO,CAAC,uBAAuB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACjD,IAAI,CAAC,UAAU,EAAE,CAAC;QACpB,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,IAAA,sBAAQ,EAAC,KAAK,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACP,CAAC;IAES,YAAY,CAAC,IAAa;QAClC,IAAA,cAAM,EAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC;YAC1B,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,IAAI;SACL,CAAC,CAAC;QAEH,oEAAoE;QACpE,8CAA8C;QAC9C,iDAAiD;QACjD,MAAM,KAAK,GAAG,IAAA,qBAAa,EAAC,IAAI,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,IAAA,qBAAa,EAAC,KAAK,CAAC,CAAC;QACpC,uBAAA,IAAI,qCAAS,CAAC,gBAAgB,CAAC,uBAAuB,MAAM,IAAI,CAAC,CAAC;IACpE,CAAC;IAEO,UAAU,CAAC,KAAuB;QACxC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEvC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,IAAA,4BAAoB,EAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,uBAAA,IAAI,kCAAM,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,IAAA,cAAM,EAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,yBAAyB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC3D,CAAC;CACF;AA5ED,oDA4EC","sourcesContent":["import type { PostMessageEvent } from '@metamask/post-message-stream';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { isValidStreamMessage } from '@metamask/post-message-stream/dist/utils';\nimport { logError } from '@metamask/snaps-utils';\nimport { assert, bytesToBase64, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\nexport type WebViewStreamArgs = {\n name: string;\n target: string;\n getWebView: () => Promise<WebViewInterface>;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\n *\n * @param args - Options bag.\n * @param args.name - The name of the stream. Used to differentiate between\n * multiple streams sharing the same window object.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.getWebView - A asynchronous getter for the webview.\n */\n constructor({ name, target, getWebView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This is a bit atypical from other post-message streams.\n // We have to wait for the WebView to fully load before we can continue using the stream.\n getWebView()\n .then((webView) => {\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n webView.registerMessageListener(this._onMessage);\n this._handshake();\n })\n .catch((error) => {\n logError(error);\n });\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we base64 encode the message before injecting it.\n // This adds significant performance overhead.\n // TODO: Should we use mobile native base64 here?\n const bytes = stringToBytes(json);\n const base64 = bytesToBase64(bytes);\n this.#webView.injectJavaScript(`window.postMessage('${base64}')`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\n\n // Notice that we don't check targetWindow or targetOrigin here.\n // This doesn't seem possible to do in RN.\n if (!isValidStreamMessage(message) || message.target !== this.#name) {\n return;\n }\n\n this._onData(message.data);\n }\n\n _destroy() {\n assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"]}
1
+ {"version":3,"file":"WebViewMessageStream.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAAsE;AACtE,uEAAgF;AAChF,2CAAwD;AAcxD;;GAEG;AACH,MAAa,oBAAqB,SAAQ,2CAAqB;IAO7D;;;;;;;;OAQG;IACH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAqB;QACtD,KAAK,EAAE,CAAC;QAhBV,6CAAM;QAEN,+CAAQ;QAER,gDAAuC;QAcrC,uBAAA,IAAI,8BAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QAEtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE7C,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;QACxB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,uBAAuB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACvD,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IAES,YAAY,CAAC,IAAa;QAClC,IAAA,cAAM,EAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC;YAC1B,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,IAAI;SACL,CAAC,CAAC;QAEH,6DAA6D;QAC7D,kEAAkE;QAClE,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,IAAA,qBAAa,EAAC,IAAI,CAAC,CAAC,CAAC;QAElD,uBAAA,IAAI,qCAAS,CAAC,gBAAgB,CAAC,uBAAuB,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC9E,CAAC;IAEO,UAAU,CAAC,KAAuB;QACxC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEvC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,IAAA,4BAAoB,EAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,uBAAA,IAAI,kCAAM,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,IAAA,cAAM,EAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,yBAAyB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC3D,CAAC;CACF;AAnED,oDAmEC","sourcesContent":["import type { PostMessageEvent } from '@metamask/post-message-stream';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { isValidStreamMessage } from '@metamask/post-message-stream/dist/utils';\nimport { assert, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\nexport type WebViewStreamArgs = {\n name: string;\n target: string;\n webView: WebViewInterface;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\n *\n * @param args - Options bag.\n * @param args.name - The name of the stream. Used to differentiate between\n * multiple streams sharing the same window object.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.webView - A reference to the WebView.\n */\n constructor({ name, target, webView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.registerMessageListener(this._onMessage);\n this._handshake();\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we encode the message before injecting it.\n // This adds significant performance overhead for larger messages.\n const bytes = new Uint8Array(stringToBytes(json));\n\n this.#webView.injectJavaScript(`window.postMessage([${bytes.toString()}])`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\n\n // Notice that we don't check targetWindow or targetOrigin here.\n // This doesn't seem possible to do in RN.\n if (!isValidStreamMessage(message) || message.target !== this.#name) {\n return;\n }\n\n this._onData(message.data);\n }\n\n _destroy() {\n assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"]}
@@ -8,7 +8,7 @@ export type WebViewInterface = {
8
8
  export type WebViewStreamArgs = {
9
9
  name: string;
10
10
  target: string;
11
- getWebView: () => Promise<WebViewInterface>;
11
+ webView: WebViewInterface;
12
12
  };
13
13
  /**
14
14
  * A special postMessage stream used to interface with a WebView.
@@ -22,9 +22,9 @@ export declare class WebViewMessageStream extends BasePostMessageStream {
22
22
  * @param args.name - The name of the stream. Used to differentiate between
23
23
  * multiple streams sharing the same window object.
24
24
  * @param args.target - The name of the stream to exchange messages with.
25
- * @param args.getWebView - A asynchronous getter for the webview.
25
+ * @param args.webView - A reference to the WebView.
26
26
  */
27
- constructor({ name, target, getWebView }: WebViewStreamArgs);
27
+ constructor({ name, target, webView }: WebViewStreamArgs);
28
28
  protected _postMessage(data: unknown): void;
29
29
  private _onMessage;
30
30
  _destroy(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewMessageStream.d.cts","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,sCAAsC;AACtE,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AAKtE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,uBAAuB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;IAC3E,yBAAyB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,CAAC;CAC7C,CAAC;AAEF;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,qBAAqB;;IAO7D;;;;;;;;OAQG;gBACS,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,iBAAiB;IAuB3D,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAe3C,OAAO,CAAC,UAAU;IAgBlB,QAAQ;CAMT"}
1
+ {"version":3,"file":"WebViewMessageStream.d.cts","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,sCAAsC;AACtE,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AAItE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,uBAAuB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;IAC3E,yBAAyB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,qBAAqB;;IAO7D;;;;;;;;OAQG;gBACS,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,iBAAiB;IAexD,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAc3C,OAAO,CAAC,UAAU;IAgBlB,QAAQ;CAMT"}
@@ -8,7 +8,7 @@ export type WebViewInterface = {
8
8
  export type WebViewStreamArgs = {
9
9
  name: string;
10
10
  target: string;
11
- getWebView: () => Promise<WebViewInterface>;
11
+ webView: WebViewInterface;
12
12
  };
13
13
  /**
14
14
  * A special postMessage stream used to interface with a WebView.
@@ -22,9 +22,9 @@ export declare class WebViewMessageStream extends BasePostMessageStream {
22
22
  * @param args.name - The name of the stream. Used to differentiate between
23
23
  * multiple streams sharing the same window object.
24
24
  * @param args.target - The name of the stream to exchange messages with.
25
- * @param args.getWebView - A asynchronous getter for the webview.
25
+ * @param args.webView - A reference to the WebView.
26
26
  */
27
- constructor({ name, target, getWebView }: WebViewStreamArgs);
27
+ constructor({ name, target, webView }: WebViewStreamArgs);
28
28
  protected _postMessage(data: unknown): void;
29
29
  private _onMessage;
30
30
  _destroy(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewMessageStream.d.mts","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,sCAAsC;AACtE,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AAKtE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,uBAAuB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;IAC3E,yBAAyB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,CAAC;CAC7C,CAAC;AAEF;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,qBAAqB;;IAO7D;;;;;;;;OAQG;gBACS,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,iBAAiB;IAuB3D,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAe3C,OAAO,CAAC,UAAU;IAgBlB,QAAQ;CAMT"}
1
+ {"version":3,"file":"WebViewMessageStream.d.mts","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,sCAAsC;AACtE,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AAItE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,uBAAuB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;IAC3E,yBAAyB,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,qBAAqB;;IAO7D;;;;;;;;OAQG;gBACS,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,iBAAiB;IAexD,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAc3C,OAAO,CAAC,UAAU;IAgBlB,QAAQ;CAMT"}
@@ -12,8 +12,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
12
12
  var _WebViewMessageStream_name, _WebViewMessageStream_target, _WebViewMessageStream_webView;
13
13
  import { BasePostMessageStream } from "@metamask/post-message-stream";
14
14
  import { isValidStreamMessage } from "@metamask/post-message-stream/dist/utils.js";
15
- import { logError } from "@metamask/snaps-utils";
16
- import { assert, bytesToBase64, stringToBytes } from "@metamask/utils";
15
+ import { assert, stringToBytes } from "@metamask/utils";
17
16
  /**
18
17
  * A special postMessage stream used to interface with a WebView.
19
18
  */
@@ -25,9 +24,9 @@ export class WebViewMessageStream extends BasePostMessageStream {
25
24
  * @param args.name - The name of the stream. Used to differentiate between
26
25
  * multiple streams sharing the same window object.
27
26
  * @param args.target - The name of the stream to exchange messages with.
28
- * @param args.getWebView - A asynchronous getter for the webview.
27
+ * @param args.webView - A reference to the WebView.
29
28
  */
30
- constructor({ name, target, getWebView }) {
29
+ constructor({ name, target, webView }) {
31
30
  super();
32
31
  _WebViewMessageStream_name.set(this, void 0);
33
32
  _WebViewMessageStream_target.set(this, void 0);
@@ -35,19 +34,11 @@ export class WebViewMessageStream extends BasePostMessageStream {
35
34
  __classPrivateFieldSet(this, _WebViewMessageStream_name, name, "f");
36
35
  __classPrivateFieldSet(this, _WebViewMessageStream_target, target, "f");
37
36
  this._onMessage = this._onMessage.bind(this);
38
- // This is a bit atypical from other post-message streams.
39
- // We have to wait for the WebView to fully load before we can continue using the stream.
40
- getWebView()
41
- .then((webView) => {
42
- __classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
43
- // This method is already bound.
44
- // eslint-disable-next-line @typescript-eslint/unbound-method
45
- webView.registerMessageListener(this._onMessage);
46
- this._handshake();
47
- })
48
- .catch((error) => {
49
- logError(error);
50
- });
37
+ __classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
38
+ // This method is already bound.
39
+ // eslint-disable-next-line @typescript-eslint/unbound-method
40
+ __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").registerMessageListener(this._onMessage);
41
+ this._handshake();
51
42
  }
52
43
  _postMessage(data) {
53
44
  assert(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
@@ -55,12 +46,10 @@ export class WebViewMessageStream extends BasePostMessageStream {
55
46
  target: __classPrivateFieldGet(this, _WebViewMessageStream_target, "f"),
56
47
  data,
57
48
  });
58
- // To prevent XSS, we base64 encode the message before injecting it.
59
- // This adds significant performance overhead.
60
- // TODO: Should we use mobile native base64 here?
61
- const bytes = stringToBytes(json);
62
- const base64 = bytesToBase64(bytes);
63
- __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage('${base64}')`);
49
+ // To prevent XSS, we encode the message before injecting it.
50
+ // This adds significant performance overhead for larger messages.
51
+ const bytes = new Uint8Array(stringToBytes(json));
52
+ __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage([${bytes.toString()}])`);
64
53
  }
65
54
  _onMessage(event) {
66
55
  if (typeof event.data !== 'string') {
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewMessageStream.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AACtE,OAAO,EAAE,oBAAoB,EAAE,oDAAiD;AAChF,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AACjD,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,wBAAwB;AAcvE;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,qBAAqB;IAO7D;;;;;;;;OAQG;IACH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAqB;QACzD,KAAK,EAAE,CAAC;QAhBV,6CAAM;QAEN,+CAAQ;QAER,gDAAuC;QAcrC,uBAAA,IAAI,8BAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QAEtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE7C,0DAA0D;QAC1D,yFAAyF;QACzF,UAAU,EAAE;aACT,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAChB,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;YACxB,gCAAgC;YAChC,6DAA6D;YAC7D,OAAO,CAAC,uBAAuB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACjD,IAAI,CAAC,UAAU,EAAE,CAAC;QACpB,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,QAAQ,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACP,CAAC;IAES,YAAY,CAAC,IAAa;QAClC,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC;YAC1B,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,IAAI;SACL,CAAC,CAAC;QAEH,oEAAoE;QACpE,8CAA8C;QAC9C,iDAAiD;QACjD,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;QACpC,uBAAA,IAAI,qCAAS,CAAC,gBAAgB,CAAC,uBAAuB,MAAM,IAAI,CAAC,CAAC;IACpE,CAAC;IAEO,UAAU,CAAC,KAAuB;QACxC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEvC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,uBAAA,IAAI,kCAAM,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,yBAAyB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC3D,CAAC;CACF","sourcesContent":["import type { PostMessageEvent } from '@metamask/post-message-stream';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { isValidStreamMessage } from '@metamask/post-message-stream/dist/utils';\nimport { logError } from '@metamask/snaps-utils';\nimport { assert, bytesToBase64, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\nexport type WebViewStreamArgs = {\n name: string;\n target: string;\n getWebView: () => Promise<WebViewInterface>;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\n *\n * @param args - Options bag.\n * @param args.name - The name of the stream. Used to differentiate between\n * multiple streams sharing the same window object.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.getWebView - A asynchronous getter for the webview.\n */\n constructor({ name, target, getWebView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This is a bit atypical from other post-message streams.\n // We have to wait for the WebView to fully load before we can continue using the stream.\n getWebView()\n .then((webView) => {\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n webView.registerMessageListener(this._onMessage);\n this._handshake();\n })\n .catch((error) => {\n logError(error);\n });\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we base64 encode the message before injecting it.\n // This adds significant performance overhead.\n // TODO: Should we use mobile native base64 here?\n const bytes = stringToBytes(json);\n const base64 = bytesToBase64(bytes);\n this.#webView.injectJavaScript(`window.postMessage('${base64}')`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\n\n // Notice that we don't check targetWindow or targetOrigin here.\n // This doesn't seem possible to do in RN.\n if (!isValidStreamMessage(message) || message.target !== this.#name) {\n return;\n }\n\n this._onData(message.data);\n }\n\n _destroy() {\n assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"]}
1
+ {"version":3,"file":"WebViewMessageStream.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AACtE,OAAO,EAAE,oBAAoB,EAAE,oDAAiD;AAChF,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,wBAAwB;AAcxD;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,qBAAqB;IAO7D;;;;;;;;OAQG;IACH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAqB;QACtD,KAAK,EAAE,CAAC;QAhBV,6CAAM;QAEN,+CAAQ;QAER,gDAAuC;QAcrC,uBAAA,IAAI,8BAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QAEtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE7C,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;QACxB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,uBAAuB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACvD,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IAES,YAAY,CAAC,IAAa;QAClC,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC;YAC1B,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,IAAI;SACL,CAAC,CAAC;QAEH,6DAA6D;QAC7D,kEAAkE;QAClE,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC;QAElD,uBAAA,IAAI,qCAAS,CAAC,gBAAgB,CAAC,uBAAuB,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC9E,CAAC;IAEO,UAAU,CAAC,KAAuB;QACxC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEvC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,uBAAA,IAAI,kCAAM,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,yBAAyB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC3D,CAAC;CACF","sourcesContent":["import type { PostMessageEvent } from '@metamask/post-message-stream';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { isValidStreamMessage } from '@metamask/post-message-stream/dist/utils';\nimport { assert, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\nexport type WebViewStreamArgs = {\n name: string;\n target: string;\n webView: WebViewInterface;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\n *\n * @param args - Options bag.\n * @param args.name - The name of the stream. Used to differentiate between\n * multiple streams sharing the same window object.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.webView - A reference to the WebView.\n */\n constructor({ name, target, webView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.registerMessageListener(this._onMessage);\n this._handshake();\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we encode the message before injecting it.\n // This adds significant performance overhead for larger messages.\n const bytes = new Uint8Array(stringToBytes(json));\n\n this.#webView.injectJavaScript(`window.postMessage([${bytes.toString()}])`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\n\n // Notice that we don't check targetWindow or targetOrigin here.\n // This doesn't seem possible to do in RN.\n if (!isValidStreamMessage(message) || message.target !== this.#name) {\n return;\n }\n\n this._onData(message.data);\n }\n\n _destroy() {\n assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"]}
@@ -32,8 +32,9 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
32
32
  * @param args.setupSnapProvider - The function to use to set up the snap
33
33
  * provider.
34
34
  */
35
- constructor({ documentUrl, messenger, setupSnapProvider, }) {
35
+ constructor({ documentUrl, messenger, setupSnapProvider, ...args }) {
36
36
  super({
37
+ ...args,
37
38
  messenger,
38
39
  setupSnapProvider,
39
40
  });
@@ -1 +1 @@
1
- {"version":3,"file":"WebWorkerExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAAwE;AACxE,uDAAqD;AACrD,2CAAyC;AACzC,mCAAgC;AAMhC,8EAAuE;AACvE,0EAAmE;AAMtD,QAAA,cAAc,GAAG,mBAAmB,CAAC;AAElD,MAAa,yBAA0B,SAAQ,mDAAgC;IAK7E;;;;;;;;;;OAUG;IACH,YAAY,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,GACwB;QACzC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAvBL,yDAAkB;QAElB,2DAAuC;QAuBrC,uBAAA,IAAI,0CAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAA,cAAM,EAAC,uBAAA,IAAI,gDAAe,EAAE,iCAAiC,CAAC,CAAC;QAC/D,uBAAA,IAAI,gDAAe,CAAC,KAAK,CAAC;YACxB,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,KAAa;QACzC,0CAA0C;QAC1C,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;QAE5B,+DAA+D;QAC/D,IAAA,cAAM,EAAC,uBAAA,IAAI,gDAAe,EAAE,iCAAiC,CAAC,CAAC;QAE/D,MAAM,MAAM,GAAG,IAAI,+CAAsB,CAAC;YACxC,MAAM,EAAE,uBAAA,IAAI,gDAAe;YAC3B,KAAK;SACN,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,cAAc;QAC1B,wCAAwC;QACxC,IAAI,QAAQ,CAAC,cAAc,CAAC,sBAAc,CAAC,EAAE,CAAC;YAC5C,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,IAAA,0BAAY,EAAC;YAChC,GAAG,EAAE,uBAAA,IAAI,8CAAa,CAAC,IAAI;YAC3B,EAAE,EAAE,sBAAc;YAClB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,uBAAA,IAAI,4CAAkB,IAAI,6CAAuB,CAAC;YAChD,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,MAAM;YACpB,YAAY,EAAE,GAAG;SAClB,CAAC,MAAA,CAAC;IACL,CAAC;CACF;AA9FD,8DA8FC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype WebWorkerExecutionEnvironmentServiceArgs = {\n documentUrl: URL;\n} & ExecutionServiceArgs;\n\nexport const WORKER_POOL_ID = 'snaps-worker-pool';\n\nexport class WebWorkerExecutionService extends AbstractExecutionService<string> {\n #documentUrl: URL;\n\n #runtimeStream?: BasePostMessageStream;\n\n /**\n * Create a new webworker execution service.\n *\n * @param args - The constructor arguments.\n * @param args.documentUrl - The URL of the worker pool document to use as the\n * execution environment.\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 */\n constructor({\n documentUrl,\n messenger,\n setupSnapProvider,\n }: WebWorkerExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n\n this.#documentUrl = documentUrl;\n }\n\n /**\n * Send a termination command to the worker pool document.\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 assert(this.#runtimeStream, 'Runtime stream not initialized.');\n this.#runtimeStream.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 job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Lazily create the worker pool document.\n await this.createDocument();\n\n // `createDocument` should have initialized the runtime stream.\n assert(this.#runtimeStream, 'Runtime stream not initialized.');\n\n const stream = new ProxyPostMessageStream({\n stream: this.#runtimeStream,\n jobId,\n });\n\n return { worker: jobId, stream };\n }\n\n /**\n * Creates the worker pool document to be used as the execution environment.\n *\n * If the document already exists, this does nothing.\n */\n private async createDocument() {\n // We only want to create a single pool.\n if (document.getElementById(WORKER_POOL_ID)) {\n return;\n }\n\n const window = await createWindow({\n uri: this.#documentUrl.href,\n id: WORKER_POOL_ID,\n sandbox: false,\n });\n\n this.#runtimeStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window,\n targetOrigin: '*',\n });\n }\n}\n"]}
1
+ {"version":3,"file":"WebWorkerExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAAwE;AACxE,uDAAqD;AACrD,2CAAyC;AACzC,mCAAgC;AAMhC,8EAAuE;AACvE,0EAAmE;AAMtD,QAAA,cAAc,GAAG,mBAAmB,CAAC;AAElD,MAAa,yBAA0B,SAAQ,mDAAgC;IAK7E;;;;;;;;;;OAUG;IACH,YAAY,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACkC;QACzC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAzBL,yDAAkB;QAElB,2DAAuC;QAyBrC,uBAAA,IAAI,0CAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAA,cAAM,EAAC,uBAAA,IAAI,gDAAe,EAAE,iCAAiC,CAAC,CAAC;QAC/D,uBAAA,IAAI,gDAAe,CAAC,KAAK,CAAC;YACxB,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,KAAa;QACzC,0CAA0C;QAC1C,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;QAE5B,+DAA+D;QAC/D,IAAA,cAAM,EAAC,uBAAA,IAAI,gDAAe,EAAE,iCAAiC,CAAC,CAAC;QAE/D,MAAM,MAAM,GAAG,IAAI,+CAAsB,CAAC;YACxC,MAAM,EAAE,uBAAA,IAAI,gDAAe;YAC3B,KAAK;SACN,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,cAAc;QAC1B,wCAAwC;QACxC,IAAI,QAAQ,CAAC,cAAc,CAAC,sBAAc,CAAC,EAAE,CAAC;YAC5C,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,IAAA,0BAAY,EAAC;YAChC,GAAG,EAAE,uBAAA,IAAI,8CAAa,CAAC,IAAI;YAC3B,EAAE,EAAE,sBAAc;YAClB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,uBAAA,IAAI,4CAAkB,IAAI,6CAAuB,CAAC;YAChD,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,MAAM;YACpB,YAAY,EAAE,GAAG;SAClB,CAAC,MAAA,CAAC;IACL,CAAC;CACF;AAhGD,8DAgGC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype WebWorkerExecutionEnvironmentServiceArgs = {\n documentUrl: URL;\n} & ExecutionServiceArgs;\n\nexport const WORKER_POOL_ID = 'snaps-worker-pool';\n\nexport class WebWorkerExecutionService extends AbstractExecutionService<string> {\n #documentUrl: URL;\n\n #runtimeStream?: BasePostMessageStream;\n\n /**\n * Create a new webworker execution service.\n *\n * @param args - The constructor arguments.\n * @param args.documentUrl - The URL of the worker pool document to use as the\n * execution environment.\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 */\n constructor({\n documentUrl,\n messenger,\n setupSnapProvider,\n ...args\n }: WebWorkerExecutionEnvironmentServiceArgs) {\n super({\n ...args,\n messenger,\n setupSnapProvider,\n });\n\n this.#documentUrl = documentUrl;\n }\n\n /**\n * Send a termination command to the worker pool document.\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 assert(this.#runtimeStream, 'Runtime stream not initialized.');\n this.#runtimeStream.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 job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Lazily create the worker pool document.\n await this.createDocument();\n\n // `createDocument` should have initialized the runtime stream.\n assert(this.#runtimeStream, 'Runtime stream not initialized.');\n\n const stream = new ProxyPostMessageStream({\n stream: this.#runtimeStream,\n jobId,\n });\n\n return { worker: jobId, stream };\n }\n\n /**\n * Creates the worker pool document to be used as the execution environment.\n *\n * If the document already exists, this does nothing.\n */\n private async createDocument() {\n // We only want to create a single pool.\n if (document.getElementById(WORKER_POOL_ID)) {\n return;\n }\n\n const window = await createWindow({\n uri: this.#documentUrl.href,\n id: WORKER_POOL_ID,\n sandbox: false,\n });\n\n this.#runtimeStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window,\n targetOrigin: '*',\n });\n }\n}\n"]}
@@ -18,7 +18,7 @@ export declare class WebWorkerExecutionService extends AbstractExecutionService<
18
18
  * @param args.setupSnapProvider - The function to use to set up the snap
19
19
  * provider.
20
20
  */
21
- constructor({ documentUrl, messenger, setupSnapProvider, }: WebWorkerExecutionEnvironmentServiceArgs);
21
+ constructor({ documentUrl, messenger, setupSnapProvider, ...args }: WebWorkerExecutionEnvironmentServiceArgs);
22
22
  /**
23
23
  * Send a termination command to the worker pool document.
24
24
  *
@@ -1 +1 @@
1
- {"version":3,"file":"WebWorkerExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,wCAAwC,GAAG;IAC9C,WAAW,EAAE,GAAG,CAAC;CAClB,GAAG,oBAAoB,CAAC;AAEzB,eAAO,MAAM,cAAc,sBAAsB,CAAC;AAElD,qBAAa,yBAA0B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAK7E;;;;;;;;;;OAUG;gBACS,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,GAClB,EAAE,wCAAwC;IAS3C;;;;OAIG;cACa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAc1D;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;IAe3C;;;;OAIG;YACW,cAAc;CAmB7B"}
1
+ {"version":3,"file":"WebWorkerExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,wCAAwC,GAAG;IAC9C,WAAW,EAAE,GAAG,CAAC;CAClB,GAAG,oBAAoB,CAAC;AAEzB,eAAO,MAAM,cAAc,sBAAsB,CAAC;AAElD,qBAAa,yBAA0B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAK7E;;;;;;;;;;OAUG;gBACS,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,wCAAwC;IAU3C;;;;OAIG;cACa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAc1D;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;IAe3C;;;;OAIG;YACW,cAAc;CAmB7B"}
@@ -18,7 +18,7 @@ export declare class WebWorkerExecutionService extends AbstractExecutionService<
18
18
  * @param args.setupSnapProvider - The function to use to set up the snap
19
19
  * provider.
20
20
  */
21
- constructor({ documentUrl, messenger, setupSnapProvider, }: WebWorkerExecutionEnvironmentServiceArgs);
21
+ constructor({ documentUrl, messenger, setupSnapProvider, ...args }: WebWorkerExecutionEnvironmentServiceArgs);
22
22
  /**
23
23
  * Send a termination command to the worker pool document.
24
24
  *
@@ -1 +1 @@
1
- {"version":3,"file":"WebWorkerExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,wCAAwC,GAAG;IAC9C,WAAW,EAAE,GAAG,CAAC;CAClB,GAAG,oBAAoB,CAAC;AAEzB,eAAO,MAAM,cAAc,sBAAsB,CAAC;AAElD,qBAAa,yBAA0B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAK7E;;;;;;;;;;OAUG;gBACS,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,GAClB,EAAE,wCAAwC;IAS3C;;;;OAIG;cACa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAc1D;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;IAe3C;;;;OAIG;YACW,cAAc;CAmB7B"}
1
+ {"version":3,"file":"WebWorkerExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,wCAAwC,GAAG;IAC9C,WAAW,EAAE,GAAG,CAAC;CAClB,GAAG,oBAAoB,CAAC;AAEzB,eAAO,MAAM,cAAc,sBAAsB,CAAC;AAElD,qBAAa,yBAA0B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAK7E;;;;;;;;;;OAUG;gBACS,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,wCAAwC;IAU3C;;;;OAIG;cACa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAc1D;;;;;;OAMG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM;;;;IAe3C;;;;OAIG;YACW,cAAc;CAmB7B"}
@@ -29,8 +29,9 @@ export class WebWorkerExecutionService extends AbstractExecutionService {
29
29
  * @param args.setupSnapProvider - The function to use to set up the snap
30
30
  * provider.
31
31
  */
32
- constructor({ documentUrl, messenger, setupSnapProvider, }) {
32
+ constructor({ documentUrl, messenger, setupSnapProvider, ...args }) {
33
33
  super({
34
+ ...args,
34
35
  messenger,
35
36
  setupSnapProvider,
36
37
  });