Skip to content

Commit

Permalink
fix(endo): Sync with deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jun 26, 2021
1 parent 437bf54 commit e0eed22
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/endo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test": "ava"
},
"dependencies": {
"@agoric/babel-standalone": "^7.14.3",
"@endo/compartment-mapper": "^0.4.1",
"ses": "^0.13.4"
},
Expand Down
97 changes: 63 additions & 34 deletions packages/endo/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* eslint no-shadow: [0] */
import '@agoric/babel-standalone';
import './lockdown.js';
import subprocess from 'child_process';
import {
search,
writeArchive,
compartmentMapForNodeModules,
mapLocation,
hashLocation,
loadArchive,
} from '@endo/compartment-mapper';
import {
makeNodeReadPowers,
makeNodeWritePowers,
} from '@endo/compartment-mapper/node-powers.js';

const mitmPath = new URL('../mitm', import.meta.url).pathname;

Expand Down Expand Up @@ -41,20 +47,59 @@ async function parameter(args, handle, usage) {
return handle(arg, rest);
}

async function run(args, { cwd, read, write, stdout, env }) {
async function run(
args,
{ cwd, read, canonical, computeSha512, write, stdout, env },
) {
const currentLocation = new URL(`${cwd()}/`, 'file:///');

function locate(path) {
return new URL(path, currentLocation);
}

async function map(args) {
async function handleEntry(applicationPath, args) {
if (args.length) {
return usage(`unexpected arguments: ${JSON.stringify(args)}`);
}
const currentLocation = new URL(`${cwd()}/`, 'file:///');
const applicationLocation = new URL(applicationPath, currentLocation);
const { packageLocation } = await search(read, applicationLocation);
const compartmentMap = await compartmentMapForNodeModules(
read,
packageLocation,
const applicationLocation = locate(applicationPath);
const compartmentMapBytes = await mapLocation(
{ read, canonical, computeSha512 },
applicationLocation,
);
stdout.write(`${JSON.stringify(compartmentMap, null, 2)}\n`);
stdout.write(compartmentMapBytes);
return 0;
}
return parameter(args, handleEntry, noEntryUsage);
}

async function hash(args) {
async function handleEntry(applicationPath, args) {
if (args.length) {
return usage(`unexpected arguments: ${JSON.stringify(args)}`);
}
const applicationLocation = locate(applicationPath);
const sha512 = await hashLocation(
{ read, canonical, computeSha512 },
applicationLocation,
);
stdout.write(`${sha512}\n`);
return 0;
}
return parameter(args, handleEntry, noEntryUsage);
}

async function hashArchive(args) {
async function handleEntry(archivePath, args) {
if (args.length) {
return usage(`unexpected arguments: ${JSON.stringify(args)}`);
}
const archiveLocation = locate(archivePath);
const { sha512 } = await loadArchive(
{ read, canonical, computeSha512 },
archiveLocation,
);
stdout.write(`${sha512}\n`);
return 0;
}
return parameter(args, handleEntry, noEntryUsage);
Expand All @@ -66,9 +111,8 @@ async function run(args, { cwd, read, write, stdout, env }) {
if (args.length) {
return usage(`unexpected arguments: ${JSON.stringify(args)}`);
}
const currentLocation = new URL(`${cwd()}/`, 'file:///');
const archiveLocation = new URL(archivePath, currentLocation);
const applicationLocation = new URL(applicationPath, currentLocation);
const archiveLocation = locate(archivePath);
const applicationLocation = locate(applicationPath);
await writeArchive(write, read, archiveLocation, applicationLocation);
return 0;
}
Expand All @@ -85,35 +129,20 @@ async function run(args, { cwd, read, write, stdout, env }) {
return new Promise(resolve => child.on('exit', resolve));
}

return subcommand(args, { map, archive, exec });
return subcommand(args, { map, hash, hagar: hashArchive, archive, exec });
}

export async function main(process, modules) {
const { fs } = modules;
const { fs, crypto } = modules;
const { cwd, stdout, env } = process;

// Filesystem errors often don't have stacks:

async function read(location) {
try {
return await fs.readFile(new URL(location).pathname);
} catch (error) {
throw new Error(error.message);
}
}

async function write(location, content) {
try {
return await fs.writeFile(new URL(location).pathname, content);
} catch (error) {
throw new Error(error.message);
}
}
const readPowers = makeNodeReadPowers(fs, crypto);
const writePowers = makeNodeWritePowers(fs);

try {
process.exitCode = await run(process.argv.slice(2), {
read,
write,
...readPowers,
...writePowers,
cwd,
stdout,
env,
Expand Down

0 comments on commit e0eed22

Please sign in to comment.