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ACpB,IAAI,EAAE;YACJ,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;SAC9B;KACF,CAAC,CAAC;IAEH,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,CAC3D,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,iBAAiB,CAAC,QAAQ,CAAC,UAAyB,CAAC,CACvE,CAAC;IAEF,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC9B,sCAAsC;QACtC,OAAO,CAAC,IAAI,CACV,mCAAmC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC/D,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,8DAA8D;IAC9D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC;AAED,IAAI,UAAU,CAAC,OAAO,YAAY,OAAO,EAAE,CAAC;IAC1C,wEAAwE;IACxE,oBAAoB;IACpB,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,KAAK,CAAC,CAAC,KAAY,EAAE,EAAE;QAC3D,sCAAsC;QACtC,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;QACnD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC;KAAM,CAAC;IACN,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC","sourcesContent":["// eslint-disable-next-line import-x/no-unassigned-import\nimport 'ses/lockdown';\n\nimport { assert } from '@metamask/utils';\nimport { readFileSync } from 'fs';\n\nimport type { HandlerType } from './handlers';\nimport { SNAP_EXPORT_NAMES } from './handlers';\nimport { generateMockEndowments } from './mock';\n\nlockdown({\n  errorTaming: 'unsafe',\n  stackFiltering: 'verbose',\n  overrideTaming: 'severe',\n  localeTaming: 'unsafe',\n\n  // We disable domain taming, because it does not work in certain cases when\n  // running tests. This is unlikely to be a problem in production, because\n  // Node.js domains are deprecated.\n  domainTaming: 'unsafe',\n});\n\nconst snapFilePath = process.argv[2];\n\nconst snapModule: { exports?: any } = { exports: {} };\n\nconst compartment = new Compartment({\n  ...generateMockEndowments(),\n  module: snapModule,\n  exports: snapModule.exports,\n});\n\n// Add self referential properties for compatibility with 3rd party libraries.\n// This mirrors the implementation in the Snaps execution environment.\ncompartment.globalThis.self = compartment.globalThis;\ncompartment.globalThis.global = compartment.globalThis;\ncompartment.globalThis.window = compartment.globalThis;\n\ncompartment.evaluate(readFileSync(snapFilePath, 'utf8'));\n\n/**\n * Check the exports of the Snap module to ensure they are valid, and exit the\n * worker process.\n *\n * @param exports - The exports of the Snap module.\n */\nfunction checkExports(exports: any) {\n  // eslint-disable-next-line @typescript-eslint/unbound-method\n  assert(process.send, 'This script must be run as a child process.');\n\n  process.send({\n    type: 'snap-exports',\n    data: {\n      exports: Object.keys(exports),\n    },\n  });\n\n  const invalidExports = Object.keys(snapModule.exports).filter(\n    (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),\n  );\n\n  if (invalidExports.length > 0) {\n    // eslint-disable-next-line no-console\n    console.warn(\n      `Invalid snap exports detected:\\n${invalidExports.join('\\n')}`,\n    );\n  }\n\n  // To ensure the worker exits we explicitly call exit here. If we didn't, the\n  // worker would wait for timers set during `Compartment` eval.\n  process.exit(0);\n}\n\nif (snapModule.exports instanceof Promise) {\n  // The Snap may use async logic (e.g., when loading WASM), so we need to\n  // handle that case.\n  snapModule.exports.then(checkExports).catch((error: Error) => {\n    // eslint-disable-next-line no-console\n    console.error('Error loading Snap module:', error);\n    process.exit(1);\n  });\n} else {\n  checkExports(snapModule.exports);\n}\n"]}