Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/extension/src/endoify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import endoified from '@ocap/test-utils/endoified';
import { describe } from 'vitest';
import * as vitest from 'vitest';

/* eslint-disable vitest/valid-describe-callback */
describe(
`endoify`,
endoified(
// eslint-disable-next-line @typescript-eslint/no-shadow
({ it, expect }) => {
for (const assertion of [
() => typeof globalThis === 'object',
() => typeof lockdown === 'function',
() => typeof repairIntrinsics === 'function',
() => typeof Compartment === 'function',
() => typeof assert === 'function',
() => typeof HandledPromise === 'function',
() => typeof harden === 'function',
() => typeof getStackString === 'function',
() => {
try {
return !Object.assign(harden({ a: 1 }), { b: 2 });
} catch {
return true;
}
},
]) {
it(`asserts ${String(assertion)}`, () => {
console.log(`${String(assertion)} => ${assertion()}`);
expect(assertion(), `${String(assertion)}`).toBe(true);
});
}
},
{ ...vitest },
),
);

declare global {
// eslint-disable-next-line no-var
var getStackString: (error: Error) => string;
// eslint-disable-next-line no-var, @typescript-eslint/consistent-type-imports
var HandledPromise: import('@endo/eventual-send').HandledPromiseConstructor;
}
3 changes: 0 additions & 3 deletions packages/extension/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ const staticCopyTargets: readonly string[] = [
// External modules
'dev-console.js',
'../../shims/dist/endoify.mjs',
// Dependencies of external modules
'../../shims/dist/eventual-send.mjs',
'../../../node_modules/ses/dist/ses.mjs',
];

// https://vitejs.dev/config/
Expand Down
54 changes: 34 additions & 20 deletions packages/shims/scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
// eslint-disable-next-line import-x/no-unassigned-import
import 'ses';
// eslint-disable-next-line import-x/no-unassigned-import, import-x/extensions
import '@endo/lockdown/commit.js';

import bundleSource from '@endo/bundle-source';
import { copyFile, mkdir, writeFile } from 'fs/promises';
import { mkdir, writeFile } from 'fs/promises';
import path from 'path';
import { rimraf } from 'rimraf';
import { fileURLToPath } from 'url';

console.log('Bundling shims...');

const rootDir = path.resolve(import.meta.dirname, '..');
const rootDir = fileURLToPath(new URL('..', import.meta.url));
const src = path.resolve(rootDir, 'src');
const dist = path.resolve(rootDir, 'dist');

await mkdir(dist, { recursive: true });
await rimraf(`${dist}/*`, { glob: true });

const fileNames = {
endoify: 'endoify.mjs',
eventualSend: 'eventual-send.mjs',
applyLockdown: 'apply-lockdown.mjs',
};

await mkdir(dist, { recursive: true });
await rimraf(`${dist}/*`);
const { source: endoifyBundleSource } = await bundleSource(
path.resolve(src, fileNames.endoify),
{ format: 'endoScript' },
);

for (const fileName of [fileNames.endoify, fileNames.applyLockdown]) {
await copyFile(path.resolve(src, fileName), path.resolve(dist, fileName));
}
await writeFile(path.resolve(dist, fileNames.endoify), endoifyBundleSource);

const eventualSendSourcePath = fileURLToPath(
import.meta.resolve('@endo/eventual-send/shim.js'),
);
// const fileNames = {
// endoify: 'endoify.mjs',
// eventualSend: 'eventual-send.mjs',
// applyLockdown: 'apply-lockdown.mjs',
// };

const { source: eventualSendBundleSource } = await bundleSource(
eventualSendSourcePath,
{ format: 'endoScript' },
);
// for (const fileName of [fileNames.endoify, fileNames.applyLockdown]) {
// await copyFile(path.resolve(src, fileName), path.resolve(dist, fileName));
// }

await writeFile(
path.resolve(dist, fileNames.eventualSend),
eventualSendBundleSource,
);
// const eventualSendSourcePath = fileURLToPath(
// import.meta.resolve('@endo/eventual-send/shim.js'),
// );

// const { source: eventualSendBundleSource } = await bundleSource(
// eventualSendSourcePath,
// { format: 'endoScript' },
// );

// await writeFile(
// path.resolve(dist, fileNames.eventualSend),
// eventualSendBundleSource,
// );

console.log('Success!');
11 changes: 0 additions & 11 deletions packages/shims/src/apply-lockdown.mjs

This file was deleted.

4 changes: 2 additions & 2 deletions packages/shims/src/endoify.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './ses.mjs';
import './eventual-send.mjs';
import 'ses';
import '@endo/eventual-send/shim.js';

lockdown({
consoleTaming: 'unsafe',
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
"type": "module",
"exports": {
".": "./src/index.ts",
"./endoified": "./src/endoified.ts",
"./package.json": "./package.json"
},
"scripts": {
"clean": "rimraf --glob ./dist './*.tsbuildinfo'",
"test": "echo 'No tests.' && exit 0"
},
"devDependencies": {
"jsdom": "^24.1.1",
"rimraf": "^6.0.1",
"ses": "^1.7.0",
"typescript": "~4.9.5",
Expand Down
78 changes: 78 additions & 0 deletions packages/test-utils/src/endoified.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable n/no-sync */
import jsdom from 'jsdom';
import { spawnSync } from 'node:child_process';
import { readFileSync, existsSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const endoifyBundleSourceTextURL = new URL(
'../../shims/dist/endoify.mjs',
import.meta.url.replace(/^https?:\/\/.*?\/@fs\//u, 'file:///'),
);
const endoifyShimsBundleSourceLocation = fileURLToPath(
endoifyBundleSourceTextURL.href,
);

/**
* Run an endoified test with endowments.
*
* @param test - The test to run. It must be a synchronous function that takes `endowments` as its only parameter.
* @param endowments - Endowments passed to the test. They must include `vitest` capabilities like `it` and `expect`.
* @param endoifySourceLocation - Optional path to the endoify bundle. It must either be absolute or relative to `process.cwd()`.
* @returns The return value of the test.
*/
export default function endoified<
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
Endowments extends Partial<typeof import('vitest')>,
>(
test: (endowments: Endowments) => void,
endowments: Endowments,
endoifySourceLocation?: string | URL,
): () => void {
return () => {
// console.log({ endoifySourceLocation: `${endoifySourceLocation}` });

const absoluteEndoifySourcePath =
// eslint-disable-next-line no-nested-ternary
endoifySourceLocation && typeof endoifySourceLocation === 'string'
? endoifySourceLocation
: endoifySourceLocation && endoifySourceLocation instanceof URL
? fileURLToPath(endoifySourceLocation.href)
: endoifyShimsBundleSourceLocation;

const relativeEndoifySourcePath = path.relative(
'.',
absoluteEndoifySourcePath,
);

if (
absoluteEndoifySourcePath === endoifyShimsBundleSourceLocation &&
!existsSync(endoifyShimsBundleSourceLocation)
) {
spawnSync(
'node',
[
fileURLToPath(
new URL(
'../../shims/scripts/bundle.js',
import.meta.url.replace(/^https?:\/\/.*?\/@fs\//u, 'file:///'),
).href,
),
],
{ stdio: 'inherit' },
);
}

// console.log({ relativeEndoifySourcePath });

const evaluatorSourceText = String(test);
const endoifySourceText = readFileSync(relativeEndoifySourcePath, 'utf-8');

// console.log({ endoifySourceText });

const dom = new jsdom.JSDOM(``, { runScripts: 'outside-only' });

dom.window.eval(endoifySourceText);
(dom.window.eval(evaluatorSourceText) as typeof test)(endowments);
};
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@ocap/test-utils@workspace:packages/test-utils"
dependencies:
jsdom: "npm:^24.1.1"
rimraf: "npm:^6.0.1"
ses: "npm:^1.7.0"
typescript: "npm:~4.9.5"
Expand Down