@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,16 +1,4 @@
|
|
|
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 _WebWorkerPool_instances, _WebWorkerPool_poolSize, _WebWorkerPool_stream, _WebWorkerPool_url, _WebWorkerPool_workerSourceURL, _WebWorkerPool_onData, _WebWorkerPool_initializeJob, _WebWorkerPool_terminateJob, _WebWorkerPool_getWorker, _WebWorkerPool_updatePool, _WebWorkerPool_createWorker, _WebWorkerPool_getWorkerURL;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.WebWorkerPool = void 0;
|
|
16
4
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
@@ -26,6 +14,12 @@ const non_secure_1 = require("nanoid/non-secure");
|
|
|
26
14
|
* executing the snap.
|
|
27
15
|
*/
|
|
28
16
|
class WebWorkerPool {
|
|
17
|
+
#poolSize;
|
|
18
|
+
#stream;
|
|
19
|
+
#url;
|
|
20
|
+
pool = [];
|
|
21
|
+
jobs = new Map();
|
|
22
|
+
#workerSourceURL;
|
|
29
23
|
/* istanbul ignore next - Constructor arguments. */
|
|
30
24
|
static initialize(stream = new post_message_stream_1.WindowPostMessageStream({
|
|
31
25
|
name: 'child',
|
|
@@ -36,133 +30,142 @@ class WebWorkerPool {
|
|
|
36
30
|
return new WebWorkerPool(stream, url, poolSize);
|
|
37
31
|
}
|
|
38
32
|
constructor(stream, url, poolSize = 3) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.pool = [];
|
|
44
|
-
this.jobs = new Map();
|
|
45
|
-
_WebWorkerPool_workerSourceURL.set(this, void 0);
|
|
46
|
-
__classPrivateFieldSet(this, _WebWorkerPool_stream, stream, "f");
|
|
47
|
-
__classPrivateFieldSet(this, _WebWorkerPool_url, url, "f");
|
|
48
|
-
__classPrivateFieldSet(this, _WebWorkerPool_poolSize, poolSize, "f");
|
|
49
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").on('data', __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_onData).bind(this));
|
|
33
|
+
this.#stream = stream;
|
|
34
|
+
this.#url = url;
|
|
35
|
+
this.#poolSize = poolSize;
|
|
36
|
+
this.#stream.on('data', this.#onData.bind(this));
|
|
50
37
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Handle an incoming message from the `WebWorkerExecutionService`. This
|
|
40
|
+
* assumes that the message contains a `jobId` property, and a JSON-RPC
|
|
41
|
+
* request in the `data` property.
|
|
42
|
+
*
|
|
43
|
+
* @param data - The message data.
|
|
44
|
+
* @param data.data - The JSON-RPC request.
|
|
45
|
+
* @param data.jobId - The job ID.
|
|
46
|
+
*/
|
|
47
|
+
#onData(data) {
|
|
48
|
+
const { jobId, data: request } = data;
|
|
49
|
+
const job = this.jobs.get(jobId);
|
|
50
|
+
if (!job) {
|
|
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
|
+
(0, snaps_utils_1.logError)('[Worker] Error initializing job:', error.toString());
|
|
60
|
+
this.#stream.write({
|
|
61
|
+
jobId,
|
|
70
62
|
data: {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
name: 'command',
|
|
64
|
+
data: {
|
|
65
|
+
jsonrpc: '2.0',
|
|
66
|
+
id: request.id ?? null,
|
|
67
|
+
error: {
|
|
68
|
+
code: -32000,
|
|
69
|
+
message: 'Internal error',
|
|
70
|
+
},
|
|
76
71
|
},
|
|
77
72
|
},
|
|
78
|
-
}
|
|
73
|
+
});
|
|
79
74
|
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// This is a method specific to the `WebWorkerPool`, as the service itself
|
|
78
|
+
// does not have access to the workers directly.
|
|
79
|
+
if (request.method === 'terminateJob') {
|
|
80
|
+
this.#terminateJob(jobId);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
job.stream.write(request);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a new worker and set up a stream to communicate with it.
|
|
87
|
+
*
|
|
88
|
+
* @param jobId - The job ID.
|
|
89
|
+
* @returns The job.
|
|
90
|
+
*/
|
|
91
|
+
async #initializeJob(jobId) {
|
|
92
|
+
const worker = await this.#getWorker();
|
|
93
|
+
const jobStream = new post_message_stream_1.WebWorkerParentPostMessageStream({
|
|
94
|
+
worker,
|
|
80
95
|
});
|
|
81
|
-
|
|
96
|
+
// Write messages from the worker to the parent, wrapped with the job ID.
|
|
97
|
+
jobStream.on('data', (data) => {
|
|
98
|
+
this.#stream.write({ data, jobId });
|
|
99
|
+
});
|
|
100
|
+
const job = { id: jobId, worker, stream: jobStream };
|
|
101
|
+
this.jobs.set(jobId, job);
|
|
102
|
+
return job;
|
|
82
103
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
104
|
+
/**
|
|
105
|
+
* Terminate the job with the given ID. This will close the worker and delete
|
|
106
|
+
* the job from the internal job map.
|
|
107
|
+
*
|
|
108
|
+
* @param jobId - The job ID.
|
|
109
|
+
*/
|
|
110
|
+
#terminateJob(jobId) {
|
|
111
|
+
const job = this.jobs.get(jobId);
|
|
112
|
+
(0, utils_1.assert)(job, `Job "${jobId}" not found.`);
|
|
113
|
+
job.stream.destroy();
|
|
114
|
+
job.worker.terminate();
|
|
115
|
+
this.jobs.delete(jobId);
|
|
88
116
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
worker,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
jobStream.on('data', (data) => {
|
|
104
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").write({ data, jobId });
|
|
105
|
-
});
|
|
106
|
-
const job = { id: jobId, worker, stream: jobStream };
|
|
107
|
-
this.jobs.set(jobId, job);
|
|
108
|
-
return job;
|
|
109
|
-
}, _WebWorkerPool_terminateJob = function _WebWorkerPool_terminateJob(jobId) {
|
|
110
|
-
const job = this.jobs.get(jobId);
|
|
111
|
-
(0, utils_1.assert)(job, `Job "${jobId}" not found.`);
|
|
112
|
-
job.stream.destroy();
|
|
113
|
-
job.worker.terminate();
|
|
114
|
-
this.jobs.delete(jobId);
|
|
115
|
-
}, _WebWorkerPool_getWorker =
|
|
116
|
-
/**
|
|
117
|
-
* Get a worker from the pool. A new worker will be created automatically.
|
|
118
|
-
*
|
|
119
|
-
* @returns The worker.
|
|
120
|
-
*/
|
|
121
|
-
async function _WebWorkerPool_getWorker() {
|
|
122
|
-
// Lazily create the pool of workers.
|
|
123
|
-
if (this.pool.length === 0) {
|
|
124
|
-
await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_updatePool).call(this);
|
|
117
|
+
/**
|
|
118
|
+
* Get a worker from the pool. A new worker will be created automatically.
|
|
119
|
+
*
|
|
120
|
+
* @returns The worker.
|
|
121
|
+
*/
|
|
122
|
+
async #getWorker() {
|
|
123
|
+
// Lazily create the pool of workers.
|
|
124
|
+
if (this.pool.length === 0) {
|
|
125
|
+
await this.#updatePool();
|
|
126
|
+
}
|
|
127
|
+
const worker = this.pool.shift();
|
|
128
|
+
(0, utils_1.assert)(worker, 'Worker not found.');
|
|
129
|
+
await this.#updatePool();
|
|
130
|
+
return worker;
|
|
125
131
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
async function _WebWorkerPool_updatePool() {
|
|
136
|
-
while (this.pool.length < __classPrivateFieldGet(this, _WebWorkerPool_poolSize, "f")) {
|
|
137
|
-
const worker = await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_createWorker).call(this);
|
|
138
|
-
this.pool.push(worker);
|
|
132
|
+
/**
|
|
133
|
+
* Update the pool of workers. This will create new workers if the pool is
|
|
134
|
+
* below the minimum size.
|
|
135
|
+
*/
|
|
136
|
+
async #updatePool() {
|
|
137
|
+
while (this.pool.length < this.#poolSize) {
|
|
138
|
+
const worker = await this.#createWorker();
|
|
139
|
+
this.pool.push(worker);
|
|
140
|
+
}
|
|
139
141
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Create a new worker. This will fetch the worker source if it has not
|
|
144
|
+
* already been fetched.
|
|
145
|
+
*
|
|
146
|
+
* @returns The worker.
|
|
147
|
+
*/
|
|
148
|
+
async #createWorker() {
|
|
149
|
+
return new Worker(await this.#getWorkerURL(), {
|
|
150
|
+
name: `worker-${(0, non_secure_1.nanoid)()}`,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get the URL of the worker source. This will fetch the worker source if it
|
|
155
|
+
* has not already been fetched.
|
|
156
|
+
*
|
|
157
|
+
* @returns The worker source URL, as a `blob:` URL.
|
|
158
|
+
*/
|
|
159
|
+
async #getWorkerURL() {
|
|
160
|
+
if (this.#workerSourceURL) {
|
|
161
|
+
return this.#workerSourceURL;
|
|
162
|
+
}
|
|
163
|
+
const blob = await fetch(this.#url)
|
|
164
|
+
.then(async (response) => response.blob())
|
|
165
|
+
.then(URL.createObjectURL.bind(URL));
|
|
166
|
+
this.#workerSourceURL = blob;
|
|
167
|
+
return blob;
|
|
161
168
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.then(URL.createObjectURL.bind(URL));
|
|
165
|
-
__classPrivateFieldSet(this, _WebWorkerPool_workerSourceURL, blob, "f");
|
|
166
|
-
return blob;
|
|
167
|
-
};
|
|
169
|
+
}
|
|
170
|
+
exports.WebWorkerPool = WebWorkerPool;
|
|
168
171
|
//# sourceMappingURL=WebWorkerPool.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerPool.cjs","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebWorkerPool.cjs","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":";;;AACA,uEAGuC;AACvC,uDAAiD;AAEjD,2CAAyC;AACzC,kDAA2C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;;OAKG;IACH,KAAK,CAAC,aAAa;QACjB,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC,gBAAgB,CAAC;QAC/B,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;aAChC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;aACzC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAEvC,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA1LD,sCA0LC","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,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 _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
1
|
import $metamaskpostmessagestream from "@metamask/post-message-stream";
|
|
14
2
|
const { WebWorkerParentPostMessageStream, WindowPostMessageStream } = $metamaskpostmessagestream;
|
|
15
3
|
import { logError } from "@metamask/snaps-utils";
|
|
@@ -24,6 +12,12 @@ import { nanoid } from "nanoid/non-secure";
|
|
|
24
12
|
* executing the snap.
|
|
25
13
|
*/
|
|
26
14
|
export class WebWorkerPool {
|
|
15
|
+
#poolSize;
|
|
16
|
+
#stream;
|
|
17
|
+
#url;
|
|
18
|
+
pool = [];
|
|
19
|
+
jobs = new Map();
|
|
20
|
+
#workerSourceURL;
|
|
27
21
|
/* istanbul ignore next - Constructor arguments. */
|
|
28
22
|
static initialize(stream = new WindowPostMessageStream({
|
|
29
23
|
name: 'child',
|
|
@@ -34,132 +28,141 @@ export class WebWorkerPool {
|
|
|
34
28
|
return new WebWorkerPool(stream, url, poolSize);
|
|
35
29
|
}
|
|
36
30
|
constructor(stream, url, poolSize = 3) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.pool = [];
|
|
42
|
-
this.jobs = new Map();
|
|
43
|
-
_WebWorkerPool_workerSourceURL.set(this, void 0);
|
|
44
|
-
__classPrivateFieldSet(this, _WebWorkerPool_stream, stream, "f");
|
|
45
|
-
__classPrivateFieldSet(this, _WebWorkerPool_url, url, "f");
|
|
46
|
-
__classPrivateFieldSet(this, _WebWorkerPool_poolSize, poolSize, "f");
|
|
47
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").on('data', __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_onData).bind(this));
|
|
31
|
+
this.#stream = stream;
|
|
32
|
+
this.#url = url;
|
|
33
|
+
this.#poolSize = poolSize;
|
|
34
|
+
this.#stream.on('data', this.#onData.bind(this));
|
|
48
35
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Handle an incoming message from the `WebWorkerExecutionService`. This
|
|
38
|
+
* assumes that the message contains a `jobId` property, and a JSON-RPC
|
|
39
|
+
* request in the `data` property.
|
|
40
|
+
*
|
|
41
|
+
* @param data - The message data.
|
|
42
|
+
* @param data.data - The JSON-RPC request.
|
|
43
|
+
* @param data.jobId - The job ID.
|
|
44
|
+
*/
|
|
45
|
+
#onData(data) {
|
|
46
|
+
const { jobId, data: request } = data;
|
|
47
|
+
const job = this.jobs.get(jobId);
|
|
48
|
+
if (!job) {
|
|
49
|
+
// This ensures that a job is initialized before it is used. To avoid
|
|
50
|
+
// code duplication, we call the `#onData` method again, which will
|
|
51
|
+
// run the rest of the logic after initialization.
|
|
52
|
+
this.#initializeJob(jobId)
|
|
53
|
+
.then(() => {
|
|
54
|
+
this.#onData(data);
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
logError('[Worker] Error initializing job:', error.toString());
|
|
58
|
+
this.#stream.write({
|
|
59
|
+
jobId,
|
|
67
60
|
data: {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
61
|
+
name: 'command',
|
|
62
|
+
data: {
|
|
63
|
+
jsonrpc: '2.0',
|
|
64
|
+
id: request.id ?? null,
|
|
65
|
+
error: {
|
|
66
|
+
code: -32000,
|
|
67
|
+
message: 'Internal error',
|
|
68
|
+
},
|
|
73
69
|
},
|
|
74
70
|
},
|
|
75
|
-
}
|
|
71
|
+
});
|
|
76
72
|
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// This is a method specific to the `WebWorkerPool`, as the service itself
|
|
76
|
+
// does not have access to the workers directly.
|
|
77
|
+
if (request.method === 'terminateJob') {
|
|
78
|
+
this.#terminateJob(jobId);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
job.stream.write(request);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create a new worker and set up a stream to communicate with it.
|
|
85
|
+
*
|
|
86
|
+
* @param jobId - The job ID.
|
|
87
|
+
* @returns The job.
|
|
88
|
+
*/
|
|
89
|
+
async #initializeJob(jobId) {
|
|
90
|
+
const worker = await this.#getWorker();
|
|
91
|
+
const jobStream = new WebWorkerParentPostMessageStream({
|
|
92
|
+
worker,
|
|
93
|
+
});
|
|
94
|
+
// Write messages from the worker to the parent, wrapped with the job ID.
|
|
95
|
+
jobStream.on('data', (data) => {
|
|
96
|
+
this.#stream.write({ data, jobId });
|
|
77
97
|
});
|
|
78
|
-
|
|
98
|
+
const job = { id: jobId, worker, stream: jobStream };
|
|
99
|
+
this.jobs.set(jobId, job);
|
|
100
|
+
return job;
|
|
79
101
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Terminate the job with the given ID. This will close the worker and delete
|
|
104
|
+
* the job from the internal job map.
|
|
105
|
+
*
|
|
106
|
+
* @param jobId - The job ID.
|
|
107
|
+
*/
|
|
108
|
+
#terminateJob(jobId) {
|
|
109
|
+
const job = this.jobs.get(jobId);
|
|
110
|
+
assert(job, `Job "${jobId}" not found.`);
|
|
111
|
+
job.stream.destroy();
|
|
112
|
+
job.worker.terminate();
|
|
113
|
+
this.jobs.delete(jobId);
|
|
85
114
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
worker,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
jobStream.on('data', (data) => {
|
|
101
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").write({ data, jobId });
|
|
102
|
-
});
|
|
103
|
-
const job = { id: jobId, worker, stream: jobStream };
|
|
104
|
-
this.jobs.set(jobId, job);
|
|
105
|
-
return job;
|
|
106
|
-
}, _WebWorkerPool_terminateJob = function _WebWorkerPool_terminateJob(jobId) {
|
|
107
|
-
const job = this.jobs.get(jobId);
|
|
108
|
-
assert(job, `Job "${jobId}" not found.`);
|
|
109
|
-
job.stream.destroy();
|
|
110
|
-
job.worker.terminate();
|
|
111
|
-
this.jobs.delete(jobId);
|
|
112
|
-
}, _WebWorkerPool_getWorker =
|
|
113
|
-
/**
|
|
114
|
-
* Get a worker from the pool. A new worker will be created automatically.
|
|
115
|
-
*
|
|
116
|
-
* @returns The worker.
|
|
117
|
-
*/
|
|
118
|
-
async function _WebWorkerPool_getWorker() {
|
|
119
|
-
// Lazily create the pool of workers.
|
|
120
|
-
if (this.pool.length === 0) {
|
|
121
|
-
await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_updatePool).call(this);
|
|
115
|
+
/**
|
|
116
|
+
* Get a worker from the pool. A new worker will be created automatically.
|
|
117
|
+
*
|
|
118
|
+
* @returns The worker.
|
|
119
|
+
*/
|
|
120
|
+
async #getWorker() {
|
|
121
|
+
// Lazily create the pool of workers.
|
|
122
|
+
if (this.pool.length === 0) {
|
|
123
|
+
await this.#updatePool();
|
|
124
|
+
}
|
|
125
|
+
const worker = this.pool.shift();
|
|
126
|
+
assert(worker, 'Worker not found.');
|
|
127
|
+
await this.#updatePool();
|
|
128
|
+
return worker;
|
|
122
129
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
async function _WebWorkerPool_updatePool() {
|
|
133
|
-
while (this.pool.length < __classPrivateFieldGet(this, _WebWorkerPool_poolSize, "f")) {
|
|
134
|
-
const worker = await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_createWorker).call(this);
|
|
135
|
-
this.pool.push(worker);
|
|
130
|
+
/**
|
|
131
|
+
* Update the pool of workers. This will create new workers if the pool is
|
|
132
|
+
* below the minimum size.
|
|
133
|
+
*/
|
|
134
|
+
async #updatePool() {
|
|
135
|
+
while (this.pool.length < this.#poolSize) {
|
|
136
|
+
const worker = await this.#createWorker();
|
|
137
|
+
this.pool.push(worker);
|
|
138
|
+
}
|
|
136
139
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
});
|
|
148
|
-
}, _WebWorkerPool_getWorkerURL =
|
|
149
|
-
/**
|
|
150
|
-
* Get the URL of the worker source. This will fetch the worker source if it
|
|
151
|
-
* has not already been fetched.
|
|
152
|
-
*
|
|
153
|
-
* @returns The worker source URL, as a `blob:` URL.
|
|
154
|
-
*/
|
|
155
|
-
async function _WebWorkerPool_getWorkerURL() {
|
|
156
|
-
if (__classPrivateFieldGet(this, _WebWorkerPool_workerSourceURL, "f")) {
|
|
157
|
-
return __classPrivateFieldGet(this, _WebWorkerPool_workerSourceURL, "f");
|
|
140
|
+
/**
|
|
141
|
+
* Create a new worker. This will fetch the worker source if it has not
|
|
142
|
+
* already been fetched.
|
|
143
|
+
*
|
|
144
|
+
* @returns The worker.
|
|
145
|
+
*/
|
|
146
|
+
async #createWorker() {
|
|
147
|
+
return new Worker(await this.#getWorkerURL(), {
|
|
148
|
+
name: `worker-${nanoid()}`,
|
|
149
|
+
});
|
|
158
150
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
151
|
+
/**
|
|
152
|
+
* Get the URL of the worker source. This will fetch the worker source if it
|
|
153
|
+
* has not already been fetched.
|
|
154
|
+
*
|
|
155
|
+
* @returns The worker source URL, as a `blob:` URL.
|
|
156
|
+
*/
|
|
157
|
+
async #getWorkerURL() {
|
|
158
|
+
if (this.#workerSourceURL) {
|
|
159
|
+
return this.#workerSourceURL;
|
|
160
|
+
}
|
|
161
|
+
const blob = await fetch(this.#url)
|
|
162
|
+
.then(async (response) => response.blob())
|
|
163
|
+
.then(URL.createObjectURL.bind(URL));
|
|
164
|
+
this.#workerSourceURL = blob;
|
|
165
|
+
return blob;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
165
168
|
//# sourceMappingURL=WebWorkerPool.mjs.map
|