@metamask/snaps-execution-environments 7.2.2 → 8.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 +20 -1
- package/dist/common/BaseSnapExecutor.cjs +12 -10
- package/dist/common/BaseSnapExecutor.cjs.map +1 -1
- package/dist/common/BaseSnapExecutor.d.cts.map +1 -1
- package/dist/common/BaseSnapExecutor.d.mts.map +1 -1
- package/dist/common/BaseSnapExecutor.mjs +12 -10
- package/dist/common/BaseSnapExecutor.mjs.map +1 -1
- package/dist/common/endowments/network.cjs +4 -4
- package/dist/common/endowments/network.cjs.map +1 -1
- package/dist/common/endowments/network.d.cts.map +1 -1
- package/dist/common/endowments/network.d.mts.map +1 -1
- package/dist/common/endowments/network.mjs +4 -4
- package/dist/common/endowments/network.mjs.map +1 -1
- package/dist/node-process/ChildProcessSnapExecutor.cjs +2 -2
- package/dist/node-process/ChildProcessSnapExecutor.cjs.map +1 -1
- package/dist/node-process/ChildProcessSnapExecutor.mjs +1 -1
- package/dist/node-process/ChildProcessSnapExecutor.mjs.map +1 -1
- package/dist/node-thread/ThreadSnapExecutor.cjs +2 -2
- package/dist/node-thread/ThreadSnapExecutor.cjs.map +1 -1
- package/dist/node-thread/ThreadSnapExecutor.mjs +1 -1
- package/dist/node-thread/ThreadSnapExecutor.mjs.map +1 -1
- package/dist/webpack/iframe/bundle.js +2 -0
- package/dist/webpack/iframe/bundle.js.LICENSE.txt +10 -0
- package/dist/webpack/iframe/index.html +13146 -0
- package/dist/webpack/node-process/bundle.js +12834 -0
- package/dist/{browserify/worker-pool/index.html → webpack/node-process/bundle.js.LICENSE.txt} +11668 -12229
- package/dist/webpack/node-thread/bundle.js +12834 -0
- package/dist/{browserify/iframe/index.html → webpack/node-thread/bundle.js.LICENSE.txt} +11668 -12229
- package/dist/webpack/webview/bundle.js.LICENSE.txt +10 -0
- package/dist/webpack/webview/index.html +13147 -0
- package/dist/webview/WebViewExecutorStream.cjs +1 -2
- package/dist/webview/WebViewExecutorStream.cjs.map +1 -1
- package/dist/webview/WebViewExecutorStream.d.cts.map +1 -1
- package/dist/webview/WebViewExecutorStream.d.mts.map +1 -1
- package/dist/webview/WebViewExecutorStream.mjs +1 -2
- package/dist/webview/WebViewExecutorStream.mjs.map +1 -1
- package/package.json +22 -23
- package/dist/browserify/iframe/bundle.js +0 -9
- package/dist/browserify/node-process/bundle.js +0 -13390
- package/dist/browserify/node-thread/bundle.js +0 -13390
- package/dist/browserify/webview/index.html +0 -13402
- package/dist/browserify/worker-executor/bundle.js +0 -13392
- package/dist/browserify/worker-pool/bundle.js +0 -9
- package/dist/webworker/executor/WebWorkerSnapExecutor.cjs +0 -37
- package/dist/webworker/executor/WebWorkerSnapExecutor.cjs.map +0 -1
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.cts +0 -14
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.cts.map +0 -1
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.mts +0 -14
- package/dist/webworker/executor/WebWorkerSnapExecutor.d.mts.map +0 -1
- package/dist/webworker/executor/WebWorkerSnapExecutor.mjs +0 -37
- package/dist/webworker/executor/WebWorkerSnapExecutor.mjs.map +0 -1
- package/dist/webworker/executor/index.cjs +0 -10
- package/dist/webworker/executor/index.cjs.map +0 -1
- package/dist/webworker/executor/index.d.cts +0 -2
- package/dist/webworker/executor/index.d.cts.map +0 -1
- package/dist/webworker/executor/index.d.mts +0 -2
- package/dist/webworker/executor/index.d.mts.map +0 -1
- package/dist/webworker/executor/index.mjs +0 -8
- package/dist/webworker/executor/index.mjs.map +0 -1
- package/dist/webworker/pool/WebWorkerPool.cjs +0 -168
- package/dist/webworker/pool/WebWorkerPool.cjs.map +0 -1
- package/dist/webworker/pool/WebWorkerPool.d.cts +0 -24
- package/dist/webworker/pool/WebWorkerPool.d.cts.map +0 -1
- package/dist/webworker/pool/WebWorkerPool.d.mts +0 -24
- package/dist/webworker/pool/WebWorkerPool.d.mts.map +0 -1
- package/dist/webworker/pool/WebWorkerPool.mjs +0 -164
- package/dist/webworker/pool/WebWorkerPool.mjs.map +0 -1
- package/dist/webworker/pool/index.cjs +0 -10
- package/dist/webworker/pool/index.cjs.map +0 -1
- package/dist/webworker/pool/index.d.cts +0 -2
- package/dist/webworker/pool/index.d.cts.map +0 -1
- package/dist/webworker/pool/index.d.mts +0 -2
- package/dist/webworker/pool/index.d.mts.map +0 -1
- package/dist/webworker/pool/index.mjs +0 -8
- package/dist/webworker/pool/index.mjs.map +0 -1
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.WebWorkerSnapExecutor = void 0;
|
|
7
|
-
const object_multiplex_1 = __importDefault(require("@metamask/object-multiplex"));
|
|
8
|
-
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
9
|
-
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
10
|
-
const readable_stream_1 = require("readable-stream");
|
|
11
|
-
const BaseSnapExecutor_1 = require("../../common/BaseSnapExecutor.cjs");
|
|
12
|
-
const logging_1 = require("../../logging.cjs");
|
|
13
|
-
class WebWorkerSnapExecutor extends BaseSnapExecutor_1.BaseSnapExecutor {
|
|
14
|
-
/**
|
|
15
|
-
* Initialize the WebWorkerSnapExecutor. This creates a post message stream
|
|
16
|
-
* from and to the parent window, for two-way communication with the iframe.
|
|
17
|
-
*
|
|
18
|
-
* @param stream - The stream to use for communication.
|
|
19
|
-
* @returns An instance of `WebWorkerSnapExecutor`, with the initialized post
|
|
20
|
-
* message streams.
|
|
21
|
-
*/
|
|
22
|
-
static initialize(stream = new post_message_stream_1.WebWorkerPostMessageStream()) {
|
|
23
|
-
(0, logging_1.log)('Worker: Connecting to parent.');
|
|
24
|
-
const mux = new object_multiplex_1.default();
|
|
25
|
-
(0, readable_stream_1.pipeline)(stream, mux, stream, (error) => {
|
|
26
|
-
if (error) {
|
|
27
|
-
(0, snaps_utils_1.logError)(`Parent stream failure, closing worker.`, error);
|
|
28
|
-
}
|
|
29
|
-
self.close();
|
|
30
|
-
});
|
|
31
|
-
const commandStream = mux.createStream(snaps_utils_1.SNAP_STREAM_NAMES.COMMAND);
|
|
32
|
-
const rpcStream = mux.createStream(snaps_utils_1.SNAP_STREAM_NAMES.JSON_RPC);
|
|
33
|
-
return new WebWorkerSnapExecutor(commandStream, rpcStream);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.WebWorkerSnapExecutor = WebWorkerSnapExecutor;
|
|
37
|
-
//# sourceMappingURL=WebWorkerSnapExecutor.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerSnapExecutor.cjs","sourceRoot":"","sources":["../../../src/webworker/executor/WebWorkerSnapExecutor.ts"],"names":[],"mappings":";;;;;;AAAA,kFAAyD;AAEzD,uEAA2E;AAC3E,uDAAoE;AACpE,qDAA2C;AAE3C,wEAAiE;AACjE,+CAAoC;AAEpC,MAAa,qBAAsB,SAAQ,mCAAgB;IACzD;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,gDAA0B,EAAE;QAEhE,IAAA,aAAG,EAAC,+BAA+B,CAAC,CAAC;QAErC,MAAM,GAAG,GAAG,IAAI,0BAAe,EAAE,CAAC;QAClC,IAAA,0BAAQ,EAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACtC,IAAI,KAAK,EAAE,CAAC;gBACV,IAAA,sBAAQ,EAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QAEH,MAAM,aAAa,GAAG,GAAG,CAAC,YAAY,CAAC,+BAAiB,CAAC,OAAO,CAAC,CAAC;QAClE,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,+BAAiB,CAAC,QAAQ,CAAC,CAAC;QAE/D,OAAO,IAAI,qBAAqB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAC7D,CAAC;CACF;AA3BD,sDA2BC","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WebWorkerPostMessageStream } from '@metamask/post-message-stream';\nimport { logError, SNAP_STREAM_NAMES } from '@metamask/snaps-utils';\nimport { pipeline } from 'readable-stream';\n\nimport { BaseSnapExecutor } from '../../common/BaseSnapExecutor';\nimport { log } from '../../logging';\n\nexport class WebWorkerSnapExecutor extends BaseSnapExecutor {\n /**\n * Initialize the WebWorkerSnapExecutor. This creates a post message stream\n * from and to the parent window, for two-way communication with the iframe.\n *\n * @param stream - The stream to use for communication.\n * @returns An instance of `WebWorkerSnapExecutor`, with the initialized post\n * message streams.\n */\n static initialize(\n stream: BasePostMessageStream = new WebWorkerPostMessageStream(),\n ) {\n log('Worker: Connecting to parent.');\n\n const mux = new ObjectMultiplex();\n pipeline(stream, mux, stream, (error) => {\n if (error) {\n logError(`Parent stream failure, closing worker.`, error);\n }\n self.close();\n });\n\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n return new WebWorkerSnapExecutor(commandStream, rpcStream);\n }\n}\n"]}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { BasePostMessageStream } from "@metamask/post-message-stream";
|
|
2
|
-
import { BaseSnapExecutor } from "../../common/BaseSnapExecutor.cjs";
|
|
3
|
-
export declare class WebWorkerSnapExecutor extends BaseSnapExecutor {
|
|
4
|
-
/**
|
|
5
|
-
* Initialize the WebWorkerSnapExecutor. This creates a post message stream
|
|
6
|
-
* from and to the parent window, for two-way communication with the iframe.
|
|
7
|
-
*
|
|
8
|
-
* @param stream - The stream to use for communication.
|
|
9
|
-
* @returns An instance of `WebWorkerSnapExecutor`, with the initialized post
|
|
10
|
-
* message streams.
|
|
11
|
-
*/
|
|
12
|
-
static initialize(stream?: BasePostMessageStream): WebWorkerSnapExecutor;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=WebWorkerSnapExecutor.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerSnapExecutor.d.cts","sourceRoot":"","sources":["../../../src/webworker/executor/WebWorkerSnapExecutor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAK3E,OAAO,EAAE,gBAAgB,EAAE,0CAAsC;AAGjE,qBAAa,qBAAsB,SAAQ,gBAAgB;IACzD;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CACf,MAAM,GAAE,qBAAwD;CAiBnE"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { BasePostMessageStream } from "@metamask/post-message-stream";
|
|
2
|
-
import { BaseSnapExecutor } from "../../common/BaseSnapExecutor.mjs";
|
|
3
|
-
export declare class WebWorkerSnapExecutor extends BaseSnapExecutor {
|
|
4
|
-
/**
|
|
5
|
-
* Initialize the WebWorkerSnapExecutor. This creates a post message stream
|
|
6
|
-
* from and to the parent window, for two-way communication with the iframe.
|
|
7
|
-
*
|
|
8
|
-
* @param stream - The stream to use for communication.
|
|
9
|
-
* @returns An instance of `WebWorkerSnapExecutor`, with the initialized post
|
|
10
|
-
* message streams.
|
|
11
|
-
*/
|
|
12
|
-
static initialize(stream?: BasePostMessageStream): WebWorkerSnapExecutor;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=WebWorkerSnapExecutor.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerSnapExecutor.d.mts","sourceRoot":"","sources":["../../../src/webworker/executor/WebWorkerSnapExecutor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAK3E,OAAO,EAAE,gBAAgB,EAAE,0CAAsC;AAGjE,qBAAa,qBAAsB,SAAQ,gBAAgB;IACzD;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CACf,MAAM,GAAE,qBAAwD;CAiBnE"}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
function $importDefault(module) {
|
|
2
|
-
if (module?.__esModule) {
|
|
3
|
-
return module.default;
|
|
4
|
-
}
|
|
5
|
-
return module;
|
|
6
|
-
}
|
|
7
|
-
import $ObjectMultiplex from "@metamask/object-multiplex";
|
|
8
|
-
const ObjectMultiplex = $importDefault($ObjectMultiplex);
|
|
9
|
-
import { WebWorkerPostMessageStream } from "@metamask/post-message-stream";
|
|
10
|
-
import { logError, SNAP_STREAM_NAMES } from "@metamask/snaps-utils";
|
|
11
|
-
import { pipeline } from "readable-stream";
|
|
12
|
-
import { BaseSnapExecutor } from "../../common/BaseSnapExecutor.mjs";
|
|
13
|
-
import { log } from "../../logging.mjs";
|
|
14
|
-
export class WebWorkerSnapExecutor extends BaseSnapExecutor {
|
|
15
|
-
/**
|
|
16
|
-
* Initialize the WebWorkerSnapExecutor. This creates a post message stream
|
|
17
|
-
* from and to the parent window, for two-way communication with the iframe.
|
|
18
|
-
*
|
|
19
|
-
* @param stream - The stream to use for communication.
|
|
20
|
-
* @returns An instance of `WebWorkerSnapExecutor`, with the initialized post
|
|
21
|
-
* message streams.
|
|
22
|
-
*/
|
|
23
|
-
static initialize(stream = new WebWorkerPostMessageStream()) {
|
|
24
|
-
log('Worker: Connecting to parent.');
|
|
25
|
-
const mux = new ObjectMultiplex();
|
|
26
|
-
pipeline(stream, mux, stream, (error) => {
|
|
27
|
-
if (error) {
|
|
28
|
-
logError(`Parent stream failure, closing worker.`, error);
|
|
29
|
-
}
|
|
30
|
-
self.close();
|
|
31
|
-
});
|
|
32
|
-
const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);
|
|
33
|
-
const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);
|
|
34
|
-
return new WebWorkerSnapExecutor(commandStream, rpcStream);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
//# sourceMappingURL=WebWorkerSnapExecutor.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerSnapExecutor.mjs","sourceRoot":"","sources":["../../../src/webworker/executor/WebWorkerSnapExecutor.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,gBAAe,mCAAmC;;AAEzD,OAAO,EAAE,0BAA0B,EAAE,sCAAsC;AAC3E,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,8BAA8B;AACpE,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAE3C,OAAO,EAAE,gBAAgB,EAAE,0CAAsC;AACjE,OAAO,EAAE,GAAG,EAAE,0BAAsB;AAEpC,MAAM,OAAO,qBAAsB,SAAQ,gBAAgB;IACzD;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,0BAA0B,EAAE;QAEhE,GAAG,CAAC,+BAA+B,CAAC,CAAC;QAErC,MAAM,GAAG,GAAG,IAAI,eAAe,EAAE,CAAC;QAClC,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACtC,IAAI,KAAK,EAAE,CAAC;gBACV,QAAQ,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QAEH,MAAM,aAAa,GAAG,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QAClE,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAE/D,OAAO,IAAI,qBAAqB,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAC7D,CAAC;CACF","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WebWorkerPostMessageStream } from '@metamask/post-message-stream';\nimport { logError, SNAP_STREAM_NAMES } from '@metamask/snaps-utils';\nimport { pipeline } from 'readable-stream';\n\nimport { BaseSnapExecutor } from '../../common/BaseSnapExecutor';\nimport { log } from '../../logging';\n\nexport class WebWorkerSnapExecutor extends BaseSnapExecutor {\n /**\n * Initialize the WebWorkerSnapExecutor. This creates a post message stream\n * from and to the parent window, for two-way communication with the iframe.\n *\n * @param stream - The stream to use for communication.\n * @returns An instance of `WebWorkerSnapExecutor`, with the initialized post\n * message streams.\n */\n static initialize(\n stream: BasePostMessageStream = new WebWorkerPostMessageStream(),\n ) {\n log('Worker: Connecting to parent.');\n\n const mux = new ObjectMultiplex();\n pipeline(stream, mux, stream, (error) => {\n if (error) {\n logError(`Parent stream failure, closing worker.`, error);\n }\n self.close();\n });\n\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n return new WebWorkerSnapExecutor(commandStream, rpcStream);\n }\n}\n"]}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const WebWorkerSnapExecutor_1 = require("./WebWorkerSnapExecutor.cjs");
|
|
4
|
-
const lockdown_events_1 = require("../../common/lockdown/lockdown-events.cjs");
|
|
5
|
-
const lockdown_more_1 = require("../../common/lockdown/lockdown-more.cjs");
|
|
6
|
-
// Lockdown is already applied in LavaMoat
|
|
7
|
-
(0, lockdown_more_1.executeLockdownMore)();
|
|
8
|
-
(0, lockdown_events_1.executeLockdownEvents)();
|
|
9
|
-
WebWorkerSnapExecutor_1.WebWorkerSnapExecutor.initialize();
|
|
10
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/webworker/executor/index.ts"],"names":[],"mappings":";;AAAA,uEAAgE;AAChE,+EAA8E;AAC9E,2EAA0E;AAE1E,0CAA0C;AAC1C,IAAA,mCAAmB,GAAE,CAAC;AACtB,IAAA,uCAAqB,GAAE,CAAC;AAExB,6CAAqB,CAAC,UAAU,EAAE,CAAC","sourcesContent":["import { WebWorkerSnapExecutor } from './WebWorkerSnapExecutor';\nimport { executeLockdownEvents } from '../../common/lockdown/lockdown-events';\nimport { executeLockdownMore } from '../../common/lockdown/lockdown-more';\n\n// Lockdown is already applied in LavaMoat\nexecuteLockdownMore();\nexecuteLockdownEvents();\n\nWebWorkerSnapExecutor.initialize();\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/webworker/executor/index.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/webworker/executor/index.ts"],"names":[],"mappings":""}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { WebWorkerSnapExecutor } from "./WebWorkerSnapExecutor.mjs";
|
|
2
|
-
import { executeLockdownEvents } from "../../common/lockdown/lockdown-events.mjs";
|
|
3
|
-
import { executeLockdownMore } from "../../common/lockdown/lockdown-more.mjs";
|
|
4
|
-
// Lockdown is already applied in LavaMoat
|
|
5
|
-
executeLockdownMore();
|
|
6
|
-
executeLockdownEvents();
|
|
7
|
-
WebWorkerSnapExecutor.initialize();
|
|
8
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/webworker/executor/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,OAAO,EAAE,qBAAqB,EAAE,kDAA8C;AAC9E,OAAO,EAAE,mBAAmB,EAAE,gDAA4C;AAE1E,0CAA0C;AAC1C,mBAAmB,EAAE,CAAC;AACtB,qBAAqB,EAAE,CAAC;AAExB,qBAAqB,CAAC,UAAU,EAAE,CAAC","sourcesContent":["import { WebWorkerSnapExecutor } from './WebWorkerSnapExecutor';\nimport { executeLockdownEvents } from '../../common/lockdown/lockdown-events';\nimport { executeLockdownMore } from '../../common/lockdown/lockdown-more';\n\n// Lockdown is already applied in LavaMoat\nexecuteLockdownMore();\nexecuteLockdownEvents();\n\nWebWorkerSnapExecutor.initialize();\n"]}
|
|
@@ -1,168 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.WebWorkerPool = void 0;
|
|
16
|
-
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
17
|
-
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
18
|
-
const utils_1 = require("@metamask/utils");
|
|
19
|
-
const non_secure_1 = require("nanoid/non-secure");
|
|
20
|
-
/**
|
|
21
|
-
* A snap executor using the WebWorker API.
|
|
22
|
-
*
|
|
23
|
-
* This is not a traditional snap executor, as it does not execute snaps itself.
|
|
24
|
-
* Instead, it creates a pool of webworkers for each snap execution, and sends
|
|
25
|
-
* the snap execution request to the webworker. The webworker is responsible for
|
|
26
|
-
* executing the snap.
|
|
27
|
-
*/
|
|
28
|
-
class WebWorkerPool {
|
|
29
|
-
/* istanbul ignore next - Constructor arguments. */
|
|
30
|
-
static initialize(stream = new post_message_stream_1.WindowPostMessageStream({
|
|
31
|
-
name: 'child',
|
|
32
|
-
target: 'parent',
|
|
33
|
-
targetWindow: self.parent,
|
|
34
|
-
targetOrigin: '*',
|
|
35
|
-
}), url = '../executor/bundle.js', poolSize) {
|
|
36
|
-
return new WebWorkerPool(stream, url, poolSize);
|
|
37
|
-
}
|
|
38
|
-
constructor(stream, url, poolSize = 3) {
|
|
39
|
-
_WebWorkerPool_instances.add(this);
|
|
40
|
-
_WebWorkerPool_poolSize.set(this, void 0);
|
|
41
|
-
_WebWorkerPool_stream.set(this, void 0);
|
|
42
|
-
_WebWorkerPool_url.set(this, void 0);
|
|
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));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
exports.WebWorkerPool = WebWorkerPool;
|
|
53
|
-
_WebWorkerPool_poolSize = new WeakMap(), _WebWorkerPool_stream = new WeakMap(), _WebWorkerPool_url = new WeakMap(), _WebWorkerPool_workerSourceURL = new WeakMap(), _WebWorkerPool_instances = new WeakSet(), _WebWorkerPool_onData = function _WebWorkerPool_onData(data) {
|
|
54
|
-
const { jobId, data: request } = data;
|
|
55
|
-
const job = this.jobs.get(jobId);
|
|
56
|
-
if (!job) {
|
|
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
|
-
__classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_initializeJob).call(this, jobId)
|
|
61
|
-
.then(() => {
|
|
62
|
-
__classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_onData).call(this, data);
|
|
63
|
-
})
|
|
64
|
-
.catch((error) => {
|
|
65
|
-
(0, snaps_utils_1.logError)('[Worker] Error initializing job:', error.toString());
|
|
66
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").write({
|
|
67
|
-
jobId,
|
|
68
|
-
data: {
|
|
69
|
-
name: 'command',
|
|
70
|
-
data: {
|
|
71
|
-
jsonrpc: '2.0',
|
|
72
|
-
id: request.id ?? null,
|
|
73
|
-
error: {
|
|
74
|
-
code: -32000,
|
|
75
|
-
message: 'Internal error',
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
// This is a method specific to the `WebWorkerPool`, as the service itself
|
|
84
|
-
// does not have access to the workers directly.
|
|
85
|
-
if (request.method === 'terminateJob') {
|
|
86
|
-
__classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_terminateJob).call(this, jobId);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
job.stream.write(request);
|
|
90
|
-
}, _WebWorkerPool_initializeJob =
|
|
91
|
-
/**
|
|
92
|
-
* Create a new worker and set up a stream to communicate with it.
|
|
93
|
-
*
|
|
94
|
-
* @param jobId - The job ID.
|
|
95
|
-
* @returns The job.
|
|
96
|
-
*/
|
|
97
|
-
async function _WebWorkerPool_initializeJob(jobId) {
|
|
98
|
-
const worker = await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_getWorker).call(this);
|
|
99
|
-
const jobStream = new post_message_stream_1.WebWorkerParentPostMessageStream({
|
|
100
|
-
worker,
|
|
101
|
-
});
|
|
102
|
-
// Write messages from the worker to the parent, wrapped with the job ID.
|
|
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);
|
|
125
|
-
}
|
|
126
|
-
const worker = this.pool.shift();
|
|
127
|
-
(0, utils_1.assert)(worker, 'Worker not found.');
|
|
128
|
-
await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_updatePool).call(this);
|
|
129
|
-
return worker;
|
|
130
|
-
}, _WebWorkerPool_updatePool =
|
|
131
|
-
/**
|
|
132
|
-
* Update the pool of workers. This will create new workers if the pool is
|
|
133
|
-
* below the minimum size.
|
|
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);
|
|
139
|
-
}
|
|
140
|
-
}, _WebWorkerPool_createWorker =
|
|
141
|
-
/**
|
|
142
|
-
* Create a new worker. This will fetch the worker source if it has not
|
|
143
|
-
* already been fetched.
|
|
144
|
-
*
|
|
145
|
-
* @returns The worker.
|
|
146
|
-
*/
|
|
147
|
-
async function _WebWorkerPool_createWorker() {
|
|
148
|
-
return new Worker(await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_getWorkerURL).call(this), {
|
|
149
|
-
name: `worker-${(0, non_secure_1.nanoid)()}`,
|
|
150
|
-
});
|
|
151
|
-
}, _WebWorkerPool_getWorkerURL =
|
|
152
|
-
/**
|
|
153
|
-
* Get the URL of the worker source. This will fetch the worker source if it
|
|
154
|
-
* has not already been fetched.
|
|
155
|
-
*
|
|
156
|
-
* @returns The worker source URL, as a `blob:` URL.
|
|
157
|
-
*/
|
|
158
|
-
async function _WebWorkerPool_getWorkerURL() {
|
|
159
|
-
if (__classPrivateFieldGet(this, _WebWorkerPool_workerSourceURL, "f")) {
|
|
160
|
-
return __classPrivateFieldGet(this, _WebWorkerPool_workerSourceURL, "f");
|
|
161
|
-
}
|
|
162
|
-
const blob = await fetch(__classPrivateFieldGet(this, _WebWorkerPool_url, "f"))
|
|
163
|
-
.then(async (response) => response.blob())
|
|
164
|
-
.then(URL.createObjectURL.bind(URL));
|
|
165
|
-
__classPrivateFieldSet(this, _WebWorkerPool_workerSourceURL, blob, "f");
|
|
166
|
-
return blob;
|
|
167
|
-
};
|
|
168
|
-
//# sourceMappingURL=WebWorkerPool.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerPool.cjs","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAGuC;AACvC,uDAAiD;AAEjD,2CAAyC;AACzC,kDAA2C;AAQ3C;;;;;;;GAOG;AACH,MAAa,aAAa;IAaxB,mDAAmD;IACnD,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,6CAAuB,CAAC;QAC1D,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,QAAQ;QAChB,YAAY,EAAE,IAAI,CAAC,MAAM;QACzB,YAAY,EAAE,GAAG;KAClB,CAAC,EACF,GAAG,GAAG,uBAAuB,EAC7B,QAAiB;QAEjB,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC;IAED,YAAY,MAA6B,EAAE,GAAW,EAAE,QAAQ,GAAG,CAAC;;QA1B3D,0CAAU;QAEV,wCAA+B;QAE/B,qCAAa;QAEb,SAAI,GAAa,EAAE,CAAC;QAEpB,SAAI,GAA6B,IAAI,GAAG,EAAE,CAAC;QAEpD,iDAA0B;QAiBxB,uBAAA,IAAI,yBAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,sBAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,2BAAa,QAAQ,MAAA,CAAC;QAE1B,uBAAA,IAAI,6BAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,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AY,MAAhB,IAAI,CAAc,CAAC;IAEzB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,KAAK;IACH,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,uBAAA,IAAI,+BAAU,EAAE,CAAC;QACzC,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,6DAAc,MAAlB,IAAI,CAAgB,CAAC;QAC1C,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,KAAK;IACH,OAAO,IAAI,MAAM,CAAC,MAAM,uBAAA,IAAI,6DAAc,MAAlB,IAAI,CAAgB,EAAE;QAC5C,IAAI,EAAE,UAAU,IAAA,mBAAM,GAAE,EAAE;KAC3B,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,KAAK;IACH,IAAI,uBAAA,IAAI,sCAAiB,EAAE,CAAC;QAC1B,OAAO,uBAAA,IAAI,sCAAiB,CAAC;IAC/B,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,uBAAA,IAAI,0BAAK,CAAC;SAChC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;SACzC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAEvC,uBAAA,IAAI,kCAAoB,IAAI,MAAA,CAAC;IAC7B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n WebWorkerParentPostMessageStream,\n WindowPostMessageStream,\n} from '@metamask/post-message-stream';\nimport { logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid/non-secure';\n\ntype ExecutorJob = {\n id: string;\n worker: Worker;\n stream: WebWorkerParentPostMessageStream;\n};\n\n/**\n * A snap executor using the WebWorker API.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates a pool of webworkers for each snap execution, and sends\n * the snap execution request to the webworker. The webworker is responsible for\n * executing the snap.\n */\nexport class WebWorkerPool {\n readonly #poolSize;\n\n readonly #stream: BasePostMessageStream;\n\n readonly #url: string;\n\n readonly pool: Worker[] = [];\n\n readonly jobs: Map<string, ExecutorJob> = new Map();\n\n #workerSourceURL?: string;\n\n /* istanbul ignore next - Constructor arguments. */\n static initialize(\n stream: BasePostMessageStream = new WindowPostMessageStream({\n name: 'child',\n target: 'parent',\n targetWindow: self.parent,\n targetOrigin: '*',\n }),\n url = '../executor/bundle.js',\n poolSize?: number,\n ) {\n return new WebWorkerPool(stream, url, poolSize);\n }\n\n constructor(stream: BasePostMessageStream, url: string, poolSize = 3) {\n this.#stream = stream;\n this.#url = url;\n this.#poolSize = poolSize;\n\n this.#stream.on('data', this.#onData.bind(this));\n }\n\n /**\n * Handle an incoming message from the `WebWorkerExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n const job = this.jobs.get(jobId);\n if (!job) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error.toString());\n\n this.#stream.write({\n jobId,\n data: {\n name: 'command',\n data: {\n jsonrpc: '2.0',\n id: request.id ?? null,\n error: {\n code: -32000,\n message: 'Internal error',\n },\n },\n },\n });\n });\n\n return;\n }\n\n // This is a method specific to the `WebWorkerPool`, as the service itself\n // does not have access to the workers directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n job.stream.write(request);\n }\n\n /**\n * Create a new worker and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The job.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const worker = await this.#getWorker();\n const jobStream = new WebWorkerParentPostMessageStream({\n worker,\n });\n\n // Write messages from the worker to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n const job = { id: jobId, worker, stream: jobStream };\n this.jobs.set(jobId, job);\n return job;\n }\n\n /**\n * Terminate the job with the given ID. This will close the worker and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n const job = this.jobs.get(jobId);\n assert(job, `Job \"${jobId}\" not found.`);\n\n job.stream.destroy();\n job.worker.terminate();\n\n this.jobs.delete(jobId);\n }\n\n /**\n * Get a worker from the pool. A new worker will be created automatically.\n *\n * @returns The worker.\n */\n async #getWorker() {\n // Lazily create the pool of workers.\n if (this.pool.length === 0) {\n await this.#updatePool();\n }\n\n const worker = this.pool.shift();\n assert(worker, 'Worker not found.');\n\n await this.#updatePool();\n\n return worker;\n }\n\n /**\n * Update the pool of workers. This will create new workers if the pool is\n * below the minimum size.\n */\n async #updatePool() {\n while (this.pool.length < this.#poolSize) {\n const worker = await this.#createWorker();\n this.pool.push(worker);\n }\n }\n\n /**\n * Create a new worker. This will fetch the worker source if it has not\n * already been fetched.\n *\n * @returns The worker.\n */\n async #createWorker() {\n return new Worker(await this.#getWorkerURL(), {\n name: `worker-${nanoid()}`,\n });\n }\n\n /**\n * Get the URL of the worker source. This will fetch the worker source if it\n * has not already been fetched.\n *\n * @returns The worker source URL, as a `blob:` URL.\n */\n async #getWorkerURL() {\n if (this.#workerSourceURL) {\n return this.#workerSourceURL;\n }\n\n const blob = await fetch(this.#url)\n .then(async (response) => response.blob())\n .then(URL.createObjectURL.bind(URL));\n\n this.#workerSourceURL = blob;\n return blob;\n }\n}\n"]}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { BasePostMessageStream } from "@metamask/post-message-stream";
|
|
2
|
-
import { WebWorkerParentPostMessageStream } from "@metamask/post-message-stream";
|
|
3
|
-
type ExecutorJob = {
|
|
4
|
-
id: string;
|
|
5
|
-
worker: Worker;
|
|
6
|
-
stream: WebWorkerParentPostMessageStream;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* A snap executor using the WebWorker API.
|
|
10
|
-
*
|
|
11
|
-
* This is not a traditional snap executor, as it does not execute snaps itself.
|
|
12
|
-
* Instead, it creates a pool of webworkers for each snap execution, and sends
|
|
13
|
-
* the snap execution request to the webworker. The webworker is responsible for
|
|
14
|
-
* executing the snap.
|
|
15
|
-
*/
|
|
16
|
-
export declare class WebWorkerPool {
|
|
17
|
-
#private;
|
|
18
|
-
readonly pool: Worker[];
|
|
19
|
-
readonly jobs: Map<string, ExecutorJob>;
|
|
20
|
-
static initialize(stream?: BasePostMessageStream, url?: string, poolSize?: number): WebWorkerPool;
|
|
21
|
-
constructor(stream: BasePostMessageStream, url: string, poolSize?: number);
|
|
22
|
-
}
|
|
23
|
-
export {};
|
|
24
|
-
//# sourceMappingURL=WebWorkerPool.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerPool.d.cts","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAC3E,OAAO,EACL,gCAAgC,EAEjC,sCAAsC;AAMvC,KAAK,WAAW,GAAG;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,gCAAgC,CAAC;CAC1C,CAAC;AAEF;;;;;;;GAOG;AACH,qBAAa,aAAa;;IAOxB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,CAAM;IAE7B,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAa;IAKpD,MAAM,CAAC,UAAU,CACf,MAAM,GAAE,qBAKN,EACF,GAAG,SAA0B,EAC7B,QAAQ,CAAC,EAAE,MAAM;gBAKP,MAAM,EAAE,qBAAqB,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,SAAI;CA+JrE"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { BasePostMessageStream } from "@metamask/post-message-stream";
|
|
2
|
-
import { WebWorkerParentPostMessageStream } from "@metamask/post-message-stream";
|
|
3
|
-
type ExecutorJob = {
|
|
4
|
-
id: string;
|
|
5
|
-
worker: Worker;
|
|
6
|
-
stream: WebWorkerParentPostMessageStream;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* A snap executor using the WebWorker API.
|
|
10
|
-
*
|
|
11
|
-
* This is not a traditional snap executor, as it does not execute snaps itself.
|
|
12
|
-
* Instead, it creates a pool of webworkers for each snap execution, and sends
|
|
13
|
-
* the snap execution request to the webworker. The webworker is responsible for
|
|
14
|
-
* executing the snap.
|
|
15
|
-
*/
|
|
16
|
-
export declare class WebWorkerPool {
|
|
17
|
-
#private;
|
|
18
|
-
readonly pool: Worker[];
|
|
19
|
-
readonly jobs: Map<string, ExecutorJob>;
|
|
20
|
-
static initialize(stream?: BasePostMessageStream, url?: string, poolSize?: number): WebWorkerPool;
|
|
21
|
-
constructor(stream: BasePostMessageStream, url: string, poolSize?: number);
|
|
22
|
-
}
|
|
23
|
-
export {};
|
|
24
|
-
//# sourceMappingURL=WebWorkerPool.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerPool.d.mts","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAC3E,OAAO,EACL,gCAAgC,EAEjC,sCAAsC;AAMvC,KAAK,WAAW,GAAG;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,gCAAgC,CAAC;CAC1C,CAAC;AAEF;;;;;;;GAOG;AACH,qBAAa,aAAa;;IAOxB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,CAAM;IAE7B,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAa;IAKpD,MAAM,CAAC,UAAU,CACf,MAAM,GAAE,qBAKN,EACF,GAAG,SAA0B,EAC7B,QAAQ,CAAC,EAAE,MAAM;gBAKP,MAAM,EAAE,qBAAqB,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,SAAI;CA+JrE"}
|
|
@@ -1,164 +0,0 @@
|
|
|
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
|
-
import { WebWorkerParentPostMessageStream, WindowPostMessageStream } from "@metamask/post-message-stream";
|
|
14
|
-
import { logError } from "@metamask/snaps-utils";
|
|
15
|
-
import { assert } from "@metamask/utils";
|
|
16
|
-
import { nanoid } from "nanoid/non-secure";
|
|
17
|
-
/**
|
|
18
|
-
* A snap executor using the WebWorker API.
|
|
19
|
-
*
|
|
20
|
-
* This is not a traditional snap executor, as it does not execute snaps itself.
|
|
21
|
-
* Instead, it creates a pool of webworkers for each snap execution, and sends
|
|
22
|
-
* the snap execution request to the webworker. The webworker is responsible for
|
|
23
|
-
* executing the snap.
|
|
24
|
-
*/
|
|
25
|
-
export class WebWorkerPool {
|
|
26
|
-
/* istanbul ignore next - Constructor arguments. */
|
|
27
|
-
static initialize(stream = new WindowPostMessageStream({
|
|
28
|
-
name: 'child',
|
|
29
|
-
target: 'parent',
|
|
30
|
-
targetWindow: self.parent,
|
|
31
|
-
targetOrigin: '*',
|
|
32
|
-
}), url = '../executor/bundle.js', poolSize) {
|
|
33
|
-
return new WebWorkerPool(stream, url, poolSize);
|
|
34
|
-
}
|
|
35
|
-
constructor(stream, url, poolSize = 3) {
|
|
36
|
-
_WebWorkerPool_instances.add(this);
|
|
37
|
-
_WebWorkerPool_poolSize.set(this, void 0);
|
|
38
|
-
_WebWorkerPool_stream.set(this, void 0);
|
|
39
|
-
_WebWorkerPool_url.set(this, void 0);
|
|
40
|
-
this.pool = [];
|
|
41
|
-
this.jobs = new Map();
|
|
42
|
-
_WebWorkerPool_workerSourceURL.set(this, void 0);
|
|
43
|
-
__classPrivateFieldSet(this, _WebWorkerPool_stream, stream, "f");
|
|
44
|
-
__classPrivateFieldSet(this, _WebWorkerPool_url, url, "f");
|
|
45
|
-
__classPrivateFieldSet(this, _WebWorkerPool_poolSize, poolSize, "f");
|
|
46
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").on('data', __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_onData).bind(this));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
_WebWorkerPool_poolSize = new WeakMap(), _WebWorkerPool_stream = new WeakMap(), _WebWorkerPool_url = new WeakMap(), _WebWorkerPool_workerSourceURL = new WeakMap(), _WebWorkerPool_instances = new WeakSet(), _WebWorkerPool_onData = function _WebWorkerPool_onData(data) {
|
|
50
|
-
const { jobId, data: request } = data;
|
|
51
|
-
const job = this.jobs.get(jobId);
|
|
52
|
-
if (!job) {
|
|
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
|
-
__classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_initializeJob).call(this, jobId)
|
|
57
|
-
.then(() => {
|
|
58
|
-
__classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_onData).call(this, data);
|
|
59
|
-
})
|
|
60
|
-
.catch((error) => {
|
|
61
|
-
logError('[Worker] Error initializing job:', error.toString());
|
|
62
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").write({
|
|
63
|
-
jobId,
|
|
64
|
-
data: {
|
|
65
|
-
name: 'command',
|
|
66
|
-
data: {
|
|
67
|
-
jsonrpc: '2.0',
|
|
68
|
-
id: request.id ?? null,
|
|
69
|
-
error: {
|
|
70
|
-
code: -32000,
|
|
71
|
-
message: 'Internal error',
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
// This is a method specific to the `WebWorkerPool`, as the service itself
|
|
80
|
-
// does not have access to the workers directly.
|
|
81
|
-
if (request.method === 'terminateJob') {
|
|
82
|
-
__classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_terminateJob).call(this, jobId);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
job.stream.write(request);
|
|
86
|
-
}, _WebWorkerPool_initializeJob =
|
|
87
|
-
/**
|
|
88
|
-
* Create a new worker and set up a stream to communicate with it.
|
|
89
|
-
*
|
|
90
|
-
* @param jobId - The job ID.
|
|
91
|
-
* @returns The job.
|
|
92
|
-
*/
|
|
93
|
-
async function _WebWorkerPool_initializeJob(jobId) {
|
|
94
|
-
const worker = await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_getWorker).call(this);
|
|
95
|
-
const jobStream = new WebWorkerParentPostMessageStream({
|
|
96
|
-
worker,
|
|
97
|
-
});
|
|
98
|
-
// Write messages from the worker to the parent, wrapped with the job ID.
|
|
99
|
-
jobStream.on('data', (data) => {
|
|
100
|
-
__classPrivateFieldGet(this, _WebWorkerPool_stream, "f").write({ data, jobId });
|
|
101
|
-
});
|
|
102
|
-
const job = { id: jobId, worker, stream: jobStream };
|
|
103
|
-
this.jobs.set(jobId, job);
|
|
104
|
-
return job;
|
|
105
|
-
}, _WebWorkerPool_terminateJob = function _WebWorkerPool_terminateJob(jobId) {
|
|
106
|
-
const job = this.jobs.get(jobId);
|
|
107
|
-
assert(job, `Job "${jobId}" not found.`);
|
|
108
|
-
job.stream.destroy();
|
|
109
|
-
job.worker.terminate();
|
|
110
|
-
this.jobs.delete(jobId);
|
|
111
|
-
}, _WebWorkerPool_getWorker =
|
|
112
|
-
/**
|
|
113
|
-
* Get a worker from the pool. A new worker will be created automatically.
|
|
114
|
-
*
|
|
115
|
-
* @returns The worker.
|
|
116
|
-
*/
|
|
117
|
-
async function _WebWorkerPool_getWorker() {
|
|
118
|
-
// Lazily create the pool of workers.
|
|
119
|
-
if (this.pool.length === 0) {
|
|
120
|
-
await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_updatePool).call(this);
|
|
121
|
-
}
|
|
122
|
-
const worker = this.pool.shift();
|
|
123
|
-
assert(worker, 'Worker not found.');
|
|
124
|
-
await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_updatePool).call(this);
|
|
125
|
-
return worker;
|
|
126
|
-
}, _WebWorkerPool_updatePool =
|
|
127
|
-
/**
|
|
128
|
-
* Update the pool of workers. This will create new workers if the pool is
|
|
129
|
-
* below the minimum size.
|
|
130
|
-
*/
|
|
131
|
-
async function _WebWorkerPool_updatePool() {
|
|
132
|
-
while (this.pool.length < __classPrivateFieldGet(this, _WebWorkerPool_poolSize, "f")) {
|
|
133
|
-
const worker = await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_createWorker).call(this);
|
|
134
|
-
this.pool.push(worker);
|
|
135
|
-
}
|
|
136
|
-
}, _WebWorkerPool_createWorker =
|
|
137
|
-
/**
|
|
138
|
-
* Create a new worker. This will fetch the worker source if it has not
|
|
139
|
-
* already been fetched.
|
|
140
|
-
*
|
|
141
|
-
* @returns The worker.
|
|
142
|
-
*/
|
|
143
|
-
async function _WebWorkerPool_createWorker() {
|
|
144
|
-
return new Worker(await __classPrivateFieldGet(this, _WebWorkerPool_instances, "m", _WebWorkerPool_getWorkerURL).call(this), {
|
|
145
|
-
name: `worker-${nanoid()}`,
|
|
146
|
-
});
|
|
147
|
-
}, _WebWorkerPool_getWorkerURL =
|
|
148
|
-
/**
|
|
149
|
-
* Get the URL of the worker source. This will fetch the worker source if it
|
|
150
|
-
* has not already been fetched.
|
|
151
|
-
*
|
|
152
|
-
* @returns The worker source URL, as a `blob:` URL.
|
|
153
|
-
*/
|
|
154
|
-
async function _WebWorkerPool_getWorkerURL() {
|
|
155
|
-
if (__classPrivateFieldGet(this, _WebWorkerPool_workerSourceURL, "f")) {
|
|
156
|
-
return __classPrivateFieldGet(this, _WebWorkerPool_workerSourceURL, "f");
|
|
157
|
-
}
|
|
158
|
-
const blob = await fetch(__classPrivateFieldGet(this, _WebWorkerPool_url, "f"))
|
|
159
|
-
.then(async (response) => response.blob())
|
|
160
|
-
.then(URL.createObjectURL.bind(URL));
|
|
161
|
-
__classPrivateFieldSet(this, _WebWorkerPool_workerSourceURL, blob, "f");
|
|
162
|
-
return blob;
|
|
163
|
-
};
|
|
164
|
-
//# sourceMappingURL=WebWorkerPool.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerPool.mjs","sourceRoot":"","sources":["../../../src/webworker/pool/WebWorkerPool.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,gCAAgC,EAChC,uBAAuB,EACxB,sCAAsC;AACvC,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAEjD,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,EAAE,MAAM,EAAE,0BAA0B;AAQ3C;;;;;;;GAOG;AACH,MAAM,OAAO,aAAa;IAaxB,mDAAmD;IACnD,MAAM,CAAC,UAAU,CACf,SAAgC,IAAI,uBAAuB,CAAC;QAC1D,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,QAAQ;QAChB,YAAY,EAAE,IAAI,CAAC,MAAM;QACzB,YAAY,EAAE,GAAG;KAClB,CAAC,EACF,GAAG,GAAG,uBAAuB,EAC7B,QAAiB;QAEjB,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC;IAED,YAAY,MAA6B,EAAE,GAAW,EAAE,QAAQ,GAAG,CAAC;;QA1B3D,0CAAU;QAEV,wCAA+B;QAE/B,qCAAa;QAEb,SAAI,GAAa,EAAE,CAAC;QAEpB,SAAI,GAA6B,IAAI,GAAG,EAAE,CAAC;QAEpD,iDAA0B;QAiBxB,uBAAA,IAAI,yBAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,sBAAQ,GAAG,MAAA,CAAC;QAChB,uBAAA,IAAI,2BAAa,QAAQ,MAAA,CAAC;QAE1B,uBAAA,IAAI,6BAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,uDAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,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GAGG;AACH,KAAK;IACH,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,uBAAA,IAAI,+BAAU,EAAE,CAAC;QACzC,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,6DAAc,MAAlB,IAAI,CAAgB,CAAC;QAC1C,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,KAAK;IACH,OAAO,IAAI,MAAM,CAAC,MAAM,uBAAA,IAAI,6DAAc,MAAlB,IAAI,CAAgB,EAAE;QAC5C,IAAI,EAAE,UAAU,MAAM,EAAE,EAAE;KAC3B,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,KAAK;IACH,IAAI,uBAAA,IAAI,sCAAiB,EAAE,CAAC;QAC1B,OAAO,uBAAA,IAAI,sCAAiB,CAAC;IAC/B,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,uBAAA,IAAI,0BAAK,CAAC;SAChC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;SACzC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAEvC,uBAAA,IAAI,kCAAoB,IAAI,MAAA,CAAC;IAC7B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n WebWorkerParentPostMessageStream,\n WindowPostMessageStream,\n} from '@metamask/post-message-stream';\nimport { logError } from '@metamask/snaps-utils';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid/non-secure';\n\ntype ExecutorJob = {\n id: string;\n worker: Worker;\n stream: WebWorkerParentPostMessageStream;\n};\n\n/**\n * A snap executor using the WebWorker API.\n *\n * This is not a traditional snap executor, as it does not execute snaps itself.\n * Instead, it creates a pool of webworkers for each snap execution, and sends\n * the snap execution request to the webworker. The webworker is responsible for\n * executing the snap.\n */\nexport class WebWorkerPool {\n readonly #poolSize;\n\n readonly #stream: BasePostMessageStream;\n\n readonly #url: string;\n\n readonly pool: Worker[] = [];\n\n readonly jobs: Map<string, ExecutorJob> = new Map();\n\n #workerSourceURL?: string;\n\n /* istanbul ignore next - Constructor arguments. */\n static initialize(\n stream: BasePostMessageStream = new WindowPostMessageStream({\n name: 'child',\n target: 'parent',\n targetWindow: self.parent,\n targetOrigin: '*',\n }),\n url = '../executor/bundle.js',\n poolSize?: number,\n ) {\n return new WebWorkerPool(stream, url, poolSize);\n }\n\n constructor(stream: BasePostMessageStream, url: string, poolSize = 3) {\n this.#stream = stream;\n this.#url = url;\n this.#poolSize = poolSize;\n\n this.#stream.on('data', this.#onData.bind(this));\n }\n\n /**\n * Handle an incoming message from the `WebWorkerExecutionService`. This\n * assumes that the message contains a `jobId` property, and a JSON-RPC\n * request in the `data` property.\n *\n * @param data - The message data.\n * @param data.data - The JSON-RPC request.\n * @param data.jobId - The job ID.\n */\n #onData(data: { data: JsonRpcRequest; jobId: string }) {\n const { jobId, data: request } = data;\n\n const job = this.jobs.get(jobId);\n if (!job) {\n // This ensures that a job is initialized before it is used. To avoid\n // code duplication, we call the `#onData` method again, which will\n // run the rest of the logic after initialization.\n this.#initializeJob(jobId)\n .then(() => {\n this.#onData(data);\n })\n .catch((error) => {\n logError('[Worker] Error initializing job:', error.toString());\n\n this.#stream.write({\n jobId,\n data: {\n name: 'command',\n data: {\n jsonrpc: '2.0',\n id: request.id ?? null,\n error: {\n code: -32000,\n message: 'Internal error',\n },\n },\n },\n });\n });\n\n return;\n }\n\n // This is a method specific to the `WebWorkerPool`, as the service itself\n // does not have access to the workers directly.\n if (request.method === 'terminateJob') {\n this.#terminateJob(jobId);\n return;\n }\n\n job.stream.write(request);\n }\n\n /**\n * Create a new worker and set up a stream to communicate with it.\n *\n * @param jobId - The job ID.\n * @returns The job.\n */\n async #initializeJob(jobId: string): Promise<ExecutorJob> {\n const worker = await this.#getWorker();\n const jobStream = new WebWorkerParentPostMessageStream({\n worker,\n });\n\n // Write messages from the worker to the parent, wrapped with the job ID.\n jobStream.on('data', (data) => {\n this.#stream.write({ data, jobId });\n });\n\n const job = { id: jobId, worker, stream: jobStream };\n this.jobs.set(jobId, job);\n return job;\n }\n\n /**\n * Terminate the job with the given ID. This will close the worker and delete\n * the job from the internal job map.\n *\n * @param jobId - The job ID.\n */\n #terminateJob(jobId: string) {\n const job = this.jobs.get(jobId);\n assert(job, `Job \"${jobId}\" not found.`);\n\n job.stream.destroy();\n job.worker.terminate();\n\n this.jobs.delete(jobId);\n }\n\n /**\n * Get a worker from the pool. A new worker will be created automatically.\n *\n * @returns The worker.\n */\n async #getWorker() {\n // Lazily create the pool of workers.\n if (this.pool.length === 0) {\n await this.#updatePool();\n }\n\n const worker = this.pool.shift();\n assert(worker, 'Worker not found.');\n\n await this.#updatePool();\n\n return worker;\n }\n\n /**\n * Update the pool of workers. This will create new workers if the pool is\n * below the minimum size.\n */\n async #updatePool() {\n while (this.pool.length < this.#poolSize) {\n const worker = await this.#createWorker();\n this.pool.push(worker);\n }\n }\n\n /**\n * Create a new worker. This will fetch the worker source if it has not\n * already been fetched.\n *\n * @returns The worker.\n */\n async #createWorker() {\n return new Worker(await this.#getWorkerURL(), {\n name: `worker-${nanoid()}`,\n });\n }\n\n /**\n * Get the URL of the worker source. This will fetch the worker source if it\n * has not already been fetched.\n *\n * @returns The worker source URL, as a `blob:` URL.\n */\n async #getWorkerURL() {\n if (this.#workerSourceURL) {\n return this.#workerSourceURL;\n }\n\n const blob = await fetch(this.#url)\n .then(async (response) => response.blob())\n .then(URL.createObjectURL.bind(URL));\n\n this.#workerSourceURL = blob;\n return blob;\n }\n}\n"]}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const WebWorkerPool_1 = require("./WebWorkerPool.cjs");
|
|
4
|
-
const lockdown_events_1 = require("../../common/lockdown/lockdown-events.cjs");
|
|
5
|
-
const lockdown_more_1 = require("../../common/lockdown/lockdown-more.cjs");
|
|
6
|
-
// Lockdown is already applied in LavaMoat
|
|
7
|
-
(0, lockdown_more_1.executeLockdownMore)();
|
|
8
|
-
(0, lockdown_events_1.executeLockdownEvents)();
|
|
9
|
-
WebWorkerPool_1.WebWorkerPool.initialize();
|
|
10
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/webworker/pool/index.ts"],"names":[],"mappings":";;AAAA,uDAAgD;AAChD,+EAA8E;AAC9E,2EAA0E;AAE1E,0CAA0C;AAC1C,IAAA,mCAAmB,GAAE,CAAC;AACtB,IAAA,uCAAqB,GAAE,CAAC;AAExB,6BAAa,CAAC,UAAU,EAAE,CAAC","sourcesContent":["import { WebWorkerPool } from './WebWorkerPool';\nimport { executeLockdownEvents } from '../../common/lockdown/lockdown-events';\nimport { executeLockdownMore } from '../../common/lockdown/lockdown-more';\n\n// Lockdown is already applied in LavaMoat\nexecuteLockdownMore();\nexecuteLockdownEvents();\n\nWebWorkerPool.initialize();\n"]}
|