Skip to content

Commit

Permalink
Fix ESM module import to use dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jul 5, 2024
1 parent 873c0fe commit 5fd45b5
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions packages/snaps-utils/src/eval-worker.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand Down

0 comments on commit 5fd45b5

Please sign in to comment.