From 1c621d57690a2076486479f7e04b05299b19fca5 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Mon, 24 Jun 2024 20:47:59 -0400 Subject: [PATCH] Fix ESM module import to use dynamic import --- packages/snaps-utils/src/eval-worker.ts | 64 ++++++++++++++----------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/packages/snaps-utils/src/eval-worker.ts b/packages/snaps-utils/src/eval-worker.ts index 5c7fa4dc2f..5b23ae3b8b 100644 --- a/packages/snaps-utils/src/eval-worker.ts +++ b/packages/snaps-utils/src/eval-worker.ts @@ -1,43 +1,49 @@ -// eslint-disable-next-line import/no-unassigned-import -import 'ses/lockdown'; - import { readFileSync } from 'fs'; import type { HandlerType } from './handler-types'; import { SNAP_EXPORT_NAMES } from './handler-types'; import { generateMockEndowments } from './mock'; -declare let lockdown: any, Compartment: any; - -lockdown({ - consoleTaming: 'unsafe', - errorTaming: 'unsafe', - mathTaming: 'unsafe', - dateTaming: 'unsafe', - overrideTaming: 'severe', - - // We disable domain taming, because it does not work in certain cases when - // running tests. This is unlikely to be a problem in production, because - // Node.js domains are deprecated. - domainTaming: 'unsafe', -}); +import('ses/lockdown') + .then(() => + lockdown({ + consoleTaming: 'unsafe', + errorTaming: 'unsafe', + mathTaming: 'unsafe', + dateTaming: 'unsafe', + overrideTaming: 'severe', + + // We disable domain taming, because it does not work in certain cases when + // running tests. This is unlikely to be a problem in production, because + // Node.js domains are deprecated. + domainTaming: 'unsafe', + }), + ) + .catch((error) => { + throw error; + }); const snapFilePath = process.argv[2]; const snapModule: { exports?: any } = { exports: {} }; -const compartment = new Compartment({ - ...generateMockEndowments(), - module: snapModule, - exports: snapModule.exports, -}); - -// Mirror BaseSnapExecutor -compartment.globalThis.self = compartment.globalThis; -compartment.globalThis.global = compartment.globalThis; -compartment.globalThis.window = compartment.globalThis; - -compartment.evaluate(readFileSync(snapFilePath, 'utf8')); +import('ses/lockdown') + .then(() => { + const compartment = new Compartment({ + ...generateMockEndowments(), + module: snapModule, + exports: snapModule.exports, + }); + // Mirror BaseSnapExecutor + compartment.globalThis.self = compartment.globalThis; + compartment.globalThis.global = compartment.globalThis; + compartment.globalThis.window = compartment.globalThis; + + compartment.evaluate(readFileSync(snapFilePath, 'utf8')); + }) + .catch((error) => { + throw error; + }); const invalidExports = Object.keys(snapModule.exports).filter( (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),