@metamask/snaps-execution-environments 6.7.2 → 6.8.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 +105 -1
- package/dist/browserify/iframe/bundle.js +1 -1
- package/dist/browserify/node-process/bundle.js +1 -1
- package/dist/browserify/node-thread/bundle.js +1 -1
- package/dist/browserify/webview/index.html +4 -4
- package/dist/browserify/worker-executor/bundle.js +3 -3
- package/dist/browserify/worker-pool/bundle.js +3 -3
- package/dist/common/BaseSnapExecutor.cjs +58 -58
- package/dist/common/BaseSnapExecutor.cjs.map +1 -1
- package/dist/common/BaseSnapExecutor.mjs +58 -58
- package/dist/common/BaseSnapExecutor.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/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/dist/webworker/pool/WebWorkerPool.cjs +133 -130
- package/dist/webworker/pool/WebWorkerPool.cjs.map +1 -1
- package/dist/webworker/pool/WebWorkerPool.mjs +132 -129
- package/dist/webworker/pool/WebWorkerPool.mjs.map +1 -1
- package/package.json +37 -26
|
@@ -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");
|
|
@@ -36,6 +24,9 @@ const IFRAME_URL = `https://execution.metamask.io/iframe/${package_json_1.defaul
|
|
|
36
24
|
* acts as a proxy between the client and the iframe execution environment.
|
|
37
25
|
*/
|
|
38
26
|
class ProxySnapExecutor {
|
|
27
|
+
#stream;
|
|
28
|
+
#frameUrl;
|
|
29
|
+
jobs = {};
|
|
39
30
|
/**
|
|
40
31
|
* Initialize the executor with the given stream. This is a wrapper around the
|
|
41
32
|
* constructor.
|
|
@@ -48,65 +39,77 @@ class ProxySnapExecutor {
|
|
|
48
39
|
return new ProxySnapExecutor(stream, frameUrl);
|
|
49
40
|
}
|
|
50
41
|
constructor(stream, frameUrl) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.jobs = {};
|
|
55
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_stream, stream, "f");
|
|
56
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").on('data', __classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).bind(this));
|
|
57
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_frameUrl, frameUrl, "f");
|
|
42
|
+
this.#stream = stream;
|
|
43
|
+
this.#stream.on('data', this.#onData.bind(this));
|
|
44
|
+
this.#frameUrl = frameUrl;
|
|
58
45
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
.
|
|
72
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Handle an incoming message from a `ProxyExecutionService`. This
|
|
48
|
+
* assumes that the message contains a `jobId` property, and a JSON-RPC
|
|
49
|
+
* request in the `data` property.
|
|
50
|
+
*
|
|
51
|
+
* @param data - The message data.
|
|
52
|
+
* @param data.data - The JSON-RPC request.
|
|
53
|
+
* @param data.jobId - The job ID.
|
|
54
|
+
*/
|
|
55
|
+
#onData(data) {
|
|
56
|
+
const { jobId, data: request } = data;
|
|
57
|
+
if (!this.jobs[jobId]) {
|
|
58
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
59
|
+
// code duplication, we call the `#onData` method again, which will
|
|
60
|
+
// run the rest of the logic after initialization.
|
|
61
|
+
this.#initializeJob(jobId)
|
|
62
|
+
.then(() => {
|
|
63
|
+
this.#onData(data);
|
|
64
|
+
})
|
|
65
|
+
.catch((error) => {
|
|
66
|
+
(0, snaps_utils_1.logError)('[Worker] Error initializing job:', error);
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
71
|
+
// itself does not have access to the iframes directly.
|
|
72
|
+
if (request.method === 'terminateJob') {
|
|
73
|
+
this.#terminateJob(jobId);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
this.jobs[jobId].stream.write(request);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create a new iframe and set up a stream to communicate with it.
|
|
80
|
+
*
|
|
81
|
+
* @param jobId - The job ID.
|
|
82
|
+
* @returns The executor job object.
|
|
83
|
+
*/
|
|
84
|
+
async #initializeJob(jobId) {
|
|
85
|
+
const window = await (0, snaps_utils_1.createWindow)(this.#frameUrl, jobId);
|
|
86
|
+
const jobStream = new post_message_stream_1.WindowPostMessageStream({
|
|
87
|
+
name: 'parent',
|
|
88
|
+
target: 'child',
|
|
89
|
+
targetWindow: window, // iframe's internal window
|
|
90
|
+
targetOrigin: '*',
|
|
73
91
|
});
|
|
74
|
-
|
|
92
|
+
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
93
|
+
jobStream.on('data', (data) => {
|
|
94
|
+
this.#stream.write({ data, jobId });
|
|
95
|
+
});
|
|
96
|
+
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
97
|
+
return this.jobs[jobId];
|
|
75
98
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Terminate the job with the given ID. This will close the iframe and delete
|
|
101
|
+
* the job from the internal job map.
|
|
102
|
+
*
|
|
103
|
+
* @param jobId - The job ID.
|
|
104
|
+
*/
|
|
105
|
+
#terminateJob(jobId) {
|
|
106
|
+
(0, utils_1.assert)(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
107
|
+
const iframe = document.getElementById(jobId);
|
|
108
|
+
(0, utils_1.assert)(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
109
|
+
iframe.remove();
|
|
110
|
+
this.jobs[jobId].stream.destroy();
|
|
111
|
+
delete this.jobs[jobId];
|
|
81
112
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Create a new iframe and set up a stream to communicate with it.
|
|
86
|
-
*
|
|
87
|
-
* @param jobId - The job ID.
|
|
88
|
-
* @returns The executor job object.
|
|
89
|
-
*/
|
|
90
|
-
async function _ProxySnapExecutor_initializeJob(jobId) {
|
|
91
|
-
const window = await (0, snaps_utils_1.createWindow)(__classPrivateFieldGet(this, _ProxySnapExecutor_frameUrl, "f"), jobId);
|
|
92
|
-
const jobStream = new post_message_stream_1.WindowPostMessageStream({
|
|
93
|
-
name: 'parent',
|
|
94
|
-
target: 'child',
|
|
95
|
-
targetWindow: window, // iframe's internal window
|
|
96
|
-
targetOrigin: '*',
|
|
97
|
-
});
|
|
98
|
-
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
99
|
-
jobStream.on('data', (data) => {
|
|
100
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").write({ data, jobId });
|
|
101
|
-
});
|
|
102
|
-
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
103
|
-
return this.jobs[jobId];
|
|
104
|
-
}, _ProxySnapExecutor_terminateJob = function _ProxySnapExecutor_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];
|
|
111
|
-
};
|
|
113
|
+
}
|
|
114
|
+
exports.ProxySnapExecutor = ProxySnapExecutor;
|
|
112
115
|
//# sourceMappingURL=ProxySnapExecutor.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxySnapExecutor.cjs","sourceRoot":"","sources":["../../src/proxy/ProxySnapExecutor.ts"],"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AAC,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';\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(this.#frameUrl, 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 $metamaskpostmessagestream from "@metamask/post-message-stream";
|
|
14
2
|
const { WindowPostMessageStream } = $metamaskpostmessagestream;
|
|
15
3
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
@@ -31,6 +19,9 @@ const IFRAME_URL = `https://execution.metamask.io/iframe/${packageJson.version}/
|
|
|
31
19
|
* acts as a proxy between the client and the iframe execution environment.
|
|
32
20
|
*/
|
|
33
21
|
export class ProxySnapExecutor {
|
|
22
|
+
#stream;
|
|
23
|
+
#frameUrl;
|
|
24
|
+
jobs = {};
|
|
34
25
|
/**
|
|
35
26
|
* Initialize the executor with the given stream. This is a wrapper around the
|
|
36
27
|
* constructor.
|
|
@@ -43,64 +34,76 @@ export class ProxySnapExecutor {
|
|
|
43
34
|
return new ProxySnapExecutor(stream, frameUrl);
|
|
44
35
|
}
|
|
45
36
|
constructor(stream, frameUrl) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
this.jobs = {};
|
|
50
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_stream, stream, "f");
|
|
51
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").on('data', __classPrivateFieldGet(this, _ProxySnapExecutor_instances, "m", _ProxySnapExecutor_onData).bind(this));
|
|
52
|
-
__classPrivateFieldSet(this, _ProxySnapExecutor_frameUrl, frameUrl, "f");
|
|
37
|
+
this.#stream = stream;
|
|
38
|
+
this.#stream.on('data', this.#onData.bind(this));
|
|
39
|
+
this.#frameUrl = frameUrl;
|
|
53
40
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Handle an incoming message from a `ProxyExecutionService`. This
|
|
43
|
+
* assumes that the message contains a `jobId` property, and a JSON-RPC
|
|
44
|
+
* request in the `data` property.
|
|
45
|
+
*
|
|
46
|
+
* @param data - The message data.
|
|
47
|
+
* @param data.data - The JSON-RPC request.
|
|
48
|
+
* @param data.jobId - The job ID.
|
|
49
|
+
*/
|
|
50
|
+
#onData(data) {
|
|
51
|
+
const { jobId, data: request } = data;
|
|
52
|
+
if (!this.jobs[jobId]) {
|
|
53
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
54
|
+
// code duplication, we call the `#onData` method again, which will
|
|
55
|
+
// run the rest of the logic after initialization.
|
|
56
|
+
this.#initializeJob(jobId)
|
|
57
|
+
.then(() => {
|
|
58
|
+
this.#onData(data);
|
|
59
|
+
})
|
|
60
|
+
.catch((error) => {
|
|
61
|
+
logError('[Worker] Error initializing job:', error);
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// This is a method specific to the `OffscreenSnapExecutor`, as the service
|
|
66
|
+
// itself does not have access to the iframes directly.
|
|
67
|
+
if (request.method === 'terminateJob') {
|
|
68
|
+
this.#terminateJob(jobId);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.jobs[jobId].stream.write(request);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a new iframe and set up a stream to communicate with it.
|
|
75
|
+
*
|
|
76
|
+
* @param jobId - The job ID.
|
|
77
|
+
* @returns The executor job object.
|
|
78
|
+
*/
|
|
79
|
+
async #initializeJob(jobId) {
|
|
80
|
+
const window = await createWindow(this.#frameUrl, jobId);
|
|
81
|
+
const jobStream = new WindowPostMessageStream({
|
|
82
|
+
name: 'parent',
|
|
83
|
+
target: 'child',
|
|
84
|
+
targetWindow: window, // iframe's internal window
|
|
85
|
+
targetOrigin: '*',
|
|
67
86
|
});
|
|
68
|
-
|
|
87
|
+
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
88
|
+
jobStream.on('data', (data) => {
|
|
89
|
+
this.#stream.write({ data, jobId });
|
|
90
|
+
});
|
|
91
|
+
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
92
|
+
return this.jobs[jobId];
|
|
69
93
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Terminate the job with the given ID. This will close the iframe and delete
|
|
96
|
+
* the job from the internal job map.
|
|
97
|
+
*
|
|
98
|
+
* @param jobId - The job ID.
|
|
99
|
+
*/
|
|
100
|
+
#terminateJob(jobId) {
|
|
101
|
+
assert(this.jobs[jobId], `Job "${jobId}" not found.`);
|
|
102
|
+
const iframe = document.getElementById(jobId);
|
|
103
|
+
assert(iframe, `Iframe with ID "${jobId}" not found.`);
|
|
104
|
+
iframe.remove();
|
|
105
|
+
this.jobs[jobId].stream.destroy();
|
|
106
|
+
delete this.jobs[jobId];
|
|
75
107
|
}
|
|
76
|
-
|
|
77
|
-
}, _ProxySnapExecutor_initializeJob =
|
|
78
|
-
/**
|
|
79
|
-
* Create a new iframe and set up a stream to communicate with it.
|
|
80
|
-
*
|
|
81
|
-
* @param jobId - The job ID.
|
|
82
|
-
* @returns The executor job object.
|
|
83
|
-
*/
|
|
84
|
-
async function _ProxySnapExecutor_initializeJob(jobId) {
|
|
85
|
-
const window = await createWindow(__classPrivateFieldGet(this, _ProxySnapExecutor_frameUrl, "f"), jobId);
|
|
86
|
-
const jobStream = new WindowPostMessageStream({
|
|
87
|
-
name: 'parent',
|
|
88
|
-
target: 'child',
|
|
89
|
-
targetWindow: window, // iframe's internal window
|
|
90
|
-
targetOrigin: '*',
|
|
91
|
-
});
|
|
92
|
-
// Write messages from the iframe to the parent, wrapped with the job ID.
|
|
93
|
-
jobStream.on('data', (data) => {
|
|
94
|
-
__classPrivateFieldGet(this, _ProxySnapExecutor_stream, "f").write({ data, jobId });
|
|
95
|
-
});
|
|
96
|
-
this.jobs[jobId] = { id: jobId, window, stream: jobStream };
|
|
97
|
-
return this.jobs[jobId];
|
|
98
|
-
}, _ProxySnapExecutor_terminateJob = function _ProxySnapExecutor_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];
|
|
105
|
-
};
|
|
108
|
+
}
|
|
106
109
|
//# 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/proxy/ProxySnapExecutor.ts"],"names":[],"mappings":";;AAEA,6DAA6D;AAC7D,OAAO,WAAW,kFAA4D;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,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';\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(this.#frameUrl, 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,22 +1,13 @@
|
|
|
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 _WebViewExecutorStream_name, _WebViewExecutorStream_target, _WebViewExecutorStream_targetWindow;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.WebViewExecutorStream = void 0;
|
|
16
4
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
17
5
|
const utils_1 = require("@metamask/post-message-stream/dist/utils.js");
|
|
18
6
|
const utils_2 = require("@metamask/utils");
|
|
19
7
|
class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream {
|
|
8
|
+
#name;
|
|
9
|
+
#target;
|
|
10
|
+
#targetWindow;
|
|
20
11
|
/**
|
|
21
12
|
* A special post-message-stream to be used by the WebView executor.
|
|
22
13
|
*
|
|
@@ -33,12 +24,9 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
33
24
|
*/
|
|
34
25
|
constructor({ name, target, targetWindow }) {
|
|
35
26
|
super();
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
__classPrivateFieldSet(this, _WebViewExecutorStream_name, name, "f");
|
|
40
|
-
__classPrivateFieldSet(this, _WebViewExecutorStream_target, target, "f");
|
|
41
|
-
__classPrivateFieldSet(this, _WebViewExecutorStream_targetWindow, targetWindow, "f");
|
|
27
|
+
this.#name = name;
|
|
28
|
+
this.#target = target;
|
|
29
|
+
this.#targetWindow = targetWindow;
|
|
42
30
|
this._onMessage = this._onMessage.bind(this);
|
|
43
31
|
// This method is already bound.
|
|
44
32
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
@@ -50,8 +38,8 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
50
38
|
* Reference: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md?plain=1#L471
|
|
51
39
|
*/
|
|
52
40
|
_postMessage(data) {
|
|
53
|
-
|
|
54
|
-
target:
|
|
41
|
+
this.#targetWindow.postMessage(JSON.stringify({
|
|
42
|
+
target: this.#target,
|
|
55
43
|
data,
|
|
56
44
|
}));
|
|
57
45
|
}
|
|
@@ -63,7 +51,7 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
63
51
|
const message = JSON.parse((0, utils_2.bytesToString)(bytes));
|
|
64
52
|
// Notice that we don't check targetWindow or targetOrigin here.
|
|
65
53
|
// This doesn't seem possible to do in RN.
|
|
66
|
-
if (!(0, utils_1.isValidStreamMessage)(message) || message.target !==
|
|
54
|
+
if (!(0, utils_1.isValidStreamMessage)(message) || message.target !== this.#name) {
|
|
67
55
|
return;
|
|
68
56
|
}
|
|
69
57
|
this._onData(message.data);
|
|
@@ -75,5 +63,4 @@ class WebViewExecutorStream extends post_message_stream_1.BasePostMessageStream
|
|
|
75
63
|
}
|
|
76
64
|
}
|
|
77
65
|
exports.WebViewExecutorStream = WebViewExecutorStream;
|
|
78
|
-
_WebViewExecutorStream_name = new WeakMap(), _WebViewExecutorStream_target = new WeakMap(), _WebViewExecutorStream_targetWindow = new WeakMap();
|
|
79
66
|
//# sourceMappingURL=WebViewExecutorStream.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewExecutorStream.cjs","sourceRoot":"","sources":["../../src/webview/WebViewExecutorStream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebViewExecutorStream.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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,gCAAgC;QAChC,6DAA6D;QAC7D,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,UAAiB,EAAE,KAAK,CAAC,CAAC;IACvE,CAAC;CACF;AA1ED,sDA0EC","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,20 +1,11 @@
|
|
|
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 _WebViewExecutorStream_name, _WebViewExecutorStream_target, _WebViewExecutorStream_targetWindow;
|
|
13
1
|
import $metamaskpostmessagestream from "@metamask/post-message-stream";
|
|
14
2
|
const { BasePostMessageStream } = $metamaskpostmessagestream;
|
|
15
3
|
import { isValidStreamMessage } from "@metamask/post-message-stream/dist/utils.js";
|
|
16
4
|
import { base64ToBytes, bytesToString } from "@metamask/utils";
|
|
17
5
|
export class WebViewExecutorStream extends BasePostMessageStream {
|
|
6
|
+
#name;
|
|
7
|
+
#target;
|
|
8
|
+
#targetWindow;
|
|
18
9
|
/**
|
|
19
10
|
* A special post-message-stream to be used by the WebView executor.
|
|
20
11
|
*
|
|
@@ -31,12 +22,9 @@ export class WebViewExecutorStream extends BasePostMessageStream {
|
|
|
31
22
|
*/
|
|
32
23
|
constructor({ name, target, targetWindow }) {
|
|
33
24
|
super();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
__classPrivateFieldSet(this, _WebViewExecutorStream_name, name, "f");
|
|
38
|
-
__classPrivateFieldSet(this, _WebViewExecutorStream_target, target, "f");
|
|
39
|
-
__classPrivateFieldSet(this, _WebViewExecutorStream_targetWindow, targetWindow, "f");
|
|
25
|
+
this.#name = name;
|
|
26
|
+
this.#target = target;
|
|
27
|
+
this.#targetWindow = targetWindow;
|
|
40
28
|
this._onMessage = this._onMessage.bind(this);
|
|
41
29
|
// This method is already bound.
|
|
42
30
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
@@ -48,8 +36,8 @@ export class WebViewExecutorStream extends BasePostMessageStream {
|
|
|
48
36
|
* Reference: https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md?plain=1#L471
|
|
49
37
|
*/
|
|
50
38
|
_postMessage(data) {
|
|
51
|
-
|
|
52
|
-
target:
|
|
39
|
+
this.#targetWindow.postMessage(JSON.stringify({
|
|
40
|
+
target: this.#target,
|
|
53
41
|
data,
|
|
54
42
|
}));
|
|
55
43
|
}
|
|
@@ -61,7 +49,7 @@ export class WebViewExecutorStream extends BasePostMessageStream {
|
|
|
61
49
|
const message = JSON.parse(bytesToString(bytes));
|
|
62
50
|
// Notice that we don't check targetWindow or targetOrigin here.
|
|
63
51
|
// This doesn't seem possible to do in RN.
|
|
64
|
-
if (!isValidStreamMessage(message) || message.target !==
|
|
52
|
+
if (!isValidStreamMessage(message) || message.target !== this.#name) {
|
|
65
53
|
return;
|
|
66
54
|
}
|
|
67
55
|
this._onData(message.data);
|
|
@@ -72,5 +60,4 @@ export class WebViewExecutorStream extends BasePostMessageStream {
|
|
|
72
60
|
window.removeEventListener('message', this._onMessage, false);
|
|
73
61
|
}
|
|
74
62
|
}
|
|
75
|
-
_WebViewExecutorStream_name = new WeakMap(), _WebViewExecutorStream_target = new WeakMap(), _WebViewExecutorStream_targetWindow = new WeakMap();
|
|
76
63
|
//# sourceMappingURL=WebViewExecutorStream.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewExecutorStream.mjs","sourceRoot":"","sources":["../../src/webview/WebViewExecutorStream.ts"],"names":[],"mappings":"
|
|
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;IAC9D,KAAK,CAAC;IAEN,OAAO,CAAC;IAER,aAAa,CAAC;IAEd;;;;;;;;;;;;;OAaG;IAEH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAA6B;QACnE,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,aAAa,GAAG,YAAY,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,IAAI,CAAC,aAAa,CAAC,WAAW,CAC5B,IAAI,CAAC,SAAS,CAAC;YACb,MAAM,EAAE,IAAI,CAAC,OAAO;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,IAAI,CAAC,KAAK,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"]}
|