diff --git a/packages/extension/src/endoify.test.ts b/packages/extension/src/endoify.test.ts new file mode 100644 index 000000000..fae8b293b --- /dev/null +++ b/packages/extension/src/endoify.test.ts @@ -0,0 +1,31 @@ +import './endoify.js'; +// eslint-disable-next-line n/no-extraneous-import +import type { HandledPromiseConstructor } from '@endo/eventual-send'; +import { describe, expect, it } from 'vitest'; + +describe(`endoify`, () => { + const assertions = [ + (): boolean => typeof globalThis === 'object', + (): boolean => typeof lockdown === 'function', + (): boolean => typeof repairIntrinsics === 'function', + (): boolean => typeof Compartment === 'function', + (): boolean => typeof assert === 'function', + (): boolean => typeof HandledPromise === 'function', + (): boolean => typeof harden === 'function', + (): boolean => typeof getStackString === 'function', + (): boolean => Object.isFrozen(Array.prototype), + ]; + + for (const assertion of assertions) { + it(`asserts ${String(assertion).replace(/^.*?=>\s*/u, '')}`, () => { + expect(assertion()).toBe(true); + }); + } +}); + +declare global { + // eslint-disable-next-line no-var + var getStackString: (error: Error) => string; + // eslint-disable-next-line no-var + var HandledPromise: HandledPromiseConstructor; +} diff --git a/packages/extension/src/endoify.ts b/packages/extension/src/endoify.ts new file mode 100644 index 000000000..11479b6c9 --- /dev/null +++ b/packages/extension/src/endoify.ts @@ -0,0 +1,3 @@ +/* eslint-disable import-x/extensions */ +/* eslint-disable import-x/no-unassigned-import */ +import '@ocap/shims/endoify'; diff --git a/packages/extension/src/iframe-manager.test.ts b/packages/extension/src/iframe-manager.test.ts index 6c3d4c9a1..b8dd6ef16 100644 --- a/packages/extension/src/iframe-manager.test.ts +++ b/packages/extension/src/iframe-manager.test.ts @@ -1,3 +1,4 @@ +import './endoify.js'; import * as snapsUtils from '@metamask/snaps-utils'; import { delay, makePromiseKitMock } from '@ocap/test-utils'; import { vi, describe, it, expect } from 'vitest'; diff --git a/packages/extension/src/shared.test.ts b/packages/extension/src/shared.test.ts index 2412a8716..d10870d46 100644 --- a/packages/extension/src/shared.test.ts +++ b/packages/extension/src/shared.test.ts @@ -1,3 +1,4 @@ +import './endoify.js'; import { delay } from '@ocap/test-utils'; import { vi, describe, it, expect } from 'vitest'; diff --git a/packages/extension/vite.config.ts b/packages/extension/vite.config.ts index 73c446442..e50397ff1 100644 --- a/packages/extension/vite.config.ts +++ b/packages/extension/vite.config.ts @@ -72,20 +72,28 @@ function endoifyHtmlFilesPlugin(): Plugin { return { name: 'externalize-plugin', async transformIndexHtml(htmlString): Promise { - if (htmlString.includes('endoify.js')) { + const htmlDoc = loadHtml(htmlString); + + if (htmlDoc('script[src="endoify.ts"]').length > 0) { + throw new Error( + `HTML document should not reference "endoify.ts" directly:\n${htmlString}`, + ); + } + + if (htmlDoc('script[src="endoify.js"]').length > 0) { throw new Error( `HTML document already references endoify script:\n${htmlString}`, ); } - const htmlDoc = loadHtml(htmlString); - if (htmlDoc('head').length !== 1 || htmlDoc('head script').length < 1) { + if (htmlDoc('head').length !== 1 || htmlDoc('head > script').length < 1) { throw new Error( `Expected HTML document with a single containing at least one