@metamask/snaps-execution-environments 8.2.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -1
- package/dist/common/BaseSnapExecutor.cjs +79 -61
- package/dist/common/BaseSnapExecutor.cjs.map +1 -1
- package/dist/common/BaseSnapExecutor.mjs +79 -61
- package/dist/common/BaseSnapExecutor.mjs.map +1 -1
- package/dist/common/endowments/crypto.cjs +5 -15
- package/dist/common/endowments/crypto.cjs.map +1 -1
- package/dist/common/endowments/crypto.d.cts +10 -4
- package/dist/common/endowments/crypto.d.cts.map +1 -1
- package/dist/common/endowments/crypto.d.mts +10 -4
- package/dist/common/endowments/crypto.d.mts.map +1 -1
- package/dist/common/endowments/crypto.mjs +5 -17
- package/dist/common/endowments/crypto.mjs.map +1 -1
- package/dist/common/endowments/network.cjs +39 -52
- package/dist/common/endowments/network.cjs.map +1 -1
- package/dist/common/endowments/network.mjs +39 -52
- package/dist/common/endowments/network.mjs.map +1 -1
- package/dist/proxy/ProxySnapExecutor.cjs +72 -69
- package/dist/proxy/ProxySnapExecutor.cjs.map +1 -1
- package/dist/proxy/ProxySnapExecutor.mjs +71 -68
- package/dist/proxy/ProxySnapExecutor.mjs.map +1 -1
- package/dist/webpack/iframe/bundle.js +1 -1
- package/dist/webpack/iframe/index.html +854 -406
- package/dist/webpack/node-process/bundle.js +1 -1
- package/dist/webpack/node-thread/bundle.js +1 -1
- package/dist/webpack/webview/index.html +855 -407
- package/dist/webview/WebViewExecutorStream.cjs +9 -22
- package/dist/webview/WebViewExecutorStream.cjs.map +1 -1
- package/dist/webview/WebViewExecutorStream.mjs +9 -22
- package/dist/webview/WebViewExecutorStream.mjs.map +1 -1
- package/package.json +7 -7
|
@@ -1,19 +1,7 @@
|
|
|
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
|
-
var _ProxySnapExecutor_instances, _ProxySnapExecutor_stream, _ProxySnapExecutor_frameUrl, _ProxySnapExecutor_onData, _ProxySnapExecutor_initializeJob, _ProxySnapExecutor_terminateJob;
|
|
17
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
6
|
exports.ProxySnapExecutor = void 0;
|
|
19
7
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
@@ -35,6 +23,9 @@ const IFRAME_URL = `https://execution.metamask.io/iframe/${package_json_1.defaul
|
|
|
35
23
|
* acts as a proxy between the client and the iframe execution environment.
|
|
36
24
|
*/
|
|
37
25
|
class ProxySnapExecutor {
|
|
26
|
+
#stream;
|
|
27
|
+
#frameUrl;
|
|
28
|
+
jobs = {};
|
|
38
29
|
/**
|
|
39
30
|
* Initialize the executor with the given stream. This is a wrapper around the
|
|
40
31
|
* constructor.
|
|
@@ -47,65 +38,77 @@ class ProxySnapExecutor {
|
|
|
47
38
|
return new ProxySnapExecutor(stream, frameUrl);
|
|
48
39
|
}
|
|
49
40
|
constructor(stream, frameUrl) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.jobs = {};
|
|
54
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_stream, stream, "f");
|
|
55
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").on('data', __classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).bind(this));
|
|
56
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_frameUrl, frameUrl, "f");
|
|
41
|
+
this.#stream = stream;
|
|
42
|
+
this.#stream.on('data', this.#onData.bind(this));
|
|
43
|
+
this.#frameUrl = frameUrl;
|
|
57
44
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
.
|
|
71
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Handle an incoming message from a `ProxyExecutionService`. This
|
|
47
|
+
* assumes that the message contains a `jobId` property, and a JSON-RPC
|
|
48
|
+
* request in the `data` property.
|
|
49
|
+
*
|
|
50
|
+
* @param data - The message data.
|
|
51
|
+
* @param data.data - The JSON-RPC request.
|
|
52
|
+
* @param data.jobId - The job ID.
|
|
53
|
+
*/
|
|
54
|
+
#onData(data) {
|
|
55
|
+
const { jobId, data: request } = data;
|
|
56
|
+
if (!this.jobs[jobId]) {
|
|
57
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
58
|
+
// code duplication, we call the `#onData` method again, which will
|
|
59
|
+
// run the rest of the logic after initialization.
|
|
60
|
+
this.#initializeJob(jobId)
|
|
61
|
+
.then(() => {
|
|
62
|
+
this.#onData(data);
|
|
63
|
+
})
|
|
64
|
+
.catch((error) => {
|
|
65
|
+
(0, snaps_utils_1.logError)('[Worker] Error initializing job:', error);
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
70
|
+
// itself does not have access to the iframes directly.
|
|
71
|
+
if (request.method === 'terminateJob') {
|
|
72
|
+
this.#terminateJob(jobId);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
this.jobs[jobId].stream.write(request);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a new iframe and set up a stream to communicate with it.
|
|
79
|
+
*
|
|
80
|
+
* @param jobId - The job ID.
|
|
81
|
+
* @returns The executor job object.
|
|
82
|
+
*/
|
|
83
|
+
async #initializeJob(jobId) {
|
|
84
|
+
const window = await (0, snaps_utils_1.createWindow)({ uri: this.#frameUrl, id: jobId });
|
|
85
|
+
const jobStream = new post_message_stream_1.WindowPostMessageStream({
|
|
86
|
+
name: 'parent',
|
|
87
|
+
target: 'child',
|
|
88
|
+
targetWindow: window, // iframe's internal window
|
|
89
|
+
targetOrigin: '*',
|
|
72
90
|
});
|
|
73
|
-
|
|
91
|
+
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
92
|
+
jobStream.on('data', (data) => {
|
|
93
|
+
this.#stream.write({ data, jobId });
|
|
94
|
+
});
|
|
95
|
+
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
96
|
+
return this.jobs[jobId];
|
|
74
97
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Terminate the job with the given ID. This will close the iframe and delete
|
|
100
|
+
* the job from the internal job map.
|
|
101
|
+
*
|
|
102
|
+
* @param jobId - The job ID.
|
|
103
|
+
*/
|
|
104
|
+
#terminateJob(jobId) {
|
|
105
|
+
(0, utils_1.assert)(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
106
|
+
const iframe = document.getElementById(jobId);
|
|
107
|
+
(0, utils_1.assert)(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
108
|
+
iframe.remove();
|
|
109
|
+
this.jobs[jobId].stream.destroy();
|
|
110
|
+
delete this.jobs[jobId];
|
|
80
111
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Create a new iframe and set up a stream to communicate with it.
|
|
85
|
-
*
|
|
86
|
-
* @param jobId - The job ID.
|
|
87
|
-
* @returns The executor job object.
|
|
88
|
-
*/
|
|
89
|
-
async function _ProxySnapExecutor_initializeJob(jobId) {
|
|
90
|
-
const window = await (0, snaps_utils_1.createWindow)({ uri: __classPrivateFieldGet(this, _ProxySnapExecutor_frameUrl, "f"), id: jobId });
|
|
91
|
-
const jobStream = new post_message_stream_1.WindowPostMessageStream({
|
|
92
|
-
name: 'parent',
|
|
93
|
-
target: 'child',
|
|
94
|
-
targetWindow: window, // iframe's internal window
|
|
95
|
-
targetOrigin: '*',
|
|
96
|
-
});
|
|
97
|
-
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
98
|
-
jobStream.on('data', (data) => {
|
|
99
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").write({ data, jobId });
|
|
100
|
-
});
|
|
101
|
-
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
102
|
-
return this.jobs[jobId];
|
|
103
|
-
}, _ProxySnapExecutor_terminateJob = function _ProxySnapExecutor_terminateJob(jobId) {
|
|
104
|
-
(0, utils_1.assert)(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
105
|
-
const iframe = document.getElementById(jobId);
|
|
106
|
-
(0, utils_1.assert)(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
107
|
-
iframe.remove();
|
|
108
|
-
this.jobs[jobId].stream.destroy();
|
|
109
|
-
delete this.jobs[jobId];
|
|
110
|
-
};
|
|
112
|
+
}
|
|
113
|
+
exports.ProxySnapExecutor = ProxySnapExecutor;
|
|
111
114
|
//# sourceMappingURL=ProxySnapExecutor.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxySnapExecutor.cjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ProxySnapExecutor.cjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":";;;;;;AACA,uEAAwE;AACxE,uDAA+D;AAE/D,2CAAyC;AAEzC,uGAA8E;AAQ9E,MAAM,UAAU,GAAG,wCAAwC,sBAAW,CAAC,OAAO,aAAa,CAAC;AAE5F;;;;;;;;;;;;GAYG;AACH,MAAa,iBAAiB;IACnB,OAAO,CAAwB;IAE/B,SAAS,CAAS;IAElB,IAAI,GAAgC,EAAE,CAAC;IAEhD;;;;;;;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;QACzD,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACjD,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC5B,CAAC;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,IAA6C;QACnD,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;QAEtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,qEAAqE;YACrE,mEAAmE;YACnE,kDAAkD;YAClD,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC;iBACvB,IAAI,CAAC,GAAG,EAAE;gBACT,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACrB,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,KAA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AAC,CAAC;QAEtD,MAAM,MAAM,GAAG,QAAQ,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAA,cAAM,EAAC,MAAM,EAAE,mBAAmB,KAAK,cAAc,CAAC,CAAC;QAEvD,MAAM,CAAC,MAAM,EAAE,CAAC;QAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAClC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;CACF;AAtGD,8CAsGC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow, logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport packageJson from '@metamask/snaps-execution-environments/package.json';\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,15 +1,3 @@
|
|
|
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 _ProxySnapExecutor_instances, _ProxySnapExecutor_stream, _ProxySnapExecutor_frameUrl, _ProxySnapExecutor_onData, _ProxySnapExecutor_initializeJob, _ProxySnapExecutor_terminateJob;
|
|
13
1
|
import { WindowPostMessageStream } from "@metamask/post-message-stream";
|
|
14
2
|
import { createWindow, logError } from "@metamask/snaps-utils";
|
|
15
3
|
import { assert } from "@metamask/utils";
|
|
@@ -29,6 +17,9 @@ const IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/
|
|
|
29
17
|
* acts as a proxy between the client and the iframe execution environment.
|
|
30
18
|
*/
|
|
31
19
|
export class ProxySnapExecutor {
|
|
20
|
+
#stream;
|
|
21
|
+
#frameUrl;
|
|
22
|
+
jobs = {};
|
|
32
23
|
/**
|
|
33
24
|
* Initialize the executor with the given stream. This is a wrapper around the
|
|
34
25
|
* constructor.
|
|
@@ -41,64 +32,76 @@ export class ProxySnapExecutor {
|
|
|
41
32
|
return new ProxySnapExecutor(stream, frameUrl);
|
|
42
33
|
}
|
|
43
34
|
constructor(stream, frameUrl) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this.jobs = {};
|
|
48
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_stream, stream, "f");
|
|
49
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").on('data', __classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).bind(this));
|
|
50
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_frameUrl, frameUrl, "f");
|
|
35
|
+
this.#stream = stream;
|
|
36
|
+
this.#stream.on('data', this.#onData.bind(this));
|
|
37
|
+
this.#frameUrl = frameUrl;
|
|
51
38
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Handle an incoming message from a `ProxyExecutionService`. This
|
|
41
|
+
* assumes that the message contains a `jobId` property, and a JSON-RPC
|
|
42
|
+
* request in the `data` property.
|
|
43
|
+
*
|
|
44
|
+
* @param data - The message data.
|
|
45
|
+
* @param data.data - The JSON-RPC request.
|
|
46
|
+
* @param data.jobId - The job ID.
|
|
47
|
+
*/
|
|
48
|
+
#onData(data) {
|
|
49
|
+
const { jobId, data: request } = data;
|
|
50
|
+
if (!this.jobs[jobId]) {
|
|
51
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
52
|
+
// code duplication, we call the `#onData` method again, which will
|
|
53
|
+
// run the rest of the logic after initialization.
|
|
54
|
+
this.#initializeJob(jobId)
|
|
55
|
+
.then(() => {
|
|
56
|
+
this.#onData(data);
|
|
57
|
+
})
|
|
58
|
+
.catch((error) => {
|
|
59
|
+
logError('[Worker] Error initializing job:', error);
|
|
60
|
+
});
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
64
|
+
// itself does not have access to the iframes directly.
|
|
65
|
+
if (request.method === 'terminateJob') {
|
|
66
|
+
this.#terminateJob(jobId);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this.jobs[jobId].stream.write(request);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a new iframe and set up a stream to communicate with it.
|
|
73
|
+
*
|
|
74
|
+
* @param jobId - The job ID.
|
|
75
|
+
* @returns The executor job object.
|
|
76
|
+
*/
|
|
77
|
+
async #initializeJob(jobId) {
|
|
78
|
+
const window = await createWindow({ uri: this.#frameUrl, id: jobId });
|
|
79
|
+
const jobStream = new WindowPostMessageStream({
|
|
80
|
+
name: 'parent',
|
|
81
|
+
target: 'child',
|
|
82
|
+
targetWindow: window, // iframe's internal window
|
|
83
|
+
targetOrigin: '*',
|
|
65
84
|
});
|
|
66
|
-
|
|
85
|
+
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
86
|
+
jobStream.on('data', (data) => {
|
|
87
|
+
this.#stream.write({ data, jobId });
|
|
88
|
+
});
|
|
89
|
+
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
90
|
+
return this.jobs[jobId];
|
|
67
91
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Terminate the job with the given ID. This will close the iframe and delete
|
|
94
|
+
* the job from the internal job map.
|
|
95
|
+
*
|
|
96
|
+
* @param jobId - The job ID.
|
|
97
|
+
*/
|
|
98
|
+
#terminateJob(jobId) {
|
|
99
|
+
assert(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
100
|
+
const iframe = document.getElementById(jobId);
|
|
101
|
+
assert(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
102
|
+
iframe.remove();
|
|
103
|
+
this.jobs[jobId].stream.destroy();
|
|
104
|
+
delete this.jobs[jobId];
|
|
73
105
|
}
|
|
74
|
-
|
|
75
|
-
}, _ProxySnapExecutor_initializeJob =
|
|
76
|
-
/**
|
|
77
|
-
* Create a new iframe and set up a stream to communicate with it.
|
|
78
|
-
*
|
|
79
|
-
* @param jobId - The job ID.
|
|
80
|
-
* @returns The executor job object.
|
|
81
|
-
*/
|
|
82
|
-
async function _ProxySnapExecutor_initializeJob(jobId) {
|
|
83
|
-
const window = await createWindow({ uri: __classPrivateFieldGet(this, _ProxySnapExecutor_frameUrl, "f"), id: jobId });
|
|
84
|
-
const jobStream = new WindowPostMessageStream({
|
|
85
|
-
name: 'parent',
|
|
86
|
-
target: 'child',
|
|
87
|
-
targetWindow: window, // iframe's internal window
|
|
88
|
-
targetOrigin: '*',
|
|
89
|
-
});
|
|
90
|
-
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
91
|
-
jobStream.on('data', (data) => {
|
|
92
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").write({ data, jobId });
|
|
93
|
-
});
|
|
94
|
-
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
95
|
-
return this.jobs[jobId];
|
|
96
|
-
}, _ProxySnapExecutor_terminateJob = function _ProxySnapExecutor_terminateJob(jobId) {
|
|
97
|
-
assert(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
98
|
-
const iframe = document.getElementById(jobId);
|
|
99
|
-
assert(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
100
|
-
iframe.remove();
|
|
101
|
-
this.jobs[jobId].stream.destroy();
|
|
102
|
-
delete this.jobs[jobId];
|
|
103
|
-
};
|
|
106
|
+
}
|
|
104
107
|
//# sourceMappingURL=ProxySnapExecutor.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxySnapExecutor.mjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ProxySnapExecutor.mjs","sourceRoot":"","sources":["../../src/pro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C;QAC9C,MAAM,CAAC,MAAM,EAAE,mBAAmB,KAAK,cAAc,CAAC,CAAC;QAEvD,MAAM,CAAC,MAAM,EAAE,CAAC;QAChB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAClC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow, logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport packageJson from '@metamask/snaps-execution-environments/package.json';\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"]}
|