@metamask/snaps-controllers 0.24.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/LICENSE +18 -0
- package/README.md +3 -0
- package/dist/cronjob/CronjobController.d.ts +115 -0
- package/dist/cronjob/CronjobController.js +231 -0
- package/dist/cronjob/CronjobController.js.map +1 -0
- package/dist/cronjob/index.d.ts +1 -0
- package/dist/cronjob/index.js +18 -0
- package/dist/cronjob/index.js.map +1 -0
- package/dist/fsm.d.ts +24 -0
- package/dist/fsm.js +75 -0
- package/dist/fsm.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/multichain/MultiChainController.d.ts +135 -0
- package/dist/multichain/MultiChainController.js +348 -0
- package/dist/multichain/MultiChainController.js.map +1 -0
- package/dist/multichain/index.d.ts +3 -0
- package/dist/multichain/index.js +20 -0
- package/dist/multichain/index.js.map +1 -0
- package/dist/multichain/matching.d.ts +9 -0
- package/dist/multichain/matching.js +58 -0
- package/dist/multichain/matching.js.map +1 -0
- package/dist/multichain/middleware.d.ts +14 -0
- package/dist/multichain/middleware.js +42 -0
- package/dist/multichain/middleware.js.map +1 -0
- package/dist/services/AbstractExecutionService.d.ts +124 -0
- package/dist/services/AbstractExecutionService.js +316 -0
- package/dist/services/AbstractExecutionService.js.map +1 -0
- package/dist/services/ExecutionService.d.ts +68 -0
- package/dist/services/ExecutionService.js +4 -0
- package/dist/services/ExecutionService.js.map +1 -0
- package/dist/services/browser.d.ts +3 -0
- package/dist/services/browser.js +21 -0
- package/dist/services/browser.js.map +1 -0
- package/dist/services/iframe/IframeExecutionService.d.ts +25 -0
- package/dist/services/iframe/IframeExecutionService.js +82 -0
- package/dist/services/iframe/IframeExecutionService.js.map +1 -0
- package/dist/services/iframe/index.d.ts +1 -0
- package/dist/services/iframe/index.js +18 -0
- package/dist/services/iframe/index.js.map +1 -0
- package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.d.ts +3 -0
- package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js +74 -0
- package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js.map +1 -0
- package/dist/services/iframe/test/server.d.ts +11 -0
- package/dist/services/iframe/test/server.js +71 -0
- package/dist/services/iframe/test/server.js.map +1 -0
- package/dist/services/index.d.ts +4 -0
- package/dist/services/index.js +21 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/node/NodeProcessExecutionService.d.ts +11 -0
- package/dist/services/node/NodeProcessExecutionService.js +18 -0
- package/dist/services/node/NodeProcessExecutionService.js.map +1 -0
- package/dist/services/node/NodeThreadExecutionService.d.ts +11 -0
- package/dist/services/node/NodeThreadExecutionService.js +18 -0
- package/dist/services/node/NodeThreadExecutionService.js.map +1 -0
- package/dist/services/node/index.d.ts +2 -0
- package/dist/services/node/index.js +19 -0
- package/dist/services/node/index.js.map +1 -0
- package/dist/snaps/RequestQueue.d.ts +24 -0
- package/dist/snaps/RequestQueue.js +47 -0
- package/dist/snaps/RequestQueue.js.map +1 -0
- package/dist/snaps/SnapController.d.ts +600 -0
- package/dist/snaps/SnapController.js +1314 -0
- package/dist/snaps/SnapController.js.map +1 -0
- package/dist/snaps/Timer.d.ts +39 -0
- package/dist/snaps/Timer.js +86 -0
- package/dist/snaps/Timer.js.map +1 -0
- package/dist/snaps/endowments/cronjob.d.ts +49 -0
- package/dist/snaps/endowments/cronjob.js +105 -0
- package/dist/snaps/endowments/cronjob.js.map +1 -0
- package/dist/snaps/endowments/enum.d.ts +8 -0
- package/dist/snaps/endowments/enum.js +13 -0
- package/dist/snaps/endowments/enum.js.map +1 -0
- package/dist/snaps/endowments/ethereum-provider.d.ts +13 -0
- package/dist/snaps/endowments/ethereum-provider.js +31 -0
- package/dist/snaps/endowments/ethereum-provider.js.map +1 -0
- package/dist/snaps/endowments/index.d.ts +67 -0
- package/dist/snaps/endowments/index.js +39 -0
- package/dist/snaps/endowments/index.js.map +1 -0
- package/dist/snaps/endowments/keyring.d.ts +39 -0
- package/dist/snaps/endowments/keyring.js +103 -0
- package/dist/snaps/endowments/keyring.js.map +1 -0
- package/dist/snaps/endowments/long-running.d.ts +13 -0
- package/dist/snaps/endowments/long-running.js +28 -0
- package/dist/snaps/endowments/long-running.js.map +1 -0
- package/dist/snaps/endowments/network-access.d.ts +13 -0
- package/dist/snaps/endowments/network-access.js +29 -0
- package/dist/snaps/endowments/network-access.js.map +1 -0
- package/dist/snaps/endowments/transaction-insight.d.ts +38 -0
- package/dist/snaps/endowments/transaction-insight.js +106 -0
- package/dist/snaps/endowments/transaction-insight.js.map +1 -0
- package/dist/snaps/index.d.ts +4 -0
- package/dist/snaps/index.js +21 -0
- package/dist/snaps/index.js.map +1 -0
- package/dist/snaps/selectors.d.ts +2 -0
- package/dist/snaps/selectors.js +6 -0
- package/dist/snaps/selectors.js.map +1 -0
- package/dist/snaps/utils/index.d.ts +2 -0
- package/dist/snaps/utils/index.js +19 -0
- package/dist/snaps/utils/index.js.map +1 -0
- package/dist/snaps/utils/npm.d.ts +14 -0
- package/dist/snaps/utils/npm.js +81 -0
- package/dist/snaps/utils/npm.js.map +1 -0
- package/dist/snaps/utils/stream.d.ts +30 -0
- package/dist/snaps/utils/stream.js +124 -0
- package/dist/snaps/utils/stream.js.map +1 -0
- package/dist/utils.d.ts +128 -0
- package/dist/utils.js +92 -0
- package/dist/utils.js.map +1 -0
- package/package.json +99 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// Subset of exports meant for browser environments, omits Node.js services
|
|
18
|
+
__exportStar(require("./AbstractExecutionService"), exports);
|
|
19
|
+
__exportStar(require("./ExecutionService"), exports);
|
|
20
|
+
__exportStar(require("./iframe"), exports);
|
|
21
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/services/browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2EAA2E;AAC3E,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB","sourcesContent":["// Subset of exports meant for browser environments, omits Node.js services\nexport * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { BasePostMessageStream } from '@metamask/post-message-stream';
|
|
2
|
+
import { Job, AbstractExecutionService, ExecutionServiceArgs } from '../AbstractExecutionService';
|
|
3
|
+
declare type IframeExecutionEnvironmentServiceArgs = {
|
|
4
|
+
iframeUrl: URL;
|
|
5
|
+
} & ExecutionServiceArgs;
|
|
6
|
+
export declare class IframeExecutionService extends AbstractExecutionService<Window> {
|
|
7
|
+
iframeUrl: URL;
|
|
8
|
+
constructor({ iframeUrl, messenger, setupSnapProvider, }: IframeExecutionEnvironmentServiceArgs);
|
|
9
|
+
protected terminateJob(jobWrapper: Job<Window>): void;
|
|
10
|
+
protected initEnvStream(jobId: string): Promise<{
|
|
11
|
+
worker: Window;
|
|
12
|
+
stream: BasePostMessageStream;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Creates the iframe to be used as the execution environment. This may run
|
|
16
|
+
* forever if the iframe never loads, but the promise should be wrapped in
|
|
17
|
+
* an initialization timeout in the SnapController.
|
|
18
|
+
*
|
|
19
|
+
* @param uri - The iframe URI.
|
|
20
|
+
* @param jobId - The job id.
|
|
21
|
+
* @returns A promise that resolves to the contentWindow of the iframe.
|
|
22
|
+
*/
|
|
23
|
+
private createWindow;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IframeExecutionService = void 0;
|
|
4
|
+
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
5
|
+
const AbstractExecutionService_1 = require("../AbstractExecutionService");
|
|
6
|
+
class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
|
|
7
|
+
constructor({ iframeUrl, messenger, setupSnapProvider, }) {
|
|
8
|
+
super({
|
|
9
|
+
messenger,
|
|
10
|
+
setupSnapProvider,
|
|
11
|
+
});
|
|
12
|
+
this.iframeUrl = iframeUrl;
|
|
13
|
+
}
|
|
14
|
+
terminateJob(jobWrapper) {
|
|
15
|
+
var _a;
|
|
16
|
+
(_a = document.getElementById(jobWrapper.id)) === null || _a === void 0 ? void 0 : _a.remove();
|
|
17
|
+
}
|
|
18
|
+
async initEnvStream(jobId) {
|
|
19
|
+
const iframeWindow = await this.createWindow(this.iframeUrl.toString(), jobId);
|
|
20
|
+
const stream = new post_message_stream_1.WindowPostMessageStream({
|
|
21
|
+
name: 'parent',
|
|
22
|
+
target: 'child',
|
|
23
|
+
targetWindow: iframeWindow,
|
|
24
|
+
targetOrigin: '*',
|
|
25
|
+
});
|
|
26
|
+
return { worker: iframeWindow, stream };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates the iframe to be used as the execution environment. This may run
|
|
30
|
+
* forever if the iframe never loads, but the promise should be wrapped in
|
|
31
|
+
* an initialization timeout in the SnapController.
|
|
32
|
+
*
|
|
33
|
+
* @param uri - The iframe URI.
|
|
34
|
+
* @param jobId - The job id.
|
|
35
|
+
* @returns A promise that resolves to the contentWindow of the iframe.
|
|
36
|
+
*/
|
|
37
|
+
createWindow(uri, jobId) {
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
const iframe = document.createElement('iframe');
|
|
40
|
+
// The order of operations appears to matter for everything except this
|
|
41
|
+
// attribute. We may as well set it here.
|
|
42
|
+
iframe.setAttribute('id', jobId);
|
|
43
|
+
// In the past, we've had problems that appear to be symptomatic of the
|
|
44
|
+
// iframe firing the `load` event before its scripts are actually loaded,
|
|
45
|
+
// which has prevented snaps from executing properly. Therefore, we set
|
|
46
|
+
// the `src` attribute and append the iframe to the DOM before attaching
|
|
47
|
+
// the `load` listener.
|
|
48
|
+
//
|
|
49
|
+
// `load` should only fire when "all dependent resources" have been
|
|
50
|
+
// loaded, which includes scripts.
|
|
51
|
+
//
|
|
52
|
+
// MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
|
|
53
|
+
// Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489
|
|
54
|
+
iframe.setAttribute('src', uri);
|
|
55
|
+
document.body.appendChild(iframe);
|
|
56
|
+
iframe.addEventListener('load', () => {
|
|
57
|
+
if (iframe.contentWindow) {
|
|
58
|
+
resolve(iframe.contentWindow);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// We don't know of a case when this would happen, but better to fail
|
|
62
|
+
// fast if it does.
|
|
63
|
+
reject(new Error(`iframe.contentWindow not present on load for job "${jobId}".`));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
// We need to set the sandbox attribute after appending the iframe to the
|
|
67
|
+
// DOM, otherwise errors in the iframe will not be propagated via `error`
|
|
68
|
+
// and `unhandledrejection` events, and we cannot catch and handle them.
|
|
69
|
+
// We wish we knew why this was the case.
|
|
70
|
+
//
|
|
71
|
+
// We set this property after adding the `load` listener because it
|
|
72
|
+
// appears to work dependably. ¯\_(ツ)_/¯
|
|
73
|
+
//
|
|
74
|
+
// We apply this property as a principle of least authority (POLA)
|
|
75
|
+
// measure.
|
|
76
|
+
// Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox
|
|
77
|
+
iframe.setAttribute('sandbox', 'allow-scripts');
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.IframeExecutionService = IframeExecutionService;
|
|
82
|
+
//# sourceMappingURL=IframeExecutionService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IframeExecutionService.js","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AACvC,0EAIqC;AAMrC,MAAa,sBAAuB,SAAQ,mDAAgC;IAG1E,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,GACqB;QACtC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAuB;;QAC5C,MAAA,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,0CAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,KAAa;QAIzC,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,YAAY,CAC1C,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,EACzB,KAAK,CACN,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,6CAAuB,CAAC;YACzC,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,OAAO;YACf,YAAY,EAAE,YAAY;YAC1B,YAAY,EAAE,GAAG;SAClB,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;IAC1C,CAAC;IAED;;;;;;;;OAQG;IACK,YAAY,CAAC,GAAW,EAAE,KAAa;QAC7C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YAChD,uEAAuE;YACvE,yCAAyC;YACzC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAEjC,uEAAuE;YACvE,yEAAyE;YACzE,uEAAuE;YACvE,wEAAwE;YACxE,uBAAuB;YACvB,EAAE;YACF,mEAAmE;YACnE,kCAAkC;YAClC,EAAE;YACF,mGAAmG;YACnG,iJAAiJ;YACjJ,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;YAChC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAElC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,IAAI,MAAM,CAAC,aAAa,EAAE;oBACxB,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;iBAC/B;qBAAM;oBACL,qEAAqE;oBACrE,mBAAmB;oBACnB,MAAM,CACJ,IAAI,KAAK,CACP,qDAAqD,KAAK,IAAI,CAC/D,CACF,CAAC;iBACH;YACH,CAAC,CAAC,CAAC;YAEH,yEAAyE;YACzE,yEAAyE;YACzE,wEAAwE;YACxE,yCAAyC;YACzC,EAAE;YACF,mEAAmE;YACnE,wCAAwC;YACxC,EAAE;YACF,kEAAkE;YAClE,WAAW;YACX,qFAAqF;YACrF,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA/FD,wDA+FC","sourcesContent":["import {\n WindowPostMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\nimport {\n Job,\n AbstractExecutionService,\n ExecutionServiceArgs,\n} from '../AbstractExecutionService';\n\ntype IframeExecutionEnvironmentServiceArgs = {\n iframeUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class IframeExecutionService extends AbstractExecutionService<Window> {\n public iframeUrl: URL;\n\n constructor({\n iframeUrl,\n messenger,\n setupSnapProvider,\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: Job<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await this.createWindow(\n this.iframeUrl.toString(),\n jobId,\n );\n const stream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: iframeWindow,\n targetOrigin: '*',\n });\n\n return { worker: iframeWindow, stream };\n }\n\n /**\n * Creates the iframe to be used as the execution environment. This may run\n * forever if the iframe never loads, but the promise should be wrapped in\n * an initialization timeout in the SnapController.\n *\n * @param uri - The iframe URI.\n * @param jobId - The job id.\n * @returns A promise that resolves to the contentWindow of the iframe.\n */\n private createWindow(uri: string, jobId: string): Promise<Window> {\n return new Promise((resolve, reject) => {\n const iframe = document.createElement('iframe');\n // The order of operations appears to matter for everything except this\n // attribute. We may as well set it here.\n iframe.setAttribute('id', jobId);\n\n // In the past, we've had problems that appear to be symptomatic of the\n // iframe firing the `load` event before its scripts are actually loaded,\n // which has prevented snaps from executing properly. Therefore, we set\n // the `src` attribute and append the iframe to the DOM before attaching\n // the `load` listener.\n //\n // `load` should only fire when \"all dependent resources\" have been\n // loaded, which includes scripts.\n //\n // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event\n // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489\n iframe.setAttribute('src', uri);\n document.body.appendChild(iframe);\n\n iframe.addEventListener('load', () => {\n if (iframe.contentWindow) {\n resolve(iframe.contentWindow);\n } else {\n // We don't know of a case when this would happen, but better to fail\n // fast if it does.\n reject(\n new Error(\n `iframe.contentWindow not present on load for job \"${jobId}\".`,\n ),\n );\n }\n });\n\n // We need to set the sandbox attribute after appending the iframe to the\n // DOM, otherwise errors in the iframe will not be propagated via `error`\n // and `unhandledrejection` events, and we cannot catch and handle them.\n // We wish we knew why this was the case.\n //\n // We set this property after adding the `load` listener because it\n // appears to work dependably. ¯\\_(ツ)_/¯\n //\n // We apply this property as a principle of least authority (POLA)\n // measure.\n // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox\n iframe.setAttribute('sandbox', 'allow-scripts');\n });\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './IframeExecutionService';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./IframeExecutionService"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/iframe/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC","sourcesContent":["export * from './IframeExecutionService';\n"]}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745
|
|
4
|
+
const fixJSDOMPostMessageEventSource = (iframeExecutionService) => {
|
|
5
|
+
const oldCreateWindow = iframeExecutionService.createWindow;
|
|
6
|
+
iframeExecutionService.createWindow = async (uri, envId, timeout) => {
|
|
7
|
+
const result = await oldCreateWindow(uri, envId, timeout);
|
|
8
|
+
const scriptElement = result.document.createElement('script');
|
|
9
|
+
if (!scriptElement) {
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
// fix the inside window
|
|
13
|
+
scriptElement.textContent = `
|
|
14
|
+
window.addEventListener('message', (postMessageEvent) => {
|
|
15
|
+
if (postMessageEvent.source === null && !postMessageEvent.origin) {
|
|
16
|
+
let source;
|
|
17
|
+
let postMessageEventOrigin;
|
|
18
|
+
if (postMessageEvent.data.target === 'child') {
|
|
19
|
+
source = window.parent;
|
|
20
|
+
postMessageEventOrigin = '*';
|
|
21
|
+
} else if (postMessageEvent.data.target === 'parent') {
|
|
22
|
+
source = window;
|
|
23
|
+
postMessageEventOrigin = window.location.origin;
|
|
24
|
+
}
|
|
25
|
+
if (postMessageEvent.data.target) {
|
|
26
|
+
postMessageEvent.stopImmediatePropagation();
|
|
27
|
+
const args = Object.assign({
|
|
28
|
+
...postMessageEvent,
|
|
29
|
+
data: postMessageEvent.data,
|
|
30
|
+
source,
|
|
31
|
+
origin: postMessageEventOrigin,
|
|
32
|
+
});
|
|
33
|
+
const postMessageEventWithOrigin = new MessageEvent(
|
|
34
|
+
'message',
|
|
35
|
+
args,
|
|
36
|
+
);
|
|
37
|
+
window.dispatchEvent(postMessageEventWithOrigin);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
`;
|
|
42
|
+
result.document.body.appendChild(scriptElement);
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
const listener = (event) => {
|
|
46
|
+
if (event.source === null && !event.origin) {
|
|
47
|
+
let source;
|
|
48
|
+
let origin;
|
|
49
|
+
if (event.data.target === 'child') {
|
|
50
|
+
source = window;
|
|
51
|
+
origin = window.location.origin;
|
|
52
|
+
}
|
|
53
|
+
else if (event.data.target === 'parent') {
|
|
54
|
+
// @ts-expect-error Accessing private property
|
|
55
|
+
const { worker } = iframeExecutionService.jobs.values().next().value;
|
|
56
|
+
source = worker;
|
|
57
|
+
origin = iframeExecutionService.iframeUrl.toString();
|
|
58
|
+
}
|
|
59
|
+
if (event.data.target) {
|
|
60
|
+
event.stopImmediatePropagation();
|
|
61
|
+
const args = Object.assign(Object.assign(Object.assign({}, event), { data: event.data, source,
|
|
62
|
+
origin }));
|
|
63
|
+
const eventWithOrigin = new MessageEvent('message', args);
|
|
64
|
+
window.dispatchEvent(eventWithOrigin);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
window.addEventListener('message', listener);
|
|
69
|
+
return () => {
|
|
70
|
+
window.removeEventListener('message', listener);
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
exports.default = fixJSDOMPostMessageEventSource;
|
|
74
|
+
//# sourceMappingURL=fixJSDOMPostMessageEventSource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixJSDOMPostMessageEventSource.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/fixJSDOMPostMessageEventSource.ts"],"names":[],"mappings":";;AAEA,yGAAyG;AACzG,MAAM,8BAA8B,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,eAAe,GAAI,sBAA8B,CAAC,YAAY,CAAC;IACpE,sBAA8B,CAAC,YAAY,GAAG,KAAK,EAClD,GAAW,EACX,KAAa,EACb,OAAe,EACf,EAAE;QACF,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;QAE1D,MAAM,aAAa,GAAG,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAE9D,IAAI,CAAC,aAAa,EAAE;YAClB,OAAO,MAAM,CAAC;SACf;QAED,wBAAwB;QACxB,aAAa,CAAC,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4B7B,CAAC;QACA,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;QAEhD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,CAAC,KAAmB,EAAE,EAAE;QACvC,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;YAC1C,IAAI,MAAM,CAAC;YACX,IAAI,MAAM,CAAC;YACX,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;gBACjC,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;aACjC;iBAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;gBACzC,8CAA8C;gBAC9C,MAAM,EAAE,MAAM,EAAE,GAAG,sBAAsB,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;gBACrE,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;aACtD;YAED,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;gBACrB,KAAK,CAAC,wBAAwB,EAAE,CAAC;gBACjC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,iCACrB,KAAK,KACR,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM;oBACN,MAAM,IACN,CAAC;gBACH,MAAM,eAAe,GAAiB,IAAI,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACxE,MAAM,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;aACvC;SACF;IACH,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAE7C,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,8BAA8B,CAAC","sourcesContent":["import { IframeExecutionService } from '../IframeExecutionService';\n\n// Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745\nconst fixJSDOMPostMessageEventSource = (\n iframeExecutionService: IframeExecutionService,\n) => {\n const oldCreateWindow = (iframeExecutionService as any).createWindow;\n (iframeExecutionService as any).createWindow = async (\n uri: string,\n envId: string,\n timeout: number,\n ) => {\n const result = await oldCreateWindow(uri, envId, timeout);\n\n const scriptElement = result.document.createElement('script');\n\n if (!scriptElement) {\n return result;\n }\n\n // fix the inside window\n scriptElement.textContent = `\n window.addEventListener('message', (postMessageEvent) => {\n if (postMessageEvent.source === null && !postMessageEvent.origin) {\n let source;\n let postMessageEventOrigin;\n if (postMessageEvent.data.target === 'child') {\n source = window.parent;\n postMessageEventOrigin = '*';\n } else if (postMessageEvent.data.target === 'parent') {\n source = window;\n postMessageEventOrigin = window.location.origin;\n }\n if (postMessageEvent.data.target) {\n postMessageEvent.stopImmediatePropagation();\n const args = Object.assign({\n ...postMessageEvent,\n data: postMessageEvent.data,\n source,\n origin: postMessageEventOrigin,\n });\n const postMessageEventWithOrigin = new MessageEvent(\n 'message',\n args,\n );\n window.dispatchEvent(postMessageEventWithOrigin);\n }\n }\n });\n `;\n result.document.body.appendChild(scriptElement);\n\n return result;\n };\n\n const listener = (event: MessageEvent) => {\n if (event.source === null && !event.origin) {\n let source;\n let origin;\n if (event.data.target === 'child') {\n source = window;\n origin = window.location.origin;\n } else if (event.data.target === 'parent') {\n // @ts-expect-error Accessing private property\n const { worker } = iframeExecutionService.jobs.values().next().value;\n source = worker;\n origin = iframeExecutionService.iframeUrl.toString();\n }\n\n if (event.data.target) {\n event.stopImmediatePropagation();\n const args = Object.assign({\n ...event,\n data: event.data,\n source,\n origin,\n });\n const eventWithOrigin: MessageEvent = new MessageEvent('message', args);\n window.dispatchEvent(eventWithOrigin);\n }\n }\n };\n\n window.addEventListener('message', listener);\n\n return () => {\n window.removeEventListener('message', listener);\n };\n};\n\nexport default fixJSDOMPostMessageEventSource;\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const PORT = 6364;
|
|
2
|
+
/**
|
|
3
|
+
* Starts a local server that serves the iframe execution environment.
|
|
4
|
+
*
|
|
5
|
+
* @param port - The port to start the server on.
|
|
6
|
+
*/
|
|
7
|
+
export declare function start(port?: number): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Stops the local server.
|
|
10
|
+
*/
|
|
11
|
+
export declare function stop(): Promise<void>;
|
|
@@ -0,0 +1,71 @@
|
|
|
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.stop = exports.start = exports.PORT = void 0;
|
|
7
|
+
const http_1 = __importDefault(require("http"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const serve_handler_1 = __importDefault(require("serve-handler"));
|
|
10
|
+
exports.PORT = 6364;
|
|
11
|
+
let server;
|
|
12
|
+
/**
|
|
13
|
+
* Starts a local server that serves the iframe execution environment.
|
|
14
|
+
*
|
|
15
|
+
* @param port - The port to start the server on.
|
|
16
|
+
*/
|
|
17
|
+
async function start(port = exports.PORT) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
if (!Number.isSafeInteger(port) || port < 0) {
|
|
20
|
+
reject(new Error(`Invalid port: "${port}"`));
|
|
21
|
+
}
|
|
22
|
+
const bundlePath = require.resolve('@metamask/snaps-execution-environments/__test__/iframe-test/bundle.js');
|
|
23
|
+
const publicPath = path_1.default.resolve(bundlePath, '../');
|
|
24
|
+
server = http_1.default.createServer(async (req, res) => {
|
|
25
|
+
await (0, serve_handler_1.default)(req, res, {
|
|
26
|
+
public: publicPath,
|
|
27
|
+
headers: [
|
|
28
|
+
{
|
|
29
|
+
source: '**/*',
|
|
30
|
+
headers: [
|
|
31
|
+
{
|
|
32
|
+
key: 'Cache-Control',
|
|
33
|
+
value: 'no-cache',
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
server.listen({ port }, () => {
|
|
41
|
+
console.log(`Server listening on: http://localhost:${port}`);
|
|
42
|
+
resolve();
|
|
43
|
+
});
|
|
44
|
+
server.on('error', (error) => {
|
|
45
|
+
console.error('Server error', error);
|
|
46
|
+
reject(error);
|
|
47
|
+
});
|
|
48
|
+
server.on('close', () => {
|
|
49
|
+
console.log('Server closed');
|
|
50
|
+
reject(new Error('Server closed'));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
exports.start = start;
|
|
55
|
+
/**
|
|
56
|
+
* Stops the local server.
|
|
57
|
+
*/
|
|
58
|
+
async function stop() {
|
|
59
|
+
await new Promise((resolve, reject) => {
|
|
60
|
+
server.close((err) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
reject(err);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
resolve();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
exports.stop = stop;
|
|
71
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/server.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AACxB,gDAAwB;AACxB,kEAAyC;AAE5B,QAAA,IAAI,GAAG,IAAI,CAAC;AAEzB,IAAI,MAAmB,CAAC;AACxB;;;;GAIG;AACI,KAAK,UAAU,KAAK,CAAC,IAAI,GAAG,YAAI;IACrC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE;YAC3C,MAAM,CAAC,IAAI,KAAK,CAAC,kBAAkB,IAAI,GAAG,CAAC,CAAC,CAAC;SAC9C;QAED,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAChC,uEAAuE,CACxE,CAAC;QACF,MAAM,UAAU,GAAG,cAAI,CAAC,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAEnD,MAAM,GAAG,cAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;YAC5C,MAAM,IAAA,uBAAY,EAAC,GAAG,EAAE,GAAG,EAAE;gBAC3B,MAAM,EAAE,UAAU;gBAClB,OAAO,EAAE;oBACP;wBACE,MAAM,EAAE,MAAM;wBACd,OAAO,EAAE;4BACP;gCACE,GAAG,EAAE,eAAe;gCACpB,KAAK,EAAE,UAAU;6BAClB;yBACF;qBACF;iBACF;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE;YAC3B,OAAO,CAAC,GAAG,CAAC,yCAAyC,IAAI,EAAE,CAAC,CAAC;YAC7D,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YAC3B,OAAO,CAAC,KAAK,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;YACrC,MAAM,CAAC,KAAK,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;YACtB,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;YAC7B,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;QACrC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AA3CD,sBA2CC;AAED;;GAEG;AACI,KAAK,UAAU,IAAI;IACxB,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACnB,IAAI,GAAG,EAAE;gBACP,MAAM,CAAC,GAAG,CAAC,CAAC;aACb;iBAAM;gBACL,OAAO,EAAE,CAAC;aACX;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAVD,oBAUC","sourcesContent":["import http from 'http';\nimport path from 'path';\nimport serveHandler from 'serve-handler';\n\nexport const PORT = 6364;\n\nlet server: http.Server;\n/**\n * Starts a local server that serves the iframe execution environment.\n *\n * @param port - The port to start the server on.\n */\nexport async function start(port = PORT) {\n return new Promise<void>((resolve, reject) => {\n if (!Number.isSafeInteger(port) || port < 0) {\n reject(new Error(`Invalid port: \"${port}\"`));\n }\n\n const bundlePath = require.resolve(\n '@metamask/snaps-execution-environments/__test__/iframe-test/bundle.js',\n );\n const publicPath = path.resolve(bundlePath, '../');\n\n server = http.createServer(async (req, res) => {\n await serveHandler(req, res, {\n public: publicPath,\n headers: [\n {\n source: '**/*',\n headers: [\n {\n key: 'Cache-Control',\n value: 'no-cache',\n },\n ],\n },\n ],\n });\n });\n\n server.listen({ port }, () => {\n console.log(`Server listening on: http://localhost:${port}`);\n resolve();\n });\n\n server.on('error', (error) => {\n console.error('Server error', error);\n reject(error);\n });\n\n server.on('close', () => {\n console.log('Server closed');\n reject(new Error('Server closed'));\n });\n });\n}\n\n/**\n * Stops the local server.\n */\nexport async function stop() {\n await new Promise<void>((resolve, reject) => {\n server.close((err) => {\n if (err) {\n reject(err);\n } else {\n resolve();\n }\n });\n });\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./AbstractExecutionService"), exports);
|
|
18
|
+
__exportStar(require("./ExecutionService"), exports);
|
|
19
|
+
__exportStar(require("./iframe"), exports);
|
|
20
|
+
__exportStar(require("./node"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB;AACzB,yCAAuB","sourcesContent":["export * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\nexport * from './node';\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ChildProcess } from 'child_process';
|
|
3
|
+
import { BasePostMessageStream } from '@metamask/post-message-stream';
|
|
4
|
+
import { AbstractExecutionService, Job } from '..';
|
|
5
|
+
export declare class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {
|
|
6
|
+
protected initEnvStream(): Promise<{
|
|
7
|
+
worker: ChildProcess;
|
|
8
|
+
stream: BasePostMessageStream;
|
|
9
|
+
}>;
|
|
10
|
+
protected terminateJob(jobWrapper: Job<ChildProcess>): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NodeProcessExecutionService = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
6
|
+
const __1 = require("..");
|
|
7
|
+
class NodeProcessExecutionService extends __1.AbstractExecutionService {
|
|
8
|
+
async initEnvStream() {
|
|
9
|
+
const worker = (0, child_process_1.fork)(require.resolve('@metamask/snaps-execution-environments/dist/webpack/node-process/bundle.js'));
|
|
10
|
+
const stream = new post_message_stream_1.ProcessParentMessageStream({ process: worker });
|
|
11
|
+
return { worker, stream };
|
|
12
|
+
}
|
|
13
|
+
terminateJob(jobWrapper) {
|
|
14
|
+
jobWrapper.worker.kill();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.NodeProcessExecutionService = NodeProcessExecutionService;
|
|
18
|
+
//# sourceMappingURL=NodeProcessExecutionService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeProcessExecutionService.js","sourceRoot":"","sources":["../../../src/services/node/NodeProcessExecutionService.ts"],"names":[],"mappings":";;;AAAA,iDAAmD;AACnD,uEAGuC;AACvC,0BAAmD;AAEnD,MAAa,2BAA4B,SAAQ,4BAAsC;IAC3E,KAAK,CAAC,aAAa;QAI3B,MAAM,MAAM,GAAG,IAAA,oBAAI,EACjB,OAAO,CAAC,OAAO,CACb,4EAA4E,CAC7E,CACF,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,gDAA0B,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QACnE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC5B,CAAC;IAES,YAAY,CAAC,UAA6B;QAClD,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;CACF;AAjBD,kEAiBC","sourcesContent":["import { ChildProcess, fork } from 'child_process';\nimport {\n ProcessParentMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\nimport { AbstractExecutionService, Job } from '..';\n\nexport class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {\n protected async initEnvStream(): Promise<{\n worker: ChildProcess;\n stream: BasePostMessageStream;\n }> {\n const worker = fork(\n require.resolve(\n '@metamask/snaps-execution-environments/dist/webpack/node-process/bundle.js',\n ),\n );\n const stream = new ProcessParentMessageStream({ process: worker });\n return { worker, stream };\n }\n\n protected terminateJob(jobWrapper: Job<ChildProcess>): void {\n jobWrapper.worker.kill();\n }\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Worker } from 'worker_threads';
|
|
3
|
+
import { BasePostMessageStream } from '@metamask/post-message-stream';
|
|
4
|
+
import { AbstractExecutionService, Job } from '..';
|
|
5
|
+
export declare class NodeThreadExecutionService extends AbstractExecutionService<Worker> {
|
|
6
|
+
protected initEnvStream(): Promise<{
|
|
7
|
+
worker: Worker;
|
|
8
|
+
stream: BasePostMessageStream;
|
|
9
|
+
}>;
|
|
10
|
+
protected terminateJob(jobWrapper: Job<Worker>): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NodeThreadExecutionService = void 0;
|
|
4
|
+
const worker_threads_1 = require("worker_threads");
|
|
5
|
+
const post_message_stream_1 = require("@metamask/post-message-stream");
|
|
6
|
+
const __1 = require("..");
|
|
7
|
+
class NodeThreadExecutionService extends __1.AbstractExecutionService {
|
|
8
|
+
async initEnvStream() {
|
|
9
|
+
const worker = new worker_threads_1.Worker(require.resolve('@metamask/snaps-execution-environments/dist/webpack/node-thread/bundle.js'));
|
|
10
|
+
const stream = new post_message_stream_1.ThreadParentMessageStream({ thread: worker });
|
|
11
|
+
return { worker, stream };
|
|
12
|
+
}
|
|
13
|
+
terminateJob(jobWrapper) {
|
|
14
|
+
jobWrapper.worker.terminate();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.NodeThreadExecutionService = NodeThreadExecutionService;
|
|
18
|
+
//# sourceMappingURL=NodeThreadExecutionService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeThreadExecutionService.js","sourceRoot":"","sources":["../../../src/services/node/NodeThreadExecutionService.ts"],"names":[],"mappings":";;;AAAA,mDAAwC;AACxC,uEAGuC;AACvC,0BAAmD;AAEnD,MAAa,0BAA2B,SAAQ,4BAAgC;IACpE,KAAK,CAAC,aAAa;QAI3B,MAAM,MAAM,GAAG,IAAI,uBAAM,CACvB,OAAO,CAAC,OAAO,CACb,2EAA2E,CAC5E,CACF,CAAC;QACF,MAAM,MAAM,GAAG,IAAI,+CAAyB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACjE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC5B,CAAC;IAES,YAAY,CAAC,UAAuB;QAC5C,UAAU,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IAChC,CAAC;CACF;AAjBD,gEAiBC","sourcesContent":["import { Worker } from 'worker_threads';\nimport {\n ThreadParentMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\nimport { AbstractExecutionService, Job } from '..';\n\nexport class NodeThreadExecutionService extends AbstractExecutionService<Worker> {\n protected async initEnvStream(): Promise<{\n worker: Worker;\n stream: BasePostMessageStream;\n }> {\n const worker = new Worker(\n require.resolve(\n '@metamask/snaps-execution-environments/dist/webpack/node-thread/bundle.js',\n ),\n );\n const stream = new ThreadParentMessageStream({ thread: worker });\n return { worker, stream };\n }\n\n protected terminateJob(jobWrapper: Job<Worker>): void {\n jobWrapper.worker.terminate();\n }\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./NodeProcessExecutionService"), exports);
|
|
18
|
+
__exportStar(require("./NodeThreadExecutionService"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/node/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA8C;AAC9C,+DAA6C","sourcesContent":["export * from './NodeProcessExecutionService';\nexport * from './NodeThreadExecutionService';\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class RequestQueue {
|
|
2
|
+
readonly maxQueueSize: number;
|
|
3
|
+
private readonly queueSizes;
|
|
4
|
+
constructor(maxQueueSize: number);
|
|
5
|
+
/**
|
|
6
|
+
* Increments the queue count for a particular origin.
|
|
7
|
+
*
|
|
8
|
+
* @param origin - A string identifying the origin.
|
|
9
|
+
*/
|
|
10
|
+
increment(origin: string): void;
|
|
11
|
+
/**
|
|
12
|
+
* Decrements the queue count for a particular origin.
|
|
13
|
+
*
|
|
14
|
+
* @param origin - A string identifying the origin.
|
|
15
|
+
*/
|
|
16
|
+
decrement(origin: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* Gets the queue count for a particular origin.
|
|
19
|
+
*
|
|
20
|
+
* @param origin - A string identifying the origin.
|
|
21
|
+
* @returns The queue count for the origin.
|
|
22
|
+
*/
|
|
23
|
+
get(origin: string): number;
|
|
24
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestQueue = void 0;
|
|
4
|
+
class RequestQueue {
|
|
5
|
+
constructor(maxQueueSize) {
|
|
6
|
+
this.maxQueueSize = maxQueueSize;
|
|
7
|
+
this.queueSizes = new Map();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Increments the queue count for a particular origin.
|
|
11
|
+
*
|
|
12
|
+
* @param origin - A string identifying the origin.
|
|
13
|
+
*/
|
|
14
|
+
increment(origin) {
|
|
15
|
+
var _a;
|
|
16
|
+
const currentCount = (_a = this.queueSizes.get(origin)) !== null && _a !== void 0 ? _a : 0;
|
|
17
|
+
if (currentCount >= this.maxQueueSize) {
|
|
18
|
+
throw new Error('Maximum number of requests reached. Try again later.');
|
|
19
|
+
}
|
|
20
|
+
this.queueSizes.set(origin, currentCount + 1);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Decrements the queue count for a particular origin.
|
|
24
|
+
*
|
|
25
|
+
* @param origin - A string identifying the origin.
|
|
26
|
+
*/
|
|
27
|
+
decrement(origin) {
|
|
28
|
+
var _a;
|
|
29
|
+
const currentCount = (_a = this.queueSizes.get(origin)) !== null && _a !== void 0 ? _a : 0;
|
|
30
|
+
if (currentCount === 0) {
|
|
31
|
+
throw new Error(`Cannot decrement, ${origin} does not have any outstanding requests.`);
|
|
32
|
+
}
|
|
33
|
+
this.queueSizes.set(origin, currentCount - 1);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Gets the queue count for a particular origin.
|
|
37
|
+
*
|
|
38
|
+
* @param origin - A string identifying the origin.
|
|
39
|
+
* @returns The queue count for the origin.
|
|
40
|
+
*/
|
|
41
|
+
get(origin) {
|
|
42
|
+
var _a;
|
|
43
|
+
return (_a = this.queueSizes.get(origin)) !== null && _a !== void 0 ? _a : 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.RequestQueue = RequestQueue;
|
|
47
|
+
//# sourceMappingURL=RequestQueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestQueue.js","sourceRoot":"","sources":["../../src/snaps/RequestQueue.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IAKvB,YAAY,YAAoB;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;;QAC7B,MAAM,YAAY,GAAG,MAAA,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,mCAAI,CAAC,CAAC;QACtD,IAAI,YAAY,IAAI,IAAI,CAAC,YAAY,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;SACzE;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACI,SAAS,CAAC,MAAc;;QAC7B,MAAM,YAAY,GAAG,MAAA,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,mCAAI,CAAC,CAAC;QACtD,IAAI,YAAY,KAAK,CAAC,EAAE;YACtB,MAAM,IAAI,KAAK,CACb,qBAAqB,MAAM,0CAA0C,CACtE,CAAC;SACH;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,MAAc;;QACvB,OAAO,MAAA,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,mCAAI,CAAC,CAAC;IAC1C,CAAC;CACF;AA/CD,oCA+CC","sourcesContent":["export class RequestQueue {\n public readonly maxQueueSize: number;\n\n private readonly queueSizes: Map<string, number>;\n\n constructor(maxQueueSize: number) {\n this.maxQueueSize = maxQueueSize;\n this.queueSizes = new Map<string, number>();\n }\n\n /**\n * Increments the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public increment(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount >= this.maxQueueSize) {\n throw new Error('Maximum number of requests reached. Try again later.');\n }\n this.queueSizes.set(origin, currentCount + 1);\n }\n\n /**\n * Decrements the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n */\n public decrement(origin: string) {\n const currentCount = this.queueSizes.get(origin) ?? 0;\n if (currentCount === 0) {\n throw new Error(\n `Cannot decrement, ${origin} does not have any outstanding requests.`,\n );\n }\n this.queueSizes.set(origin, currentCount - 1);\n }\n\n /**\n * Gets the queue count for a particular origin.\n *\n * @param origin - A string identifying the origin.\n * @returns The queue count for the origin.\n */\n public get(origin: string): number {\n return this.queueSizes.get(origin) ?? 0;\n }\n}\n"]}
|