@metamask/snaps-controllers 0.27.0 → 0.28.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 (51) hide show
  1. package/dist/services/AbstractExecutionService.js +1 -1
  2. package/dist/services/AbstractExecutionService.js.map +1 -1
  3. package/dist/services/browser.d.ts +1 -0
  4. package/dist/services/browser.js +1 -0
  5. package/dist/services/browser.js.map +1 -1
  6. package/dist/services/iframe/IframeExecutionService.d.ts +0 -10
  7. package/dist/services/iframe/IframeExecutionService.js +2 -53
  8. package/dist/services/iframe/IframeExecutionService.js.map +1 -1
  9. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js +0 -40
  10. package/dist/services/iframe/test/fixJSDOMPostMessageEventSource.js.map +1 -1
  11. package/dist/services/index.d.ts +1 -0
  12. package/dist/services/index.js +1 -0
  13. package/dist/services/index.js.map +1 -1
  14. package/dist/services/offscreen/OffscreenExecutionService.d.ts +49 -0
  15. package/dist/services/offscreen/OffscreenExecutionService.js +100 -0
  16. package/dist/services/offscreen/OffscreenExecutionService.js.map +1 -0
  17. package/dist/services/offscreen/OffscreenPostMessageStream.d.ts +35 -0
  18. package/dist/services/offscreen/OffscreenPostMessageStream.js +66 -0
  19. package/dist/services/offscreen/OffscreenPostMessageStream.js.map +1 -0
  20. package/dist/services/offscreen/index.d.ts +2 -0
  21. package/dist/services/offscreen/index.js +19 -0
  22. package/dist/services/offscreen/index.js.map +1 -0
  23. package/dist/snaps/SnapController.d.ts +15 -32
  24. package/dist/snaps/SnapController.js +49 -49
  25. package/dist/snaps/SnapController.js.map +1 -1
  26. package/dist/snaps/endowments/index.d.ts +1 -1
  27. package/dist/snaps/endowments/network-access.d.ts +1 -1
  28. package/dist/snaps/endowments/network-access.js +1 -1
  29. package/dist/snaps/endowments/network-access.js.map +1 -1
  30. package/dist/snaps/index.d.ts +1 -0
  31. package/dist/snaps/index.js +1 -0
  32. package/dist/snaps/index.js.map +1 -1
  33. package/dist/snaps/location/location.d.ts +4 -0
  34. package/dist/snaps/location/location.js +3 -1
  35. package/dist/snaps/location/location.js.map +1 -1
  36. package/dist/snaps/location/npm.d.ts +2 -1
  37. package/dist/snaps/location/npm.js +25 -12
  38. package/dist/snaps/location/npm.js.map +1 -1
  39. package/dist/snaps/registry/index.d.ts +2 -0
  40. package/dist/snaps/registry/index.js +19 -0
  41. package/dist/snaps/registry/index.js.map +1 -0
  42. package/dist/snaps/registry/json.d.ts +12 -0
  43. package/dist/snaps/registry/json.js +83 -0
  44. package/dist/snaps/registry/json.js.map +1 -0
  45. package/dist/snaps/registry/registry.d.ts +20 -0
  46. package/dist/snaps/registry/registry.js +11 -0
  47. package/dist/snaps/registry/registry.js.map +1 -0
  48. package/package.json +7 -6
  49. package/dist/services/iframe/test/server.d.ts +0 -11
  50. package/dist/services/iframe/test/server.js +0 -71
  51. package/dist/services/iframe/test/server.js.map +0 -1
@@ -73,7 +73,7 @@ class AbstractExecutionService {
73
73
  id: (0, nanoid_1.nanoid)(),
74
74
  }), __classPrivateFieldGet(this, _AbstractExecutionService_terminationTimeout, "f"));
75
75
  if (result === utils_2.hasTimedOut || result !== 'OK') {
76
- // We tried to shutdown gracefully but failed. This probably means the Snap is in infite loop and
76
+ // We tried to shutdown gracefully but failed. This probably means the Snap is in infinite loop and
77
77
  // hogging down the whole JS process.
78
78
  // TODO(ritave): It might be doing weird things such as posting a lot of setTimeouts. Add a test to ensure that this behaviour
79
79
  // doesn't leak into other workers. Especially important in IframeExecutionEnvironment since they all share the same
@@ -1 +1 @@
1
- {"version":3,"file":"AbstractExecutionService.js","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,kFAAyD;AAEzD,uDAI+B;AAC/B,2CAMyB;AACzB,qDAKyB;AACzB,2EAAoE;AACpE,mCAAgC;AAChC,gDAAwB;AAGxB,oCAAoD;AAQpD,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAuB1C,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I,8CAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7C,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,SAAS,KAAK,uBAAuB,CAAC,CAAC;KACxD;IAED,uBAAA,IAAI,8CAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACjC,uBAAA,IAAI,8CAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAClC,uBAAA,IAAI,sFAAiB,MAArB,IAAI,EAAkB,MAAM,CAAC,CAAC;AAChC,CAAC;AAyBH;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,0BAAe,EAAE,CAAC;IAClC,IAAA,cAAI,EACF,gBAAgB;IAChB,iCAAiC;IACjC,GAAwB,EACxB,gBAAgB,EAChB,CAAC,KAAK,EAAE,EAAE;QACR,IAAI,KAAK,EAAE;YACT,UAAU;gBACR,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC;gBACzD,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC1B;IACH,CAAC,CACF,CAAC;IACF,OAAO,GAAG,CAAC;AACb,CAAC;AAnBD,wCAmBC","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n SnapRpcHook,\n SnapRpcHookArgs,\n SNAP_STREAM_NAMES,\n} from '@metamask/snaps-utils';\nimport {\n Duration,\n isJsonRpcNotification,\n isObject,\n Json,\n JsonRpcNotification,\n} from '@metamask/utils';\nimport {\n JsonRpcEngine,\n // TODO: Replace with @metamask/utils version after bumping json-rpc-engine\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from 'json-rpc-engine';\nimport { createStreamMiddleware } from 'json-rpc-middleware-stream';\nimport { nanoid } from 'nanoid';\nimport pump from 'pump';\nimport { Duplex } from 'stream';\n\nimport { hasTimedOut, withTimeout } from '../utils';\nimport {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n terminationTimeout?: number;\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 abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n #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 private readonly setupSnapProvider: SetupSnapProvider;\n\n #snapToJobMap: Map<string, string>;\n\n #jobToSnapMap: Map<string, string>;\n\n #messenger: ExecutionServiceMessenger;\n\n #terminationTimeout: number;\n\n constructor({\n setupSnapProvider,\n messenger,\n terminationTimeout = Duration.Second,\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.#terminationTimeout = terminationTimeout;\n\n this.registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\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 (snapData: SnapExecutionData) => this.executeSnap(snapData),\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: Job<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 // 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 infite 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 console.error(`Job \"${jobId}\" failed to terminate gracefully.`, result);\n }\n\n Object.values(jobWrapper.streams).forEach((stream) => {\n try {\n !stream.destroyed && stream.destroy();\n stream.removeAllListeners();\n } catch (error) {\n console.error('Error while destroying stream', error);\n }\n });\n\n this.terminateJob(jobWrapper);\n\n this.#removeSnapAndJobMapping(jobId);\n this.jobs.delete(jobId);\n console.log(`Job \"${jobId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a snap.\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 * @returns Information regarding the created job.\n */\n protected async initJob(): Promise<Job<WorkerType>> {\n const jobId = nanoid();\n const { streams, worker } = await this.initStreams(jobId);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pump(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream);\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 * 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 * @param jobId - The id of the job.\n * @returns The streams to communicate with the worker and the worker itself.\n */\n protected async initStreams(\n jobId: string,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const { worker, stream: envStream } = await this.initEnvStream(jobId);\n // Typecast justification: stream type mismatch\n const mux = setupMultiplex(\n envStream as unknown as Duplex,\n `Job: \"${jobId}\"`,\n );\n\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<unknown>\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (!isJsonRpcNotification(message)) {\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 console.error(\n new Error(\n `Received malformed \"${message.method}\" command stream notification.`,\n ),\n );\n }\n } else {\n console.error(\n new Error(\n `Received unexpected command stream notification \"${message.method}\".`,\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 as unknown as Duplex,\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 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 * 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 * @param snapData - Data needed for Snap execution.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error.\n */\n async executeSnap(snapData: SnapExecutionData): Promise<string> {\n if (this.#snapToJobMap.has(snapData.snapId)) {\n throw new Error(`Snap \"${snapData.snapId}\" is already being executed.`);\n }\n\n const job = await this.initJob();\n this.#mapSnapAndJob(snapData.snapId, job.id);\n\n // Ping the worker to ensure that it started up\n await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n });\n\n const rpcStream = job.streams.rpc as unknown as Duplex;\n\n this.setupSnapProvider(snapData.snapId, rpcStream);\n\n const result = await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: snapData,\n id: nanoid(),\n });\n this.#createSnapHooks(snapData.snapId, job.id);\n return result as string;\n }\n\n // Cannot be hash private yet because of tests.\n private async command(\n jobId: string,\n message: JsonRpcRequest<unknown>,\n ): Promise<unknown> {\n if (typeof message !== 'object') {\n throw new Error('Must send object.');\n }\n\n const job = this.jobs.get(jobId);\n if (!job) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\n }\n\n console.log('Parent: Sending Command', message);\n const response: PendingJsonRpcResponse<unknown> =\n await job.rpcEngine.handle(message);\n if (response.error) {\n throw new Error(response.error.message);\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,\n target: snapId,\n },\n });\n };\n\n this.#snapRpcHooks.set(snapId, rpcHook);\n }\n\n /**\n * Gets the job id for a given snap.\n *\n * @param snapId - A given snap id.\n * @returns The ID of the snap's job.\n */\n #getJobForSnap(snapId: string): string | undefined {\n return this.#snapToJobMap.get(snapId);\n }\n\n /**\n * Gets the snap id for a given job.\n *\n * @param jobId - A given job id.\n * @returns The ID of the snap that is running the job.\n */\n #getSnapForJob(jobId: string): string | undefined {\n return this.#jobToSnapMap.get(jobId);\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 = await 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 pump(\n connectionStream,\n // Typecast: stream type mismatch\n mux as unknown as Duplex,\n connectionStream,\n (error) => {\n if (error) {\n streamName\n ? console.error(`\"${streamName}\" stream failure.`, error)\n : console.error(error);\n }\n },\n );\n return mux;\n}\n"]}
1
+ {"version":3,"file":"AbstractExecutionService.js","sourceRoot":"","sources":["../../src/services/AbstractExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,kFAAyD;AAEzD,uDAI+B;AAC/B,2CAMyB;AACzB,qDAKyB;AACzB,2EAAoE;AACpE,mCAAgC;AAChC,gDAAwB;AAGxB,oCAAoD;AAQpD,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAuB1C,MAAsB,wBAAwB;IAmB5C,YAAY,EACV,iBAAiB,EACjB,SAAS,EACT,kBAAkB,GAAG,gBAAQ,CAAC,MAAM,GACf;;QApBvB,yDAAwC;QAQxC,yDAAmC;QAEnC,yDAAmC;QAEnC,sDAAsC;QAEtC,+DAA4B;QAO1B,uBAAA,IAAI,0CAA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C;IACjC,uBAAA,IAAI,8CAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAClC,uBAAA,IAAI,sFAAiB,MAArB,IAAI,EAAkB,MAAM,CAAC,CAAC;AAChC,CAAC;AAyBH;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,gBAAwB,EACxB,UAAkB;IAElB,MAAM,GAAG,GAAG,IAAI,0BAAe,EAAE,CAAC;IAClC,IAAA,cAAI,EACF,gBAAgB;IAChB,iCAAiC;IACjC,GAAwB,EACxB,gBAAgB,EAChB,CAAC,KAAK,EAAE,EAAE;QACR,IAAI,KAAK,EAAE;YACT,UAAU;gBACR,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,UAAU,mBAAmB,EAAE,KAAK,CAAC;gBACzD,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC1B;IACH,CAAC,CACF,CAAC;IACF,OAAO,GAAG,CAAC;AACb,CAAC;AAnBD,wCAmBC","sourcesContent":["import ObjectMultiplex from '@metamask/object-multiplex';\nimport { BasePostMessageStream } from '@metamask/post-message-stream';\nimport {\n SnapRpcHook,\n SnapRpcHookArgs,\n SNAP_STREAM_NAMES,\n} from '@metamask/snaps-utils';\nimport {\n Duration,\n isJsonRpcNotification,\n isObject,\n Json,\n JsonRpcNotification,\n} from '@metamask/utils';\nimport {\n JsonRpcEngine,\n // TODO: Replace with @metamask/utils version after bumping json-rpc-engine\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from 'json-rpc-engine';\nimport { createStreamMiddleware } from 'json-rpc-middleware-stream';\nimport { nanoid } from 'nanoid';\nimport pump from 'pump';\nimport { Duplex } from 'stream';\n\nimport { hasTimedOut, withTimeout } from '../utils';\nimport {\n ExecutionService,\n ExecutionServiceMessenger,\n SnapErrorJson,\n SnapExecutionData,\n} from './ExecutionService';\n\nconst controllerName = 'ExecutionService';\n\nexport type SetupSnapProvider = (snapId: string, stream: Duplex) => void;\n\nexport type ExecutionServiceArgs = {\n setupSnapProvider: SetupSnapProvider;\n messenger: ExecutionServiceMessenger;\n terminationTimeout?: number;\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 abstract class AbstractExecutionService<WorkerType>\n implements ExecutionService\n{\n #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 private readonly setupSnapProvider: SetupSnapProvider;\n\n #snapToJobMap: Map<string, string>;\n\n #jobToSnapMap: Map<string, string>;\n\n #messenger: ExecutionServiceMessenger;\n\n #terminationTimeout: number;\n\n constructor({\n setupSnapProvider,\n messenger,\n terminationTimeout = Duration.Second,\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.#terminationTimeout = terminationTimeout;\n\n this.registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering the controller's messaging system\n * actions.\n */\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 (snapData: SnapExecutionData) => this.executeSnap(snapData),\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: Job<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 // 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 console.error(`Job \"${jobId}\" failed to terminate gracefully.`, result);\n }\n\n Object.values(jobWrapper.streams).forEach((stream) => {\n try {\n !stream.destroyed && stream.destroy();\n stream.removeAllListeners();\n } catch (error) {\n console.error('Error while destroying stream', error);\n }\n });\n\n this.terminateJob(jobWrapper);\n\n this.#removeSnapAndJobMapping(jobId);\n this.jobs.delete(jobId);\n console.log(`Job \"${jobId}\" terminated.`);\n }\n\n /**\n * Initiates a job for a snap.\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 * @returns Information regarding the created job.\n */\n protected async initJob(): Promise<Job<WorkerType>> {\n const jobId = nanoid();\n const { streams, worker } = await this.initStreams(jobId);\n const rpcEngine = new JsonRpcEngine();\n\n const jsonRpcConnection = createStreamMiddleware();\n\n pump(jsonRpcConnection.stream, streams.command, jsonRpcConnection.stream);\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 * 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 * @param jobId - The id of the job.\n * @returns The streams to communicate with the worker and the worker itself.\n */\n protected async initStreams(\n jobId: string,\n ): Promise<{ streams: JobStreams; worker: WorkerType }> {\n const { worker, stream: envStream } = await this.initEnvStream(jobId);\n // Typecast justification: stream type mismatch\n const mux = setupMultiplex(\n envStream as unknown as Duplex,\n `Job: \"${jobId}\"`,\n );\n\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<unknown>\n | JsonRpcNotification<Json[] | Record<string, Json>>,\n ) => {\n if (!isJsonRpcNotification(message)) {\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 console.error(\n new Error(\n `Received malformed \"${message.method}\" command stream notification.`,\n ),\n );\n }\n } else {\n console.error(\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 as unknown as Duplex,\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 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 * 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 * @param snapData - Data needed for Snap execution.\n * @returns A string `OK` if execution succeeded.\n * @throws If the execution service returns an error.\n */\n async executeSnap(snapData: SnapExecutionData): Promise<string> {\n if (this.#snapToJobMap.has(snapData.snapId)) {\n throw new Error(`Snap \"${snapData.snapId}\" is already being executed.`);\n }\n\n const job = await this.initJob();\n this.#mapSnapAndJob(snapData.snapId, job.id);\n\n // Ping the worker to ensure that it started up\n await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'ping',\n id: nanoid(),\n });\n\n const rpcStream = job.streams.rpc as unknown as Duplex;\n\n this.setupSnapProvider(snapData.snapId, rpcStream);\n\n const result = await this.command(job.id, {\n jsonrpc: '2.0',\n method: 'executeSnap',\n params: snapData,\n id: nanoid(),\n });\n this.#createSnapHooks(snapData.snapId, job.id);\n return result as string;\n }\n\n // Cannot be hash private yet because of tests.\n private async command(\n jobId: string,\n message: JsonRpcRequest<unknown>,\n ): Promise<unknown> {\n if (typeof message !== 'object') {\n throw new Error('Must send object.');\n }\n\n const job = this.jobs.get(jobId);\n if (!job) {\n throw new Error(`Job with id \"${jobId}\" not found.`);\n }\n\n console.log('Parent: Sending Command', message);\n const response: PendingJsonRpcResponse<unknown> =\n await job.rpcEngine.handle(message);\n if (response.error) {\n throw new Error(response.error.message);\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,\n target: snapId,\n },\n });\n };\n\n this.#snapRpcHooks.set(snapId, rpcHook);\n }\n\n /**\n * Gets the job id for a given snap.\n *\n * @param snapId - A given snap id.\n * @returns The ID of the snap's job.\n */\n #getJobForSnap(snapId: string): string | undefined {\n return this.#snapToJobMap.get(snapId);\n }\n\n /**\n * Gets the snap id for a given job.\n *\n * @param jobId - A given job id.\n * @returns The ID of the snap that is running the job.\n */\n #getSnapForJob(jobId: string): string | undefined {\n return this.#jobToSnapMap.get(jobId);\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 = await 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 pump(\n connectionStream,\n // Typecast: stream type mismatch\n mux as unknown as Duplex,\n connectionStream,\n (error) => {\n if (error) {\n streamName\n ? console.error(`\"${streamName}\" stream failure.`, error)\n : console.error(error);\n }\n },\n );\n return mux;\n}\n"]}
@@ -1,3 +1,4 @@
1
1
  export * from './AbstractExecutionService';
2
2
  export * from './ExecutionService';
3
3
  export * from './iframe';
4
+ export * from './offscreen';
@@ -18,4 +18,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
18
18
  __exportStar(require("./AbstractExecutionService"), exports);
19
19
  __exportStar(require("./ExecutionService"), exports);
20
20
  __exportStar(require("./iframe"), exports);
21
+ __exportStar(require("./offscreen"), exports);
21
22
  //# sourceMappingURL=browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/services/browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2EAA2E;AAC3E,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB","sourcesContent":["// Subset of exports meant for browser environments, omits Node.js services\nexport * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\n"]}
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/services/browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2EAA2E;AAC3E,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB;AACzB,8CAA4B","sourcesContent":["// Subset of exports meant for browser environments, omits Node.js services\nexport * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\nexport * from './offscreen';\n"]}
@@ -11,15 +11,5 @@ export declare class IframeExecutionService extends AbstractExecutionService<Win
11
11
  worker: Window;
12
12
  stream: BasePostMessageStream;
13
13
  }>;
14
- /**
15
- * Creates the iframe to be used as the execution environment. This may run
16
- * forever if the iframe never loads, but the promise should be wrapped in
17
- * an initialization timeout in the SnapController.
18
- *
19
- * @param uri - The iframe URI.
20
- * @param jobId - The job id.
21
- * @returns A promise that resolves to the contentWindow of the iframe.
22
- */
23
- private createWindow;
24
14
  }
25
15
  export {};
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IframeExecutionService = void 0;
4
4
  const post_message_stream_1 = require("@metamask/post-message-stream");
5
+ const snaps_utils_1 = require("@metamask/snaps-utils");
5
6
  const AbstractExecutionService_1 = require("../AbstractExecutionService");
6
7
  class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
7
8
  constructor({ iframeUrl, messenger, setupSnapProvider, }) {
@@ -16,7 +17,7 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
16
17
  (_a = document.getElementById(jobWrapper.id)) === null || _a === void 0 ? void 0 : _a.remove();
17
18
  }
18
19
  async initEnvStream(jobId) {
19
- const iframeWindow = await this.createWindow(this.iframeUrl.toString(), jobId);
20
+ const iframeWindow = await (0, snaps_utils_1.createWindow)(this.iframeUrl.toString(), jobId);
20
21
  const stream = new post_message_stream_1.WindowPostMessageStream({
21
22
  name: 'parent',
22
23
  target: 'child',
@@ -25,58 +26,6 @@ class IframeExecutionService extends AbstractExecutionService_1.AbstractExecutio
25
26
  });
26
27
  return { worker: iframeWindow, stream };
27
28
  }
28
- /**
29
- * Creates the iframe to be used as the execution environment. This may run
30
- * forever if the iframe never loads, but the promise should be wrapped in
31
- * an initialization timeout in the SnapController.
32
- *
33
- * @param uri - The iframe URI.
34
- * @param jobId - The job id.
35
- * @returns A promise that resolves to the contentWindow of the iframe.
36
- */
37
- async createWindow(uri, jobId) {
38
- return new Promise((resolve, reject) => {
39
- const iframe = document.createElement('iframe');
40
- // The order of operations appears to matter for everything except this
41
- // attribute. We may as well set it here.
42
- iframe.setAttribute('id', jobId);
43
- // In the past, we've had problems that appear to be symptomatic of the
44
- // iframe firing the `load` event before its scripts are actually loaded,
45
- // which has prevented snaps from executing properly. Therefore, we set
46
- // the `src` attribute and append the iframe to the DOM before attaching
47
- // the `load` listener.
48
- //
49
- // `load` should only fire when "all dependent resources" have been
50
- // loaded, which includes scripts.
51
- //
52
- // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
53
- // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489
54
- iframe.setAttribute('src', uri);
55
- document.body.appendChild(iframe);
56
- iframe.addEventListener('load', () => {
57
- if (iframe.contentWindow) {
58
- resolve(iframe.contentWindow);
59
- }
60
- else {
61
- // We don't know of a case when this would happen, but better to fail
62
- // fast if it does.
63
- reject(new Error(`iframe.contentWindow not present on load for job "${jobId}".`));
64
- }
65
- });
66
- // We need to set the sandbox attribute after appending the iframe to the
67
- // DOM, otherwise errors in the iframe will not be propagated via `error`
68
- // and `unhandledrejection` events, and we cannot catch and handle them.
69
- // We wish we knew why this was the case.
70
- //
71
- // We set this property after adding the `load` listener because it
72
- // appears to work dependably. ¯\_(ツ)_/¯
73
- //
74
- // We apply this property as a principle of least authority (POLA)
75
- // measure.
76
- // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox
77
- iframe.setAttribute('sandbox', 'allow-scripts');
78
- });
79
- }
80
29
  }
81
30
  exports.IframeExecutionService = IframeExecutionService;
82
31
  //# sourceMappingURL=IframeExecutionService.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"IframeExecutionService.js","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;YACnG,iJAAiJ;YACjJ,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;YAChC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAElC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,IAAI,MAAM,CAAC,aAAa,EAAE;oBACxB,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;iBAC/B;qBAAM;oBACL,qEAAqE;oBACrE,mBAAmB;oBACnB,MAAM,CACJ,IAAI,KAAK,CACP,qDAAqD,KAAK,IAAI,CAC/D,CACF,CAAC;iBACH;YACH,CAAC,CAAC,CAAC;YAEH,yEAAyE;YACzE,yEAAyE;YACzE,wEAAwE;YACxE,yCAAyC;YACzC,EAAE;YACF,mEAAmE;YACnE,wCAAwC;YACxC,EAAE;YACF,kEAAkE;YAClE,WAAW;YACX,qFAAqF;YACrF,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA/FD,wDA+FC","sourcesContent":["import {\n WindowPostMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\n\nimport {\n Job,\n AbstractExecutionService,\n ExecutionServiceArgs,\n} from '../AbstractExecutionService';\n\ntype IframeExecutionEnvironmentServiceArgs = {\n iframeUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class IframeExecutionService extends AbstractExecutionService<Window> {\n public iframeUrl: URL;\n\n constructor({\n iframeUrl,\n messenger,\n setupSnapProvider,\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: Job<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await this.createWindow(\n this.iframeUrl.toString(),\n jobId,\n );\n const stream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: iframeWindow,\n targetOrigin: '*',\n });\n\n return { worker: iframeWindow, stream };\n }\n\n /**\n * Creates the iframe to be used as the execution environment. This may run\n * forever if the iframe never loads, but the promise should be wrapped in\n * an initialization timeout in the SnapController.\n *\n * @param uri - The iframe URI.\n * @param jobId - The job id.\n * @returns A promise that resolves to the contentWindow of the iframe.\n */\n private async createWindow(uri: string, jobId: string): Promise<Window> {\n return new Promise((resolve, reject) => {\n const iframe = document.createElement('iframe');\n // The order of operations appears to matter for everything except this\n // attribute. We may as well set it here.\n iframe.setAttribute('id', jobId);\n\n // In the past, we've had problems that appear to be symptomatic of the\n // iframe firing the `load` event before its scripts are actually loaded,\n // which has prevented snaps from executing properly. Therefore, we set\n // the `src` attribute and append the iframe to the DOM before attaching\n // the `load` listener.\n //\n // `load` should only fire when \"all dependent resources\" have been\n // loaded, which includes scripts.\n //\n // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event\n // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489\n iframe.setAttribute('src', uri);\n document.body.appendChild(iframe);\n\n iframe.addEventListener('load', () => {\n if (iframe.contentWindow) {\n resolve(iframe.contentWindow);\n } else {\n // We don't know of a case when this would happen, but better to fail\n // fast if it does.\n reject(\n new Error(\n `iframe.contentWindow not present on load for job \"${jobId}\".`,\n ),\n );\n }\n });\n\n // We need to set the sandbox attribute after appending the iframe to the\n // DOM, otherwise errors in the iframe will not be propagated via `error`\n // and `unhandledrejection` events, and we cannot catch and handle them.\n // We wish we knew why this was the case.\n //\n // We set this property after adding the `load` listener because it\n // appears to work dependably. ¯\\_(ツ)_/¯\n //\n // We apply this property as a principle of least authority (POLA)\n // measure.\n // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox\n iframe.setAttribute('sandbox', 'allow-scripts');\n });\n }\n}\n"]}
1
+ {"version":3,"file":"IframeExecutionService.js","sourceRoot":"","sources":["../../../src/services/iframe/IframeExecutionService.ts"],"names":[],"mappings":";;;AAAA,uEAGuC;AACvC,uDAAqD;AAErD,0EAIqC;AAMrC,MAAa,sBAAuB,SAAQ,mDAAgC;IAG1E,YAAY,EACV,SAAS,EACT,SAAS,EACT,iBAAiB,GACqB;QACtC,KAAK,CAAC;YACJ,SAAS;YACT,iBAAiB;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAES,YAAY,CAAC,UAAuB;;QAC5C,MAAA,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,0CAAE,MAAM,EAAE,CAAC;IACnD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,KAAa;QAIzC,MAAM,YAAY,GAAG,MAAM,IAAA,0BAAY,EAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAC;QAE1E,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;AAlCD,wDAkCC","sourcesContent":["import {\n WindowPostMessageStream,\n BasePostMessageStream,\n} from '@metamask/post-message-stream';\nimport { createWindow } from '@metamask/snaps-utils';\n\nimport {\n Job,\n AbstractExecutionService,\n ExecutionServiceArgs,\n} from '../AbstractExecutionService';\n\ntype IframeExecutionEnvironmentServiceArgs = {\n iframeUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class IframeExecutionService extends AbstractExecutionService<Window> {\n public iframeUrl: URL;\n\n constructor({\n iframeUrl,\n messenger,\n setupSnapProvider,\n }: IframeExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n this.iframeUrl = iframeUrl;\n }\n\n protected terminateJob(jobWrapper: Job<Window>): void {\n document.getElementById(jobWrapper.id)?.remove();\n }\n\n protected async initEnvStream(jobId: string): Promise<{\n worker: Window;\n stream: BasePostMessageStream;\n }> {\n const iframeWindow = await createWindow(this.iframeUrl.toString(), jobId);\n\n const stream = new WindowPostMessageStream({\n name: 'parent',\n target: 'child',\n targetWindow: iframeWindow,\n targetOrigin: '*',\n });\n\n return { worker: iframeWindow, stream };\n }\n}\n"]}
@@ -2,46 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  // Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745
4
4
  const fixJSDOMPostMessageEventSource = (iframeExecutionService) => {
5
- const oldCreateWindow = iframeExecutionService.createWindow;
6
- iframeExecutionService.createWindow = async (uri, envId, timeout) => {
7
- const result = await oldCreateWindow(uri, envId, timeout);
8
- const scriptElement = result.document.createElement('script');
9
- if (!scriptElement) {
10
- return result;
11
- }
12
- // fix the inside window
13
- scriptElement.textContent = `
14
- window.addEventListener('message', (postMessageEvent) => {
15
- if (postMessageEvent.source === null && !postMessageEvent.origin) {
16
- let source;
17
- let postMessageEventOrigin;
18
- if (postMessageEvent.data.target === 'child') {
19
- source = window.parent;
20
- postMessageEventOrigin = '*';
21
- } else if (postMessageEvent.data.target === 'parent') {
22
- source = window;
23
- postMessageEventOrigin = window.location.origin;
24
- }
25
- if (postMessageEvent.data.target) {
26
- postMessageEvent.stopImmediatePropagation();
27
- const args = Object.assign({
28
- ...postMessageEvent,
29
- data: postMessageEvent.data,
30
- source,
31
- origin: postMessageEventOrigin,
32
- });
33
- const postMessageEventWithOrigin = new MessageEvent(
34
- 'message',
35
- args,
36
- );
37
- window.dispatchEvent(postMessageEventWithOrigin);
38
- }
39
- }
40
- });
41
- `;
42
- result.document.body.appendChild(scriptElement);
43
- return result;
44
- };
45
5
  const listener = (event) => {
46
6
  if (event.source === null && !event.origin) {
47
7
  let source;
@@ -1 +1 @@
1
- {"version":3,"file":"fixJSDOMPostMessageEventSource.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/fixJSDOMPostMessageEventSource.ts"],"names":[],"mappings":";;AAEA,yGAAyG;AACzG,MAAM,8BAA8B,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,eAAe,GAAI,sBAA8B,CAAC,YAAY,CAAC;IACpE,sBAA8B,CAAC,YAAY,GAAG,KAAK,EAClD,GAAW,EACX,KAAa,EACb,OAAe,EACf,EAAE;QACF,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;QAE1D,MAAM,aAAa,GAAG,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAE9D,IAAI,CAAC,aAAa,EAAE;YAClB,OAAO,MAAM,CAAC;SACf;QAED,wBAAwB;QACxB,aAAa,CAAC,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4B7B,CAAC;QACA,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;QAEhD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,CAAC,KAAmB,EAAE,EAAE;QACvC,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;YAC1C,IAAI,MAAM,CAAC;YACX,IAAI,MAAM,CAAC;YACX,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;gBACjC,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;aACjC;iBAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;gBACzC,8CAA8C;gBAC9C,MAAM,EAAE,MAAM,EAAE,GAAG,sBAAsB,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;gBACrE,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;aACtD;YAED,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;gBACrB,KAAK,CAAC,wBAAwB,EAAE,CAAC;gBACjC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,iCACrB,KAAK,KACR,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM;oBACN,MAAM,IACN,CAAC;gBACH,MAAM,eAAe,GAAiB,IAAI,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACxE,MAAM,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;aACvC;SACF;IACH,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAE7C,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,8BAA8B,CAAC","sourcesContent":["import { IframeExecutionService } from '../IframeExecutionService';\n\n// Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745\nconst fixJSDOMPostMessageEventSource = (\n iframeExecutionService: IframeExecutionService,\n) => {\n const oldCreateWindow = (iframeExecutionService as any).createWindow;\n (iframeExecutionService as any).createWindow = async (\n uri: string,\n envId: string,\n timeout: number,\n ) => {\n const result = await oldCreateWindow(uri, envId, timeout);\n\n const scriptElement = result.document.createElement('script');\n\n if (!scriptElement) {\n return result;\n }\n\n // fix the inside window\n scriptElement.textContent = `\n window.addEventListener('message', (postMessageEvent) => {\n if (postMessageEvent.source === null && !postMessageEvent.origin) {\n let source;\n let postMessageEventOrigin;\n if (postMessageEvent.data.target === 'child') {\n source = window.parent;\n postMessageEventOrigin = '*';\n } else if (postMessageEvent.data.target === 'parent') {\n source = window;\n postMessageEventOrigin = window.location.origin;\n }\n if (postMessageEvent.data.target) {\n postMessageEvent.stopImmediatePropagation();\n const args = Object.assign({\n ...postMessageEvent,\n data: postMessageEvent.data,\n source,\n origin: postMessageEventOrigin,\n });\n const postMessageEventWithOrigin = new MessageEvent(\n 'message',\n args,\n );\n window.dispatchEvent(postMessageEventWithOrigin);\n }\n }\n });\n `;\n result.document.body.appendChild(scriptElement);\n\n return result;\n };\n\n const listener = (event: MessageEvent) => {\n if (event.source === null && !event.origin) {\n let source;\n let origin;\n if (event.data.target === 'child') {\n source = window;\n origin = window.location.origin;\n } else if (event.data.target === 'parent') {\n // @ts-expect-error Accessing private property\n const { worker } = iframeExecutionService.jobs.values().next().value;\n source = worker;\n origin = iframeExecutionService.iframeUrl.toString();\n }\n\n if (event.data.target) {\n event.stopImmediatePropagation();\n const args = Object.assign({\n ...event,\n data: event.data,\n source,\n origin,\n });\n const eventWithOrigin: MessageEvent = new MessageEvent('message', args);\n window.dispatchEvent(eventWithOrigin);\n }\n }\n };\n\n window.addEventListener('message', listener);\n\n return () => {\n window.removeEventListener('message', listener);\n };\n};\n\nexport default fixJSDOMPostMessageEventSource;\n"]}
1
+ {"version":3,"file":"fixJSDOMPostMessageEventSource.js","sourceRoot":"","sources":["../../../../src/services/iframe/test/fixJSDOMPostMessageEventSource.ts"],"names":[],"mappings":";;AAEA,yGAAyG;AACzG,MAAM,8BAA8B,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,QAAQ,GAAG,CAAC,KAAmB,EAAE,EAAE;QACvC,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;YAC1C,IAAI,MAAM,CAAC;YACX,IAAI,MAAM,CAAC;YACX,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;gBACjC,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;aACjC;iBAAM,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;gBACzC,8CAA8C;gBAC9C,MAAM,EAAE,MAAM,EAAE,GAAG,sBAAsB,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;gBACrE,MAAM,GAAG,MAAM,CAAC;gBAChB,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;aACtD;YAED,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;gBACrB,KAAK,CAAC,wBAAwB,EAAE,CAAC;gBACjC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,iCACrB,KAAK,KACR,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,MAAM;oBACN,MAAM,IACN,CAAC;gBACH,MAAM,eAAe,GAAiB,IAAI,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACxE,MAAM,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;aACvC;SACF;IACH,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAE7C,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,8BAA8B,CAAC","sourcesContent":["import { IframeExecutionService } from '../IframeExecutionService';\n\n// Fix for JSDOM not setting `event.origin` or `event.source`: https://github.com/jsdom/jsdom/issues/2745\nconst fixJSDOMPostMessageEventSource = (\n iframeExecutionService: IframeExecutionService,\n) => {\n const listener = (event: MessageEvent) => {\n if (event.source === null && !event.origin) {\n let source;\n let origin;\n if (event.data.target === 'child') {\n source = window;\n origin = window.location.origin;\n } else if (event.data.target === 'parent') {\n // @ts-expect-error Accessing private property\n const { worker } = iframeExecutionService.jobs.values().next().value;\n source = worker;\n origin = iframeExecutionService.iframeUrl.toString();\n }\n\n if (event.data.target) {\n event.stopImmediatePropagation();\n const args = Object.assign({\n ...event,\n data: event.data,\n source,\n origin,\n });\n const eventWithOrigin: MessageEvent = new MessageEvent('message', args);\n window.dispatchEvent(eventWithOrigin);\n }\n }\n };\n\n window.addEventListener('message', listener);\n\n return () => {\n window.removeEventListener('message', listener);\n };\n};\n\nexport default fixJSDOMPostMessageEventSource;\n"]}
@@ -2,3 +2,4 @@ export * from './AbstractExecutionService';
2
2
  export * from './ExecutionService';
3
3
  export * from './iframe';
4
4
  export * from './node';
5
+ export * from './offscreen';
@@ -18,4 +18,5 @@ __exportStar(require("./AbstractExecutionService"), exports);
18
18
  __exportStar(require("./ExecutionService"), exports);
19
19
  __exportStar(require("./iframe"), exports);
20
20
  __exportStar(require("./node"), exports);
21
+ __exportStar(require("./offscreen"), exports);
21
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB;AACzB,yCAAuB","sourcesContent":["export * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\nexport * from './node';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,qDAAmC;AACnC,2CAAyB;AACzB,yCAAuB;AACvB,8CAA4B","sourcesContent":["export * from './AbstractExecutionService';\nexport * from './ExecutionService';\nexport * from './iframe';\nexport * from './node';\nexport * from './offscreen';\n"]}
@@ -0,0 +1,49 @@
1
+ import { AbstractExecutionService, ExecutionServiceArgs, Job } from '../AbstractExecutionService';
2
+ import { OffscreenPostMessageStream } from './OffscreenPostMessageStream';
3
+ declare type OffscreenExecutionEnvironmentServiceArgs = {
4
+ documentUrl: URL;
5
+ frameUrl: URL;
6
+ } & ExecutionServiceArgs;
7
+ export declare class OffscreenExecutionService extends AbstractExecutionService<string> {
8
+ #private;
9
+ readonly documentUrl: URL;
10
+ readonly frameUrl: URL;
11
+ /**
12
+ * Create a new offscreen execution service.
13
+ *
14
+ * @param args - The constructor arguments.
15
+ * @param args.documentUrl - The URL of the offscreen document to use as the
16
+ * execution environment. This must be a URL relative to the location where
17
+ * this is called. This cannot be a public (http(s)) URL.
18
+ * @param args.frameUrl - The URL of the iframe to load inside the offscreen
19
+ * document.
20
+ * @param args.messenger - The messenger to use for communication with the
21
+ * `SnapController`.
22
+ * @param args.setupSnapProvider - The function to use to set up the snap
23
+ * provider.
24
+ */
25
+ constructor({ documentUrl, frameUrl, messenger, setupSnapProvider, }: OffscreenExecutionEnvironmentServiceArgs);
26
+ /**
27
+ * Send a termination command to the offscreen document.
28
+ *
29
+ * @param job - The job to terminate.
30
+ */
31
+ protected terminateJob(job: Job<string>): Promise<void>;
32
+ /**
33
+ * Create a new stream for the specified job. This wraps the runtime stream
34
+ * in a stream specific to the job.
35
+ *
36
+ * @param jobId - The job ID.
37
+ */
38
+ protected initEnvStream(jobId: string): Promise<{
39
+ worker: string;
40
+ stream: OffscreenPostMessageStream;
41
+ }>;
42
+ /**
43
+ * Creates the offscreen document to be used as the execution environment.
44
+ *
45
+ * If the document already exists, this does nothing.
46
+ */
47
+ private createDocument;
48
+ }
49
+ export {};
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _OffscreenExecutionService_runtimeStream;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OffscreenExecutionService = void 0;
16
+ const post_message_stream_1 = require("@metamask/post-message-stream");
17
+ const nanoid_1 = require("nanoid");
18
+ const AbstractExecutionService_1 = require("../AbstractExecutionService");
19
+ const OffscreenPostMessageStream_1 = require("./OffscreenPostMessageStream");
20
+ class OffscreenExecutionService extends AbstractExecutionService_1.AbstractExecutionService {
21
+ /**
22
+ * Create a new offscreen execution service.
23
+ *
24
+ * @param args - The constructor arguments.
25
+ * @param args.documentUrl - The URL of the offscreen document to use as the
26
+ * execution environment. This must be a URL relative to the location where
27
+ * this is called. This cannot be a public (http(s)) URL.
28
+ * @param args.frameUrl - The URL of the iframe to load inside the offscreen
29
+ * document.
30
+ * @param args.messenger - The messenger to use for communication with the
31
+ * `SnapController`.
32
+ * @param args.setupSnapProvider - The function to use to set up the snap
33
+ * provider.
34
+ */
35
+ constructor({ documentUrl, frameUrl, messenger, setupSnapProvider, }) {
36
+ super({
37
+ messenger,
38
+ setupSnapProvider,
39
+ });
40
+ _OffscreenExecutionService_runtimeStream.set(this, void 0);
41
+ this.documentUrl = documentUrl;
42
+ this.frameUrl = frameUrl;
43
+ __classPrivateFieldSet(this, _OffscreenExecutionService_runtimeStream, new post_message_stream_1.BrowserRuntimePostMessageStream({
44
+ name: 'parent',
45
+ target: 'child',
46
+ }), "f");
47
+ }
48
+ /**
49
+ * Send a termination command to the offscreen document.
50
+ *
51
+ * @param job - The job to terminate.
52
+ */
53
+ async terminateJob(job) {
54
+ // The `AbstractExecutionService` will have already closed the job stream,
55
+ // so we write to the runtime stream directly.
56
+ __classPrivateFieldGet(this, _OffscreenExecutionService_runtimeStream, "f").write({
57
+ jobId: job.id,
58
+ data: {
59
+ jsonrpc: '2.0',
60
+ method: 'terminateJob',
61
+ id: (0, nanoid_1.nanoid)(),
62
+ },
63
+ });
64
+ }
65
+ /**
66
+ * Create a new stream for the specified job. This wraps the runtime stream
67
+ * in a stream specific to the job.
68
+ *
69
+ * @param jobId - The job ID.
70
+ */
71
+ async initEnvStream(jobId) {
72
+ // Lazily create the offscreen document.
73
+ await this.createDocument();
74
+ const stream = new OffscreenPostMessageStream_1.OffscreenPostMessageStream({
75
+ stream: __classPrivateFieldGet(this, _OffscreenExecutionService_runtimeStream, "f"),
76
+ frameUrl: this.frameUrl.toString(),
77
+ jobId,
78
+ });
79
+ return { worker: jobId, stream };
80
+ }
81
+ /**
82
+ * Creates the offscreen document to be used as the execution environment.
83
+ *
84
+ * If the document already exists, this does nothing.
85
+ */
86
+ async createDocument() {
87
+ // Extensions can only have a single offscreen document.
88
+ if (await chrome.offscreen.hasDocument()) {
89
+ return;
90
+ }
91
+ await chrome.offscreen.createDocument({
92
+ justification: 'MetaMask Snaps Execution Environment',
93
+ reasons: ['IFRAME_SCRIPTING'],
94
+ url: this.documentUrl.toString(),
95
+ });
96
+ }
97
+ }
98
+ exports.OffscreenExecutionService = OffscreenExecutionService;
99
+ _OffscreenExecutionService_runtimeStream = new WeakMap();
100
+ //# sourceMappingURL=OffscreenExecutionService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OffscreenExecutionService.js","sourceRoot":"","sources":["../../../src/services/offscreen/OffscreenExecutionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uEAAgF;AAChF,mCAAgC;AAEhC,0EAIqC;AACrC,6EAA0E;AAO1E,MAAa,yBAA0B,SAAQ,mDAAgC;IAO7E;;;;;;;;;;;;;OAaG;IACH,YAAY,EACV,WAAW,EACX,QAAQ,EACR,SAAS,EACT,iBAAi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WAAW,CAAC,QAAQ,EAAE;SACjC,CAAC,CAAC;IACL,CAAC;CACF;AA9FD,8DA8FC","sourcesContent":["import { BrowserRuntimePostMessageStream } from '@metamask/post-message-stream';\nimport { nanoid } from 'nanoid';\n\nimport {\n AbstractExecutionService,\n ExecutionServiceArgs,\n Job,\n} from '../AbstractExecutionService';\nimport { OffscreenPostMessageStream } from './OffscreenPostMessageStream';\n\ntype OffscreenExecutionEnvironmentServiceArgs = {\n documentUrl: URL;\n frameUrl: URL;\n} & ExecutionServiceArgs;\n\nexport class OffscreenExecutionService extends AbstractExecutionService<string> {\n public readonly documentUrl: URL;\n\n public readonly frameUrl: URL;\n\n readonly #runtimeStream: BrowserRuntimePostMessageStream;\n\n /**\n * Create a new offscreen execution service.\n *\n * @param args - The constructor arguments.\n * @param args.documentUrl - The URL of the offscreen document to use as the\n * execution environment. This must be a URL relative to the location where\n * this is called. This cannot be a public (http(s)) URL.\n * @param args.frameUrl - The URL of the iframe to load inside the offscreen\n * document.\n * @param args.messenger - The messenger to use for communication with the\n * `SnapController`.\n * @param args.setupSnapProvider - The function to use to set up the snap\n * provider.\n */\n constructor({\n documentUrl,\n frameUrl,\n messenger,\n setupSnapProvider,\n }: OffscreenExecutionEnvironmentServiceArgs) {\n super({\n messenger,\n setupSnapProvider,\n });\n\n this.documentUrl = documentUrl;\n this.frameUrl = frameUrl;\n this.#runtimeStream = new BrowserRuntimePostMessageStream({\n name: 'parent',\n target: 'child',\n });\n }\n\n /**\n * Send a termination command to the offscreen document.\n *\n * @param job - The job to terminate.\n */\n protected async terminateJob(job: Job<string>) {\n // The `AbstractExecutionService` will have already closed the job stream,\n // so we write to the runtime stream directly.\n this.#runtimeStream.write({\n jobId: job.id,\n data: {\n jsonrpc: '2.0',\n method: 'terminateJob',\n id: nanoid(),\n },\n });\n }\n\n /**\n * Create a new stream for the specified job. This wraps the runtime stream\n * in a stream specific to the job.\n *\n * @param jobId - The job ID.\n */\n protected async initEnvStream(jobId: string) {\n // Lazily create the offscreen document.\n await this.createDocument();\n\n const stream = new OffscreenPostMessageStream({\n stream: this.#runtimeStream,\n frameUrl: this.frameUrl.toString(),\n jobId,\n });\n\n return { worker: jobId, stream };\n }\n\n /**\n * Creates the offscreen document to be used as the execution environment.\n *\n * If the document already exists, this does nothing.\n */\n private async createDocument() {\n // Extensions can only have a single offscreen document.\n if (await chrome.offscreen.hasDocument()) {\n return;\n }\n\n await chrome.offscreen.createDocument({\n justification: 'MetaMask Snaps Execution Environment',\n reasons: ['IFRAME_SCRIPTING'],\n url: this.documentUrl.toString(),\n });\n }\n}\n"]}
@@ -0,0 +1,35 @@
1
+ import { BasePostMessageStream } from '@metamask/post-message-stream';
2
+ import { JsonRpcRequest } from '@metamask/utils';
3
+ export declare type OffscreenPostMessageStreamArgs = {
4
+ stream: BasePostMessageStream;
5
+ jobId: string;
6
+ frameUrl: string;
7
+ };
8
+ export declare type OffscreenPostMessage = {
9
+ jobId: string;
10
+ data: JsonRpcRequest;
11
+ };
12
+ /**
13
+ * A post message stream that wraps messages in a job ID, before sending them
14
+ * over the underlying stream.
15
+ */
16
+ export declare class OffscreenPostMessageStream extends BasePostMessageStream {
17
+ #private;
18
+ /**
19
+ * Initializes a new `OffscreenPostMessageStream` instance.
20
+ *
21
+ * @param args - The constructor arguments.
22
+ * @param args.stream - The underlying stream to use for communication.
23
+ * @param args.jobId - The ID of the job this stream is associated with.
24
+ * @param args.frameUrl - The URL of the frame to load inside the offscreen
25
+ * document.
26
+ */
27
+ constructor({ stream, jobId, frameUrl }: OffscreenPostMessageStreamArgs);
28
+ /**
29
+ * Write data to the underlying stream. This wraps the data in an object with
30
+ * the job ID.
31
+ *
32
+ * @param data - The data to write.
33
+ */
34
+ _postMessage(data: OffscreenPostMessage): void;
35
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _OffscreenPostMessageStream_instances, _OffscreenPostMessageStream_stream, _OffscreenPostMessageStream_jobId, _OffscreenPostMessageStream_frameUrl, _OffscreenPostMessageStream_onData;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OffscreenPostMessageStream = void 0;
16
+ const post_message_stream_1 = require("@metamask/post-message-stream");
17
+ /**
18
+ * A post message stream that wraps messages in a job ID, before sending them
19
+ * over the underlying stream.
20
+ */
21
+ class OffscreenPostMessageStream extends post_message_stream_1.BasePostMessageStream {
22
+ /**
23
+ * Initializes a new `OffscreenPostMessageStream` instance.
24
+ *
25
+ * @param args - The constructor arguments.
26
+ * @param args.stream - The underlying stream to use for communication.
27
+ * @param args.jobId - The ID of the job this stream is associated with.
28
+ * @param args.frameUrl - The URL of the frame to load inside the offscreen
29
+ * document.
30
+ */
31
+ constructor({ stream, jobId, frameUrl }) {
32
+ super();
33
+ _OffscreenPostMessageStream_instances.add(this);
34
+ _OffscreenPostMessageStream_stream.set(this, void 0);
35
+ _OffscreenPostMessageStream_jobId.set(this, void 0);
36
+ _OffscreenPostMessageStream_frameUrl.set(this, void 0);
37
+ __classPrivateFieldSet(this, _OffscreenPostMessageStream_stream, stream, "f");
38
+ __classPrivateFieldSet(this, _OffscreenPostMessageStream_jobId, jobId, "f");
39
+ __classPrivateFieldSet(this, _OffscreenPostMessageStream_frameUrl, frameUrl, "f");
40
+ __classPrivateFieldGet(this, _OffscreenPostMessageStream_stream, "f").on('data', __classPrivateFieldGet(this, _OffscreenPostMessageStream_instances, "m", _OffscreenPostMessageStream_onData).bind(this));
41
+ }
42
+ /**
43
+ * Write data to the underlying stream. This wraps the data in an object with
44
+ * the job ID.
45
+ *
46
+ * @param data - The data to write.
47
+ */
48
+ _postMessage(data) {
49
+ __classPrivateFieldGet(this, _OffscreenPostMessageStream_stream, "f").write({
50
+ jobId: __classPrivateFieldGet(this, _OffscreenPostMessageStream_jobId, "f"),
51
+ // TODO: Rather than injecting the frame URL here, we should come up with
52
+ // a better way to do this. The frame URL is needed to avoid hard coding
53
+ // it in the offscreen execution environment.
54
+ frameUrl: __classPrivateFieldGet(this, _OffscreenPostMessageStream_frameUrl, "f"),
55
+ data,
56
+ });
57
+ }
58
+ }
59
+ exports.OffscreenPostMessageStream = OffscreenPostMessageStream;
60
+ _OffscreenPostMessageStream_stream = new WeakMap(), _OffscreenPostMessageStream_jobId = new WeakMap(), _OffscreenPostMessageStream_frameUrl = new WeakMap(), _OffscreenPostMessageStream_instances = new WeakSet(), _OffscreenPostMessageStream_onData = function _OffscreenPostMessageStream_onData(data) {
61
+ if (data.jobId !== __classPrivateFieldGet(this, _OffscreenPostMessageStream_jobId, "f")) {
62
+ return;
63
+ }
64
+ this.push(data.data);
65
+ };
66
+ //# sourceMappingURL=OffscreenPostMessageStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OffscreenPostMessageStream.js","sourceRoot":"","sources":["../../../src/services/offscreen/OffscreenPostMessageStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uEAAsE;AActE;;;GAGG;AACH,MAAa,0BAA2B,SAAQ,2CAAqB;IAOnE;;;;;;;;OAQG;IACH,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAkC;QACrE,KAAK,EAAE,CAAC;;QAhBV,qDAAwC;QAExC,oDAAwB;QAExB,uDAA2B;QAczB,uBAAA,IAAI,sCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,qCAAU,KAAK,MAAA,CAAC;QACpB,uBAAA,IAAI,wCAAa,QAAQ,MAAA,CAAC;QAE1B,uBAAA,IAAI,0CAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,uBAAA,IAAI,iFAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnD,CAAC;IAgBD;;;;;OAKG;IACH,YAAY,CAAC,IAA0B;QACrC,uBAAA,IAAI,0CAAQ,CAAC,KAAK,CAAC;YACjB,KAAK,EAAE,uBAAA,IAAI,yCAAO;YAClB,yEAAyE;YACzE,wEAAwE;YACxE,6CAA6C;YAC7C,QAAQ,EAAE,uBAAA,IAAI,4CAAU;YACxB,IAAI;SACL,CAAC,CAAC;IACL,CAAC;CACF;AAxDD,gEAwDC;qSAxBS,IAA0B;IAChC,IAAI,IAAI,CAAC,KAAK,KAAK,uBAAA,IAAI,yCAAO,EAAE;QAC9B,OAAO;KACR;IAED,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import { BasePostMessageStream } from '@metamask/post-message-stream';\nimport { JsonRpcRequest } from '@metamask/utils';\n\nexport type OffscreenPostMessageStreamArgs = {\n stream: BasePostMessageStream;\n jobId: string;\n frameUrl: string;\n};\n\nexport type OffscreenPostMessage = {\n jobId: string;\n data: JsonRpcRequest;\n};\n\n/**\n * A post message stream that wraps messages in a job ID, before sending them\n * over the underlying stream.\n */\nexport class OffscreenPostMessageStream extends BasePostMessageStream {\n readonly #stream: BasePostMessageStream;\n\n readonly #jobId: string;\n\n readonly #frameUrl: string;\n\n /**\n * Initializes a new `OffscreenPostMessageStream` instance.\n *\n * @param args - The constructor arguments.\n * @param args.stream - The underlying stream to use for communication.\n * @param args.jobId - The ID of the job this stream is associated with.\n * @param args.frameUrl - The URL of the frame to load inside the offscreen\n * document.\n */\n constructor({ stream, jobId, frameUrl }: OffscreenPostMessageStreamArgs) {\n super();\n\n this.#stream = stream;\n this.#jobId = jobId;\n this.#frameUrl = frameUrl;\n\n this.#stream.on('data', this.#onData.bind(this));\n }\n\n /**\n * Handle incoming data from the underlying stream. This checks that the job\n * ID matches the expected job ID, and pushes the data to the stream if so.\n *\n * @param data - The data to handle.\n */\n #onData(data: OffscreenPostMessage) {\n if (data.jobId !== this.#jobId) {\n return;\n }\n\n this.push(data.data);\n }\n\n /**\n * Write data to the underlying stream. This wraps the data in an object with\n * the job ID.\n *\n * @param data - The data to write.\n */\n _postMessage(data: OffscreenPostMessage) {\n this.#stream.write({\n jobId: this.#jobId,\n // TODO: Rather than injecting the frame URL here, we should come up with\n // a better way to do this. The frame URL is needed to avoid hard coding\n // it in the offscreen execution environment.\n frameUrl: this.#frameUrl,\n data,\n });\n }\n}\n"]}