@metamask/snaps-controllers 9.9.0 → 9.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -1
- package/dist/cronjob/CronjobController.cjs +34 -21
- package/dist/cronjob/CronjobController.cjs.map +1 -1
- package/dist/cronjob/CronjobController.mjs +34 -21
- package/dist/cronjob/CronjobController.mjs.map +1 -1
- package/dist/insights/SnapInsightsController.cjs +144 -194
- package/dist/insights/SnapInsightsController.cjs.map +1 -1
- package/dist/insights/SnapInsightsController.mjs +143 -193
- package/dist/insights/SnapInsightsController.mjs.map +1 -1
- package/dist/interface/SnapInterfaceController.cjs +71 -91
- package/dist/interface/SnapInterfaceController.cjs.map +1 -1
- package/dist/interface/SnapInterfaceController.d.cts +2 -1
- package/dist/interface/SnapInterfaceController.d.cts.map +1 -1
- package/dist/interface/SnapInterfaceController.d.mts +2 -1
- package/dist/interface/SnapInterfaceController.d.mts.map +1 -1
- package/dist/interface/SnapInterfaceController.mjs +71 -91
- package/dist/interface/SnapInterfaceController.mjs.map +1 -1
- package/dist/services/AbstractExecutionService.cjs +77 -71
- package/dist/services/AbstractExecutionService.cjs.map +1 -1
- package/dist/services/AbstractExecutionService.mjs +77 -71
- package/dist/services/AbstractExecutionService.mjs.map +1 -1
- package/dist/services/ProxyPostMessageStream.cjs +26 -19
- package/dist/services/ProxyPostMessageStream.cjs.map +1 -1
- package/dist/services/ProxyPostMessageStream.mjs +26 -19
- package/dist/services/ProxyPostMessageStream.mjs.map +1 -1
- package/dist/services/iframe/IframeExecutionService.cjs +4 -2
- package/dist/services/iframe/IframeExecutionService.cjs.map +1 -1
- package/dist/services/iframe/IframeExecutionService.d.cts.map +1 -1
- package/dist/services/iframe/IframeExecutionService.d.mts.map +1 -1
- package/dist/services/iframe/IframeExecutionService.mjs +4 -2
- package/dist/services/iframe/IframeExecutionService.mjs.map +1 -1
- package/dist/services/offscreen/OffscreenExecutionService.cjs +16 -3
- package/dist/services/offscreen/OffscreenExecutionService.cjs.map +1 -1
- package/dist/services/offscreen/OffscreenExecutionService.mjs +16 -3
- package/dist/services/offscreen/OffscreenExecutionService.mjs.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.cjs +17 -4
- package/dist/services/proxy/ProxyExecutionService.cjs.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.mjs +17 -4
- package/dist/services/proxy/ProxyExecutionService.mjs.map +1 -1
- package/dist/services/webview/WebViewExecutionService.cjs +23 -9
- package/dist/services/webview/WebViewExecutionService.cjs.map +1 -1
- package/dist/services/webview/WebViewExecutionService.mjs +23 -9
- package/dist/services/webview/WebViewExecutionService.mjs.map +1 -1
- package/dist/services/webview/WebViewMessageStream.cjs +25 -12
- package/dist/services/webview/WebViewMessageStream.cjs.map +1 -1
- package/dist/services/webview/WebViewMessageStream.mjs +25 -12
- package/dist/services/webview/WebViewMessageStream.mjs.map +1 -1
- package/dist/services/webworker/WebWorkerExecutionService.cjs +27 -10
- package/dist/services/webworker/WebWorkerExecutionService.cjs.map +1 -1
- package/dist/services/webworker/WebWorkerExecutionService.mjs +27 -10
- package/dist/services/webworker/WebWorkerExecutionService.mjs.map +1 -1
- package/dist/snaps/RequestQueue.cjs +0 -2
- package/dist/snaps/RequestQueue.cjs.map +1 -1
- package/dist/snaps/RequestQueue.mjs +0 -2
- package/dist/snaps/RequestQueue.mjs.map +1 -1
- package/dist/snaps/SnapController.cjs +1001 -1141
- package/dist/snaps/SnapController.cjs.map +1 -1
- package/dist/snaps/SnapController.mjs +1000 -1140
- package/dist/snaps/SnapController.mjs.map +1 -1
- package/dist/snaps/Timer.cjs +0 -1
- package/dist/snaps/Timer.cjs.map +1 -1
- package/dist/snaps/Timer.mjs +0 -1
- package/dist/snaps/Timer.mjs.map +1 -1
- package/dist/snaps/location/http.cjs +7 -11
- package/dist/snaps/location/http.cjs.map +1 -1
- package/dist/snaps/location/http.mjs +7 -11
- package/dist/snaps/location/http.mjs.map +1 -1
- package/dist/snaps/location/local.cjs +17 -4
- package/dist/snaps/location/local.cjs.map +1 -1
- package/dist/snaps/location/local.mjs +17 -4
- package/dist/snaps/location/local.mjs.map +1 -1
- package/dist/snaps/location/npm.cjs +37 -25
- package/dist/snaps/location/npm.cjs.map +1 -1
- package/dist/snaps/location/npm.mjs +37 -25
- package/dist/snaps/location/npm.mjs.map +1 -1
- package/dist/snaps/registry/json.cjs +173 -172
- package/dist/snaps/registry/json.cjs.map +1 -1
- package/dist/snaps/registry/json.mjs +172 -171
- package/dist/snaps/registry/json.mjs.map +1 -1
- package/package.json +5 -5
|
@@ -1,8 +1,19 @@
|
|
|
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 _ProxyExecutionService_stream;
|
|
1
13
|
import { nanoid } from "nanoid";
|
|
2
14
|
import { AbstractExecutionService } from "../AbstractExecutionService.mjs";
|
|
3
15
|
import { ProxyPostMessageStream } from "../ProxyPostMessageStream.mjs";
|
|
4
16
|
export class ProxyExecutionService extends AbstractExecutionService {
|
|
5
|
-
#stream;
|
|
6
17
|
/**
|
|
7
18
|
* Create a new proxy execution service.
|
|
8
19
|
*
|
|
@@ -20,7 +31,8 @@ export class ProxyExecutionService extends AbstractExecutionService {
|
|
|
20
31
|
setupSnapProvider,
|
|
21
32
|
usePing: false,
|
|
22
33
|
});
|
|
23
|
-
this
|
|
34
|
+
_ProxyExecutionService_stream.set(this, void 0);
|
|
35
|
+
__classPrivateFieldSet(this, _ProxyExecutionService_stream, stream, "f");
|
|
24
36
|
}
|
|
25
37
|
/**
|
|
26
38
|
* Send a termination command to the proxy stream.
|
|
@@ -30,7 +42,7 @@ export class ProxyExecutionService extends AbstractExecutionService {
|
|
|
30
42
|
async terminateJob(job) {
|
|
31
43
|
// The `AbstractExecutionService` will have already closed the job stream,
|
|
32
44
|
// so we write to the runtime stream directly.
|
|
33
|
-
this
|
|
45
|
+
__classPrivateFieldGet(this, _ProxyExecutionService_stream, "f").write({
|
|
34
46
|
jobId: job.id,
|
|
35
47
|
data: {
|
|
36
48
|
jsonrpc: '2.0',
|
|
@@ -48,7 +60,7 @@ export class ProxyExecutionService extends AbstractExecutionService {
|
|
|
48
60
|
*/
|
|
49
61
|
async initEnvStream(jobId) {
|
|
50
62
|
const stream = new ProxyPostMessageStream({
|
|
51
|
-
stream: this
|
|
63
|
+
stream: __classPrivateFieldGet(this, _ProxyExecutionService_stream, "f"),
|
|
52
64
|
jobId,
|
|
53
65
|
});
|
|
54
66
|
// Send a request and await any response before continuing
|
|
@@ -65,4 +77,5 @@ export class ProxyExecutionService extends AbstractExecutionService {
|
|
|
65
77
|
return { worker: jobId, stream };
|
|
66
78
|
}
|
|
67
79
|
}
|
|
80
|
+
_ProxyExecutionService_stream = new WeakMap();
|
|
68
81
|
//# sourceMappingURL=ProxyExecutionService.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxyExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,eAAe;AAMhC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAMnE,MAAM,OAAO,qBAAsB,SAAQ,wBAAgC;
|
|
1
|
+
{"version":3,"file":"ProxyExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,MAAM,EAAE,eAAe;AAMhC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAMnE,MAAM,OAAO,qBAAsB,SAAQ,wBAAgC;IAGzE;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,GACoB;QACrC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAtBI,gDAA+B;QAwBtC,uBAAA,IAAI,iCAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,uBAAA,IAAI,qCAAQ,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,MAAM,EAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,MAAM,MAAM,GAAG,IAAI,sBAAsB,CAAC;YACxC,MAAM,EAAE,uBAAA,IAAI,qCAAQ;YACpB,KAAK;SACN,CAAC,CAAC;QAEH,0DAA0D;QAC1D,iFAAiF;QACjF,2DAA2D;QAC3D,sFAAsF;QACtF,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAC7B,MAAM,CAAC,KAAK,CAAC;gBACX,IAAI,EAAE,SAAS;gBACf,IAAI,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE;aACvD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype ProxyExecutionEnvironmentServiceArgs = {\n stream: BasePostMessageStream;\n} & ExecutionServiceArgs;\n\nexport class ProxyExecutionService extends AbstractExecutionService<string> {\n readonly #stream: BasePostMessageStream;\n\n /**\n * Create a new proxy execution service.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger to use for communication with the\n * `SnapController`.\n * @param args.setupSnapProvider - The function to use to set up the snap\n * provider.\n * @param args.stream - The stream to use for communicating with the proxy\n * executor.\n */\n constructor({\n stream,\n messenger,\n setupSnapProvider,\n }: ProxyExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n usePing: false,\n });\n\n this.#stream = stream;\n }\n\n /**\n * Send a termination command to the proxy stream.\n *\n * @param job - The job to terminate.\n */\n protected async terminateJob(job: TerminateJobArgs<string>) {\n // The `AbstractExecutionService` will have already closed the job stream,\n // so we write to the runtime stream directly.\n this.#stream.write({\n jobId: job.id,\n data: {\n jsonrpc: '2.0',\n method: 'terminateJob',\n id: nanoid(),\n },\n });\n }\n\n /**\n * Create a new stream for the specified job. This wraps the root stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId,\n });\n\n // Send a request and await any response before continuing\n // This simulates the behaviour of non-proxy environments by effectively awaiting\n // the load of the environment inside the proxy environment\n // This assumes the proxy environment is already loaded before this function is called\n await new Promise((resolve) => {\n stream.once('data', resolve);\n stream.write({\n name: 'command',\n data: { jsonrpc: '2.0', method: 'ping', id: nanoid() },\n });\n });\n\n return { worker: jobId, stream };\n }\n}\n"]}
|
|
@@ -1,10 +1,21 @@
|
|
|
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 _WebViewExecutionService_instances, _WebViewExecutionService_getWebView, _WebViewExecutionService_ensureWebViewLoaded;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.WebViewExecutionService = void 0;
|
|
4
16
|
const ProxyExecutionService_1 = require("../proxy/ProxyExecutionService.cjs");
|
|
5
17
|
const WebViewMessageStream_1 = require("./WebViewMessageStream.cjs");
|
|
6
18
|
class WebViewExecutionService extends ProxyExecutionService_1.ProxyExecutionService {
|
|
7
|
-
#getWebView;
|
|
8
19
|
constructor({ messenger, setupSnapProvider, getWebView, }) {
|
|
9
20
|
super({
|
|
10
21
|
messenger,
|
|
@@ -15,7 +26,9 @@ class WebViewExecutionService extends ProxyExecutionService_1.ProxyExecutionServ
|
|
|
15
26
|
getWebView,
|
|
16
27
|
}),
|
|
17
28
|
});
|
|
18
|
-
this
|
|
29
|
+
_WebViewExecutionService_instances.add(this);
|
|
30
|
+
_WebViewExecutionService_getWebView.set(this, void 0);
|
|
31
|
+
__classPrivateFieldSet(this, _WebViewExecutionService_getWebView, getWebView, "f");
|
|
19
32
|
}
|
|
20
33
|
/**
|
|
21
34
|
* Create a new stream for the specified job. This wraps the runtime stream
|
|
@@ -26,15 +39,16 @@ class WebViewExecutionService extends ProxyExecutionService_1.ProxyExecutionServ
|
|
|
26
39
|
*/
|
|
27
40
|
async initEnvStream(jobId) {
|
|
28
41
|
// Ensure that the WebView has been loaded before we proceed.
|
|
29
|
-
await this
|
|
42
|
+
await __classPrivateFieldGet(this, _WebViewExecutionService_instances, "m", _WebViewExecutionService_ensureWebViewLoaded).call(this);
|
|
30
43
|
return super.initEnvStream(jobId);
|
|
31
44
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Ensure that the WebView has been loaded by awaiting the getWebView promise.
|
|
34
|
-
*/
|
|
35
|
-
async #ensureWebViewLoaded() {
|
|
36
|
-
await this.#getWebView();
|
|
37
|
-
}
|
|
38
45
|
}
|
|
39
46
|
exports.WebViewExecutionService = WebViewExecutionService;
|
|
47
|
+
_WebViewExecutionService_getWebView = new WeakMap(), _WebViewExecutionService_instances = new WeakSet(), _WebViewExecutionService_ensureWebViewLoaded =
|
|
48
|
+
/**
|
|
49
|
+
* Ensure that the WebView has been loaded by awaiting the getWebView promise.
|
|
50
|
+
*/
|
|
51
|
+
async function _WebViewExecutionService_ensureWebViewLoaded() {
|
|
52
|
+
await __classPrivateFieldGet(this, _WebViewExecutionService_getWebView, "f").call(this);
|
|
53
|
+
};
|
|
40
54
|
//# sourceMappingURL=WebViewExecutionService.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebViewExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,8EAAuE;AAEvE,qEAA8D;AAM9D,MAAa,uBAAwB,SAAQ,6CAAqB;IAGhE,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,UAAU,GACkB;QAC5B,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;YACjB,MAAM,EAAE,IAAI,2CAAoB,CAAC;gBAC/B,IAAI,EAAE,QAAQ;gBACd,MAAM,EAAE,OAAO;gBACf,UAAU;aACX,CAAC;SACH,CAAC,CAAC;;QAfL,sDAAY;QAgBV,uBAAA,IAAI,uCAAe,UAAU,MAAA,CAAC;IAChC,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,6DAA6D;QAC7D,MAAM,uBAAA,IAAI,wFAAqB,MAAzB,IAAI,CAAuB,CAAC;QAElC,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CAQF;AAxCD,0DAwCC;;AANC;;GAEG;AACH,KAAK;IACH,MAAM,uBAAA,IAAI,2CAAY,MAAhB,IAAI,CAAc,CAAC;AAC3B,CAAC","sourcesContent":["import type { ExecutionServiceArgs } from '../AbstractExecutionService';\nimport { ProxyExecutionService } from '../proxy/ProxyExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n getWebView: () => Promise<WebViewInterface>;\n};\n\nexport class WebViewExecutionService extends ProxyExecutionService {\n #getWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n getWebView,\n }: WebViewExecutionServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n stream: new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n getWebView,\n }),\n });\n this.#getWebView = getWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Ensure that the WebView has been loaded before we proceed.\n await this.#ensureWebViewLoaded();\n\n return super.initEnvStream(jobId);\n }\n\n /**\n * Ensure that the WebView has been loaded by awaiting the getWebView promise.\n */\n async #ensureWebViewLoaded() {\n await this.#getWebView();\n }\n}\n"]}
|
|
@@ -1,7 +1,18 @@
|
|
|
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 _WebViewExecutionService_instances, _WebViewExecutionService_getWebView, _WebViewExecutionService_ensureWebViewLoaded;
|
|
1
13
|
import { ProxyExecutionService } from "../proxy/ProxyExecutionService.mjs";
|
|
2
14
|
import { WebViewMessageStream } from "./WebViewMessageStream.mjs";
|
|
3
15
|
export class WebViewExecutionService extends ProxyExecutionService {
|
|
4
|
-
#getWebView;
|
|
5
16
|
constructor({ messenger, setupSnapProvider, getWebView, }) {
|
|
6
17
|
super({
|
|
7
18
|
messenger,
|
|
@@ -12,7 +23,9 @@ export class WebViewExecutionService extends ProxyExecutionService {
|
|
|
12
23
|
getWebView,
|
|
13
24
|
}),
|
|
14
25
|
});
|
|
15
|
-
this
|
|
26
|
+
_WebViewExecutionService_instances.add(this);
|
|
27
|
+
_WebViewExecutionService_getWebView.set(this, void 0);
|
|
28
|
+
__classPrivateFieldSet(this, _WebViewExecutionService_getWebView, getWebView, "f");
|
|
16
29
|
}
|
|
17
30
|
/**
|
|
18
31
|
* Create a new stream for the specified job. This wraps the runtime stream
|
|
@@ -23,14 +36,15 @@ export class WebViewExecutionService extends ProxyExecutionService {
|
|
|
23
36
|
*/
|
|
24
37
|
async initEnvStream(jobId) {
|
|
25
38
|
// Ensure that the WebView has been loaded before we proceed.
|
|
26
|
-
await this
|
|
39
|
+
await __classPrivateFieldGet(this, _WebViewExecutionService_instances, "m", _WebViewExecutionService_ensureWebViewLoaded).call(this);
|
|
27
40
|
return super.initEnvStream(jobId);
|
|
28
41
|
}
|
|
29
|
-
/**
|
|
30
|
-
* Ensure that the WebView has been loaded by awaiting the getWebView promise.
|
|
31
|
-
*/
|
|
32
|
-
async #ensureWebViewLoaded() {
|
|
33
|
-
await this.#getWebView();
|
|
34
|
-
}
|
|
35
42
|
}
|
|
43
|
+
_WebViewExecutionService_getWebView = new WeakMap(), _WebViewExecutionService_instances = new WeakSet(), _WebViewExecutionService_ensureWebViewLoaded =
|
|
44
|
+
/**
|
|
45
|
+
* Ensure that the WebView has been loaded by awaiting the getWebView promise.
|
|
46
|
+
*/
|
|
47
|
+
async function _WebViewExecutionService_ensureWebViewLoaded() {
|
|
48
|
+
await __classPrivateFieldGet(this, _WebViewExecutionService_getWebView, "f").call(this);
|
|
49
|
+
};
|
|
36
50
|
//# sourceMappingURL=WebViewExecutionService.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,2CAAuC;AAEvE,OAAO,EAAE,oBAAoB,EAAE,mCAA+B;AAM9D,MAAM,OAAO,uBAAwB,SAAQ,qBAAqB;
|
|
1
|
+
{"version":3,"file":"WebViewExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,qBAAqB,EAAE,2CAAuC;AAEvE,OAAO,EAAE,oBAAoB,EAAE,mCAA+B;AAM9D,MAAM,OAAO,uBAAwB,SAAQ,qBAAqB;IAGhE,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,UAAU,GACkB;QAC5B,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;YACjB,MAAM,EAAE,IAAI,oBAAoB,CAAC;gBAC/B,IAAI,EAAE,QAAQ;gBACd,MAAM,EAAE,OAAO;gBACf,UAAU;aACX,CAAC;SACH,CAAC,CAAC;;QAfL,sDAAY;QAgBV,uBAAA,IAAI,uCAAe,UAAU,MAAA,CAAC;IAChC,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,KAAa;QACzC,6DAA6D;QAC7D,MAAM,uBAAA,IAAI,wFAAqB,MAAzB,IAAI,CAAuB,CAAC;QAElC,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CAQF;;AANC;;GAEG;AACH,KAAK;IACH,MAAM,uBAAA,IAAI,2CAAY,MAAhB,IAAI,CAAc,CAAC;AAC3B,CAAC","sourcesContent":["import type { ExecutionServiceArgs } from '../AbstractExecutionService';\nimport { ProxyExecutionService } from '../proxy/ProxyExecutionService';\nimport type { WebViewInterface } from './WebViewMessageStream';\nimport { WebViewMessageStream } from './WebViewMessageStream';\n\nexport type WebViewExecutionServiceArgs = ExecutionServiceArgs & {\n getWebView: () => Promise<WebViewInterface>;\n};\n\nexport class WebViewExecutionService extends ProxyExecutionService {\n #getWebView;\n\n constructor({\n messenger,\n setupSnapProvider,\n getWebView,\n }: WebViewExecutionServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n stream: new WebViewMessageStream({\n name: 'parent',\n target: 'child',\n getWebView,\n }),\n });\n this.#getWebView = getWebView;\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Ensure that the WebView has been loaded before we proceed.\n await this.#ensureWebViewLoaded();\n\n return super.initEnvStream(jobId);\n }\n\n /**\n * Ensure that the WebView has been loaded by awaiting the getWebView promise.\n */\n async #ensureWebViewLoaded() {\n await this.#getWebView();\n }\n}\n"]}
|
|
@@ -1,4 +1,16 @@
|
|
|
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 _WebViewMessageStream_name, _WebViewMessageStream_target, _WebViewMessageStream_webView;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.WebViewMessageStream = void 0;
|
|
4
16
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
@@ -9,9 +21,6 @@ const utils_2 = require("@metamask/utils");
|
|
|
9
21
|
* A special postMessage stream used to interface with a WebView.
|
|
10
22
|
*/
|
|
11
23
|
class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
|
|
12
|
-
#name;
|
|
13
|
-
#target;
|
|
14
|
-
#webView;
|
|
15
24
|
/**
|
|
16
25
|
* Creates a stream for communicating with other streams inside a WebView.
|
|
17
26
|
*
|
|
@@ -23,14 +32,17 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
|
|
|
23
32
|
*/
|
|
24
33
|
constructor({ name, target, getWebView }) {
|
|
25
34
|
super();
|
|
26
|
-
this
|
|
27
|
-
this
|
|
35
|
+
_WebViewMessageStream_name.set(this, void 0);
|
|
36
|
+
_WebViewMessageStream_target.set(this, void 0);
|
|
37
|
+
_WebViewMessageStream_webView.set(this, void 0);
|
|
38
|
+
__classPrivateFieldSet(this, _WebViewMessageStream_name, name, "f");
|
|
39
|
+
__classPrivateFieldSet(this, _WebViewMessageStream_target, target, "f");
|
|
28
40
|
this._onMessage = this._onMessage.bind(this);
|
|
29
41
|
// This is a bit atypical from other post-message streams.
|
|
30
42
|
// We have to wait for the WebView to fully load before we can continue using the stream.
|
|
31
43
|
getWebView()
|
|
32
44
|
.then((webView) => {
|
|
33
|
-
this
|
|
45
|
+
__classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
|
|
34
46
|
// This method is already bound.
|
|
35
47
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
36
48
|
webView.registerMessageListener(this._onMessage);
|
|
@@ -41,9 +53,9 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
|
|
|
41
53
|
});
|
|
42
54
|
}
|
|
43
55
|
_postMessage(data) {
|
|
44
|
-
(0, utils_2.assert)(this
|
|
56
|
+
(0, utils_2.assert)(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
|
|
45
57
|
const json = JSON.stringify({
|
|
46
|
-
target: this
|
|
58
|
+
target: __classPrivateFieldGet(this, _WebViewMessageStream_target, "f"),
|
|
47
59
|
data,
|
|
48
60
|
});
|
|
49
61
|
// To prevent XSS, we base64 encode the message before injecting it.
|
|
@@ -51,7 +63,7 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
|
|
|
51
63
|
// TODO: Should we use mobile native base64 here?
|
|
52
64
|
const bytes = (0, utils_2.stringToBytes)(json);
|
|
53
65
|
const base64 = (0, utils_2.bytesToBase64)(bytes);
|
|
54
|
-
this
|
|
66
|
+
__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage('${base64}')`);
|
|
55
67
|
}
|
|
56
68
|
_onMessage(event) {
|
|
57
69
|
if (typeof event.data !== 'string') {
|
|
@@ -60,17 +72,18 @@ class WebViewMessageStream extends post_message_stream_1.BasePostMessageStream {
|
|
|
60
72
|
const message = JSON.parse(event.data);
|
|
61
73
|
// Notice that we don't check targetWindow or targetOrigin here.
|
|
62
74
|
// This doesn't seem possible to do in RN.
|
|
63
|
-
if (!(0, utils_1.isValidStreamMessage)(message) || message.target !== this
|
|
75
|
+
if (!(0, utils_1.isValidStreamMessage)(message) || message.target !== __classPrivateFieldGet(this, _WebViewMessageStream_name, "f")) {
|
|
64
76
|
return;
|
|
65
77
|
}
|
|
66
78
|
this._onData(message.data);
|
|
67
79
|
}
|
|
68
80
|
_destroy() {
|
|
69
|
-
(0, utils_2.assert)(this
|
|
81
|
+
(0, utils_2.assert)(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
|
|
70
82
|
// This method is already bound.
|
|
71
83
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
72
|
-
this
|
|
84
|
+
__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").unregisterMessageListener(this._onMessage);
|
|
73
85
|
}
|
|
74
86
|
}
|
|
75
87
|
exports.WebViewMessageStream = WebViewMessageStream;
|
|
88
|
+
_WebViewMessageStream_name = new WeakMap(), _WebViewMessageStream_target = new WeakMap(), _WebViewMessageStream_webView = new WeakMap();
|
|
76
89
|
//# sourceMappingURL=WebViewMessageStream.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewMessageStream.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebViewMessageStream.cjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAAsE;AACtE,uEAAgF;AAChF,uDAAiD;AACjD,2CAAuE;AAcvE;;GAEG;AACH,MAAa,oBAAqB,SAAQ,2CAAqB;IAO7D;;;;;;;;OAQG;IACH,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAqB;QACzD,KAAK,EAAE,CAAC;QAhBV,6CAAM;QAEN,+CAAQ;QAER,gDAAuC;QAcrC,uBAAA,IAAI,8BAAS,IAAI,MAAA,CAAC;QAClB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QAEtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE7C,0DAA0D;QAC1D,yFAAyF;QACzF,UAAU,EAAE;aACT,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAChB,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;YACxB,gCAAgC;YAChC,6DAA6D;YAC7D,OAAO,CAAC,uBAAuB,CAAC,IAAI,CAAC,UAAU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AAC,CAAC;IAC3D,CAAC;CACF;AA5ED,oDA4EC","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 { logError } from '@metamask/snaps-utils';\nimport { assert, bytesToBase64, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\nexport type WebViewStreamArgs = {\n name: string;\n target: string;\n getWebView: () => Promise<WebViewInterface>;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\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.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.getWebView - A asynchronous getter for the webview.\n */\n constructor({ name, target, getWebView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This is a bit atypical from other post-message streams.\n // We have to wait for the WebView to fully load before we can continue using the stream.\n getWebView()\n .then((webView) => {\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n webView.registerMessageListener(this._onMessage);\n this._handshake();\n })\n .catch((error) => {\n logError(error);\n });\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we base64 encode the message before injecting it.\n // This adds significant performance overhead.\n // TODO: Should we use mobile native base64 here?\n const bytes = stringToBytes(json);\n const base64 = bytesToBase64(bytes);\n this.#webView.injectJavaScript(`window.postMessage('${base64}')`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\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 assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"]}
|
|
@@ -1,3 +1,15 @@
|
|
|
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 _WebViewMessageStream_name, _WebViewMessageStream_target, _WebViewMessageStream_webView;
|
|
1
13
|
import $metamaskpostmessagestream from "@metamask/post-message-stream";
|
|
2
14
|
const { BasePostMessageStream } = $metamaskpostmessagestream;
|
|
3
15
|
import { isValidStreamMessage } from "@metamask/post-message-stream/dist/utils.js";
|
|
@@ -7,9 +19,6 @@ import { assert, bytesToBase64, stringToBytes } from "@metamask/utils";
|
|
|
7
19
|
* A special postMessage stream used to interface with a WebView.
|
|
8
20
|
*/
|
|
9
21
|
export class WebViewMessageStream extends BasePostMessageStream {
|
|
10
|
-
#name;
|
|
11
|
-
#target;
|
|
12
|
-
#webView;
|
|
13
22
|
/**
|
|
14
23
|
* Creates a stream for communicating with other streams inside a WebView.
|
|
15
24
|
*
|
|
@@ -21,14 +30,17 @@ export class WebViewMessageStream extends BasePostMessageStream {
|
|
|
21
30
|
*/
|
|
22
31
|
constructor({ name, target, getWebView }) {
|
|
23
32
|
super();
|
|
24
|
-
this
|
|
25
|
-
this
|
|
33
|
+
_WebViewMessageStream_name.set(this, void 0);
|
|
34
|
+
_WebViewMessageStream_target.set(this, void 0);
|
|
35
|
+
_WebViewMessageStream_webView.set(this, void 0);
|
|
36
|
+
__classPrivateFieldSet(this, _WebViewMessageStream_name, name, "f");
|
|
37
|
+
__classPrivateFieldSet(this, _WebViewMessageStream_target, target, "f");
|
|
26
38
|
this._onMessage = this._onMessage.bind(this);
|
|
27
39
|
// This is a bit atypical from other post-message streams.
|
|
28
40
|
// We have to wait for the WebView to fully load before we can continue using the stream.
|
|
29
41
|
getWebView()
|
|
30
42
|
.then((webView) => {
|
|
31
|
-
this
|
|
43
|
+
__classPrivateFieldSet(this, _WebViewMessageStream_webView, webView, "f");
|
|
32
44
|
// This method is already bound.
|
|
33
45
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
34
46
|
webView.registerMessageListener(this._onMessage);
|
|
@@ -39,9 +51,9 @@ export class WebViewMessageStream extends BasePostMessageStream {
|
|
|
39
51
|
});
|
|
40
52
|
}
|
|
41
53
|
_postMessage(data) {
|
|
42
|
-
assert(this
|
|
54
|
+
assert(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
|
|
43
55
|
const json = JSON.stringify({
|
|
44
|
-
target: this
|
|
56
|
+
target: __classPrivateFieldGet(this, _WebViewMessageStream_target, "f"),
|
|
45
57
|
data,
|
|
46
58
|
});
|
|
47
59
|
// To prevent XSS, we base64 encode the message before injecting it.
|
|
@@ -49,7 +61,7 @@ export class WebViewMessageStream extends BasePostMessageStream {
|
|
|
49
61
|
// TODO: Should we use mobile native base64 here?
|
|
50
62
|
const bytes = stringToBytes(json);
|
|
51
63
|
const base64 = bytesToBase64(bytes);
|
|
52
|
-
this
|
|
64
|
+
__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").injectJavaScript(`window.postMessage('${base64}')`);
|
|
53
65
|
}
|
|
54
66
|
_onMessage(event) {
|
|
55
67
|
if (typeof event.data !== 'string') {
|
|
@@ -58,16 +70,17 @@ export class WebViewMessageStream extends BasePostMessageStream {
|
|
|
58
70
|
const message = JSON.parse(event.data);
|
|
59
71
|
// Notice that we don't check targetWindow or targetOrigin here.
|
|
60
72
|
// This doesn't seem possible to do in RN.
|
|
61
|
-
if (!isValidStreamMessage(message) || message.target !== this
|
|
73
|
+
if (!isValidStreamMessage(message) || message.target !== __classPrivateFieldGet(this, _WebViewMessageStream_name, "f")) {
|
|
62
74
|
return;
|
|
63
75
|
}
|
|
64
76
|
this._onData(message.data);
|
|
65
77
|
}
|
|
66
78
|
_destroy() {
|
|
67
|
-
assert(this
|
|
79
|
+
assert(__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f"));
|
|
68
80
|
// This method is already bound.
|
|
69
81
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
70
|
-
this
|
|
82
|
+
__classPrivateFieldGet(this, _WebViewMessageStream_webView, "f").unregisterMessageListener(this._onMessage);
|
|
71
83
|
}
|
|
72
84
|
}
|
|
85
|
+
_WebViewMessageStream_name = new WeakMap(), _WebViewMessageStream_target = new WeakMap(), _WebViewMessageStream_webView = new WeakMap();
|
|
73
86
|
//# sourceMappingURL=WebViewMessageStream.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewMessageStream.mjs","sourceRoot":"","sources":["../../../src/services/webview/WebViewMessageStream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebViewMessageStream.mjs","sourceRoot":"","sources":["../../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AAC,IAAI,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;QACpC,uBAAA,IAAI,qCAAS,CAAC,gBAAgB,CAAC,uBAAuB,MAAM,IAAI,CAAC,CAAC;IACpE,CAAC;IAEO,UAAU,CAAC,KAAuB;QACxC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEvC,gEAAgE;QAChE,0CAA0C;QAC1C,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,uBAAA,IAAI,kCAAM,EAAE,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,MAAM,CAAC,uBAAA,IAAI,qCAAS,CAAC,CAAC;QACtB,gCAAgC;QAChC,6DAA6D;QAC7D,uBAAA,IAAI,qCAAS,CAAC,yBAAyB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC3D,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 { logError } from '@metamask/snaps-utils';\nimport { assert, bytesToBase64, stringToBytes } from '@metamask/utils';\n\nexport type WebViewInterface = {\n injectJavaScript(js: string): void;\n registerMessageListener(listener: (event: PostMessageEvent) => void): void;\n unregisterMessageListener(listener: (event: PostMessageEvent) => void): void;\n};\n\nexport type WebViewStreamArgs = {\n name: string;\n target: string;\n getWebView: () => Promise<WebViewInterface>;\n};\n\n/**\n * A special postMessage stream used to interface with a WebView.\n */\nexport class WebViewMessageStream extends BasePostMessageStream {\n #name;\n\n #target;\n\n #webView: WebViewInterface | undefined;\n\n /**\n * Creates a stream for communicating with other streams inside a WebView.\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.\n * @param args.target - The name of the stream to exchange messages with.\n * @param args.getWebView - A asynchronous getter for the webview.\n */\n constructor({ name, target, getWebView }: WebViewStreamArgs) {\n super();\n\n this.#name = name;\n this.#target = target;\n\n this._onMessage = this._onMessage.bind(this);\n\n // This is a bit atypical from other post-message streams.\n // We have to wait for the WebView to fully load before we can continue using the stream.\n getWebView()\n .then((webView) => {\n this.#webView = webView;\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n webView.registerMessageListener(this._onMessage);\n this._handshake();\n })\n .catch((error) => {\n logError(error);\n });\n }\n\n protected _postMessage(data: unknown): void {\n assert(this.#webView);\n const json = JSON.stringify({\n target: this.#target,\n data,\n });\n\n // To prevent XSS, we base64 encode the message before injecting it.\n // This adds significant performance overhead.\n // TODO: Should we use mobile native base64 here?\n const bytes = stringToBytes(json);\n const base64 = bytesToBase64(bytes);\n this.#webView.injectJavaScript(`window.postMessage('${base64}')`);\n }\n\n private _onMessage(event: PostMessageEvent): void {\n if (typeof event.data !== 'string') {\n return;\n }\n\n const message = JSON.parse(event.data);\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 assert(this.#webView);\n // This method is already bound.\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.#webView.unregisterMessageListener(this._onMessage);\n }\n}\n"]}
|
|
@@ -1,4 +1,16 @@
|
|
|
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 _WebWorkerExecutionService_documentUrl, _WebWorkerExecutionService_runtimeStream;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.WebWorkerExecutionService = exports.WORKER_POOL_ID = void 0;
|
|
4
16
|
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
@@ -9,8 +21,6 @@ const AbstractExecutionService_1 = require("../AbstractExecutionService.cjs");
|
|
|
9
21
|
const ProxyPostMessageStream_1 = require("../ProxyPostMessageStream.cjs");
|
|
10
22
|
exports.WORKER_POOL_ID = 'snaps-worker-pool';
|
|
11
23
|
class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
|
|
12
|
-
#documentUrl;
|
|
13
|
-
#runtimeStream;
|
|
14
24
|
/**
|
|
15
25
|
* Create a new webworker execution service.
|
|
16
26
|
*
|
|
@@ -27,7 +37,9 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
|
|
|
27
37
|
messenger,
|
|
28
38
|
setupSnapProvider,
|
|
29
39
|
});
|
|
30
|
-
this
|
|
40
|
+
_WebWorkerExecutionService_documentUrl.set(this, void 0);
|
|
41
|
+
_WebWorkerExecutionService_runtimeStream.set(this, void 0);
|
|
42
|
+
__classPrivateFieldSet(this, _WebWorkerExecutionService_documentUrl, documentUrl, "f");
|
|
31
43
|
}
|
|
32
44
|
/**
|
|
33
45
|
* Send a termination command to the worker pool document.
|
|
@@ -37,8 +49,8 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
|
|
|
37
49
|
async terminateJob(job) {
|
|
38
50
|
// The `AbstractExecutionService` will have already closed the job stream,
|
|
39
51
|
// so we write to the runtime stream directly.
|
|
40
|
-
(0, utils_1.assert)(this
|
|
41
|
-
this
|
|
52
|
+
(0, utils_1.assert)(__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"), 'Runtime stream not initialized.');
|
|
53
|
+
__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f").write({
|
|
42
54
|
jobId: job.id,
|
|
43
55
|
data: {
|
|
44
56
|
jsonrpc: '2.0',
|
|
@@ -58,9 +70,9 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
|
|
|
58
70
|
// Lazily create the worker pool document.
|
|
59
71
|
await this.createDocument();
|
|
60
72
|
// `createDocument` should have initialized the runtime stream.
|
|
61
|
-
(0, utils_1.assert)(this
|
|
73
|
+
(0, utils_1.assert)(__classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"), 'Runtime stream not initialized.');
|
|
62
74
|
const stream = new ProxyPostMessageStream_1.ProxyPostMessageStream({
|
|
63
|
-
stream: this
|
|
75
|
+
stream: __classPrivateFieldGet(this, _WebWorkerExecutionService_runtimeStream, "f"),
|
|
64
76
|
jobId,
|
|
65
77
|
});
|
|
66
78
|
return { worker: jobId, stream };
|
|
@@ -75,14 +87,19 @@ class WebWorkerExecutionService extends AbstractExecutionService_1.AbstractExecu
|
|
|
75
87
|
if (document.getElementById(exports.WORKER_POOL_ID)) {
|
|
76
88
|
return;
|
|
77
89
|
}
|
|
78
|
-
const window = await (0, snaps_utils_1.createWindow)(
|
|
79
|
-
|
|
90
|
+
const window = await (0, snaps_utils_1.createWindow)({
|
|
91
|
+
uri: __classPrivateFieldGet(this, _WebWorkerExecutionService_documentUrl, "f").href,
|
|
92
|
+
id: exports.WORKER_POOL_ID,
|
|
93
|
+
sandbox: false,
|
|
94
|
+
});
|
|
95
|
+
__classPrivateFieldSet(this, _WebWorkerExecutionService_runtimeStream, new post_message_stream_1.WindowPostMessageStream({
|
|
80
96
|
name: 'parent',
|
|
81
97
|
target: 'child',
|
|
82
98
|
targetWindow: window,
|
|
83
99
|
targetOrigin: '*',
|
|
84
|
-
});
|
|
100
|
+
}), "f");
|
|
85
101
|
}
|
|
86
102
|
}
|
|
87
103
|
exports.WebWorkerExecutionService = WebWorkerExecutionService;
|
|
104
|
+
_WebWorkerExecutionService_documentUrl = new WeakMap(), _WebWorkerExecutionService_runtimeStream = new WeakMap();
|
|
88
105
|
//# sourceMappingURL=WebWorkerExecutionService.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebWorkerExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebWorkerExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/webworker/WebWorkerExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uEAAwE;AACxE,uDAAqD;AACrD,2CAAyC;AACzC,mCAAgC;AAMhC,8EAAuE;AACvE,0EAAmE;AAMtD,QAAA,cAAc,GAAG,mBAAmB,CAAC;AAElD,MAAa,yBAA0B,SAAQ,mDAAgC;IAK7E;;;;;;;;;;OAUG;IACH,YAAY,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,GACwB;QACzC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QAvBL,yDAAkB;QAElB,2DAAuC;QAuBrC,uBAAA,IAAI,0CAAgB,WAAW,MAAA,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C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ostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } from '../AbstractExecutionService';\nimport { ProxyPostMessageStream } from '../ProxyPostMessageStream';\n\ntype WebWorkerExecutionEnvironmentServiceArgs = {\n documentUrl: URL;\n} & ExecutionServiceArgs;\n\nexport const WORKER_POOL_ID = 'snaps-worker-pool';\n\nexport class WebWorkerExecutionService extends AbstractExecutionService<string> {\n #documentUrl: URL;\n\n #runtimeStream?: BasePostMessageStream;\n\n /**\n * Create a new webworker execution service.\n *\n * @param args - The constructor arguments.\n * @param args.documentUrl - The URL of the worker pool document to use as the\n * execution environment.\n * @param args.messenger - The messenger to use for communication with the\n * `SnapController`.\n * @param args.setupSnapProvider - The function to use to set up the snap\n * provider.\n */\n constructor({\n documentUrl,\n messenger,\n setupSnapProvider,\n }: WebWorkerExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n\n this.#documentUrl = documentUrl;\n }\n\n /**\n * Send a termination command to the worker pool document.\n *\n * @param job - The job to terminate.\n */\n protected async terminateJob(job: TerminateJobArgs<string>) {\n // The `AbstractExecutionService` will have already closed the job stream,\n // so we write to the runtime stream directly.\n assert(this.#runtimeStream, 'Runtime stream not initialized.');\n this.#runtimeStream.write({\n jobId: job.id,\n data: {\n jsonrpc: '2.0',\n method: 'terminateJob',\n id: nanoid(),\n },\n });\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(jobId: string) {\n // Lazily create the worker pool document.\n await this.createDocument();\n\n // `createDocument` should have initialized the runtime stream.\n assert(this.#runtimeStream, 'Runtime stream not initialized.');\n\n const stream = new ProxyPostMessageStream({\n stream: this.#runtimeStream,\n jobId,\n });\n\n return { worker: jobId, stream };\n }\n\n /**\n * Creates the worker pool document to be used as the execution environment.\n *\n * If the document already exists, this does nothing.\n */\n private async createDocument() {\n // We only want to create a single pool.\n if (document.getElementById(WORKER_POOL_ID)) {\n return;\n }\n\n const window = await createWindow({\n uri: this.#documentUrl.href,\n id: WORKER_POOL_ID,\n sandbox: false,\n });\n\n this.#runtimeStream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: window,\n targetOrigin: '*',\n });\n }\n}\n"]}
|