@metamask/snaps-controllers 14.0.0 → 14.0.2
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 +21 -4
- package/dist/cronjob/CronjobController.cjs +8 -0
- package/dist/cronjob/CronjobController.cjs.map +1 -1
- package/dist/cronjob/CronjobController.d.cts.map +1 -1
- package/dist/cronjob/CronjobController.d.mts.map +1 -1
- package/dist/cronjob/CronjobController.mjs +8 -0
- package/dist/cronjob/CronjobController.mjs.map +1 -1
- package/dist/services/AbstractExecutionService.cjs +28 -3
- package/dist/services/AbstractExecutionService.cjs.map +1 -1
- package/dist/services/AbstractExecutionService.d.cts +16 -0
- package/dist/services/AbstractExecutionService.d.cts.map +1 -1
- package/dist/services/AbstractExecutionService.d.mts +16 -0
- package/dist/services/AbstractExecutionService.d.mts.map +1 -1
- package/dist/services/AbstractExecutionService.mjs +28 -3
- package/dist/services/AbstractExecutionService.mjs.map +1 -1
- package/dist/services/iframe/IframeExecutionService.cjs +1 -0
- 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 +1 -0
- package/dist/services/iframe/IframeExecutionService.mjs.map +1 -1
- package/dist/services/node-js/NodeProcessExecutionService.cjs +2 -1
- package/dist/services/node-js/NodeProcessExecutionService.cjs.map +1 -1
- package/dist/services/node-js/NodeProcessExecutionService.d.cts +1 -1
- package/dist/services/node-js/NodeProcessExecutionService.d.cts.map +1 -1
- package/dist/services/node-js/NodeProcessExecutionService.d.mts +1 -1
- package/dist/services/node-js/NodeProcessExecutionService.d.mts.map +1 -1
- package/dist/services/node-js/NodeProcessExecutionService.mjs +2 -1
- package/dist/services/node-js/NodeProcessExecutionService.mjs.map +1 -1
- package/dist/services/node-js/NodeThreadExecutionService.cjs +2 -1
- package/dist/services/node-js/NodeThreadExecutionService.cjs.map +1 -1
- package/dist/services/node-js/NodeThreadExecutionService.d.cts +1 -1
- package/dist/services/node-js/NodeThreadExecutionService.d.cts.map +1 -1
- package/dist/services/node-js/NodeThreadExecutionService.d.mts +1 -1
- package/dist/services/node-js/NodeThreadExecutionService.d.mts.map +1 -1
- package/dist/services/node-js/NodeThreadExecutionService.mjs +2 -1
- package/dist/services/node-js/NodeThreadExecutionService.mjs.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.cjs +1 -0
- package/dist/services/proxy/ProxyExecutionService.cjs.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.d.cts.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.d.mts.map +1 -1
- package/dist/services/proxy/ProxyExecutionService.mjs +1 -0
- package/dist/services/proxy/ProxyExecutionService.mjs.map +1 -1
- package/dist/services/webview/WebViewExecutionService.cjs +1 -0
- package/dist/services/webview/WebViewExecutionService.cjs.map +1 -1
- package/dist/services/webview/WebViewExecutionService.d.cts.map +1 -1
- package/dist/services/webview/WebViewExecutionService.d.mts.map +1 -1
- package/dist/services/webview/WebViewExecutionService.mjs +1 -0
- package/dist/services/webview/WebViewExecutionService.mjs.map +1 -1
- package/dist/snaps/SnapController.cjs +4 -1
- package/dist/snaps/SnapController.cjs.map +1 -1
- package/dist/snaps/SnapController.d.cts.map +1 -1
- package/dist/snaps/SnapController.d.mts.map +1 -1
- package/dist/snaps/SnapController.mjs +4 -1
- package/dist/snaps/SnapController.mjs.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractExecutionService.d.cts","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAE1D,OAAO,eAAe,mCAAmC;AACzD,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAgB7D,OAAO,KAAK,EA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AW,gBAAgB;;IAE3B,IAAI,EAAE,OAAO,cAAc,CAAkB;IAE7C,KAAK,OAAQ;gBAkBD,EACV,iBAAiB,EACjB,SAAS,EACT,WAAiD,EACjD,WAAgD,EAChD,kBAAuD,EACvD,OAAc,GACf,EAAE,oBAAoB;IAwCvB;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,CAAC,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,UAAU,CAAC,GAAG,IAAI;IAExE;;;;;;;OAOG;IACU,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoKzD;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACxD,MAAM,EAAE,UAAU,CAAC;QACnB,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;IAEF;;;;;OAKG;IACH,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe;IAIzD,iBAAiB;IAMvB;;;;;;;;;OASG;IACG,WAAW,CAAC,EAChB,MAAM,EACN,UAAU,EACV,UAAU,GACX,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;IA2FtC;;;;;;OAMG;IACU,gBAAgB,CAC3B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,OAAO,CAAC;CAepB;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,gBAAgB,EAAE,MAAM,EACxB,UAAU,EAAE,MAAM,GACjB,eAAe,CAQjB"}
|
|
@@ -26,6 +26,15 @@ export type Job<WorkerType> = {
|
|
|
26
26
|
worker: WorkerType;
|
|
27
27
|
};
|
|
28
28
|
export type TerminateJobArgs<WorkerType> = Partial<Job<WorkerType>> & Pick<Job<WorkerType>, 'id'>;
|
|
29
|
+
/**
|
|
30
|
+
Statuses used for diagnostic purposes
|
|
31
|
+
- created: The initial state, no initialization has started
|
|
32
|
+
- initializing: Snap execution environment is initializing
|
|
33
|
+
- initialized: Snap execution environment has initialized
|
|
34
|
+
- executing: Snap source code is being executed
|
|
35
|
+
- running: Snap executed and ready for RPC requests
|
|
36
|
+
*/
|
|
37
|
+
type ExecutionStatus = 'created' | 'initializing' | 'initialized' | 'executing' | 'running';
|
|
29
38
|
export declare abstract class AbstractExecutionService<WorkerType> implements ExecutionService {
|
|
30
39
|
#private;
|
|
31
40
|
name: typeof controllerName;
|
|
@@ -57,6 +66,13 @@ export declare abstract class AbstractExecutionService<WorkerType> implements Ex
|
|
|
57
66
|
worker: WorkerType;
|
|
58
67
|
stream: BasePostMessageStream;
|
|
59
68
|
}>;
|
|
69
|
+
/**
|
|
70
|
+
* Set the execution status of the Snap.
|
|
71
|
+
*
|
|
72
|
+
* @param snapId - The Snap ID.
|
|
73
|
+
* @param status - The current execution status.
|
|
74
|
+
*/
|
|
75
|
+
protected setSnapStatus(snapId: string, status: ExecutionStatus): void;
|
|
60
76
|
terminateAllSnaps(): Promise<void>;
|
|
61
77
|
/**
|
|
62
78
|
* Initializes and executes a Snap, setting up the communication channels to the Snap etc.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractExecutionService.d.mts","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAE1D,OAAO,eAAe,mCAAmC;AACzD,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAgB7D,OAAO,KAAK,EAAE,MAAM,EAAE,wBAAwB;AAE9C,OAAO,KAAK,EACV,gBAAgB,EAChB,yBAAyB,EAEzB,iBAAiB,EAClB,+BAA2B;AAK5B,QAAA,MAAM,cAAc,qBAAqB,CAAC;AAE1C,MAAM,MAAM,iBAAiB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KA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C,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,UAAU,CAAC,GAAG,IAAI;IAExE;;;;;;;OAOG;IACU,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoKzD;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACxD,MAAM,EAAE,UAAU,CAAC;QACnB,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;IAEF;;;;;OAKG;IACH,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe;IAIzD,iBAAiB;IAMvB;;;;;;;;;OASG;IACG,WAAW,CAAC,EAChB,MAAM,EACN,UAAU,EACV,UAAU,GACX,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;IA2FtC;;;;;;OAMG;IACU,gBAAgB,CAC3B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,OAAO,CAAC;CAepB;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,gBAAgB,EAAE,MAAM,EACxB,UAAU,EAAE,MAAM,GACjB,eAAe,CAQjB"}
|
|
@@ -21,6 +21,7 @@ export class AbstractExecutionService {
|
|
|
21
21
|
name = controllerName;
|
|
22
22
|
state = null;
|
|
23
23
|
#jobs;
|
|
24
|
+
#status;
|
|
24
25
|
#setupSnapProvider;
|
|
25
26
|
#messenger;
|
|
26
27
|
#initTimeout;
|
|
@@ -29,6 +30,7 @@ export class AbstractExecutionService {
|
|
|
29
30
|
#usePing;
|
|
30
31
|
constructor({ setupSnapProvider, messenger, initTimeout = inMilliseconds(60, Duration.Second), pingTimeout = inMilliseconds(2, Duration.Second), terminationTimeout = inMilliseconds(1, Duration.Second), usePing = true, }) {
|
|
31
32
|
this.#jobs = new Map();
|
|
33
|
+
this.#status = new Map();
|
|
32
34
|
this.#setupSnapProvider = setupSnapProvider;
|
|
33
35
|
this.#messenger = messenger;
|
|
34
36
|
this.#initTimeout = initTimeout;
|
|
@@ -91,6 +93,7 @@ export class AbstractExecutionService {
|
|
|
91
93
|
});
|
|
92
94
|
this.terminateJob(job);
|
|
93
95
|
this.#jobs.delete(snapId);
|
|
96
|
+
this.#status.delete(snapId);
|
|
94
97
|
log(`Snap "${snapId}" terminated.`);
|
|
95
98
|
}
|
|
96
99
|
/**
|
|
@@ -133,7 +136,12 @@ export class AbstractExecutionService {
|
|
|
133
136
|
if (result === hasTimedOut) {
|
|
134
137
|
// For certain environments, such as the iframe we may have already created the worker and wish to terminate it.
|
|
135
138
|
this.terminateJob({ id: snapId });
|
|
136
|
-
|
|
139
|
+
const status = this.#status.get(snapId);
|
|
140
|
+
if (status === 'created') {
|
|
141
|
+
// Currently this error can only be thrown by OffscreenExecutionService.
|
|
142
|
+
throw new Error(`The executor for "${snapId}" couldn't start initialization. The offscreen document may not exist.`);
|
|
143
|
+
}
|
|
144
|
+
throw new Error(`The executor for "${snapId}" failed to initialize. The iframe/webview/worker failed to load.`);
|
|
137
145
|
}
|
|
138
146
|
const { worker, stream: envStream } = result;
|
|
139
147
|
const mux = setupMultiplex(envStream, `Snap: "${snapId}"`);
|
|
@@ -171,6 +179,15 @@ export class AbstractExecutionService {
|
|
|
171
179
|
worker,
|
|
172
180
|
};
|
|
173
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Set the execution status of the Snap.
|
|
184
|
+
*
|
|
185
|
+
* @param snapId - The Snap ID.
|
|
186
|
+
* @param status - The current execution status.
|
|
187
|
+
*/
|
|
188
|
+
setSnapStatus(snapId, status) {
|
|
189
|
+
this.#status.set(snapId, status);
|
|
190
|
+
}
|
|
174
191
|
async terminateAllSnaps() {
|
|
175
192
|
await Promise.all([...this.#jobs.keys()].map(async (snapId) => this.terminateSnap(snapId)));
|
|
176
193
|
}
|
|
@@ -188,6 +205,7 @@ export class AbstractExecutionService {
|
|
|
188
205
|
if (this.#jobs.has(snapId)) {
|
|
189
206
|
throw new Error(`"${snapId}" is already running.`);
|
|
190
207
|
}
|
|
208
|
+
this.setSnapStatus(snapId, 'created');
|
|
191
209
|
const timer = new Timer(this.#initTimeout);
|
|
192
210
|
// This may resolve even if the environment has failed to start up fully
|
|
193
211
|
const job = await this.#initJob(snapId, timer);
|
|
@@ -200,12 +218,15 @@ export class AbstractExecutionService {
|
|
|
200
218
|
id: nanoid(),
|
|
201
219
|
}), this.#pingTimeout);
|
|
202
220
|
if (pingResult === hasTimedOut) {
|
|
203
|
-
throw new Error(
|
|
221
|
+
throw new Error(`The executor for "${snapId}" was unreachable. The executor did not respond in time.`);
|
|
204
222
|
}
|
|
205
223
|
}
|
|
206
224
|
const rpcStream = job.streams.rpc;
|
|
207
225
|
this.#setupSnapProvider(snapId, rpcStream);
|
|
208
|
-
|
|
226
|
+
// Use the remaining time as the timer, but ensure that the
|
|
227
|
+
// Snap gets at least half the init timeout.
|
|
228
|
+
const remainingTime = Math.max(timer.remaining, this.#initTimeout / 2);
|
|
229
|
+
this.setSnapStatus(snapId, 'initialized');
|
|
209
230
|
const request = {
|
|
210
231
|
jsonrpc: '2.0',
|
|
211
232
|
method: 'executeSnap',
|
|
@@ -213,10 +234,14 @@ export class AbstractExecutionService {
|
|
|
213
234
|
id: nanoid(),
|
|
214
235
|
};
|
|
215
236
|
assertIsJsonRpcRequest(request);
|
|
237
|
+
this.setSnapStatus(snapId, 'executing');
|
|
216
238
|
const result = await withTimeout(this.#command(job.id, request), remainingTime);
|
|
217
239
|
if (result === hasTimedOut) {
|
|
218
240
|
throw new Error(`${snapId} failed to start.`);
|
|
219
241
|
}
|
|
242
|
+
if (result === 'OK') {
|
|
243
|
+
this.setSnapStatus(snapId, 'running');
|
|
244
|
+
}
|
|
220
245
|
return result;
|
|
221
246
|
}
|
|
222
247
|
async #command(snapId, message) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractExecutionService.mjs","sourceRoot":"","sources":["../../src/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,OAAyB;gBAClC,MAAM,EAAE,MAAM;aACf;SACF,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,eAAe,EAAE,CAAC;IAClC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE,gBAAgB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC1D,IAAI,KAAK,EAAE,CAAC;YACV,QAAQ,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC,CAAC;QACrD,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { createStreamMiddleware } from '@metamask/json-rpc-middleware-stream';\nimport ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { SNAP_STREAM_NAMES, logError } from '@metamask/snaps-utils';\nimport type {\n Json,\n JsonRpcNotification,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport {\n Duration,\n assertIsJsonRpcRequest,\n hasProperty,\n inMilliseconds,\n isJsonRpcFailure,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\nimport { pipeline } from 'readable-stream';\nimport type { Duplex } from 'readable-stream';\n\nimport type {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\nimport { log } from '../logging';\nimport { Timer } from '../snaps/Timer';\nimport { hasTimedOut, withTimeout } from '../utils';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n initTimeout?: number;\n pingTimeout?: number;\n terminationTimeout?: number;\n usePing?: boolean;\n};\n\nexport type JobStreams = {\n command: Duplex;\n rpc: Duplex;\n _connection: BasePostMessageStream;\n};\n\nexport type Job<WorkerType> = {\n id: string;\n streams: JobStreams;\n rpcEngine: JsonRpcEngine;\n worker: WorkerType;\n};\n\nexport type TerminateJobArgs<WorkerType> = Partial<Job<WorkerType>> &\n Pick<Job<WorkerType>, 'id'>;\n\nexport abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n name: typeof controllerName = controllerName;\n\n state = null;\n\n readonly #jobs: Map<string, Job<WorkerType>>;\n\n readonly #setupSnapProvider: SetupSnapProvider;\n\n readonly #messenger: ExecutionServiceMessenger;\n\n readonly #initTimeout: number;\n\n readonly #pingTimeout: number;\n\n readonly #terminationTimeout: number;\n\n readonly #usePing: boolean;\n\n constructor({\n setupSnapProvider,\n messenger,\n initTimeout = inMilliseconds(60, Duration.Second),\n pingTimeout = inMilliseconds(2, Duration.Second),\n terminationTimeout = inMilliseconds(1, Duration.Second),\n usePing = true,\n }: ExecutionServiceArgs) {\n this.#jobs = new Map();\n this.#setupSnapProvider = setupSnapProvider;\n this.#messenger = messenger;\n this.#initTimeout = initTimeout;\n this.#pingTimeout = pingTimeout;\n this.#terminationTimeout = terminationTimeout;\n this.#usePing = usePing;\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${controllerName}:handleRpcRequest`,\n async (snapId: string, options: SnapRpcHookArgs) =>\n this.handleRpcRequest(snapId, options),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:executeSnap`,\n async (data: SnapExecutionData) => this.executeSnap(data),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateSnap`,\n async (snapId: string) => this.terminateSnap(snapId),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateAllSnaps`,\n async () => this.terminateAllSnaps(),\n );\n }\n\n /**\n * Performs additional necessary work during job termination. **MUST** be\n * implemented by concrete implementations. See\n * {@link AbstractExecutionService.terminate} for details.\n *\n * @param job - The object corresponding to the job to be terminated.\n */\n protected abstract terminateJob(job: TerminateJobArgs<WorkerType>): void;\n\n /**\n * Terminates the Snap with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the Snap will fail with an error.\n * Throws an error if the specified Snap does not exist, or if termination\n * fails unexpectedly.\n *\n * @param snapId - The id of the Snap to be terminated.\n */\n public async terminateSnap(snapId: string): Promise<void> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n throw new Error(`\"${snapId}\" is not currently running.`);\n }\n\n try {\n // Ping worker and tell it to run teardown, continue with termination if it takes too long\n const result = await withTimeout(\n this.#command(snapId, {\n jsonrpc: '2.0',\n method: 'terminate',\n params: [],\n id: nanoid(),\n }),\n this.#terminationTimeout,\n );\n\n if (result === hasTimedOut || result !== 'OK') {\n // We tried to shutdown gracefully but failed. This probably means the Snap is in infinite loop and\n // hogging down the whole JS process.\n // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour\n // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same\n // JS process.\n logError(`Snap \"${snapId}\" failed to terminate gracefully.`, result);\n }\n } catch {\n // Ignore\n }\n\n Object.values(job.streams).forEach((stream) => {\n try {\n !stream.destroyed && stream.destroy();\n stream.removeAllListeners();\n } catch (error) {\n logError('Error while destroying stream', error);\n }\n });\n\n this.terminateJob(job);\n\n this.#jobs.delete(snapId);\n log(`Snap \"${snapId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a Snap.\n *\n * @param snapId - The ID of the Snap to initiate a job for.\n * @param timer - The timer to use for timeouts.\n * @returns Information regarding the created job.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initJob(snapId: string, timer: Timer): Promise<Job<WorkerType>> {\n const { streams, worker } = await this.#initStreams(snapId, timer);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pipeline(\n jsonRpcConnection.stream,\n streams.command,\n jsonRpcConnection.stream,\n (error) => {\n if (error) {\n logError(`Command stream failure.`, error);\n }\n },\n );\n\n rpcEngine.push(jsonRpcConnection.middleware);\n\n const envMetadata = {\n id: snapId,\n streams,\n rpcEngine,\n worker,\n };\n this.#jobs.set(snapId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated job.\n *\n * @param snapId - The Snap ID.\n * @param timer - The timer to use for timeouts.\n * @returns The streams to communicate with the worker and the worker itself.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initStreams(\n snapId: string,\n timer: Timer,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const result = await withTimeout(this.initEnvStream(snapId), timer);\n\n if (result === hasTimedOut) {\n // For certain environments, such as the iframe we may have already created the worker and wish to terminate it.\n this.terminateJob({ id: snapId });\n throw new Error('The Snaps execution environment failed to start.');\n }\n\n const { worker, stream: envStream } = result;\n const mux = setupMultiplex(envStream, `Snap: \"${snapId}\"`);\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n\n // Handle out-of-band errors, i.e. errors thrown from the Snap outside of the req/res cycle.\n // Also keep track of outbound request/responses\n const notificationHandler = (\n message:\n | JsonRpcRequest\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (hasProperty(message, 'id')) {\n return;\n }\n\n if (message.method === 'OutboundRequest') {\n this.#messenger.publish('ExecutionService:outboundRequest', snapId);\n } else if (message.method === 'OutboundResponse') {\n this.#messenger.publish('ExecutionService:outboundResponse', snapId);\n } else if (message.method === 'UnhandledError') {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n (message.params as { error: SnapErrorJson }).error,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n logError(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\n ),\n );\n }\n };\n\n commandStream.on('data', notificationHandler);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n // Typecast: stream type mismatch\n return {\n streams: {\n command: commandStream,\n rpc: rpcStream,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n _connection: envStream,\n },\n worker,\n };\n }\n\n /**\n * Abstract function implemented by implementing class that spins up a new worker for a job.\n *\n * Depending on the execution environment, this may run forever if the Snap fails to start up properly, therefore any call to this function should be wrapped in a timeout.\n */\n protected abstract initEnvStream(snapId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.#jobs.keys()].map(async (snapId) => this.terminateSnap(snapId)),\n );\n }\n\n /**\n * Initializes and executes a Snap, setting up the communication channels to the Snap etc.\n *\n * @param snapData - Data needed for Snap execution.\n * @param snapData.snapId - The ID of the Snap to execute.\n * @param snapData.sourceCode - The source code of the Snap to execute.\n * @param snapData.endowments - The endowments available to the executing Snap.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error or execution times out.\n */\n async executeSnap({\n snapId,\n sourceCode,\n endowments,\n }: SnapExecutionData): Promise<string> {\n if (this.#jobs.has(snapId)) {\n throw new Error(`\"${snapId}\" is already running.`);\n }\n\n const timer = new Timer(this.#initTimeout);\n\n // This may resolve even if the environment has failed to start up fully\n const job = await this.#initJob(snapId, timer);\n\n // Certain environments use ping as part of their initialization and thus can skip it here\n if (this.#usePing) {\n // Ping the worker to ensure that it started up\n const pingResult = await withTimeout(\n this.#command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n }),\n this.#pingTimeout,\n );\n\n if (pingResult === hasTimedOut) {\n throw new Error('The Snaps execution environment failed to start.');\n }\n }\n\n const rpcStream = job.streams.rpc;\n\n this.#setupSnapProvider(snapId, rpcStream);\n\n const remainingTime = timer.remaining;\n\n const request = {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: { snapId, sourceCode, endowments },\n id: nanoid(),\n };\n\n assertIsJsonRpcRequest(request);\n\n const result = await withTimeout(\n this.#command(job.id, request),\n remainingTime,\n );\n\n if (result === hasTimedOut) {\n throw new Error(`${snapId} failed to start.`);\n }\n\n return result as string;\n }\n\n async #command(\n snapId: string,\n message: JsonRpcRequest,\n ): Promise<Json | undefined> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n throw new Error(`\"${snapId}\" is not currently running.`);\n }\n\n log('Parent: Sending Command', message);\n const response = await job.rpcEngine.handle(message);\n\n if (isJsonRpcFailure(response)) {\n throw new JsonRpcError(\n response.error.code,\n response.error.message,\n response.error.data,\n );\n }\n\n return response.result;\n }\n\n /**\n * Handle RPC request.\n *\n * @param snapId - The ID of the recipient Snap.\n * @param options - Bag of options to pass to the RPC handler.\n * @returns Promise that can handle the request.\n */\n public async handleRpcRequest(\n snapId: string,\n options: SnapRpcHookArgs,\n ): Promise<unknown> {\n const { handler, request, origin } = options;\n\n return await this.#command(snapId, {\n id: nanoid(),\n jsonrpc: '2.0',\n method: 'snapRpc',\n params: {\n origin,\n handler,\n request: request as JsonRpcRequest,\n target: snapId,\n },\n });\n }\n}\n\n/**\n * Sets up stream multiplexing for the given stream.\n *\n * @param connectionStream - The stream to mux.\n * @param streamName - The name of the stream, for identification in errors.\n * @returns The multiplexed stream.\n */\nexport function setupMultiplex(\n connectionStream: Duplex,\n streamName: string,\n): ObjectMultiplex {\n const mux = new ObjectMultiplex();\n pipeline(connectionStream, mux, connectionStream, (error) => {\n if (error) {\n logError(`\"${streamName}\" stream failure.`, error);\n }\n });\n return mux;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AbstractExecutionService.mjs","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,6CAA6C;AAC9E,OAAO,gBAAe,mCAAmC;;AAEzD,OAAO,EAAE,YAAY,EAAE,6BAA6B;AAEpD,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,8BAA8B;AAMpE,OAAO,EACL,QAA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AAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAErD,IAAI,gBAAgB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC/B,MAAM,IAAI,YAAY,CACpB,QAAQ,CAAC,KAAK,CAAC,IAAI,EACnB,QAAQ,CAAC,KAAK,CAAC,OAAO,EACtB,QAAQ,CAAC,KAAK,CAAC,IAAI,CACpB,CAAC;QACJ,CAAC;QAED,OAAO,QAAQ,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,gBAAgB,CAC3B,MAAc,EACd,OAAwB;QAExB,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAE7C,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE;YACjC,EAAE,EAAE,MAAM,EAAE;YACZ,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,SAAS;YACjB,MAAM,EAAE;gBACN,MAAM;gBACN,OAAO;gBACP,OAAO,EAAE,OAAyB;gBAClC,MAAM,EAAE,MAAM;aACf;SACF,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,eAAe,EAAE,CAAC;IAClC,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE,gBAAgB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC1D,IAAI,KAAK,EAAE,CAAC;YACV,QAAQ,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC,CAAC;QACrD,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { createStreamMiddleware } from '@metamask/json-rpc-middleware-stream';\nimport ObjectMultiplex from '@metamask/object-multiplex';\nimport type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { SNAP_STREAM_NAMES, logError } from '@metamask/snaps-utils';\nimport type {\n Json,\n JsonRpcNotification,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport {\n Duration,\n assertIsJsonRpcRequest,\n hasProperty,\n inMilliseconds,\n isJsonRpcFailure,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\nimport { pipeline } from 'readable-stream';\nimport type { Duplex } from 'readable-stream';\n\nimport type {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\nimport { log } from '../logging';\nimport { Timer } from '../snaps/Timer';\nimport { hasTimedOut, withTimeout } from '../utils';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n initTimeout?: number;\n pingTimeout?: number;\n terminationTimeout?: number;\n usePing?: boolean;\n};\n\nexport type JobStreams = {\n command: Duplex;\n rpc: Duplex;\n _connection: BasePostMessageStream;\n};\n\nexport type Job<WorkerType> = {\n id: string;\n streams: JobStreams;\n rpcEngine: JsonRpcEngine;\n worker: WorkerType;\n};\n\nexport type TerminateJobArgs<WorkerType> = Partial<Job<WorkerType>> &\n Pick<Job<WorkerType>, 'id'>;\n\n/** \n Statuses used for diagnostic purposes\n - created: The initial state, no initialization has started \n - initializing: Snap execution environment is initializing\n - initialized: Snap execution environment has initialized\n - executing: Snap source code is being executed\n - running: Snap executed and ready for RPC requests\n */\ntype ExecutionStatus =\n | 'created'\n | 'initializing'\n | 'initialized'\n | 'executing'\n | 'running';\n\nexport abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n name: typeof controllerName = controllerName;\n\n state = null;\n\n readonly #jobs: Map<string, Job<WorkerType>>;\n\n readonly #status: Map<string, ExecutionStatus>;\n\n readonly #setupSnapProvider: SetupSnapProvider;\n\n readonly #messenger: ExecutionServiceMessenger;\n\n readonly #initTimeout: number;\n\n readonly #pingTimeout: number;\n\n readonly #terminationTimeout: number;\n\n readonly #usePing: boolean;\n\n constructor({\n setupSnapProvider,\n messenger,\n initTimeout = inMilliseconds(60, Duration.Second),\n pingTimeout = inMilliseconds(2, Duration.Second),\n terminationTimeout = inMilliseconds(1, Duration.Second),\n usePing = true,\n }: ExecutionServiceArgs) {\n this.#jobs = new Map();\n this.#status = new Map();\n this.#setupSnapProvider = setupSnapProvider;\n this.#messenger = messenger;\n this.#initTimeout = initTimeout;\n this.#pingTimeout = pingTimeout;\n this.#terminationTimeout = terminationTimeout;\n this.#usePing = usePing;\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.#messenger.registerActionHandler(\n `${controllerName}:handleRpcRequest`,\n async (snapId: string, options: SnapRpcHookArgs) =>\n this.handleRpcRequest(snapId, options),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:executeSnap`,\n async (data: SnapExecutionData) => this.executeSnap(data),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateSnap`,\n async (snapId: string) => this.terminateSnap(snapId),\n );\n\n this.#messenger.registerActionHandler(\n `${controllerName}:terminateAllSnaps`,\n async () => this.terminateAllSnaps(),\n );\n }\n\n /**\n * Performs additional necessary work during job termination. **MUST** be\n * implemented by concrete implementations. See\n * {@link AbstractExecutionService.terminate} for details.\n *\n * @param job - The object corresponding to the job to be terminated.\n */\n protected abstract terminateJob(job: TerminateJobArgs<WorkerType>): void;\n\n /**\n * Terminates the Snap with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the Snap will fail with an error.\n * Throws an error if the specified Snap does not exist, or if termination\n * fails unexpectedly.\n *\n * @param snapId - The id of the Snap to be terminated.\n */\n public async terminateSnap(snapId: string): Promise<void> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n throw new Error(`\"${snapId}\" is not currently running.`);\n }\n\n try {\n // Ping worker and tell it to run teardown, continue with termination if it takes too long\n const result = await withTimeout(\n this.#command(snapId, {\n jsonrpc: '2.0',\n method: 'terminate',\n params: [],\n id: nanoid(),\n }),\n this.#terminationTimeout,\n );\n\n if (result === hasTimedOut || result !== 'OK') {\n // We tried to shutdown gracefully but failed. This probably means the Snap is in infinite loop and\n // hogging down the whole JS process.\n // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour\n // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same\n // JS process.\n logError(`Snap \"${snapId}\" failed to terminate gracefully.`, result);\n }\n } catch {\n // Ignore\n }\n\n Object.values(job.streams).forEach((stream) => {\n try {\n !stream.destroyed && stream.destroy();\n stream.removeAllListeners();\n } catch (error) {\n logError('Error while destroying stream', error);\n }\n });\n\n this.terminateJob(job);\n\n this.#jobs.delete(snapId);\n this.#status.delete(snapId);\n log(`Snap \"${snapId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a Snap.\n *\n * @param snapId - The ID of the Snap to initiate a job for.\n * @param timer - The timer to use for timeouts.\n * @returns Information regarding the created job.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initJob(snapId: string, timer: Timer): Promise<Job<WorkerType>> {\n const { streams, worker } = await this.#initStreams(snapId, timer);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pipeline(\n jsonRpcConnection.stream,\n streams.command,\n jsonRpcConnection.stream,\n (error) => {\n if (error) {\n logError(`Command stream failure.`, error);\n }\n },\n );\n\n rpcEngine.push(jsonRpcConnection.middleware);\n\n const envMetadata = {\n id: snapId,\n streams,\n rpcEngine,\n worker,\n };\n this.#jobs.set(snapId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated job.\n *\n * @param snapId - The Snap ID.\n * @param timer - The timer to use for timeouts.\n * @returns The streams to communicate with the worker and the worker itself.\n * @throws If the execution service returns an error or execution times out.\n */\n async #initStreams(\n snapId: string,\n timer: Timer,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const result = await withTimeout(this.initEnvStream(snapId), timer);\n\n if (result === hasTimedOut) {\n // For certain environments, such as the iframe we may have already created the worker and wish to terminate it.\n this.terminateJob({ id: snapId });\n\n const status = this.#status.get(snapId);\n if (status === 'created') {\n // Currently this error can only be thrown by OffscreenExecutionService.\n throw new Error(\n `The executor for \"${snapId}\" couldn't start initialization. The offscreen document may not exist.`,\n );\n }\n throw new Error(\n `The executor for \"${snapId}\" failed to initialize. The iframe/webview/worker failed to load.`,\n );\n }\n\n const { worker, stream: envStream } = result;\n const mux = setupMultiplex(envStream, `Snap: \"${snapId}\"`);\n const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);\n\n // Handle out-of-band errors, i.e. errors thrown from the Snap outside of the req/res cycle.\n // Also keep track of outbound request/responses\n const notificationHandler = (\n message:\n | JsonRpcRequest\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (hasProperty(message, 'id')) {\n return;\n }\n\n if (message.method === 'OutboundRequest') {\n this.#messenger.publish('ExecutionService:outboundRequest', snapId);\n } else if (message.method === 'OutboundResponse') {\n this.#messenger.publish('ExecutionService:outboundResponse', snapId);\n } else if (message.method === 'UnhandledError') {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n (message.params as { error: SnapErrorJson }).error,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n logError(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\n ),\n );\n }\n };\n\n commandStream.on('data', notificationHandler);\n const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);\n\n // Typecast: stream type mismatch\n return {\n streams: {\n command: commandStream,\n rpc: rpcStream,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n _connection: envStream,\n },\n worker,\n };\n }\n\n /**\n * Abstract function implemented by implementing class that spins up a new worker for a job.\n *\n * Depending on the execution environment, this may run forever if the Snap fails to start up properly, therefore any call to this function should be wrapped in a timeout.\n */\n protected abstract initEnvStream(snapId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n /**\n * Set the execution status of the Snap.\n *\n * @param snapId - The Snap ID.\n * @param status - The current execution status.\n */\n protected setSnapStatus(snapId: string, status: ExecutionStatus) {\n this.#status.set(snapId, status);\n }\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.#jobs.keys()].map(async (snapId) => this.terminateSnap(snapId)),\n );\n }\n\n /**\n * Initializes and executes a Snap, setting up the communication channels to the Snap etc.\n *\n * @param snapData - Data needed for Snap execution.\n * @param snapData.snapId - The ID of the Snap to execute.\n * @param snapData.sourceCode - The source code of the Snap to execute.\n * @param snapData.endowments - The endowments available to the executing Snap.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error or execution times out.\n */\n async executeSnap({\n snapId,\n sourceCode,\n endowments,\n }: SnapExecutionData): Promise<string> {\n if (this.#jobs.has(snapId)) {\n throw new Error(`\"${snapId}\" is already running.`);\n }\n\n this.setSnapStatus(snapId, 'created');\n\n const timer = new Timer(this.#initTimeout);\n\n // This may resolve even if the environment has failed to start up fully\n const job = await this.#initJob(snapId, timer);\n\n // Certain environments use ping as part of their initialization and thus can skip it here\n if (this.#usePing) {\n // Ping the worker to ensure that it started up\n const pingResult = await withTimeout(\n this.#command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n }),\n this.#pingTimeout,\n );\n\n if (pingResult === hasTimedOut) {\n throw new Error(\n `The executor for \"${snapId}\" was unreachable. The executor did not respond in time.`,\n );\n }\n }\n\n const rpcStream = job.streams.rpc;\n\n this.#setupSnapProvider(snapId, rpcStream);\n\n // Use the remaining time as the timer, but ensure that the\n // Snap gets at least half the init timeout.\n const remainingTime = Math.max(timer.remaining, this.#initTimeout / 2);\n\n this.setSnapStatus(snapId, 'initialized');\n\n const request = {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: { snapId, sourceCode, endowments },\n id: nanoid(),\n };\n\n assertIsJsonRpcRequest(request);\n\n this.setSnapStatus(snapId, 'executing');\n\n const result = await withTimeout(\n this.#command(job.id, request),\n remainingTime,\n );\n\n if (result === hasTimedOut) {\n throw new Error(`${snapId} failed to start.`);\n }\n\n if (result === 'OK') {\n this.setSnapStatus(snapId, 'running');\n }\n\n return result as string;\n }\n\n async #command(\n snapId: string,\n message: JsonRpcRequest,\n ): Promise<Json | undefined> {\n const job = this.#jobs.get(snapId);\n if (!job) {\n throw new Error(`\"${snapId}\" is not currently running.`);\n }\n\n log('Parent: Sending Command', message);\n const response = await job.rpcEngine.handle(message);\n\n if (isJsonRpcFailure(response)) {\n throw new JsonRpcError(\n response.error.code,\n response.error.message,\n response.error.data,\n );\n }\n\n return response.result;\n }\n\n /**\n * Handle RPC request.\n *\n * @param snapId - The ID of the recipient Snap.\n * @param options - Bag of options to pass to the RPC handler.\n * @returns Promise that can handle the request.\n */\n public async handleRpcRequest(\n snapId: string,\n options: SnapRpcHookArgs,\n ): Promise<unknown> {\n const { handler, request, origin } = options;\n\n return await this.#command(snapId, {\n id: nanoid(),\n jsonrpc: '2.0',\n method: 'snapRpc',\n params: {\n origin,\n handler,\n request: request as JsonRpcRequest,\n target: snapId,\n },\n });\n }\n}\n\n/**\n * Sets up stream multiplexing for the given stream.\n *\n * @param connectionStream - The stream to mux.\n * @param streamName - The name of the stream, for identification in errors.\n * @returns The multiplexed stream.\n */\nexport function setupMultiplex(\n connectionStream: Duplex,\n streamName: string,\n): ObjectMultiplex {\n const mux = new ObjectMultiplex();\n pipeline(connectionStream, mux, connectionStream, (error) => {\n if (error) {\n logError(`\"${streamName}\" stream failure.`, error);\n }\n });\n return mux;\n}\n"]}
|
|
@@ -18,6 +18,7 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
|
|
|
18
18
|
document.getElementById(jobWrapper.id)?.remove();
|
|
19
19
|
}
|
|
20
20
|
async initEnvStream(snapId) {
|
|
21
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
21
22
|
const iframeWindow = await (0, snaps_utils_1.createWindow)({
|
|
22
23
|
uri: this.iframeUrl.toString(),
|
|
23
24
|
id: snapId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AACA,uEAAwE;AACxE,uDAAqD;AAMrD,8EAAuE;AAMvE,MAAa,sBAAuB,SAAQ,mDAAgC;IACnE,SAAS,CAAM;IAEtB,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC+B;QACtC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAoC;QACzD,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,MAAM,YAAY,GAAG,MAAM,IAAA,0BAAY,EAAC;YACtC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;YAC9B,EAAE,EAAE,MAAM;SACX,CAAC,CAAC;QAEH,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;CACF;
|
|
1
|
+
{"version":3,"file":"IframeExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AACA,uEAAwE;AACxE,uDAAqD;AAMrD,8EAAuE;AAMvE,MAAa,sBAAuB,SAAQ,mDAAgC;IACnE,SAAS,CAAM;IAEtB,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC+B;QACtC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAoC;QACzD,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,YAAY,GAAG,MAAM,IAAA,0BAAY,EAAC;YACtC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;YAC9B,EAAE,EAAE,MAAM;SACX,CAAC,CAAC;QAEH,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;CACF;AAzCD,wDAyCC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } 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 ...args\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n ...args,\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(snapId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n this.setSnapStatus(snapId, 'initializing');\n\n const iframeWindow = await createWindow({\n uri: this.iframeUrl.toString(),\n id: snapId,\n });\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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAI3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAEvE,KAAK,qCAAqC,GAAG;IAC3C,SAAS,EAAE,GAAG,CAAC;CAChB,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,sBAAuB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;IACnE,SAAS,EAAE,GAAG,CAAC;gBAEV,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,qCAAqC;IASxC,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,IAAI;cAIlD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;
|
|
1
|
+
{"version":3,"file":"IframeExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAI3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAEvE,KAAK,qCAAqC,GAAG;IAC3C,SAAS,EAAE,GAAG,CAAC;CAChB,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,sBAAuB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;IACnE,SAAS,EAAE,GAAG,CAAC;gBAEV,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,qCAAqC;IASxC,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,IAAI;cAIlD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;CAiBH"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAI3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAEvE,KAAK,qCAAqC,GAAG;IAC3C,SAAS,EAAE,GAAG,CAAC;CAChB,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,sBAAuB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;IACnE,SAAS,EAAE,GAAG,CAAC;gBAEV,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,qCAAqC;IASxC,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,IAAI;cAIlD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;
|
|
1
|
+
{"version":3,"file":"IframeExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAI3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAEvE,KAAK,qCAAqC,GAAG;IAC3C,SAAS,EAAE,GAAG,CAAC;CAChB,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,sBAAuB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;IACnE,SAAS,EAAE,GAAG,CAAC;gBAEV,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,qCAAqC;IASxC,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,IAAI;cAIlD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;CAiBH"}
|
|
@@ -15,6 +15,7 @@ export class IframeExecutionService extends AbstractExecutionService {
|
|
|
15
15
|
document.getElementById(jobWrapper.id)?.remove();
|
|
16
16
|
}
|
|
17
17
|
async initEnvStream(snapId) {
|
|
18
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
18
19
|
const iframeWindow = await createWindow({
|
|
19
20
|
uri: this.iframeUrl.toString(),
|
|
20
21
|
id: snapId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IframeExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,sCAAsC;AACxE,OAAO,EAAE,YAAY,EAAE,8BAA8B;AAMrD,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAMvE,MAAM,OAAO,sBAAuB,SAAQ,wBAAgC;IACnE,SAAS,CAAM;IAEtB,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC+B;QACtC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAoC;QACzD,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,MAAM,YAAY,GAAG,MAAM,YAAY,CAAC;YACtC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;YAC9B,EAAE,EAAE,MAAM;SACX,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,uBAAuB,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;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } 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 ...args\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n ...args,\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(snapId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await createWindow({\n uri: this.iframeUrl.toString(),\n id: snapId,\n });\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"]}
|
|
1
|
+
{"version":3,"file":"IframeExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,sCAAsC;AACxE,OAAO,EAAE,YAAY,EAAE,8BAA8B;AAMrD,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAMvE,MAAM,OAAO,sBAAuB,SAAQ,wBAAgC;IACnE,SAAS,CAAM;IAEtB,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC+B;QACtC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAoC;QACzD,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,YAAY,GAAG,MAAM,YAAY,CAAC;YACtC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;YAC9B,EAAE,EAAE,MAAM;SACX,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,uBAAuB,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;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { WindowPostMessageStream } from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\n\nimport type {\n ExecutionServiceArgs,\n TerminateJobArgs,\n} from '../AbstractExecutionService';\nimport { AbstractExecutionService } 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 ...args\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n ...args,\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(snapId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n this.setSnapStatus(snapId, 'initializing');\n\n const iframeWindow = await createWindow({\n uri: this.iframeUrl.toString(),\n id: snapId,\n });\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"]}
|
|
@@ -5,7 +5,8 @@ const node_1 = require("@metamask/post-message-stream/node");
|
|
|
5
5
|
const child_process_1 = require("child_process");
|
|
6
6
|
const __1 = require("../index.cjs");
|
|
7
7
|
class NodeProcessExecutionService extends __1.AbstractExecutionService {
|
|
8
|
-
async initEnvStream() {
|
|
8
|
+
async initEnvStream(snapId) {
|
|
9
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
9
10
|
const worker = (0, child_process_1.fork)(require.resolve('@metamask/snaps-execution-environments/node-process'), {
|
|
10
11
|
stdio: 'pipe',
|
|
11
12
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeProcessExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";;;AACA,6DAAgF;AAEhF,iDAAqC;AAGrC,oCAA8C;AAE9C,MAAa,2BAA4B,SAAQ,4BAAsC;IAC3E,KAAK,CAAC,aAAa;
|
|
1
|
+
{"version":3,"file":"NodeProcessExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";;;AACA,6DAAgF;AAEhF,iDAAqC;AAGrC,oCAA8C;AAE9C,MAAa,2BAA4B,SAAQ,4BAAsC;IAC3E,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAA,oBAAI,EACjB,OAAO,CAAC,OAAO,CAAC,qDAAqD,CAAC,EACtE;YACE,KAAK,EAAE,MAAM;SACd,CACF,CAAC;QAEF,2EAA2E;QAC3E,mEAAmE;QACnE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACjC,sCAAsC;YACtC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACjC,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,iCAA0B,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QACnE,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;IAES,YAAY,CAAC,UAA0C;QAC/D,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC;IAC5B,CAAC;CACF;AAjCD,kEAiCC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { ProcessParentMessageStream } from '@metamask/post-message-stream/node';\nimport type { ChildProcess } from 'child_process';\nimport { fork } from 'child_process';\n\nimport type { TerminateJobArgs } from '..';\nimport { AbstractExecutionService } from '..';\n\nexport class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {\n protected async initEnvStream(snapId: string): Promise<{\n worker: ChildProcess;\n stream: BasePostMessageStream;\n }> {\n this.setSnapStatus(snapId, 'initializing');\n\n const worker = fork(\n require.resolve('@metamask/snaps-execution-environments/node-process'),\n {\n stdio: 'pipe',\n },\n );\n\n // Capturing `stdout` and `stderr` from the worker prevents the worker from\n // writing to them directly, making it easier to capture them Jest.\n worker.stdout?.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.log(data.toString());\n });\n\n worker.stderr?.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.error(data.toString());\n });\n\n const stream = new ProcessParentMessageStream({ process: worker });\n return Promise.resolve({ worker, stream });\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<ChildProcess>): void {\n jobWrapper.worker?.kill();\n }\n}\n"]}
|
|
@@ -4,7 +4,7 @@ import type { ChildProcess } from "child_process";
|
|
|
4
4
|
import type { TerminateJobArgs } from "../index.cjs";
|
|
5
5
|
import { AbstractExecutionService } from "../index.cjs";
|
|
6
6
|
export declare class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {
|
|
7
|
-
protected initEnvStream(): Promise<{
|
|
7
|
+
protected initEnvStream(snapId: string): Promise<{
|
|
8
8
|
worker: ChildProcess;
|
|
9
9
|
stream: BasePostMessageStream;
|
|
10
10
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeProcessExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,KAAK,EAAE,YAAY,EAAE,sBAAsB;AAGlD,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,2BAA4B,SAAQ,wBAAwB,CAAC,YAAY,CAAC;cACrE,aAAa,
|
|
1
|
+
{"version":3,"file":"NodeProcessExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,KAAK,EAAE,YAAY,EAAE,sBAAsB;AAGlD,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,2BAA4B,SAAQ,wBAAwB,CAAC,YAAY,CAAC;cACrE,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,YAAY,CAAC;QACrB,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;IA0BF,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,YAAY,CAAC,GAAG,IAAI;CAGzE"}
|
|
@@ -4,7 +4,7 @@ import type { ChildProcess } from "child_process";
|
|
|
4
4
|
import type { TerminateJobArgs } from "../index.mjs";
|
|
5
5
|
import { AbstractExecutionService } from "../index.mjs";
|
|
6
6
|
export declare class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {
|
|
7
|
-
protected initEnvStream(): Promise<{
|
|
7
|
+
protected initEnvStream(snapId: string): Promise<{
|
|
8
8
|
worker: ChildProcess;
|
|
9
9
|
stream: BasePostMessageStream;
|
|
10
10
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeProcessExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,KAAK,EAAE,YAAY,EAAE,sBAAsB;AAGlD,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,2BAA4B,SAAQ,wBAAwB,CAAC,YAAY,CAAC;cACrE,aAAa,
|
|
1
|
+
{"version":3,"file":"NodeProcessExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,KAAK,EAAE,YAAY,EAAE,sBAAsB;AAGlD,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,2BAA4B,SAAQ,wBAAwB,CAAC,YAAY,CAAC;cACrE,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,YAAY,CAAC;QACrB,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;IA0BF,SAAS,CAAC,YAAY,CAAC,UAAU,EAAE,gBAAgB,CAAC,YAAY,CAAC,GAAG,IAAI;CAGzE"}
|
|
@@ -4,7 +4,8 @@ import { ProcessParentMessageStream } from "@metamask/post-message-stream/node";
|
|
|
4
4
|
import { fork } from "child_process";
|
|
5
5
|
import { AbstractExecutionService } from "../index.mjs";
|
|
6
6
|
export class NodeProcessExecutionService extends AbstractExecutionService {
|
|
7
|
-
async initEnvStream() {
|
|
7
|
+
async initEnvStream(snapId) {
|
|
8
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
8
9
|
const worker = fork($require.resolve('@metamask/snaps-execution-environments/node-process'), {
|
|
9
10
|
stdio: 'pipe',
|
|
10
11
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeProcessExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,0BAA0B,EAAE,2CAA2C;AAEhF,OAAO,EAAE,IAAI,EAAE,sBAAsB;AAGrC,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,MAAM,OAAO,2BAA4B,SAAQ,wBAAsC;IAC3E,KAAK,CAAC,aAAa;
|
|
1
|
+
{"version":3,"file":"NodeProcessExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeProcessExecutionService.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,0BAA0B,EAAE,2CAA2C;AAEhF,OAAO,EAAE,IAAI,EAAE,sBAAsB;AAGrC,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,MAAM,OAAO,2BAA4B,SAAQ,wBAAsC;IAC3E,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,kBACD,qDAAqD,GACrE;YACE,KAAK,EAAE,MAAM;SACd,CACF,CAAC;QAEF,2EAA2E;QAC3E,mEAAmE;QACnE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACjC,sCAAsC;YACtC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACjC,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,0BAA0B,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QACnE,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;IAES,YAAY,CAAC,UAA0C;QAC/D,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC;IAC5B,CAAC;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { ProcessParentMessageStream } from '@metamask/post-message-stream/node';\nimport type { ChildProcess } from 'child_process';\nimport { fork } from 'child_process';\n\nimport type { TerminateJobArgs } from '..';\nimport { AbstractExecutionService } from '..';\n\nexport class NodeProcessExecutionService extends AbstractExecutionService<ChildProcess> {\n protected async initEnvStream(snapId: string): Promise<{\n worker: ChildProcess;\n stream: BasePostMessageStream;\n }> {\n this.setSnapStatus(snapId, 'initializing');\n\n const worker = fork(\n require.resolve('@metamask/snaps-execution-environments/node-process'),\n {\n stdio: 'pipe',\n },\n );\n\n // Capturing `stdout` and `stderr` from the worker prevents the worker from\n // writing to them directly, making it easier to capture them Jest.\n worker.stdout?.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.log(data.toString());\n });\n\n worker.stderr?.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.error(data.toString());\n });\n\n const stream = new ProcessParentMessageStream({ process: worker });\n return Promise.resolve({ worker, stream });\n }\n\n protected terminateJob(jobWrapper: TerminateJobArgs<ChildProcess>): void {\n jobWrapper.worker?.kill();\n }\n}\n"]}
|
|
@@ -5,7 +5,8 @@ const node_1 = require("@metamask/post-message-stream/node");
|
|
|
5
5
|
const worker_threads_1 = require("worker_threads");
|
|
6
6
|
const __1 = require("../index.cjs");
|
|
7
7
|
class NodeThreadExecutionService extends __1.AbstractExecutionService {
|
|
8
|
-
async initEnvStream() {
|
|
8
|
+
async initEnvStream(snapId) {
|
|
9
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
9
10
|
const worker = new worker_threads_1.Worker(require.resolve('@metamask/snaps-execution-environments/node-thread'), {
|
|
10
11
|
stdout: true,
|
|
11
12
|
stderr: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeThreadExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";;;AACA,6DAA+E;AAC/E,mDAAwC;AAGxC,oCAA8C;AAE9C,MAAa,0BAA2B,SAAQ,4BAAgC;IACpE,KAAK,CAAC,aAAa;
|
|
1
|
+
{"version":3,"file":"NodeThreadExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";;;AACA,6DAA+E;AAC/E,mDAAwC;AAGxC,oCAA8C;AAE9C,MAAa,0BAA2B,SAAQ,4BAAgC;IACpE,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,uBAAM,CACvB,OAAO,CAAC,OAAO,CAAC,oDAAoD,CAAC,EACrE;YACE,MAAM,EAAE,IAAI;YACZ,MAAM,EAAE,IAAI;SACb,CACF,CAAC;QAEF,2EAA2E;QAC3E,mEAAmE;QACnE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YAChC,sCAAsC;YACtC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YAChC,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,gCAAyB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACjE,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,wEAAwE;IACxE,WAAW;IACX,kEAAkE;IACxD,KAAK,CAAC,YAAY,CAC1B,UAAoC;QAEpC,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC;IACvC,CAAC;CACF;AAvCD,gEAuCC","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { ThreadParentMessageStream } from '@metamask/post-message-stream/node';\nimport { Worker } from 'worker_threads';\n\nimport type { TerminateJobArgs } from '..';\nimport { AbstractExecutionService } from '..';\n\nexport class NodeThreadExecutionService extends AbstractExecutionService<Worker> {\n protected async initEnvStream(snapId: string): Promise<{\n worker: Worker;\n stream: BasePostMessageStream;\n }> {\n this.setSnapStatus(snapId, 'initializing');\n\n const worker = new Worker(\n require.resolve('@metamask/snaps-execution-environments/node-thread'),\n {\n stdout: true,\n stderr: true,\n },\n );\n\n // Capturing `stdout` and `stderr` from the worker prevents the worker from\n // writing to them directly, making it easier to capture them Jest.\n worker.stdout.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.log(data.toString());\n });\n\n worker.stderr.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.error(data.toString());\n });\n\n const stream = new ThreadParentMessageStream({ thread: worker });\n return Promise.resolve({ worker, stream });\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n protected async terminateJob(\n jobWrapper: TerminateJobArgs<Worker>,\n ): Promise<void> {\n await jobWrapper.worker?.terminate();\n }\n}\n"]}
|
|
@@ -4,7 +4,7 @@ import { Worker } from "worker_threads";
|
|
|
4
4
|
import type { TerminateJobArgs } from "../index.cjs";
|
|
5
5
|
import { AbstractExecutionService } from "../index.cjs";
|
|
6
6
|
export declare class NodeThreadExecutionService extends AbstractExecutionService<Worker> {
|
|
7
|
-
protected initEnvStream(): Promise<{
|
|
7
|
+
protected initEnvStream(snapId: string): Promise<{
|
|
8
8
|
worker: Worker;
|
|
9
9
|
stream: BasePostMessageStream;
|
|
10
10
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeThreadExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,EAAE,MAAM,EAAE,uBAAuB;AAExC,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,0BAA2B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;cAC9D,aAAa,
|
|
1
|
+
{"version":3,"file":"NodeThreadExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,EAAE,MAAM,EAAE,uBAAuB;AAExC,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,0BAA2B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;cAC9D,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;cA8Bc,YAAY,CAC1B,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,GACnC,OAAO,CAAC,IAAI,CAAC;CAGjB"}
|
|
@@ -4,7 +4,7 @@ import { Worker } from "worker_threads";
|
|
|
4
4
|
import type { TerminateJobArgs } from "../index.mjs";
|
|
5
5
|
import { AbstractExecutionService } from "../index.mjs";
|
|
6
6
|
export declare class NodeThreadExecutionService extends AbstractExecutionService<Worker> {
|
|
7
|
-
protected initEnvStream(): Promise<{
|
|
7
|
+
protected initEnvStream(snapId: string): Promise<{
|
|
8
8
|
worker: Worker;
|
|
9
9
|
stream: BasePostMessageStream;
|
|
10
10
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeThreadExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,EAAE,MAAM,EAAE,uBAAuB;AAExC,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,0BAA2B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;cAC9D,aAAa,
|
|
1
|
+
{"version":3,"file":"NodeThreadExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAE3E,OAAO,EAAE,MAAM,EAAE,uBAAuB;AAExC,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAW;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,qBAAa,0BAA2B,SAAQ,wBAAwB,CAAC,MAAM,CAAC;cAC9D,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QACrD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;cA8Bc,YAAY,CAC1B,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,GACnC,OAAO,CAAC,IAAI,CAAC;CAGjB"}
|
|
@@ -4,7 +4,8 @@ import { ThreadParentMessageStream } from "@metamask/post-message-stream/node";
|
|
|
4
4
|
import { Worker } from "worker_threads";
|
|
5
5
|
import { AbstractExecutionService } from "../index.mjs";
|
|
6
6
|
export class NodeThreadExecutionService extends AbstractExecutionService {
|
|
7
|
-
async initEnvStream() {
|
|
7
|
+
async initEnvStream(snapId) {
|
|
8
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
8
9
|
const worker = new Worker($require.resolve('@metamask/snaps-execution-environments/node-thread'), {
|
|
9
10
|
stdout: true,
|
|
10
11
|
stderr: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeThreadExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,yBAAyB,EAAE,2CAA2C;AAC/E,OAAO,EAAE,MAAM,EAAE,uBAAuB;AAGxC,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,MAAM,OAAO,0BAA2B,SAAQ,wBAAgC;IACpE,KAAK,CAAC,aAAa;
|
|
1
|
+
{"version":3,"file":"NodeThreadExecutionService.mjs","sourceRoot":"","sources":["../../../src/services/node-js/NodeThreadExecutionService.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,yBAAyB,EAAE,2CAA2C;AAC/E,OAAO,EAAE,MAAM,EAAE,uBAAuB;AAGxC,OAAO,EAAE,wBAAwB,EAAE,qBAAW;AAE9C,MAAM,OAAO,0BAA2B,SAAQ,wBAAgC;IACpE,KAAK,CAAC,aAAa,CAAC,MAAc;QAI1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,MAAM,kBACP,oDAAoD,GACpE;YACE,MAAM,EAAE,IAAI;YACZ,MAAM,EAAE,IAAI;SACb,CACF,CAAC;QAEF,2EAA2E;QAC3E,mEAAmE;QACnE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YAChC,sCAAsC;YACtC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YAChC,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,yBAAyB,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACjE,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,wEAAwE;IACxE,WAAW;IACX,kEAAkE;IACxD,KAAK,CAAC,YAAY,CAC1B,UAAoC;QAEpC,MAAM,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC;IACvC,CAAC;CACF","sourcesContent":["import type { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { ThreadParentMessageStream } from '@metamask/post-message-stream/node';\nimport { Worker } from 'worker_threads';\n\nimport type { TerminateJobArgs } from '..';\nimport { AbstractExecutionService } from '..';\n\nexport class NodeThreadExecutionService extends AbstractExecutionService<Worker> {\n protected async initEnvStream(snapId: string): Promise<{\n worker: Worker;\n stream: BasePostMessageStream;\n }> {\n this.setSnapStatus(snapId, 'initializing');\n\n const worker = new Worker(\n require.resolve('@metamask/snaps-execution-environments/node-thread'),\n {\n stdout: true,\n stderr: true,\n },\n );\n\n // Capturing `stdout` and `stderr` from the worker prevents the worker from\n // writing to them directly, making it easier to capture them Jest.\n worker.stdout.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.log(data.toString());\n });\n\n worker.stderr.on('data', (data) => {\n // eslint-disable-next-line no-console\n console.error(data.toString());\n });\n\n const stream = new ThreadParentMessageStream({ thread: worker });\n return Promise.resolve({ worker, stream });\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n protected async terminateJob(\n jobWrapper: TerminateJobArgs<Worker>,\n ): Promise<void> {\n await jobWrapper.worker?.terminate();\n }\n}\n"]}
|
|
@@ -54,6 +54,7 @@ class ProxyExecutionService extends AbstractExecutionService_1.AbstractExecution
|
|
|
54
54
|
* @returns An object with the worker ID and stream.
|
|
55
55
|
*/
|
|
56
56
|
async initEnvStream(snapId) {
|
|
57
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
57
58
|
const stream = new ProxyPostMessageStream_1.ProxyPostMessageStream({
|
|
58
59
|
stream: this.#stream,
|
|
59
60
|
jobId: snapId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxyExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":";;;AACA,mCAAgC;AAMhC,8EAAuE;AACvE,0EAAmE;AAMnE,MAAa,qBAAsB,SAAQ,mDAAgC;IAChE,OAAO,CAAwB;IAExC;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC8B;QACrC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACH,wEAAwE;IACxE,WAAW;IACX,kEAAkE;IACxD,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,IAAA,eAAM,GAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,MAAc;QAC1C,MAAM,MAAM,GAAG,IAAI,+CAAsB,CAAC;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,KAAK,EAAE,MAAM;SACd,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,IAAA,eAAM,GAAE,EAAE;aACvD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IACpC,CAAC;CACF;
|
|
1
|
+
{"version":3,"file":"ProxyExecutionService.cjs","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":";;;AACA,mCAAgC;AAMhC,8EAAuE;AACvE,0EAAmE;AAMnE,MAAa,qBAAsB,SAAQ,mDAAgC;IAChE,OAAO,CAAwB;IAExC;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC8B;QACrC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACH,wEAAwE;IACxE,WAAW;IACX,kEAAkE;IACxD,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,IAAI,EAAE;gBACJ,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,cAAc;gBACtB,EAAE,EAAE,IAAA,eAAM,GAAE;aACb;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,aAAa,CAAC,MAAc;QAC1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,+CAAsB,CAAC;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,KAAK,EAAE,MAAM;SACd,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,IAAA,eAAM,GAAE,EAAE;aACvD,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IACpC,CAAC;CACF;AAhFD,sDAgFC","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 ...args\n }: ProxyExecutionEnvironmentServiceArgs) {\n super({\n ...args,\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 // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\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 Snap. This wraps the root stream\n * in a stream specific to the Snap.\n *\n * @param snapId - The Snap ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(snapId: string) {\n this.setSnapStatus(snapId, 'initializing');\n\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId: snapId,\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: snapId, stream };\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxyExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAG3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,oCAAoC,GAAG;IAC1C,MAAM,EAAE,qBAAqB,CAAC;CAC/B,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,qBAAsB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAGzE;;;;;;;;;;OAUG;gBACS,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,oCAAoC;IAWvC;;;;OAIG;cAIa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAa1D;;;;;;OAMG;cACa,aAAa,CAAC,MAAM,EAAE,MAAM;;;;
|
|
1
|
+
{"version":3,"file":"ProxyExecutionService.d.cts","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAG3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,oCAAoC,GAAG;IAC1C,MAAM,EAAE,qBAAqB,CAAC;CAC/B,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,qBAAsB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAGzE;;;;;;;;;;OAUG;gBACS,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,oCAAoC;IAWvC;;;;OAIG;cAIa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAa1D;;;;;;OAMG;cACa,aAAa,CAAC,MAAM,EAAE,MAAM;;;;CAsB7C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProxyExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAG3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,oCAAoC,GAAG;IAC1C,MAAM,EAAE,qBAAqB,CAAC;CAC/B,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,qBAAsB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAGzE;;;;;;;;;;OAUG;gBACS,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,oCAAoC;IAWvC;;;;OAIG;cAIa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAa1D;;;;;;OAMG;cACa,aAAa,CAAC,MAAM,EAAE,MAAM;;;;
|
|
1
|
+
{"version":3,"file":"ProxyExecutionService.d.mts","sourceRoot":"","sources":["../../../src/services/proxy/ProxyExecutionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,sCAAsC;AAG3E,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EACjB,wCAAoC;AACrC,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AACvE,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,KAAK,oCAAoC,GAAG;IAC1C,MAAM,EAAE,qBAAqB,CAAC;CAC/B,GAAG,oBAAoB,CAAC;AAEzB,qBAAa,qBAAsB,SAAQ,wBAAwB,CAAC,MAAM,CAAC;;IAGzE;;;;;;;;;;OAUG;gBACS,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EACR,EAAE,oCAAoC;IAWvC;;;;OAIG;cAIa,YAAY,CAAC,GAAG,EAAE,gBAAgB,CAAC,MAAM,CAAC;IAa1D;;;;;;OAMG;cACa,aAAa,CAAC,MAAM,EAAE,MAAM;;;;CAsB7C"}
|
|
@@ -51,6 +51,7 @@ export class ProxyExecutionService extends AbstractExecutionService {
|
|
|
51
51
|
* @returns An object with the worker ID and stream.
|
|
52
52
|
*/
|
|
53
53
|
async initEnvStream(snapId) {
|
|
54
|
+
this.setSnapStatus(snapId, 'initializing');
|
|
54
55
|
const stream = new ProxyPostMessageStream({
|
|
55
56
|
stream: this.#stream,
|
|
56
57
|
jobId: snapId,
|
|
@@ -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;IAChE,OAAO,CAAwB;IAExC;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC8B;QACrC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACH,wEAAwE;IACxE,WAAW;IACX,kEAAkE;IACxD,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAI,CAAC,OAAO,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,MAAc;QAC1C,MAAM,MAAM,GAAG,IAAI,sBAAsB,CAAC;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,KAAK,EAAE,MAAM;SACd,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,MAAM,EAAE,MAAM,EAAE,CAAC;IACpC,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 ...args\n }: ProxyExecutionEnvironmentServiceArgs) {\n super({\n ...args,\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 // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\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 Snap. This wraps the root stream\n * in a stream specific to the Snap.\n *\n * @param snapId - The Snap ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(snapId: string) {\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId: snapId,\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: snapId, stream };\n }\n}\n"]}
|
|
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;IAChE,OAAO,CAAwB;IAExC;;;;;;;;;;OAUG;IACH,YAAY,EACV,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,GAAG,IAAI,EAC8B;QACrC,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,SAAS;YACT,iBAAiB;YACjB,OAAO,EAAE,KAAK;SACf,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACH,wEAAwE;IACxE,WAAW;IACX,kEAAkE;IACxD,KAAK,CAAC,YAAY,CAAC,GAA6B;QACxD,0EAA0E;QAC1E,8CAA8C;QAC9C,IAAI,CAAC,OAAO,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,MAAc;QAC1C,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,sBAAsB,CAAC;YACxC,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,KAAK,EAAE,MAAM;SACd,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,MAAM,EAAE,MAAM,EAAE,CAAC;IACpC,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 ...args\n }: ProxyExecutionEnvironmentServiceArgs) {\n super({\n ...args,\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 // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\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 Snap. This wraps the root stream\n * in a stream specific to the Snap.\n *\n * @param snapId - The Snap ID.\n * @returns An object with the worker ID and stream.\n */\n protected async initEnvStream(snapId: string) {\n this.setSnapStatus(snapId, 'initializing');\n\n const stream = new ProxyPostMessageStream({\n stream: this.#stream,\n jobId: snapId,\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: snapId, stream };\n }\n}\n"]}
|