@metamask/snaps-execution-environments 6.10.0 → 6.11.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 (34) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/browserify/iframe/bundle.js +4 -4
  3. package/dist/browserify/node-process/bundle.js +3 -3
  4. package/dist/browserify/node-thread/bundle.js +3 -3
  5. package/dist/browserify/webview/index.html +4 -4
  6. package/dist/browserify/worker-executor/bundle.js +2 -2
  7. package/dist/browserify/worker-pool/bundle.js +2 -2
  8. package/dist/common/BaseSnapExecutor.cjs +6 -4
  9. package/dist/common/BaseSnapExecutor.cjs.map +1 -1
  10. package/dist/common/BaseSnapExecutor.d.cts.map +1 -1
  11. package/dist/common/BaseSnapExecutor.d.mts.map +1 -1
  12. package/dist/common/BaseSnapExecutor.mjs +6 -4
  13. package/dist/common/BaseSnapExecutor.mjs.map +1 -1
  14. package/dist/common/commands.cjs +1 -0
  15. package/dist/common/commands.cjs.map +1 -1
  16. package/dist/common/commands.d.cts.map +1 -1
  17. package/dist/common/commands.d.mts.map +1 -1
  18. package/dist/common/commands.mjs +1 -0
  19. package/dist/common/commands.mjs.map +1 -1
  20. package/dist/iframe/IFrameSnapExecutor.mjs +1 -2
  21. package/dist/iframe/IFrameSnapExecutor.mjs.map +1 -1
  22. package/dist/node-process/ChildProcessSnapExecutor.mjs +1 -2
  23. package/dist/node-process/ChildProcessSnapExecutor.mjs.map +1 -1
  24. package/dist/node-thread/ThreadSnapExecutor.mjs +1 -2
  25. package/dist/node-thread/ThreadSnapExecutor.mjs.map +1 -1
  26. package/dist/proxy/ProxySnapExecutor.mjs +1 -2
  27. package/dist/proxy/ProxySnapExecutor.mjs.map +1 -1
  28. package/dist/webview/WebViewExecutorStream.mjs +1 -2
  29. package/dist/webview/WebViewExecutorStream.mjs.map +1 -1
  30. package/dist/webworker/executor/WebWorkerSnapExecutor.mjs +1 -2
  31. package/dist/webworker/executor/WebWorkerSnapExecutor.mjs.map +1 -1
  32. package/dist/webworker/pool/WebWorkerPool.mjs +1 -2
  33. package/dist/webworker/pool/WebWorkerPool.mjs.map +1 -1
  34. package/package.json +4 -4
@@ -1 +1 @@
1
- {"version":3,"file":"ProxySnapExecutor.mjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,6DAA6D;AAC7D,OAAO,WAAW,kFAA4D;AAC9E,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,8BAA8B;AAE/D,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAQzC,MAAM,UAAU,GAAG,wCAAwC,WAAW,CAAC,OAAO,aAAa,CAAC;AAE5F;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,iBAAiB;IAO5B;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,MAA6B,EAAE,QAAQ,GAAG,UAAU;QACpE,OAAO,IAAI,iBAAiB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAED,YAAY,MAA6B,EAAE,QAAgB;;QAlBlD,4CAA+B;QAE/B,8CAAkB;QAElB,SAAI,GAAgC,EAAE,CAAC;QAe9C,uBAAA,IAAI,6BAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,iCAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,+DAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACjD,uBAAA,IAAI,+BAAa,QAAQ,MAAA,CAAC;IAC5B,CAAC;CA+EF;qMApES,IAA6C;IACnD,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAEtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACtB,qEAAqE;QACrE,mEAAmE;QACnE,kDAAkD;QAClD,uBAAA,IAAI,sEAAe,MAAnB,IAAI,EAAgB,KAAK,CAA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AC1B,CAAC,6EAQa,KAAa;IACzB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,QAAQ,KAAK,cAAc,CAAC,CAAC;IAEtD,MAAM,MAAM,GAAG,QAAQ,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAC9C,MAAM,CAAC,MAAM,EAAE,mBAAmB,KAAK,cAAc,CAAC,CAAC;IAEvD,MAAM,CAAC,MAAM,EAAE,CAAC;IAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAClC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport packageJson from '@metamask/snaps-execution-environments/package.json';\nimport { createWindow, logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\ntype ExecutorJob = {\n id: string;\n window: Window;\n stream: WindowPostMessageStream;\n};\n\nconst IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/index.html`;\n\n/**\n * A \"proxy\" snap executor that uses a level of indirection to execute snaps.\n *\n * Useful for multiple execution environments.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates an iframe window for each snap execution, and sends the\n * snap execution request to the iframe window. The iframe window is responsible\n * for executing the snap.\n *\n * This executor is persisted between snap executions. The executor essentially\n * acts as a proxy between the client and the iframe execution environment.\n */\nexport class ProxySnapExecutor {\n readonly #stream: BasePostMessageStream;\n\n readonly #frameUrl: string;\n\n readonly jobs: Record<string, ExecutorJob> = {};\n\n /**\n * Initialize the executor with the given stream. This is a wrapper around the\n * constructor.\n *\n * @param stream - The stream to use for communication.\n * @param frameUrl - An optional URL for the iframe to use.\n * @returns The initialized executor.\n */\n static initialize(stream: BasePostMessageStream, frameUrl = IFRAME_URL) {\n return new ProxySnapExecutor(stream, frameUrl);\n }\n\n constructor(stream: BasePostMessageStream, frameUrl: string) {\n this.#stream = stream;\n this.#stream.on('data', this.#onData.bind(this));\n this.#frameUrl = frameUrl;\n }\n\n /**\n * Handle an incoming message from a `ProxyExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n if (!this.jobs[jobId]) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error);\n });\n\n return;\n }\n\n // This is a method specific to the `OffscreenSnapExecutor`, as the service\n // itself does not have access to the iframes directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n this.jobs[jobId].stream.write(request);\n }\n\n /**\n * Create a new iframe and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The executor job object.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const window = await createWindow({ uri: this.#frameUrl, id: jobId });\n const jobStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window, // iframe's internal window\n targetOrigin: '*',\n });\n\n // Write messages from the iframe to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n this.jobs[jobId] = { id: jobId, window, stream: jobStream };\n return this.jobs[jobId];\n }\n\n /**\n * Terminate the job with the given ID. This will close the iframe and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n assert(this.jobs[jobId], `Job \"${jobId}\" not found.`);\n\n const iframe = document.getElementById(jobId);\n assert(iframe, `Iframe with ID \"${jobId}\" not found.`);\n\n iframe.remove();\n this.jobs[jobId].stream.destroy();\n delete this.jobs[jobId];\n }\n}\n"]}
1
+ {"version":3,"file":"ProxySnapExecutor.mjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,uBAAuB,EAAE,sCAAsC;AACxE,6DAA6D;AAC7D,OAAO,WAAW,kFAA4D;AAC9E,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,8BAA8B;AAE/D,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAQzC,MAAM,UAAU,GAAG,wCAAwC,WAAW,CAAC,OAAO,aAAa,CAAC;AAE5F;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,iBAAiB;IAO5B;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,MAA6B,EAAE,QAAQ,GAAG,UAAU;QACpE,OAAO,IAAI,iBAAiB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAED,YAAY,MAA6B,EAAE,QAAgB;;QAlBlD,4CAA+B;QAE/B,8CAAkB;QAElB,SAAI,GAAgC,EAAE,CAAC;QAe9C,uBAAA,IAAI,6BAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,iCAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,+DAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACjD,uBAAA,IAAI,+BAAa,QAAQ,MAAA,CAAC;IAC5B,CAAC;CA+EF;qMApES,IAA6C;IACnD,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAEtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACtB,qEAAqE;QACrE,mEAAmE;QACnE,kDAAkD;QAClD,uBAAA,IAAI,sEAAe,MAAnB,IAAI,EAAgB,KAAK,CAAC;aACvB,IAAI,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AQa,KAAa;IACzB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,QAAQ,KAAK,cAAc,CAAC,CAAC;IAEtD,MAAM,MAAM,GAAG,QAAQ,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAC9C,MAAM,CAAC,MAAM,EAAE,mBAAmB,KAAK,cAAc,CAAC,CAAC;IAEvD,MAAM,CAAC,MAAM,EAAE,CAAC;IAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAClC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport packageJson from '@metamask/snaps-execution-environments/package.json';\nimport { createWindow, logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\ntype ExecutorJob = {\n id: string;\n window: Window;\n stream: WindowPostMessageStream;\n};\n\nconst IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/index.html`;\n\n/**\n * A \"proxy\" snap executor that uses a level of indirection to execute snaps.\n *\n * Useful for multiple execution environments.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates an iframe window for each snap execution, and sends the\n * snap execution request to the iframe window. The iframe window is responsible\n * for executing the snap.\n *\n * This executor is persisted between snap executions. The executor essentially\n * acts as a proxy between the client and the iframe execution environment.\n */\nexport class ProxySnapExecutor {\n readonly #stream: BasePostMessageStream;\n\n readonly #frameUrl: string;\n\n readonly jobs: Record<string, ExecutorJob> = {};\n\n /**\n * Initialize the executor with the given stream. This is a wrapper around the\n * constructor.\n *\n * @param stream - The stream to use for communication.\n * @param frameUrl - An optional URL for the iframe to use.\n * @returns The initialized executor.\n */\n static initialize(stream: BasePostMessageStream, frameUrl = IFRAME_URL) {\n return new ProxySnapExecutor(stream, frameUrl);\n }\n\n constructor(stream: BasePostMessageStream, frameUrl: string) {\n this.#stream = stream;\n this.#stream.on('data', this.#onData.bind(this));\n this.#frameUrl = frameUrl;\n }\n\n /**\n * Handle an incoming message from a `ProxyExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n if (!this.jobs[jobId]) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error);\n });\n\n return;\n }\n\n // This is a method specific to the `OffscreenSnapExecutor`, as the service\n // itself does not have access to the iframes directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n this.jobs[jobId].stream.write(request);\n }\n\n /**\n * Create a new iframe and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The executor job object.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const window = await createWindow({ uri: this.#frameUrl, id: jobId });\n const jobStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window, // iframe's internal window\n targetOrigin: '*',\n });\n\n // Write messages from the iframe to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n this.jobs[jobId] = { id: jobId, window, stream: jobStream };\n return this.jobs[jobId];\n }\n\n /**\n * Terminate the job with the given ID. This will close the iframe and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n assert(this.jobs[jobId], `Job \"${jobId}\" not found.`);\n\n const iframe = document.getElementById(jobId);\n assert(iframe, `Iframe with ID \"${jobId}\" not found.`);\n\n iframe.remove();\n this.jobs[jobId].stream.destroy();\n delete this.jobs[jobId];\n }\n}\n"]}
@@ -10,8 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _WebViewExecutorStream_name, _WebViewExecutorStream_target, _WebViewExecutorStream_targetWindow;
13
- import $metamaskpostmessagestream from "@metamask/post-message-stream";
14
- const { BasePostMessageStream } = $metamaskpostmessagestream;
13
+ import { BasePostMessageStream } from "@metamask/post-message-stream";
15
14
  import { isValidStreamMessage } from "@metamask/post-message-stream/dist/utils.js";
16
15
  import { base64ToBytes, bytesToString } from "@metamask/utils";
17
16
  export class WebViewExecutorStream extends BasePostMessageStream {
@@ -1 +1 @@
1
- {"version":3,"file":"WebViewExecutorStream.mjs","sourceRoot":"","sources":["../../src/webview/WebViewExecutorStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,OAAO,EAAE,oBAAoB,EAAE,oDAAiD;AAChF,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,wBAAwB;AAQ/D,MAAM,OAAO,qBAAsB,SAAQ,qBAAqB;IAO9D;;;;;;;;;;;;;OAaG;IAEH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAA6B;QACnE,KAAK,EAAE,CAAC;QAtBV,8CAAM;QAEN,gDAAQ;QAER,sDAAc;QAoBZ,uBAAA,IAAI,+BAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,iCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,uCAAiB,YAAY,MAAA,CAAC;QAElC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE7C,gCAAgC;QAChC,6DAA6D;QAC7D,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,UAAiB,EAAE,KAAK,CAAC,CAAC;QAElE,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IAED;;;OAGG;IAEO,YAAY,CAAC,IAAa;QAClC,uBAAA,IAAI,2CAAc,CAAC,WAAW,CAC5B,IAAI,CAAC,SAAS,CAAC;YACb,MAAM,EAAE,uBAAA,IAAI,qCAAQ;YACpB,IAAI;SACL,CAAC,CACH,CAAC;IACJ,CAAC;IAEO,UAAU,CAAC,KAAuB;QACxC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO;QACT,CAAC;QAED,MAAM,KAAK,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACxC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;QAEjD,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,uBAAA,IAAI,mCAAM,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,gCAAgC;QAChC,6DAA6D;QAC7D,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,UAAiB,EAAE,KAAK,CAAC,CAAC;IACvE,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 { base64ToBytes, bytesToString } from '@metamask/utils';\n\ntype WebViewExecutorStreamArgs = {\n name: string;\n target: string;\n targetWindow: Window['ReactNativeWebView'];\n};\n\nexport class WebViewExecutorStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #targetWindow;\n\n /**\n * A special post-message-stream to be used by the WebView executor.\n *\n * This stream is different in a few ways:\n * - It expects data to be base64 encoded\n * - It stringifies the data it posts\n * - It does less validation of origins\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. child:WebView\n * @param args.target - The name of the stream to exchange messages with. parent:rnside\n * @param args.targetWindow - The window object of the target stream.\n */\n\n constructor({ name, target, targetWindow }: WebViewExecutorStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n this.#targetWindow = targetWindow;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n window.addEventListener('message', this._onMessage as any, false);\n\n this._handshake();\n }\n\n /**\n * Webview needs to receive strings only on postMessage. That's the main difference between this and the original window post message stream.\n * Reference: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md?plain=1#L471\n */\n\n protected _postMessage(data: unknown): void {\n this.#targetWindow.postMessage(\n JSON.stringify({\n target: this.#target,\n data,\n }),\n );\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const bytes = base64ToBytes(event.data);\n const message = JSON.parse(bytesToString(bytes));\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 // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n window.removeEventListener('message', this._onMessage as any, false);\n }\n}\n"]}
1
+ {"version":3,"file":"WebViewExecutorStream.mjs","sourceRoot":"","sources":["../../src/webview/WebViewExecutorStream.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,qBAAqB,EAAE,sCAAsC;AACtE,OAAO,EAAE,oBAAoB,EAAE,oDAAiD;AAChF,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,wBAAwB;AAQ/D,MAAM,OAAO,qBAAsB,SAAQ,qBAAqB;IAO9D;;;;;;;;;;;;;OAaG;IAEH,YAAY,EA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AAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,UAAiB,EAAE,KAAK,CAAC,CAAC;IACvE,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 { base64ToBytes, bytesToString } from '@metamask/utils';\n\ntype WebViewExecutorStreamArgs = {\n name: string;\n target: string;\n targetWindow: Window['ReactNativeWebView'];\n};\n\nexport class WebViewExecutorStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #targetWindow;\n\n /**\n * A special post-message-stream to be used by the WebView executor.\n *\n * This stream is different in a few ways:\n * - It expects data to be base64 encoded\n * - It stringifies the data it posts\n * - It does less validation of origins\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. child:WebView\n * @param args.target - The name of the stream to exchange messages with. parent:rnside\n * @param args.targetWindow - The window object of the target stream.\n */\n\n constructor({ name, target, targetWindow }: WebViewExecutorStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n this.#targetWindow = targetWindow;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n window.addEventListener('message', this._onMessage as any, false);\n\n this._handshake();\n }\n\n /**\n * Webview needs to receive strings only on postMessage. That's the main difference between this and the original window post message stream.\n * Reference: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md?plain=1#L471\n */\n\n protected _postMessage(data: unknown): void {\n this.#targetWindow.postMessage(\n JSON.stringify({\n target: this.#target,\n data,\n }),\n );\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const bytes = base64ToBytes(event.data);\n const message = JSON.parse(bytesToString(bytes));\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 // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n window.removeEventListener('message', this._onMessage as any, false);\n }\n}\n"]}
@@ -6,8 +6,7 @@ function $importDefault(module) {
6
6
  }
7
7
  import $ObjectMultiplex from "@metamask/object-multiplex";
8
8
  const ObjectMultiplex = $importDefault($ObjectMultiplex);
9
- import $metamaskpostmessagestream from "@metamask/post-message-stream";
10
- const { WebWorkerPostMessageStream } = $metamaskpostmessagestream;
9
+ import { WebWorkerPostMessageStream } from "@metamask/post-message-stream";
11
10
  import { logError, SNAP_STREAM_NAMES } from "@metamask/snaps-utils";
12
11
  import { pipeline } from "readable-stream";
13
12
  import { BaseSnapExecutor } from "../../common/BaseSnapExecutor.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"WebWorkerSnapExecutor.mjs","sourceRoot":"","sources":["../../../src/webworker/executor/WebWorkerSnapExecutor.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,gBAAe,mCAAmC;;;;AAGzD,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,8BAA8B;AACpE,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAE3C,OAAO,EAAE,gBAAgB,EAAE,0CAAsC;AACjE,OAAO,EAAE,GAAG,EAAE,0BAAsB;AAEpC,MAAM,OAAO,qBAAsB,SAAQ,gBAAgB;IACzD;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,0BAA0B,EAAE;QAEhE,GAAG,CAAC,+BAA+B,CAAC,CAAC;QAErC,MAAM,GAAG,GAAG,IAAI,eAAe,EAAE,CAAC;QAClC,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACtC,IAAI,KAAK,EAAE,CAAC;gBACV,QAAQ,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QAEH,MAAM,aAAa,GAAG,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QAClE,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAE/D,OAAO,IAAI,qBAAqB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAC7D,CAAC;CACF","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WebWorkerPostMessageStream } from '@metamask/post-message-stream';\nimport { logError, SNAP_STREAM_NAMES } from '@metamask/snaps-utils';\nimport { pipeline } from 'readable-stream';\n\nimport { BaseSnapExecutor } from '../../common/BaseSnapExecutor';\nimport { log } from '../../logging';\n\nexport class WebWorkerSnapExecutor extends BaseSnapExecutor {\n /**\n * Initialize the WebWorkerSnapExecutor. This creates a post message stream\n * from and to the parent window, for two-way communication with the iframe.\n *\n * @param stream - The stream to use for communication.\n * @returns An instance of `WebWorkerSnapExecutor`, with the initialized post\n * message streams.\n */\n static initialize(\n stream: BasePostMessageStream = new WebWorkerPostMessageStream(),\n ) {\n log('Worker: Connecting to parent.');\n\n const mux = new ObjectMultiplex();\n pipeline(stream, mux, stream, (error) => {\n if (error) {\n logError(`Parent stream failure, closing worker.`, error);\n }\n self.close();\n });\n\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n return new WebWorkerSnapExecutor(commandStream, rpcStream);\n }\n}\n"]}
1
+ {"version":3,"file":"WebWorkerSnapExecutor.mjs","sourceRoot":"","sources":["../../../src/webworker/executor/WebWorkerSnapExecutor.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,gBAAe,mCAAmC;;AAEzD,OAAO,EAAE,0BAA0B,EAAE,sCAAsC;AAC3E,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,8BAA8B;AACpE,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAE3C,OAAO,EAAE,gBAAgB,EAAE,0CAAsC;AACjE,OAAO,EAAE,GAAG,EAAE,0BAAsB;AAEpC,MAAM,OAAO,qBAAsB,SAAQ,gBAAgB;IACzD;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,0BAA0B,EAAE;QAEhE,GAAG,CAAC,+BAA+B,CAAC,CAAC;QAErC,MAAM,GAAG,GAAG,IAAI,eAAe,EAAE,CAAC;QAClC,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACtC,IAAI,KAAK,EAAE,CAAC;gBACV,QAAQ,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QAEH,MAAM,aAAa,GAAG,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QAClE,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAE/D,OAAO,IAAI,qBAAqB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAC7D,CAAC;CACF","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WebWorkerPostMessageStream } from '@metamask/post-message-stream';\nimport { logError, SNAP_STREAM_NAMES } from '@metamask/snaps-utils';\nimport { pipeline } from 'readable-stream';\n\nimport { BaseSnapExecutor } from '../../common/BaseSnapExecutor';\nimport { log } from '../../logging';\n\nexport class WebWorkerSnapExecutor extends BaseSnapExecutor {\n /**\n * Initialize the WebWorkerSnapExecutor. This creates a post message stream\n * from and to the parent window, for two-way communication with the iframe.\n *\n * @param stream - The stream to use for communication.\n * @returns An instance of `WebWorkerSnapExecutor`, with the initialized post\n * message streams.\n */\n static initialize(\n stream: BasePostMessageStream = new WebWorkerPostMessageStream(),\n ) {\n log('Worker: Connecting to parent.');\n\n const mux = new ObjectMultiplex();\n pipeline(stream, mux, stream, (error) => {\n if (error) {\n logError(`Parent stream failure, closing worker.`, error);\n }\n self.close();\n });\n\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n return new WebWorkerSnapExecutor(commandStream, rpcStream);\n }\n}\n"]}
@@ -10,8 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _WebWorkerPool_instances, _WebWorkerPool_poolSize, _WebWorkerPool_stream, _WebWorkerPool_url, _WebWorkerPool_workerSourceURL, _WebWorkerPool_onData, _WebWorkerPool_initializeJob, _WebWorkerPool_terminateJob, _WebWorkerPool_getWorker, _WebWorkerPool_updatePool, _WebWorkerPool_createWorker, _WebWorkerPool_getWorkerURL;
13
- import $metamaskpostmessagestream from "@metamask/post-message-stream";
14
- const { WebWorkerParentPostMessageStream, WindowPostMessageStream } = $metamaskpostmessagestream;
13
+ import { WebWorkerParentPostMessageStream, WindowPostMessageStream } from "@metamask/post-message-stream";
15
14
  import { logError } from "@metamask/snaps-utils";
16
15
  import { assert } from "@metamask/utils";
17
16
  import { nanoid } from "nanoid/non-secure";
@@ -1 +1 @@
1
- {"version":3,"file":"WebWorkerPool.mjs","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAKA,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAEjD,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,MAAM,EAAE,0BAA0B;AAQ3C;;;;;;;GAOG;AACH,MAAM,OAAO,aAAa;IAaxB,mDAAmD;IACnD,MAAM,CAAC,UAAU,CACf,SAAg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O,uBAAA,IAAI,sCAAiB,CAAC;IAC/B,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,uBAAA,IAAI,0BAAK,CAAC;SAChC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;SACzC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAEvC,uBAAA,IAAI,kCAAoB,IAAI,MAAA,CAAC;IAC7B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n WebWorkerParentPostMessageStream,\n WindowPostMessageStream,\n} from '@metamask/post-message-stream';\nimport { logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid/non-secure';\n\ntype ExecutorJob = {\n id: string;\n worker: Worker;\n stream: WebWorkerParentPostMessageStream;\n};\n\n/**\n * A snap executor using the WebWorker API.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates a pool of webworkers for each snap execution, and sends\n * the snap execution request to the webworker. The webworker is responsible for\n * executing the snap.\n */\nexport class WebWorkerPool {\n readonly #poolSize;\n\n readonly #stream: BasePostMessageStream;\n\n readonly #url: string;\n\n readonly pool: Worker[] = [];\n\n readonly jobs: Map<string, ExecutorJob> = new Map();\n\n #workerSourceURL?: string;\n\n /* istanbul ignore next - Constructor arguments. */\n static initialize(\n stream: BasePostMessageStream = new WindowPostMessageStream({\n name: 'child',\n target: 'parent',\n targetWindow: self.parent,\n targetOrigin: '*',\n }),\n url = '../executor/bundle.js',\n poolSize?: number,\n ) {\n return new WebWorkerPool(stream, url, poolSize);\n }\n\n constructor(stream: BasePostMessageStream, url: string, poolSize = 3) {\n this.#stream = stream;\n this.#url = url;\n this.#poolSize = poolSize;\n\n this.#stream.on('data', this.#onData.bind(this));\n }\n\n /**\n * Handle an incoming message from the `WebWorkerExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n const job = this.jobs.get(jobId);\n if (!job) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error.toString());\n\n this.#stream.write({\n jobId,\n data: {\n name: 'command',\n data: {\n jsonrpc: '2.0',\n id: request.id ?? null,\n error: {\n code: -32000,\n message: 'Internal error',\n },\n },\n },\n });\n });\n\n return;\n }\n\n // This is a method specific to the `WebWorkerPool`, as the service itself\n // does not have access to the workers directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n job.stream.write(request);\n }\n\n /**\n * Create a new worker and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The job.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const worker = await this.#getWorker();\n const jobStream = new WebWorkerParentPostMessageStream({\n worker,\n });\n\n // Write messages from the worker to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n const job = { id: jobId, worker, stream: jobStream };\n this.jobs.set(jobId, job);\n return job;\n }\n\n /**\n * Terminate the job with the given ID. This will close the worker and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n const job = this.jobs.get(jobId);\n assert(job, `Job \"${jobId}\" not found.`);\n\n job.stream.destroy();\n job.worker.terminate();\n\n this.jobs.delete(jobId);\n }\n\n /**\n * Get a worker from the pool. A new worker will be created automatically.\n *\n * @returns The worker.\n */\n async #getWorker() {\n // Lazily create the pool of workers.\n if (this.pool.length === 0) {\n await this.#updatePool();\n }\n\n const worker = this.pool.shift();\n assert(worker, 'Worker not found.');\n\n await this.#updatePool();\n\n return worker;\n }\n\n /**\n * Update the pool of workers. This will create new workers if the pool is\n * below the minimum size.\n */\n async #updatePool() {\n while (this.pool.length < this.#poolSize) {\n const worker = await this.#createWorker();\n this.pool.push(worker);\n }\n }\n\n /**\n * Create a new worker. This will fetch the worker source if it has not\n * already been fetched.\n *\n * @returns The worker.\n */\n async #createWorker() {\n return new Worker(await this.#getWorkerURL(), {\n name: `worker-${nanoid()}`,\n });\n }\n\n /**\n * Get the URL of the worker source. This will fetch the worker source if it\n * has not already been fetched.\n *\n * @returns The worker source URL, as a `blob:` URL.\n */\n async #getWorkerURL() {\n if (this.#workerSourceURL) {\n return this.#workerSourceURL;\n }\n\n const blob = await fetch(this.#url)\n .then(async (response) => response.blob())\n .then(URL.createObjectURL.bind(URL));\n\n this.#workerSourceURL = blob;\n return blob;\n }\n}\n"]}
1
+ {"version":3,"file":"WebWorkerPool.mjs","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,gCAAgC,EAChC,uBAAuB,EACxB,sCAAsC;AACvC,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAEjD,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,MAAM,EAAE,0BAA0B;AAQ3C;;;;;;;GAOG;AACH,MAAM,OAAO,aAAa;IAaxB,mDAAmD;IACnD,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,uBAAuB,CAAC;QAC1D,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,QAAQ;QAChB,YAAY,EAAE,IAAI,CAAC,MAAM;QACzB,YAAY,EAAE,GAAG;KAClB,CAAC,EACF,GAAG,GAAG,uBAAuB,EAC7B,QAAiB;QAEjB,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAClD,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AAC,CAAC;IAEvC,uBAAA,IAAI,kCAAoB,IAAI,MAAA,CAAC;IAC7B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n WebWorkerParentPostMessageStream,\n WindowPostMessageStream,\n} from '@metamask/post-message-stream';\nimport { logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid/non-secure';\n\ntype ExecutorJob = {\n id: string;\n worker: Worker;\n stream: WebWorkerParentPostMessageStream;\n};\n\n/**\n * A snap executor using the WebWorker API.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates a pool of webworkers for each snap execution, and sends\n * the snap execution request to the webworker. The webworker is responsible for\n * executing the snap.\n */\nexport class WebWorkerPool {\n readonly #poolSize;\n\n readonly #stream: BasePostMessageStream;\n\n readonly #url: string;\n\n readonly pool: Worker[] = [];\n\n readonly jobs: Map<string, ExecutorJob> = new Map();\n\n #workerSourceURL?: string;\n\n /* istanbul ignore next - Constructor arguments. */\n static initialize(\n stream: BasePostMessageStream = new WindowPostMessageStream({\n name: 'child',\n target: 'parent',\n targetWindow: self.parent,\n targetOrigin: '*',\n }),\n url = '../executor/bundle.js',\n poolSize?: number,\n ) {\n return new WebWorkerPool(stream, url, poolSize);\n }\n\n constructor(stream: BasePostMessageStream, url: string, poolSize = 3) {\n this.#stream = stream;\n this.#url = url;\n this.#poolSize = poolSize;\n\n this.#stream.on('data', this.#onData.bind(this));\n }\n\n /**\n * Handle an incoming message from the `WebWorkerExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n const job = this.jobs.get(jobId);\n if (!job) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error.toString());\n\n this.#stream.write({\n jobId,\n data: {\n name: 'command',\n data: {\n jsonrpc: '2.0',\n id: request.id ?? null,\n error: {\n code: -32000,\n message: 'Internal error',\n },\n },\n },\n });\n });\n\n return;\n }\n\n // This is a method specific to the `WebWorkerPool`, as the service itself\n // does not have access to the workers directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n job.stream.write(request);\n }\n\n /**\n * Create a new worker and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The job.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const worker = await this.#getWorker();\n const jobStream = new WebWorkerParentPostMessageStream({\n worker,\n });\n\n // Write messages from the worker to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n const job = { id: jobId, worker, stream: jobStream };\n this.jobs.set(jobId, job);\n return job;\n }\n\n /**\n * Terminate the job with the given ID. This will close the worker and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n const job = this.jobs.get(jobId);\n assert(job, `Job \"${jobId}\" not found.`);\n\n job.stream.destroy();\n job.worker.terminate();\n\n this.jobs.delete(jobId);\n }\n\n /**\n * Get a worker from the pool. A new worker will be created automatically.\n *\n * @returns The worker.\n */\n async #getWorker() {\n // Lazily create the pool of workers.\n if (this.pool.length === 0) {\n await this.#updatePool();\n }\n\n const worker = this.pool.shift();\n assert(worker, 'Worker not found.');\n\n await this.#updatePool();\n\n return worker;\n }\n\n /**\n * Update the pool of workers. This will create new workers if the pool is\n * below the minimum size.\n */\n async #updatePool() {\n while (this.pool.length < this.#poolSize) {\n const worker = await this.#createWorker();\n this.pool.push(worker);\n }\n }\n\n /**\n * Create a new worker. This will fetch the worker source if it has not\n * already been fetched.\n *\n * @returns The worker.\n */\n async #createWorker() {\n return new Worker(await this.#getWorkerURL(), {\n name: `worker-${nanoid()}`,\n });\n }\n\n /**\n * Get the URL of the worker source. This will fetch the worker source if it\n * has not already been fetched.\n *\n * @returns The worker source URL, as a `blob:` URL.\n */\n async #getWorkerURL() {\n if (this.#workerSourceURL) {\n return this.#workerSourceURL;\n }\n\n const blob = await fetch(this.#url)\n .then(async (response) => response.blob())\n .then(URL.createObjectURL.bind(URL));\n\n this.#workerSourceURL = blob;\n return blob;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-execution-environments",
3
- "version": "6.10.0",
3
+ "version": "6.11.0",
4
4
  "description": "Snap sandbox environments for executing SES javascript",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -69,8 +69,8 @@
69
69
  "@metamask/post-message-stream": "^8.1.1",
70
70
  "@metamask/providers": "^18.1.1",
71
71
  "@metamask/rpc-errors": "^7.0.1",
72
- "@metamask/snaps-sdk": "^6.11.0",
73
- "@metamask/snaps-utils": "^8.6.0",
72
+ "@metamask/snaps-sdk": "^6.14.0",
73
+ "@metamask/snaps-utils": "^8.7.0",
74
74
  "@metamask/superstruct": "^3.1.0",
75
75
  "@metamask/utils": "^10.0.0",
76
76
  "nanoid": "^3.1.31",
@@ -92,7 +92,7 @@
92
92
  "@metamask/eslint-config-typescript": "^12.1.0",
93
93
  "@swc/core": "1.3.78",
94
94
  "@swc/jest": "^0.2.26",
95
- "@ts-bridge/cli": "^0.6.0",
95
+ "@ts-bridge/cli": "^0.6.1",
96
96
  "@types/express": "^4.17.17",
97
97
  "@types/jest": "^27.5.1",
98
98
  "@types/node": "18.14.2",