@metamask/snaps-controllers 9.9.0 → 9.10.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 (82) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/cronjob/CronjobController.cjs +34 -21
  3. package/dist/cronjob/CronjobController.cjs.map +1 -1
  4. package/dist/cronjob/CronjobController.mjs +34 -21
  5. package/dist/cronjob/CronjobController.mjs.map +1 -1
  6. package/dist/insights/SnapInsightsController.cjs +144 -194
  7. package/dist/insights/SnapInsightsController.cjs.map +1 -1
  8. package/dist/insights/SnapInsightsController.mjs +143 -193
  9. package/dist/insights/SnapInsightsController.mjs.map +1 -1
  10. package/dist/interface/SnapInterfaceController.cjs +65 -90
  11. package/dist/interface/SnapInterfaceController.cjs.map +1 -1
  12. package/dist/interface/SnapInterfaceController.mjs +65 -90
  13. package/dist/interface/SnapInterfaceController.mjs.map +1 -1
  14. package/dist/interface/utils.cjs +6 -2
  15. package/dist/interface/utils.cjs.map +1 -1
  16. package/dist/interface/utils.d.cts.map +1 -1
  17. package/dist/interface/utils.d.mts.map +1 -1
  18. package/dist/interface/utils.mjs +6 -2
  19. package/dist/interface/utils.mjs.map +1 -1
  20. package/dist/services/AbstractExecutionService.cjs +77 -71
  21. package/dist/services/AbstractExecutionService.cjs.map +1 -1
  22. package/dist/services/AbstractExecutionService.mjs +77 -71
  23. package/dist/services/AbstractExecutionService.mjs.map +1 -1
  24. package/dist/services/ProxyPostMessageStream.cjs +26 -19
  25. package/dist/services/ProxyPostMessageStream.cjs.map +1 -1
  26. package/dist/services/ProxyPostMessageStream.mjs +26 -19
  27. package/dist/services/ProxyPostMessageStream.mjs.map +1 -1
  28. package/dist/services/iframe/IframeExecutionService.cjs +4 -2
  29. package/dist/services/iframe/IframeExecutionService.cjs.map +1 -1
  30. package/dist/services/iframe/IframeExecutionService.d.cts.map +1 -1
  31. package/dist/services/iframe/IframeExecutionService.d.mts.map +1 -1
  32. package/dist/services/iframe/IframeExecutionService.mjs +4 -2
  33. package/dist/services/iframe/IframeExecutionService.mjs.map +1 -1
  34. package/dist/services/offscreen/OffscreenExecutionService.cjs +16 -3
  35. package/dist/services/offscreen/OffscreenExecutionService.cjs.map +1 -1
  36. package/dist/services/offscreen/OffscreenExecutionService.mjs +16 -3
  37. package/dist/services/offscreen/OffscreenExecutionService.mjs.map +1 -1
  38. package/dist/services/proxy/ProxyExecutionService.cjs +17 -4
  39. package/dist/services/proxy/ProxyExecutionService.cjs.map +1 -1
  40. package/dist/services/proxy/ProxyExecutionService.mjs +17 -4
  41. package/dist/services/proxy/ProxyExecutionService.mjs.map +1 -1
  42. package/dist/services/webview/WebViewExecutionService.cjs +23 -9
  43. package/dist/services/webview/WebViewExecutionService.cjs.map +1 -1
  44. package/dist/services/webview/WebViewExecutionService.mjs +23 -9
  45. package/dist/services/webview/WebViewExecutionService.mjs.map +1 -1
  46. package/dist/services/webview/WebViewMessageStream.cjs +25 -12
  47. package/dist/services/webview/WebViewMessageStream.cjs.map +1 -1
  48. package/dist/services/webview/WebViewMessageStream.mjs +25 -12
  49. package/dist/services/webview/WebViewMessageStream.mjs.map +1 -1
  50. package/dist/services/webworker/WebWorkerExecutionService.cjs +27 -10
  51. package/dist/services/webworker/WebWorkerExecutionService.cjs.map +1 -1
  52. package/dist/services/webworker/WebWorkerExecutionService.mjs +27 -10
  53. package/dist/services/webworker/WebWorkerExecutionService.mjs.map +1 -1
  54. package/dist/snaps/RequestQueue.cjs +0 -2
  55. package/dist/snaps/RequestQueue.cjs.map +1 -1
  56. package/dist/snaps/RequestQueue.mjs +0 -2
  57. package/dist/snaps/RequestQueue.mjs.map +1 -1
  58. package/dist/snaps/SnapController.cjs +1001 -1141
  59. package/dist/snaps/SnapController.cjs.map +1 -1
  60. package/dist/snaps/SnapController.mjs +1000 -1140
  61. package/dist/snaps/SnapController.mjs.map +1 -1
  62. package/dist/snaps/Timer.cjs +0 -1
  63. package/dist/snaps/Timer.cjs.map +1 -1
  64. package/dist/snaps/Timer.mjs +0 -1
  65. package/dist/snaps/Timer.mjs.map +1 -1
  66. package/dist/snaps/location/http.cjs +7 -11
  67. package/dist/snaps/location/http.cjs.map +1 -1
  68. package/dist/snaps/location/http.mjs +7 -11
  69. package/dist/snaps/location/http.mjs.map +1 -1
  70. package/dist/snaps/location/local.cjs +17 -4
  71. package/dist/snaps/location/local.cjs.map +1 -1
  72. package/dist/snaps/location/local.mjs +17 -4
  73. package/dist/snaps/location/local.mjs.map +1 -1
  74. package/dist/snaps/location/npm.cjs +37 -25
  75. package/dist/snaps/location/npm.cjs.map +1 -1
  76. package/dist/snaps/location/npm.mjs +37 -25
  77. package/dist/snaps/location/npm.mjs.map +1 -1
  78. package/dist/snaps/registry/json.cjs +173 -172
  79. package/dist/snaps/registry/json.cjs.map +1 -1
  80. package/dist/snaps/registry/json.mjs +172 -171
  81. package/dist/snaps/registry/json.mjs.map +1 -1
  82. package/package.json +5 -5
@@ -1,3 +1,15 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _WebViewMessageStream_name, _WebViewMessageStream_target, _WebViewMessageStream_webView;
1
13
  import $metamaskpostmessagestream from "@metamask/post-message-stream";
2
14
  const { BasePostMessageStream } = $metamaskpostmessagestream;
3
15
  import { isValidStreamMessage } from "@metamask/post-message-stream/dist/utils.js";
@@ -7,9 +19,6 @@ import { assert, bytesToBase64, stringToBytes } from "@metamask/utils";
7
19
  * A special postMessage stream used to interface with a WebView.
8
20
  */
9
21
  export class WebViewMessageStream extends BasePostMessageStream {
10
- #name;
11
- #target;
12
- #webView;
13
22
  /**
14
23
  * Creates a stream for communicating with other streams inside a WebView.
15
24
  *
@@ -21,14 +30,17 @@ export class WebViewMessageStream extends BasePostMessageStream {
21
30
  */
22
31
  constructor({ name, target, getWebView }) {
23
32
  super();
24
- this.#name = name;
25
- this.#target = target;
33
+ _WebViewMessageStream_name.set(this, void 0);
34
+ _WebViewMessageStream_target.set(this, void 0);
35
+ _WebViewMessageStream_webView.set(this, void 0);
36
+ __classPrivateFieldSet(this, _WebViewMessageStream_name, name, "f");
37
+ __classPrivateFieldSet(this, _WebViewMessageStream_target, target, "f");
26
38
  this._onMessage = this._onMessage.bind(this);
27
39
  // This is a bit atypical from other post-message streams.
28
40
  // We have to wait for the WebView to fully load before we can continue using the stream.
29
41
  getWebView()
30
42
  .then((webView) => {
31
- this.#webView = webView;
43
+ __classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
32
44
  // This method is already bound.
33
45
  // eslint-disable-next-line @typescript-eslint/unbound-method
34
46
  webView.registerMessageListener(this._onMessage);
@@ -39,9 +51,9 @@ export class WebViewMessageStream extends BasePostMessageStream {
39
51
  });
40
52
  }
41
53
  _postMessage(data) {
42
- assert(this.#webView);
54
+ assert(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
43
55
  const json = JSON.stringify({
44
- target: this.#target,
56
+ target: __classPrivateFieldGet(this, _WebViewMessageStream_target, "f"),
45
57
  data,
46
58
  });
47
59
  // To prevent XSS, we base64 encode the message before injecting it.
@@ -49,7 +61,7 @@ export class WebViewMessageStream extends BasePostMessageStream {
49
61
  // TODO: Should we use mobile native base64 here?
50
62
  const bytes = stringToBytes(json);
51
63
  const base64 = bytesToBase64(bytes);
52
- this.#webView.injectJavaScript(`window.postMessage('${base64}')`);
64
+ __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage('${base64}')`);
53
65
  }
54
66
  _onMessage(event) {
55
67
  if (typeof event.data !== 'string') {
@@ -58,16 +70,17 @@ export class WebViewMessageStream extends BasePostMessageStream {
58
70
  const message = JSON.parse(event.data);
59
71
  // Notice that we don't check targetWindow or targetOrigin here.
60
72
  // This doesn't seem possible to do in RN.
61
- if (!isValidStreamMessage(message) || message.target !== this.#name) {
73
+ if (!isValidStreamMessage(message) || message.target !== __classPrivateFieldGet(this, _WebViewMessageStream_name, "f")) {
62
74
  return;
63
75
  }
64
76
  this._onData(message.data);
65
77
  }
66
78
  _destroy() {
67
- assert(this.#webView);
79
+ assert(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
68
80
  // This method is already bound.
69
81
  // eslint-disable-next-line @typescript-eslint/unbound-method
70
- this.#webView.unregisterMessageListener(this._onMessage);
82
+ __classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").unregisterMessageListener(this._onMessage);
71
83
  }
72
84
  }
85
+ _WebViewMessageStream_name = new WeakMap(), _WebViewMessageStream_target = new WeakMap(), _WebViewMessageStream_webView = new WeakMap();
73
86
  //# sourceMappingURL=WebViewMessageStream.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewMessageStream.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":";;AAEA,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;IAC7D,KAAK,CAAC;IAEN,OAAO,CAAC;IAER,QAAQ,CAA+B;IA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KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEvC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACtB,gCAAgC;QAChC,6DAA6D;QAC7D,IAAI,CAAC,QAAQ,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":";;;;;;;;;;;;;;AAEA,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;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,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,4 +1,16 @@
1
1
  "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _WebWorkerExecutionService_documentUrl, _WebWorkerExecutionService_runtimeStream;
2
14
  Object.defineProperty(exports, "__esModule", { value: true });
3
15
  exports.WebWorkerExecutionService = exports.WORKER_POOL_ID = void 0;
4
16
  const post_message_stream_1 = require("@metamask/post-message-stream");
@@ -9,8 +21,6 @@ const AbstractExecutionService_1 = require("../AbstractExecutionService.cjs");
9
21
  const ProxyPostMessageStream_1 = require("../ProxyPostMessageStream.cjs");
10
22
  exports.WORKER_POOL_ID = 'snaps-worker-pool';
11
23
  class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
12
- #documentUrl;
13
- #runtimeStream;
14
24
  /**
15
25
  * Create a new webworker execution service.
16
26
  *
@@ -27,7 +37,9 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
27
37
  messenger,
28
38
  setupSnapProvider,
29
39
  });
30
- this.#documentUrl = documentUrl;
40
+ _WebWorkerExecutionService_documentUrl.set(this, void 0);
41
+ _WebWorkerExecutionService_runtimeStream.set(this, void 0);
42
+ __classPrivateFieldSet(this, _WebWorkerExecutionService_documentUrl, documentUrl, "f");
31
43
  }
32
44
  /**
33
45
  * Send a termination command to the worker pool document.
@@ -37,8 +49,8 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
37
49
  async terminateJob(job) {
38
50
  // The `AbstractExecutionService` will have already closed the job stream,
39
51
  // so we write to the runtime stream directly.
40
- (0, utils_1.assert)(this.#runtimeStream, 'Runtime stream not initialized.');
41
- this.#runtimeStream.write({
52
+ (0, utils_1.assert)(__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"), 'Runtime stream not initialized.');
53
+ __classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f").write({
42
54
  jobId: job.id,
43
55
  data: {
44
56
  jsonrpc: '2.0',
@@ -58,9 +70,9 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
58
70
  // Lazily create the worker pool document.
59
71
  await this.createDocument();
60
72
  // `createDocument` should have initialized the runtime stream.
61
- (0, utils_1.assert)(this.#runtimeStream, 'Runtime stream not initialized.');
73
+ (0, utils_1.assert)(__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"), 'Runtime stream not initialized.');
62
74
  const stream = new ProxyPostMessageStream_1.ProxyPostMessageStream({
63
- stream: this.#runtimeStream,
75
+ stream: __classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"),
64
76
  jobId,
65
77
  });
66
78
  return { worker: jobId, stream };
@@ -75,14 +87,19 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
75
87
  if (document.getElementById(exports.WORKER_POOL_ID)) {
76
88
  return;
77
89
  }
78
- const window = await (0, snaps_utils_1.createWindow)(this.#documentUrl.href, exports.WORKER_POOL_ID, false);
79
- this.#runtimeStream = new post_message_stream_1.WindowPostMessageStream({
90
+ const window = await (0, snaps_utils_1.createWindow)({
91
+ uri: __classPrivateFieldGet(this, _WebWorkerExecutionService_documentUrl, "f").href,
92
+ id: exports.WORKER_POOL_ID,
93
+ sandbox: false,
94
+ });
95
+ __classPrivateFieldSet(this, _WebWorkerExecutionService_runtimeStream, new post_message_stream_1.WindowPostMessageStream({
80
96
  name: 'parent',
81
97
  target: 'child',
82
98
  targetWindow: window,
83
99
  targetOrigin: '*',
84
- });
100
+ }), "f");
85
101
  }
86
102
  }
87
103
  exports.WebWorkerExecutionService = WebWorkerExecutionService;
104
+ _WebWorkerExecutionService_documentUrl = new WeakMap(), _WebWorkerExecutionService_runtimeStream = new WeakMap();
88
105
  //# sourceMappingURL=WebWorkerExecutionService.cjs.map
@@ -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;IAC7E,YAAY,CAAM;IAElB,cAAc,CAAyB;IAEvC;;;;;;;;;;OAUG;IACH,YAAY,EACV,WAAW,EACX,SA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AO;YACf,YAAY,EAAE,MAAM;YACpB,YAAY,EAAE,GAAG;SAClB,CAAC,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 this.#documentUrl.href,\n WORKER_POOL_ID,\n 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,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,3 +1,15 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _WebWorkerExecutionService_documentUrl, _WebWorkerExecutionService_runtimeStream;
1
13
  import $metamaskpostmessagestream from "@metamask/post-message-stream";
2
14
  const { WindowPostMessageStream } = $metamaskpostmessagestream;
3
15
  import { createWindow } from "@metamask/snaps-utils";
@@ -7,8 +19,6 @@ import { AbstractExecutionService } from "../AbstractExecutionService.mjs";
7
19
  import { ProxyPostMessageStream } from "../ProxyPostMessageStream.mjs";
8
20
  export const WORKER_POOL_ID = 'snaps-worker-pool';
9
21
  export class WebWorkerExecutionService extends AbstractExecutionService {
10
- #documentUrl;
11
- #runtimeStream;
12
22
  /**
13
23
  * Create a new webworker execution service.
14
24
  *
@@ -25,7 +35,9 @@ export class WebWorkerExecutionService extends AbstractExecutionService {
25
35
  messenger,
26
36
  setupSnapProvider,
27
37
  });
28
- this.#documentUrl = documentUrl;
38
+ _WebWorkerExecutionService_documentUrl.set(this, void 0);
39
+ _WebWorkerExecutionService_runtimeStream.set(this, void 0);
40
+ __classPrivateFieldSet(this, _WebWorkerExecutionService_documentUrl, documentUrl, "f");
29
41
  }
30
42
  /**
31
43
  * Send a termination command to the worker pool document.
@@ -35,8 +47,8 @@ export class WebWorkerExecutionService extends AbstractExecutionService {
35
47
  async terminateJob(job) {
36
48
  // The `AbstractExecutionService` will have already closed the job stream,
37
49
  // so we write to the runtime stream directly.
38
- assert(this.#runtimeStream, 'Runtime stream not initialized.');
39
- this.#runtimeStream.write({
50
+ assert(__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"), 'Runtime stream not initialized.');
51
+ __classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f").write({
40
52
  jobId: job.id,
41
53
  data: {
42
54
  jsonrpc: '2.0',
@@ -56,9 +68,9 @@ export class WebWorkerExecutionService extends AbstractExecutionService {
56
68
  // Lazily create the worker pool document.
57
69
  await this.createDocument();
58
70
  // `createDocument` should have initialized the runtime stream.
59
- assert(this.#runtimeStream, 'Runtime stream not initialized.');
71
+ assert(__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"), 'Runtime stream not initialized.');
60
72
  const stream = new ProxyPostMessageStream({
61
- stream: this.#runtimeStream,
73
+ stream: __classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"),
62
74
  jobId,
63
75
  });
64
76
  return { worker: jobId, stream };
@@ -73,13 +85,18 @@ export class WebWorkerExecutionService extends AbstractExecutionService {
73
85
  if (document.getElementById(WORKER_POOL_ID)) {
74
86
  return;
75
87
  }
76
- const window = await createWindow(this.#documentUrl.href, WORKER_POOL_ID, false);
77
- this.#runtimeStream = new WindowPostMessageStream({
88
+ const window = await createWindow({
89
+ uri: __classPrivateFieldGet(this, _WebWorkerExecutionService_documentUrl, "f").href,
90
+ id: WORKER_POOL_ID,
91
+ sandbox: false,
92
+ });
93
+ __classPrivateFieldSet(this, _WebWorkerExecutionService_runtimeStream, new WindowPostMessageStream({
78
94
  name: 'parent',
79
95
  target: 'child',
80
96
  targetWindow: window,
81
97
  targetOrigin: '*',
82
- });
98
+ }), "f");
83
99
  }
84
100
  }
101
+ _WebWorkerExecutionService_documentUrl = new WeakMap(), _WebWorkerExecutionService_runtimeStream = new WeakMap();
85
102
  //# sourceMappingURL=WebWorkerExecutionService.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebWorkerExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":";;AAEA,OAAO,EAAE,YAAY,EAAE,8BAA8B;AACrD,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,MAAM,EAAE,eAAe;AAMhC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAMnE,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAElD,MAAM,OAAO,yBAA0B,SAAQ,wBAAgC;IAC7E,YAAY,CAAM;IAElB,cAAc,CAAyB;IAEvC;;;;;;;;;;OAUG;IACH,YAAY,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,GACwB;QACzC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAEH,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,iCAAiC,CAAC,CAAC;QAC/D,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC;YACxB,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,0CAA0C;QAC1C,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;QAE5B,+DAA+D;QAC/D,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,iCAAiC,CAAC,CAAC;QAE/D,MAAM,MAAM,GAAG,IAAI,sBAAsB,CAAC;YACxC,MAAM,EAAE,IAAI,CAAC,cAAc;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,cAAc,CAAC,EAAE,CAAC;YAC5C,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,YAAY,CAC/B,IAAI,CAAC,YAAY,CAAC,IAAI,EACtB,cAAc,EACd,KAAK,CACN,CAAC;QAEF,IAAI,CAAC,cAAc,GAAG,IAAI,uBAAuB,CAAC;YAChD,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,MAAM;YACpB,YAAY,EAAE,GAAG;SAClB,CAAC,CAAC;IACL,CAAC;CACF","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 this.#documentUrl.href,\n WORKER_POOL_ID,\n 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.mjs","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"m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KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,cAAc;QAC1B,wCAAwC;QACxC,IAAI,QAAQ,CAAC,cAAc,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5C,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC;YAChC,GAAG,EAAE,uBAAA,IAAI,8CAAa,CAAC,IAAI;YAC3B,EAAE,EAAE,cAAc;YAClB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,uBAAA,IAAI,4CAAkB,IAAI,uBAAuB,CAAC;YAChD,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,MAAM;YACpB,YAAY,EAAE,GAAG;SAClB,CAAC,MAAA,CAAC;IACL,CAAC;CACF","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"]}
@@ -2,8 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RequestQueue = void 0;
4
4
  class RequestQueue {
5
- maxQueueSize;
6
- queueSizes;
7
5
  constructor(maxQueueSize) {
8
6
  this.maxQueueSize = maxQueueSize;
9
7
  this.queueSizes = new Map();
@@ -1 +1 @@
1
- {"version":3,"file":"RequestQueue.cjs","sourceRoot":"","sources":["../../src/snaps/RequestQueue.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IACP,YAAY,CAAS;IAEpB,UAAU,CAAsB;IAEjD,YAAY,YAAoB;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CACb,qBAAqB,MAAM,0CAA0C,CACtE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,MAAc;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;CACF;AA/CD,oCA+CC","sourcesContent":["export class RequestQueue {\n public readonly maxQueueSize: number;\n\n private readonly queueSizes: Map<string, number>;\n\n constructor(maxQueueSize: number) {\n this.maxQueueSize = maxQueueSize;\n this.queueSizes = new Map<string, number>();\n }\n\n /**\n * Increments the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public increment(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount >= this.maxQueueSize) {\n throw new Error('Maximum number of requests reached. Try again later.');\n }\n this.queueSizes.set(origin, currentCount + 1);\n }\n\n /**\n * Decrements the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public decrement(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount === 0) {\n throw new Error(\n `Cannot decrement, ${origin} does not have any outstanding requests.`,\n );\n }\n this.queueSizes.set(origin, currentCount - 1);\n }\n\n /**\n * Gets the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n * @returns The queue count for the origin.\n */\n public get(origin: string): number {\n return this.queueSizes.get(origin) ?? 0;\n }\n}\n"]}
1
+ {"version":3,"file":"RequestQueue.cjs","sourceRoot":"","sources":["../../src/snaps/RequestQueue.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IAKvB,YAAY,YAAoB;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CACb,qBAAqB,MAAM,0CAA0C,CACtE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,MAAc;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;CACF;AA/CD,oCA+CC","sourcesContent":["export class RequestQueue {\n public readonly maxQueueSize: number;\n\n private readonly queueSizes: Map<string, number>;\n\n constructor(maxQueueSize: number) {\n this.maxQueueSize = maxQueueSize;\n this.queueSizes = new Map<string, number>();\n }\n\n /**\n * Increments the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public increment(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount >= this.maxQueueSize) {\n throw new Error('Maximum number of requests reached. Try again later.');\n }\n this.queueSizes.set(origin, currentCount + 1);\n }\n\n /**\n * Decrements the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public decrement(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount === 0) {\n throw new Error(\n `Cannot decrement, ${origin} does not have any outstanding requests.`,\n );\n }\n this.queueSizes.set(origin, currentCount - 1);\n }\n\n /**\n * Gets the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n * @returns The queue count for the origin.\n */\n public get(origin: string): number {\n return this.queueSizes.get(origin) ?? 0;\n }\n}\n"]}
@@ -1,6 +1,4 @@
1
1
  export class RequestQueue {
2
- maxQueueSize;
3
- queueSizes;
4
2
  constructor(maxQueueSize) {
5
3
  this.maxQueueSize = maxQueueSize;
6
4
  this.queueSizes = new Map();
@@ -1 +1 @@
1
- {"version":3,"file":"RequestQueue.mjs","sourceRoot":"","sources":["../../src/snaps/RequestQueue.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAY;IACP,YAAY,CAAS;IAEpB,UAAU,CAAsB;IAEjD,YAAY,YAAoB;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CACb,qBAAqB,MAAM,0CAA0C,CACtE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,MAAc;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;CACF","sourcesContent":["export class RequestQueue {\n public readonly maxQueueSize: number;\n\n private readonly queueSizes: Map<string, number>;\n\n constructor(maxQueueSize: number) {\n this.maxQueueSize = maxQueueSize;\n this.queueSizes = new Map<string, number>();\n }\n\n /**\n * Increments the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public increment(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount >= this.maxQueueSize) {\n throw new Error('Maximum number of requests reached. Try again later.');\n }\n this.queueSizes.set(origin, currentCount + 1);\n }\n\n /**\n * Decrements the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public decrement(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount === 0) {\n throw new Error(\n `Cannot decrement, ${origin} does not have any outstanding requests.`,\n );\n }\n this.queueSizes.set(origin, currentCount - 1);\n }\n\n /**\n * Gets the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n * @returns The queue count for the origin.\n */\n public get(origin: string): number {\n return this.queueSizes.get(origin) ?? 0;\n }\n}\n"]}
1
+ {"version":3,"file":"RequestQueue.mjs","sourceRoot":"","sources":["../../src/snaps/RequestQueue.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAY;IAKvB,YAAY,YAAoB;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CACb,qBAAqB,MAAM,0CAA0C,CACtE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,MAAc;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;CACF","sourcesContent":["export class RequestQueue {\n public readonly maxQueueSize: number;\n\n private readonly queueSizes: Map<string, number>;\n\n constructor(maxQueueSize: number) {\n this.maxQueueSize = maxQueueSize;\n this.queueSizes = new Map<string, number>();\n }\n\n /**\n * Increments the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public increment(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount >= this.maxQueueSize) {\n throw new Error('Maximum number of requests reached. Try again later.');\n }\n this.queueSizes.set(origin, currentCount + 1);\n }\n\n /**\n * Decrements the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public decrement(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount === 0) {\n throw new Error(\n `Cannot decrement, ${origin} does not have any outstanding requests.`,\n );\n }\n this.queueSizes.set(origin, currentCount - 1);\n }\n\n /**\n * Gets the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n * @returns The queue count for the origin.\n */\n public get(origin: string): number {\n return this.queueSizes.get(origin) ?? 0;\n }\n}\n"]}