@metamask/snaps-controllers 12.2.0 → 12.3.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/dist/cronjob/CronjobController.cjs +13 -4
  3. package/dist/cronjob/CronjobController.cjs.map +1 -1
  4. package/dist/cronjob/CronjobController.d.cts.map +1 -1
  5. package/dist/cronjob/CronjobController.d.mts.map +1 -1
  6. package/dist/cronjob/CronjobController.mjs +15 -6
  7. package/dist/cronjob/CronjobController.mjs.map +1 -1
  8. package/dist/interface/SnapInterfaceController.cjs +15 -1
  9. package/dist/interface/SnapInterfaceController.cjs.map +1 -1
  10. package/dist/interface/SnapInterfaceController.d.cts +10 -2
  11. package/dist/interface/SnapInterfaceController.d.cts.map +1 -1
  12. package/dist/interface/SnapInterfaceController.d.mts +10 -2
  13. package/dist/interface/SnapInterfaceController.d.mts.map +1 -1
  14. package/dist/interface/SnapInterfaceController.mjs +17 -3
  15. package/dist/interface/SnapInterfaceController.mjs.map +1 -1
  16. package/dist/interface/utils.cjs +104 -4
  17. package/dist/interface/utils.cjs.map +1 -1
  18. package/dist/interface/utils.d.cts +71 -2
  19. package/dist/interface/utils.d.cts.map +1 -1
  20. package/dist/interface/utils.d.mts +71 -2
  21. package/dist/interface/utils.d.mts.map +1 -1
  22. package/dist/interface/utils.mjs +101 -5
  23. package/dist/interface/utils.mjs.map +1 -1
  24. package/dist/services/AbstractExecutionService.cjs +127 -202
  25. package/dist/services/AbstractExecutionService.cjs.map +1 -1
  26. package/dist/services/AbstractExecutionService.d.cts +8 -53
  27. package/dist/services/AbstractExecutionService.d.cts.map +1 -1
  28. package/dist/services/AbstractExecutionService.d.mts +8 -53
  29. package/dist/services/AbstractExecutionService.d.mts.map +1 -1
  30. package/dist/services/AbstractExecutionService.mjs +127 -202
  31. package/dist/services/AbstractExecutionService.mjs.map +1 -1
  32. package/dist/services/iframe/IframeExecutionService.cjs +2 -2
  33. package/dist/services/iframe/IframeExecutionService.cjs.map +1 -1
  34. package/dist/services/iframe/IframeExecutionService.d.cts +1 -1
  35. package/dist/services/iframe/IframeExecutionService.d.cts.map +1 -1
  36. package/dist/services/iframe/IframeExecutionService.d.mts +1 -1
  37. package/dist/services/iframe/IframeExecutionService.d.mts.map +1 -1
  38. package/dist/services/iframe/IframeExecutionService.mjs +2 -2
  39. package/dist/services/iframe/IframeExecutionService.mjs.map +1 -1
  40. package/dist/services/offscreen/OffscreenExecutionService.cjs +5 -5
  41. package/dist/services/offscreen/OffscreenExecutionService.cjs.map +1 -1
  42. package/dist/services/offscreen/OffscreenExecutionService.d.cts +4 -4
  43. package/dist/services/offscreen/OffscreenExecutionService.d.cts.map +1 -1
  44. package/dist/services/offscreen/OffscreenExecutionService.d.mts +4 -4
  45. package/dist/services/offscreen/OffscreenExecutionService.d.mts.map +1 -1
  46. package/dist/services/offscreen/OffscreenExecutionService.mjs +5 -5
  47. package/dist/services/offscreen/OffscreenExecutionService.mjs.map +1 -1
  48. package/dist/services/proxy/ProxyExecutionService.cjs +6 -6
  49. package/dist/services/proxy/ProxyExecutionService.cjs.map +1 -1
  50. package/dist/services/proxy/ProxyExecutionService.d.cts +4 -4
  51. package/dist/services/proxy/ProxyExecutionService.d.cts.map +1 -1
  52. package/dist/services/proxy/ProxyExecutionService.d.mts +4 -4
  53. package/dist/services/proxy/ProxyExecutionService.d.mts.map +1 -1
  54. package/dist/services/proxy/ProxyExecutionService.mjs +6 -6
  55. package/dist/services/proxy/ProxyExecutionService.mjs.map +1 -1
  56. package/dist/services/webview/WebViewExecutionService.cjs +6 -6
  57. package/dist/services/webview/WebViewExecutionService.cjs.map +1 -1
  58. package/dist/services/webview/WebViewExecutionService.d.cts +5 -5
  59. package/dist/services/webview/WebViewExecutionService.d.cts.map +1 -1
  60. package/dist/services/webview/WebViewExecutionService.d.mts +5 -5
  61. package/dist/services/webview/WebViewExecutionService.d.mts.map +1 -1
  62. package/dist/services/webview/WebViewExecutionService.mjs +6 -6
  63. package/dist/services/webview/WebViewExecutionService.mjs.map +1 -1
  64. package/dist/snaps/SnapController.cjs +43 -72
  65. package/dist/snaps/SnapController.cjs.map +1 -1
  66. package/dist/snaps/SnapController.d.cts +5 -9
  67. package/dist/snaps/SnapController.d.cts.map +1 -1
  68. package/dist/snaps/SnapController.d.mts +5 -9
  69. package/dist/snaps/SnapController.d.mts.map +1 -1
  70. package/dist/snaps/SnapController.mjs +43 -72
  71. package/dist/snaps/SnapController.mjs.map +1 -1
  72. package/dist/snaps/registry/json.cjs +4 -2
  73. package/dist/snaps/registry/json.cjs.map +1 -1
  74. package/dist/snaps/registry/json.d.cts.map +1 -1
  75. package/dist/snaps/registry/json.d.mts.map +1 -1
  76. package/dist/snaps/registry/json.mjs +4 -2
  77. package/dist/snaps/registry/json.mjs.map +1 -1
  78. package/package.json +4 -4
  79. package/dist/snaps/RequestQueue.cjs +0 -44
  80. package/dist/snaps/RequestQueue.cjs.map +0 -1
  81. package/dist/snaps/RequestQueue.d.cts +0 -25
  82. package/dist/snaps/RequestQueue.d.cts.map +0 -1
  83. package/dist/snaps/RequestQueue.d.mts +0 -25
  84. package/dist/snaps/RequestQueue.d.mts.map +0 -1
  85. package/dist/snaps/RequestQueue.mjs +0 -40
  86. package/dist/snaps/RequestQueue.mjs.map +0 -1
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _AbstractExecutionService_instances, _AbstractExecutionService_snapRpcHooks, _AbstractExecutionService_snapToJobMap, _AbstractExecutionService_jobToSnapMap, _AbstractExecutionService_messenger, _AbstractExecutionService_initTimeout, _AbstractExecutionService_pingTimeout, _AbstractExecutionService_terminationTimeout, _AbstractExecutionService_usePing, _AbstractExecutionService_removeSnapHooks, _AbstractExecutionService_createSnapHooks, _AbstractExecutionService_mapSnapAndJob, _AbstractExecutionService_removeSnapAndJobMapping;
12
+ var _AbstractExecutionService_instances, _AbstractExecutionService_jobs, _AbstractExecutionService_setupSnapProvider, _AbstractExecutionService_messenger, _AbstractExecutionService_initTimeout, _AbstractExecutionService_pingTimeout, _AbstractExecutionService_terminationTimeout, _AbstractExecutionService_usePing, _AbstractExecutionService_registerMessageHandlers, _AbstractExecutionService_initJob, _AbstractExecutionService_initStreams, _AbstractExecutionService_command;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -22,7 +22,7 @@ import $ObjectMultiplex from "@metamask/object-multiplex";
22
22
  const ObjectMultiplex = $importDefault($ObjectMultiplex);
23
23
  import { JsonRpcError } from "@metamask/rpc-errors";
24
24
  import { SNAP_STREAM_NAMES, logError } from "@metamask/snaps-utils";
25
- import { Duration, assertIsJsonRpcRequest, hasProperty, inMilliseconds, isJsonRpcFailure, isObject } from "@metamask/utils";
25
+ import { Duration, assertIsJsonRpcRequest, hasProperty, inMilliseconds, isJsonRpcFailure } from "@metamask/utils";
26
26
  import { nanoid } from "nanoid";
27
27
  import { pipeline } from "readable-stream";
28
28
  import { log } from "../logging.mjs";
@@ -34,55 +34,38 @@ export class AbstractExecutionService {
34
34
  _AbstractExecutionService_instances.add(this);
35
35
  this.name = controllerName;
36
36
  this.state = null;
37
- _AbstractExecutionService_snapRpcHooks.set(this, void 0);
38
- _AbstractExecutionService_snapToJobMap.set(this, void 0);
39
- _AbstractExecutionService_jobToSnapMap.set(this, void 0);
37
+ _AbstractExecutionService_jobs.set(this, void 0);
38
+ _AbstractExecutionService_setupSnapProvider.set(this, void 0);
40
39
  _AbstractExecutionService_messenger.set(this, void 0);
41
40
  _AbstractExecutionService_initTimeout.set(this, void 0);
42
41
  _AbstractExecutionService_pingTimeout.set(this, void 0);
43
42
  _AbstractExecutionService_terminationTimeout.set(this, void 0);
44
43
  _AbstractExecutionService_usePing.set(this, void 0);
45
- __classPrivateFieldSet(this, _AbstractExecutionService_snapRpcHooks, new Map(), "f");
46
- this.jobs = new Map();
47
- this.setupSnapProvider = setupSnapProvider;
48
- __classPrivateFieldSet(this, _AbstractExecutionService_snapToJobMap, new Map(), "f");
49
- __classPrivateFieldSet(this, _AbstractExecutionService_jobToSnapMap, new Map(), "f");
44
+ __classPrivateFieldSet(this, _AbstractExecutionService_jobs, new Map(), "f");
45
+ __classPrivateFieldSet(this, _AbstractExecutionService_setupSnapProvider, setupSnapProvider, "f");
50
46
  __classPrivateFieldSet(this, _AbstractExecutionService_messenger, messenger, "f");
51
47
  __classPrivateFieldSet(this, _AbstractExecutionService_initTimeout, initTimeout, "f");
52
48
  __classPrivateFieldSet(this, _AbstractExecutionService_pingTimeout, pingTimeout, "f");
53
49
  __classPrivateFieldSet(this, _AbstractExecutionService_terminationTimeout, terminationTimeout, "f");
54
50
  __classPrivateFieldSet(this, _AbstractExecutionService_usePing, usePing, "f");
55
- this.registerMessageHandlers();
51
+ __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_registerMessageHandlers).call(this);
56
52
  }
57
53
  /**
58
- * Constructor helper for registering the controller's messaging system
59
- * actions.
60
- */
61
- // TODO: Either fix this lint violation or explain why it's necessary to
62
- // ignore.
63
- // eslint-disable-next-line no-restricted-syntax
64
- registerMessageHandlers() {
65
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:handleRpcRequest`, async (snapId, options) => this.handleRpcRequest(snapId, options));
66
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:executeSnap`, async (data) => this.executeSnap(data));
67
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateSnap`, async (snapId) => this.terminateSnap(snapId));
68
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateAllSnaps`, async () => this.terminateAllSnaps());
69
- }
70
- /**
71
- * Terminates the job with the specified ID and deletes all its associated
72
- * data. Any subsequent messages targeting the job will fail with an error.
73
- * Throws an error if the specified job does not exist, or if termination
54
+ * Terminates the Snap with the specified ID and deletes all its associated
55
+ * data. Any subsequent messages targeting the Snap will fail with an error.
56
+ * Throws an error if the specified Snap does not exist, or if termination
74
57
  * fails unexpectedly.
75
58
  *
76
- * @param jobId - The id of the job to be terminated.
59
+ * @param snapId - The id of the Snap to be terminated.
77
60
  */
78
- async terminate(jobId) {
79
- const jobWrapper = this.jobs.get(jobId);
80
- if (!jobWrapper) {
81
- throw new Error(`Job with id "${jobId}" not found.`);
61
+ async terminateSnap(snapId) {
62
+ const job = __classPrivateFieldGet(this, _AbstractExecutionService_jobs, "f").get(snapId);
63
+ if (!job) {
64
+ throw new Error(`"${snapId}" is not currently running.`);
82
65
  }
83
66
  try {
84
67
  // Ping worker and tell it to run teardown, continue with termination if it takes too long
85
- const result = await withTimeout(this.command(jobId, {
68
+ const result = await withTimeout(__classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_command).call(this, snapId, {
86
69
  jsonrpc: '2.0',
87
70
  method: 'terminate',
88
71
  params: [],
@@ -94,13 +77,13 @@ export class AbstractExecutionService {
94
77
  // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour
95
78
  // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same
96
79
  // JS process.
97
- logError(`Job "${jobId}" failed to terminate gracefully.`, result);
80
+ logError(`Snap "${snapId}" failed to terminate gracefully.`, result);
98
81
  }
99
82
  }
100
83
  catch {
101
84
  // Ignore
102
85
  }
103
- Object.values(jobWrapper.streams).forEach((stream) => {
86
+ Object.values(job.streams).forEach((stream) => {
104
87
  try {
105
88
  !stream.destroyed && stream.destroy();
106
89
  stream.removeAllListeners();
@@ -109,127 +92,15 @@ export class AbstractExecutionService {
109
92
  logError('Error while destroying stream', error);
110
93
  }
111
94
  });
112
- this.terminateJob(jobWrapper);
113
- this.jobs.delete(jobId);
114
- __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_removeSnapAndJobMapping).call(this, jobId);
115
- log(`Job "${jobId}" terminated.`);
116
- }
117
- /**
118
- * Initiates a job for a snap.
119
- *
120
- * @param jobId - The ID of the job to initiate.
121
- * @param timer - The timer to use for timeouts.
122
- * @returns Information regarding the created job.
123
- * @throws If the execution service returns an error or execution times out.
124
- */
125
- async initJob(jobId, timer) {
126
- const { streams, worker } = await this.initStreams(jobId, timer);
127
- const rpcEngine = new JsonRpcEngine();
128
- const jsonRpcConnection = createStreamMiddleware();
129
- pipeline(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream, (error) => {
130
- if (error) {
131
- logError(`Command stream failure.`, error);
132
- }
133
- });
134
- rpcEngine.push(jsonRpcConnection.middleware);
135
- const envMetadata = {
136
- id: jobId,
137
- streams,
138
- rpcEngine,
139
- worker,
140
- };
141
- this.jobs.set(jobId, envMetadata);
142
- return envMetadata;
143
- }
144
- /**
145
- * Sets up the streams for an initiated job.
146
- *
147
- * @param jobId - The id of the job.
148
- * @param timer - The timer to use for timeouts.
149
- * @returns The streams to communicate with the worker and the worker itself.
150
- * @throws If the execution service returns an error or execution times out.
151
- */
152
- async initStreams(jobId, timer) {
153
- const result = await withTimeout(this.initEnvStream(jobId), timer);
154
- if (result === hasTimedOut) {
155
- // For certain environments, such as the iframe we may have already created the worker and wish to terminate it.
156
- this.terminateJob({ id: jobId });
157
- throw new Error('The Snaps execution environment failed to start.');
158
- }
159
- const { worker, stream: envStream } = result;
160
- const mux = setupMultiplex(envStream, `Job: "${jobId}"`);
161
- const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);
162
- // Handle out-of-band errors, i.e. errors thrown from the snap outside of the req/res cycle.
163
- // Also keep track of outbound request/responses
164
- const notificationHandler = (message) => {
165
- if (hasProperty(message, 'id')) {
166
- return;
167
- }
168
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
169
- const snapId = __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").get(jobId);
170
- if (message.method === 'OutboundRequest') {
171
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:outboundRequest', snapId);
172
- }
173
- else if (message.method === 'OutboundResponse') {
174
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:outboundResponse', snapId);
175
- }
176
- else if (message.method === 'UnhandledError') {
177
- if (isObject(message.params) && message.params.error) {
178
- __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:unhandledError', snapId, message.params.error);
179
- commandStream.removeListener('data', notificationHandler);
180
- }
181
- else {
182
- logError(new Error(`Received malformed "${message.method}" command stream notification.`));
183
- }
184
- }
185
- else {
186
- logError(new Error(`Received unexpected command stream notification "${message.method}".`));
187
- }
188
- };
189
- commandStream.on('data', notificationHandler);
190
- const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);
191
- // Typecast: stream type mismatch
192
- return {
193
- streams: {
194
- command: commandStream,
195
- rpc: rpcStream,
196
- // eslint-disable-next-line @typescript-eslint/naming-convention
197
- _connection: envStream,
198
- },
199
- worker,
200
- };
201
- }
202
- /**
203
- * Terminates the Snap with the specified ID. May throw an error if
204
- * termination unexpectedly fails, but will not fail if no job for the snap
205
- * with the specified ID is found.
206
- *
207
- * @param snapId - The ID of the snap to terminate.
208
- */
209
- async terminateSnap(snapId) {
210
- const jobId = __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").get(snapId);
211
- if (jobId) {
212
- await this.terminate(jobId);
213
- }
95
+ this.terminateJob(job);
96
+ __classPrivateFieldGet(this, _AbstractExecutionService_jobs, "f").delete(snapId);
97
+ log(`Snap "${snapId}" terminated.`);
214
98
  }
215
99
  async terminateAllSnaps() {
216
- await Promise.all([...this.jobs.keys()].map(async (jobId) => this.terminate(jobId)));
217
- __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").clear();
218
- }
219
- /**
220
- * Gets the RPC request handler for the given snap.
221
- *
222
- * @param snapId - The id of the Snap whose message handler to get.
223
- * @returns The RPC request handler for the snap.
224
- */
225
- // TODO: Either fix this lint violation or explain why it's necessary to
226
- // ignore.
227
- // eslint-disable-next-line no-restricted-syntax
228
- getRpcRequestHandler(snapId) {
229
- return __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").get(snapId);
100
+ await Promise.all([...__classPrivateFieldGet(this, _AbstractExecutionService_jobs, "f").keys()].map(async (snapId) => this.terminateSnap(snapId)));
230
101
  }
231
102
  /**
232
- * Initializes and executes a snap, setting up the communication channels to the snap etc.
103
+ * Initializes and executes a Snap, setting up the communication channels to the Snap etc.
233
104
  *
234
105
  * @param snapData - Data needed for Snap execution.
235
106
  * @param snapData.snapId - The ID of the Snap to execute.
@@ -239,18 +110,16 @@ export class AbstractExecutionService {
239
110
  * @throws If the execution service returns an error or execution times out.
240
111
  */
241
112
  async executeSnap({ snapId, sourceCode, endowments, }) {
242
- if (__classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").has(snapId)) {
243
- throw new Error(`Snap "${snapId}" is already being executed.`);
113
+ if (__classPrivateFieldGet(this, _AbstractExecutionService_jobs, "f").has(snapId)) {
114
+ throw new Error(`"${snapId}" is already running.`);
244
115
  }
245
- const jobId = nanoid();
246
116
  const timer = new Timer(__classPrivateFieldGet(this, _AbstractExecutionService_initTimeout, "f"));
247
117
  // This may resolve even if the environment has failed to start up fully
248
- const job = await this.initJob(jobId, timer);
249
- __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_mapSnapAndJob).call(this, snapId, job.id);
118
+ const job = await __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_initJob).call(this, snapId, timer);
250
119
  // Certain environments use ping as part of their initialization and thus can skip it here
251
120
  if (__classPrivateFieldGet(this, _AbstractExecutionService_usePing, "f")) {
252
121
  // Ping the worker to ensure that it started up
253
- const pingResult = await withTimeout(this.command(job.id, {
122
+ const pingResult = await withTimeout(__classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_command).call(this, job.id, {
254
123
  jsonrpc: '2.0',
255
124
  method: 'ping',
256
125
  id: nanoid(),
@@ -260,7 +129,7 @@ export class AbstractExecutionService {
260
129
  }
261
130
  }
262
131
  const rpcStream = job.streams.rpc;
263
- this.setupSnapProvider(snapId, rpcStream);
132
+ __classPrivateFieldGet(this, _AbstractExecutionService_setupSnapProvider, "f").call(this, snapId, rpcStream);
264
133
  const remainingTime = timer.remaining;
265
134
  const request = {
266
135
  jsonrpc: '2.0',
@@ -269,47 +138,22 @@ export class AbstractExecutionService {
269
138
  id: nanoid(),
270
139
  };
271
140
  assertIsJsonRpcRequest(request);
272
- const result = await withTimeout(this.command(job.id, request), remainingTime);
141
+ const result = await withTimeout(__classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_command).call(this, job.id, request), remainingTime);
273
142
  if (result === hasTimedOut) {
274
143
  throw new Error(`${snapId} failed to start.`);
275
144
  }
276
- __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_createSnapHooks).call(this, snapId, job.id);
277
145
  return result;
278
146
  }
279
- // Cannot be hash private yet because of tests.
280
- // eslint-disable-next-line no-restricted-syntax
281
- async command(jobId, message) {
282
- const job = this.jobs.get(jobId);
283
- if (!job) {
284
- throw new Error(`Job with id "${jobId}" not found.`);
285
- }
286
- log('Parent: Sending Command', message);
287
- const response = await job.rpcEngine.handle(message);
288
- if (isJsonRpcFailure(response)) {
289
- throw new JsonRpcError(response.error.code, response.error.message, response.error.data);
290
- }
291
- return response.result;
292
- }
293
147
  /**
294
148
  * Handle RPC request.
295
149
  *
296
- * @param snapId - The ID of the recipient snap.
150
+ * @param snapId - The ID of the recipient Snap.
297
151
  * @param options - Bag of options to pass to the RPC handler.
298
152
  * @returns Promise that can handle the request.
299
153
  */
300
154
  async handleRpcRequest(snapId, options) {
301
- const rpcRequestHandler = this.getRpcRequestHandler(snapId);
302
- if (!rpcRequestHandler) {
303
- throw new Error(`Snap execution service returned no RPC handler for running snap "${snapId}".`);
304
- }
305
- return rpcRequestHandler(options);
306
- }
307
- }
308
- _AbstractExecutionService_snapRpcHooks = new WeakMap(), _AbstractExecutionService_snapToJobMap = new WeakMap(), _AbstractExecutionService_jobToSnapMap = new WeakMap(), _AbstractExecutionService_messenger = new WeakMap(), _AbstractExecutionService_initTimeout = new WeakMap(), _AbstractExecutionService_pingTimeout = new WeakMap(), _AbstractExecutionService_terminationTimeout = new WeakMap(), _AbstractExecutionService_usePing = new WeakMap(), _AbstractExecutionService_instances = new WeakSet(), _AbstractExecutionService_removeSnapHooks = function _AbstractExecutionService_removeSnapHooks(snapId) {
309
- __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").delete(snapId);
310
- }, _AbstractExecutionService_createSnapHooks = function _AbstractExecutionService_createSnapHooks(snapId, workerId) {
311
- const rpcHook = async ({ origin, handler, request }) => {
312
- return await this.command(workerId, {
155
+ const { handler, request, origin } = options;
156
+ return await __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_command).call(this, snapId, {
313
157
  id: nanoid(),
314
158
  jsonrpc: '2.0',
315
159
  method: 'snapRpc',
@@ -320,19 +164,102 @@ _AbstractExecutionService_snapRpcHooks = new WeakMap(), _AbstractExecutionServic
320
164
  target: snapId,
321
165
  },
322
166
  });
167
+ }
168
+ }
169
+ _AbstractExecutionService_jobs = new WeakMap(), _AbstractExecutionService_setupSnapProvider = new WeakMap(), _AbstractExecutionService_messenger = new WeakMap(), _AbstractExecutionService_initTimeout = new WeakMap(), _AbstractExecutionService_pingTimeout = new WeakMap(), _AbstractExecutionService_terminationTimeout = new WeakMap(), _AbstractExecutionService_usePing = new WeakMap(), _AbstractExecutionService_instances = new WeakSet(), _AbstractExecutionService_registerMessageHandlers = function _AbstractExecutionService_registerMessageHandlers() {
170
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:handleRpcRequest`, async (snapId, options) => this.handleRpcRequest(snapId, options));
171
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:executeSnap`, async (data) => this.executeSnap(data));
172
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateSnap`, async (snapId) => this.terminateSnap(snapId));
173
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").registerActionHandler(`${controllerName}:terminateAllSnaps`, async () => this.terminateAllSnaps());
174
+ }, _AbstractExecutionService_initJob =
175
+ /**
176
+ * Initiates a job for a Snap.
177
+ *
178
+ * @param snapId - The ID of the Snap to initiate a job for.
179
+ * @param timer - The timer to use for timeouts.
180
+ * @returns Information regarding the created job.
181
+ * @throws If the execution service returns an error or execution times out.
182
+ */
183
+ async function _AbstractExecutionService_initJob(snapId, timer) {
184
+ const { streams, worker } = await __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_initStreams).call(this, snapId, timer);
185
+ const rpcEngine = new JsonRpcEngine();
186
+ const jsonRpcConnection = createStreamMiddleware();
187
+ pipeline(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream, (error) => {
188
+ if (error) {
189
+ logError(`Command stream failure.`, error);
190
+ }
191
+ });
192
+ rpcEngine.push(jsonRpcConnection.middleware);
193
+ const envMetadata = {
194
+ id: snapId,
195
+ streams,
196
+ rpcEngine,
197
+ worker,
198
+ };
199
+ __classPrivateFieldGet(this, _AbstractExecutionService_jobs, "f").set(snapId, envMetadata);
200
+ return envMetadata;
201
+ }, _AbstractExecutionService_initStreams =
202
+ /**
203
+ * Sets up the streams for an initiated job.
204
+ *
205
+ * @param snapId - The Snap ID.
206
+ * @param timer - The timer to use for timeouts.
207
+ * @returns The streams to communicate with the worker and the worker itself.
208
+ * @throws If the execution service returns an error or execution times out.
209
+ */
210
+ async function _AbstractExecutionService_initStreams(snapId, timer) {
211
+ const result = await withTimeout(this.initEnvStream(snapId), timer);
212
+ if (result === hasTimedOut) {
213
+ // For certain environments, such as the iframe we may have already created the worker and wish to terminate it.
214
+ this.terminateJob({ id: snapId });
215
+ throw new Error('The Snaps execution environment failed to start.');
216
+ }
217
+ const { worker, stream: envStream } = result;
218
+ const mux = setupMultiplex(envStream, `Snap: "${snapId}"`);
219
+ const commandStream = mux.createStream(SNAP_STREAM_NAMES.COMMAND);
220
+ // Handle out-of-band errors, i.e. errors thrown from the Snap outside of the req/res cycle.
221
+ // Also keep track of outbound request/responses
222
+ const notificationHandler = (message) => {
223
+ if (hasProperty(message, 'id')) {
224
+ return;
225
+ }
226
+ if (message.method === 'OutboundRequest') {
227
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:outboundRequest', snapId);
228
+ }
229
+ else if (message.method === 'OutboundResponse') {
230
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:outboundResponse', snapId);
231
+ }
232
+ else if (message.method === 'UnhandledError') {
233
+ __classPrivateFieldGet(this, _AbstractExecutionService_messenger, "f").publish('ExecutionService:unhandledError', snapId, message.params.error);
234
+ commandStream.removeListener('data', notificationHandler);
235
+ }
236
+ else {
237
+ logError(new Error(`Received unexpected command stream notification "${message.method}".`));
238
+ }
323
239
  };
324
- __classPrivateFieldGet(this, _AbstractExecutionService_snapRpcHooks, "f").set(snapId, rpcHook);
325
- }, _AbstractExecutionService_mapSnapAndJob = function _AbstractExecutionService_mapSnapAndJob(snapId, jobId) {
326
- __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").set(snapId, jobId);
327
- __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").set(jobId, snapId);
328
- }, _AbstractExecutionService_removeSnapAndJobMapping = function _AbstractExecutionService_removeSnapAndJobMapping(jobId) {
329
- const snapId = __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").get(jobId);
330
- if (!snapId) {
331
- throw new Error(`job: "${jobId}" has no mapped snap.`);
240
+ commandStream.on('data', notificationHandler);
241
+ const rpcStream = mux.createStream(SNAP_STREAM_NAMES.JSON_RPC);
242
+ // Typecast: stream type mismatch
243
+ return {
244
+ streams: {
245
+ command: commandStream,
246
+ rpc: rpcStream,
247
+ // eslint-disable-next-line @typescript-eslint/naming-convention
248
+ _connection: envStream,
249
+ },
250
+ worker,
251
+ };
252
+ }, _AbstractExecutionService_command = async function _AbstractExecutionService_command(snapId, message) {
253
+ const job = __classPrivateFieldGet(this, _AbstractExecutionService_jobs, "f").get(snapId);
254
+ if (!job) {
255
+ throw new Error(`"${snapId}" is not currently running.`);
256
+ }
257
+ log('Parent: Sending Command', message);
258
+ const response = await job.rpcEngine.handle(message);
259
+ if (isJsonRpcFailure(response)) {
260
+ throw new JsonRpcError(response.error.code, response.error.message, response.error.data);
332
261
  }
333
- __classPrivateFieldGet(this, _AbstractExecutionService_jobToSnapMap, "f").delete(jobId);
334
- __classPrivateFieldGet(this, _AbstractExecutionService_snapToJobMap, "f").delete(snapId);
335
- __classPrivateFieldGet(this, _AbstractExecutionService_instances, "m", _AbstractExecutionService_removeSnapHooks).call(this, snapId);
262
+ return response.result;
336
263
  };
337
264
  /**
338
265
  * Sets up stream multiplexing for the given stream.
@@ -345,9 +272,7 @@ export function setupMultiplex(connectionStream, streamName) {
345
272
  const mux = new ObjectMultiplex();
346
273
  pipeline(connectionStream, mux, connectionStream, (error) => {
347
274
  if (error) {
348
- streamName
349
- ? logError(`"${streamName}" stream failure.`, error)
350
- : logError(error);
275
+ logError(`"${streamName}" stream failure.`, error);
351
276
  }
352
277
  });
353
278
  return mux;
@@ -1 +1 @@
1
- {"version":3,"file":"AbstractExecutionService.mjs","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAC1D,OA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AAM,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,UAAU;gBACR,CAAC,CAAC,QAAQ,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC;gBACpD,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACtB,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 { SnapRpcHook, 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 isObject,\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 #snapRpcHooks: Map<string, SnapRpcHook>;\n\n // Cannot be hash private yet because of tests.\n protected jobs: Map<string, Job<WorkerType>>;\n\n // Cannot be hash private yet because of tests.\n // eslint-disable-next-line no-restricted-syntax\n private readonly setupSnapProvider: SetupSnapProvider;\n\n readonly #snapToJobMap: Map<string, string>;\n\n readonly #jobToSnapMap: Map<string, string>;\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.#snapRpcHooks = new Map();\n this.jobs = new Map();\n this.setupSnapProvider = setupSnapProvider;\n this.#snapToJobMap = new Map();\n this.#jobToSnapMap = new Map();\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 // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line no-restricted-syntax\n private 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 job with the specified ID and deletes all its associated\n * data. Any subsequent messages targeting the job will fail with an error.\n * Throws an error if the specified job does not exist, or if termination\n * fails unexpectedly.\n *\n * @param jobId - The id of the job to be terminated.\n */\n public async terminate(jobId: string): Promise<void> {\n const jobWrapper = this.jobs.get(jobId);\n if (!jobWrapper) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\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(jobId, {\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(`Job \"${jobId}\" failed to terminate gracefully.`, result);\n }\n } catch {\n // Ignore\n }\n\n Object.values(jobWrapper.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(jobWrapper);\n\n this.jobs.delete(jobId);\n this.#removeSnapAndJobMapping(jobId);\n log(`Job \"${jobId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a snap.\n *\n * @param jobId - The ID of the job to initiate.\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 protected async initJob(\n jobId: string,\n timer: Timer,\n ): Promise<Job<WorkerType>> {\n const { streams, worker } = await this.initStreams(jobId, 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: jobId,\n streams,\n rpcEngine,\n worker,\n };\n this.jobs.set(jobId, envMetadata);\n\n return envMetadata;\n }\n\n /**\n * Sets up the streams for an initiated job.\n *\n * @param jobId - The id of the job.\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 protected async initStreams(\n jobId: string,\n timer: Timer,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const result = await withTimeout(this.initEnvStream(jobId), 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: jobId });\n throw new Error('The Snaps execution environment failed to start.');\n }\n\n const { worker, stream: envStream } = result;\n const mux = setupMultiplex(envStream, `Job: \"${jobId}\"`);\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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const snapId = this.#jobToSnapMap.get(jobId)!;\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 if (isObject(message.params) && message.params.error) {\n this.#messenger.publish(\n 'ExecutionService:unhandledError',\n snapId,\n message.params.error as SnapErrorJson,\n );\n commandStream.removeListener('data', notificationHandler);\n } else {\n logError(\n new Error(\n `Received malformed \"${message.method}\" command stream notification.`,\n ),\n );\n }\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(jobId: string): Promise<{\n worker: WorkerType;\n stream: BasePostMessageStream;\n }>;\n\n /**\n * Terminates the Snap with the specified ID. May throw an error if\n * termination unexpectedly fails, but will not fail if no job for the snap\n * with the specified ID is found.\n *\n * @param snapId - The ID of the snap to terminate.\n */\n async terminateSnap(snapId: string) {\n const jobId = this.#snapToJobMap.get(snapId);\n if (jobId) {\n await this.terminate(jobId);\n }\n }\n\n async terminateAllSnaps() {\n await Promise.all(\n [...this.jobs.keys()].map(async (jobId) => this.terminate(jobId)),\n );\n this.#snapRpcHooks.clear();\n }\n\n /**\n * Gets the RPC request handler for the given snap.\n *\n * @param snapId - The id of the Snap whose message handler to get.\n * @returns The RPC request handler for the snap.\n */\n // TODO: Either fix this lint violation or explain why it's necessary to\n // ignore.\n // eslint-disable-next-line no-restricted-syntax\n private getRpcRequestHandler(snapId: string) {\n return this.#snapRpcHooks.get(snapId);\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.#snapToJobMap.has(snapId)) {\n throw new Error(`Snap \"${snapId}\" is already being executed.`);\n }\n\n const jobId = nanoid();\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(jobId, timer);\n\n this.#mapSnapAndJob(snapId, job.id);\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 this.#createSnapHooks(snapId, job.id);\n return result as string;\n }\n\n // Cannot be hash private yet because of tests.\n // eslint-disable-next-line no-restricted-syntax\n private async command(\n jobId: string,\n message: JsonRpcRequest,\n ): Promise<Json | undefined> {\n const job = this.jobs.get(jobId);\n if (!job) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\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 #removeSnapHooks(snapId: string) {\n this.#snapRpcHooks.delete(snapId);\n }\n\n #createSnapHooks(snapId: string, workerId: string) {\n const rpcHook = async ({ origin, handler, request }: SnapRpcHookArgs) => {\n return await this.command(workerId, {\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 this.#snapRpcHooks.set(snapId, rpcHook);\n }\n\n #mapSnapAndJob(snapId: string, jobId: string): void {\n this.#snapToJobMap.set(snapId, jobId);\n this.#jobToSnapMap.set(jobId, snapId);\n }\n\n #removeSnapAndJobMapping(jobId: string): void {\n const snapId = this.#jobToSnapMap.get(jobId);\n if (!snapId) {\n throw new Error(`job: \"${jobId}\" has no mapped snap.`);\n }\n\n this.#jobToSnapMap.delete(jobId);\n this.#snapToJobMap.delete(snapId);\n this.#removeSnapHooks(snapId);\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 rpcRequestHandler = this.getRpcRequestHandler(snapId);\n\n if (!rpcRequestHandler) {\n throw new Error(\n `Snap execution service returned no RPC handler for running snap \"${snapId}\".`,\n );\n }\n\n return rpcRequestHandler(options);\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 streamName\n ? logError(`\"${streamName}\" stream failure.`, error)\n : logError(error);\n }\n });\n return mux;\n}\n"]}
1
+ {"version":3,"file":"Abstract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,CACpB,QAAQ,CAAC,KAAK,CAAC,IAAI,EACnB,QAAQ,CAAC,KAAK,CAAC,OAAO,EACtB,QAAQ,CAAC,KAAK,CAAC,IAAI,CACpB,CAAC;IACJ,CAAC;IAED,OAAO,QAAQ,CAAC,MAAM,CAAC;AACzB,CAAC;AA6BH;;;;;;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"]}
@@ -16,10 +16,10 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
16
16
  terminateJob(jobWrapper) {
17
17
  document.getElementById(jobWrapper.id)?.remove();
18
18
  }
19
- async initEnvStream(jobId) {
19
+ async initEnvStream(snapId) {
20
20
  const iframeWindow = await (0, snaps_utils_1.createWindow)({
21
21
  uri: this.iframeUrl.toString(),
22
- id: jobId,
22
+ id: snapId,
23
23
  });
24
24
  const stream = new post_message_stream_1.WindowPostMessageStream({
25
25
  name: 'parent',
@@ -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;IAG1E,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,KAAa;QAIzC,MAAM,YAAY,GAAG,MAAM,IAAA,0BAAY,EAAC;YACtC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;YAC9B,EAAE,EAAE,KAAK;SACV,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;AAvCD,wDAuCC","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(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await createWindow({\n uri: this.iframeUrl.toString(),\n id: jobId,\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.cjs","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AACA,uEAAwE;AACxE,uDAAqD;AAMrD,8EAAuE;AAMvE,MAAa,sBAAuB,SAAQ,mDAAgC;IAG1E,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;AAvCD,wDAuCC","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"]}
@@ -8,7 +8,7 @@ export declare class IframeExecutionService extends AbstractExecutionService<Win
8
8
  iframeUrl: URL;
9
9
  constructor({ iframeUrl, messenger, setupSnapProvider, ...args }: IframeExecutionEnvironmentServiceArgs);
10
10
  protected terminateJob(jobWrapper: TerminateJobArgs<Window>): void;
11
- protected initEnvStream(jobId: string): Promise<{
11
+ protected initEnvStream(snapId: string): Promise<{
12
12
  worker: Window;
13
13
  stream: BasePostMessageStream;
14
14
  }>;
@@ -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,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;QACpD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;CAeH"}
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;CAeH"}
@@ -8,7 +8,7 @@ export declare class IframeExecutionService extends AbstractExecutionService<Win
8
8
  iframeUrl: URL;
9
9
  constructor({ iframeUrl, messenger, setupSnapProvider, ...args }: IframeExecutionEnvironmentServiceArgs);
10
10
  protected terminateJob(jobWrapper: TerminateJobArgs<Window>): void;
11
- protected initEnvStream(jobId: string): Promise<{
11
+ protected initEnvStream(snapId: string): Promise<{
12
12
  worker: Window;
13
13
  stream: BasePostMessageStream;
14
14
  }>;
@@ -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,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;QACpD,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC;KAC/B,CAAC;CAeH"}
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;CAeH"}