Skip to content

Commit

Permalink
refactor(xsnap-lockdown): Migrate object-inspect bundle from Rollup t…
Browse files Browse the repository at this point in the history
…o Endo
  • Loading branch information
kriskowal committed Jan 9, 2025
1 parent 78063fc commit 22109c6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 34 deletions.
5 changes: 4 additions & 1 deletion packages/swingset-xsnap-supervisor/scripts/build-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const computeSha256 = bytes => {
const run = async () => {
fs.mkdirSync(path.dirname(bundlePaths.supervisor), { recursive: true });
const format = 'nestedEvaluate';
const bundle = await bundleSource(entryPaths.supervisor, { format });
const bundle = await bundleSource(entryPaths.supervisor, {
format,
conditions: new Set(['development']),
});
const bundleString = JSON.stringify(bundle);
const sha256 = computeSha256(bundleString);
fs.writeFileSync(bundlePaths.supervisor, bundleString);
Expand Down
19 changes: 3 additions & 16 deletions packages/xsnap-lockdown/lib/confined-object-inspect.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import objectInspectSources from '../dist/src-object-inspect.js';
import objectInspectSources from '../dist/object-inspect.js';

// Ensure the object inspector is confined.
const c = new Compartment();
harden(c.globalThis);

// Transform the imported inspector module source string into an evaluable
// string. We could have played more games with bundlers to do something less
// fragile, but even so, SES should fail-safe if this replacement doesn't match.
//
// The goal (presuming the file ends with a single export default statement):
// `...\n export default harden(inspect0);`
// becomes:
// `...\n /* export default */ harden(inspect0);`
// and we can evaluate it to obtain the completion value as the object inspector.
const src = objectInspectSources.replace(
/(^|\s)(export\s+default)(\s+)/g,
'$1/* $2 */$3',
);
const objectInspect = c.evaluate(
`${src}\n//# sourceURL=xsnap-lockdown/lib/object-inspect.js\n`,
);
`(${objectInspectSources.source})`,
)().default;

export default objectInspect;
6 changes: 1 addition & 5 deletions packages/xsnap-lockdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"type": "module",
"main": "./src/index.js",
"scripts": {
"build:bundle-rollup": "rollup --config rollup.config.js",
"build:bundle-source": "node scripts/build-bundle.js",
"build": "yarn build:bundle-rollup && yarn build:bundle-source",
"build": "node scripts/build-bundle.js",
"clean": "rm -rf dist",
"lint": "run-s --continue-on-error lint:*",
"lint:js": "eslint 'src/**/*.js' 'lib/**/*.js' 'scripts/**/*.js' 'test/**/*.js'",
Expand All @@ -24,8 +22,6 @@
"@endo/init": "^1.1.7",
"ava": "^5.3.0",
"c8": "^10.1.2",
"rollup": "^4.24.0",
"rollup-plugin-string": "^3.0.0",
"source-map": "^0.7.4"
},
"files": [
Expand Down
12 changes: 0 additions & 12 deletions packages/xsnap-lockdown/rollup.config.js

This file was deleted.

13 changes: 13 additions & 0 deletions packages/xsnap-lockdown/scripts/build-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ const make = async name => {
return { length: bundleString.length, sha256, where: spec };
};

/**
* @param {string} name
*/
const makeObjectInspect = async name => {
const spec = bundlePaths[name];
const entryPath = entryPaths[name];
await fsp.mkdir(path.dirname(spec), { recursive: true });
const bundle = await bundleSource(entryPath, { format: 'getExport' });
const bundleString = JSON.stringify(bundle);
await fsp.writeFile(spec, `export default ${bundleString};\n`);
};

const run = async () => {
await makeObjectInspect('objectInspect');
const ld = await make('lockdown');
console.log(`wrote ${ld.where}: ${ld.length} bytes`);
console.log(`lockdown.bundle SHA256: ${ld.sha256}`);
Expand Down
2 changes: 2 additions & 0 deletions packages/xsnap-lockdown/src/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const bundlePaths = {
lockdown: new URL('../dist/lockdown.bundle', import.meta.url).pathname,
lockdownDebug: new URL('../dist/lockdown-debug.bundle', import.meta.url)
.pathname,
objectInspect: new URL('../dist/object-inspect.js', import.meta.url).pathname,
};

export const hashPaths = {
Expand All @@ -18,4 +19,5 @@ export const hashPaths = {
export const entryPaths = {
lockdown: new URL('../lib/ses-boot.js', import.meta.url).pathname,
lockdownDebug: new URL('../lib/ses-boot-debug.js', import.meta.url).pathname,
objectInspect: new URL('../lib/object-inspect.js', import.meta.url).pathname,
};

0 comments on commit 22109c6

Please sign in to comment.