Skip to content

Commit

Permalink
feat: add local.plugin~.getPluginDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Sep 16, 2020
1 parent 2ba608e commit 94e7016
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions packages/SwingSet/src/devices/plugin-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export function buildRootDeviceNode(tools) {
});

return harden({
getPluginDir() {
return endowments.getPluginDir();
},
connect,
send,
registerReceiver(receiver) {
Expand Down
7 changes: 6 additions & 1 deletion packages/SwingSet/src/devices/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function buildPlugin(pluginRequire, queueThunkForKernel) {
export function buildPlugin(pluginDir, pluginRequire, queueThunkForKernel) {
const srcPath = require.resolve('./plugin-src');
let resetter;

Expand All @@ -14,6 +14,10 @@ export function buildPlugin(pluginRequire, queueThunkForKernel) {
resetter = init;
}

function getPluginDir() {
return pluginDir;
}

// srcPath and endowments are provided to buildRootDeviceNode() for use
// during configuration.
return {
Expand All @@ -22,6 +26,7 @@ export function buildPlugin(pluginRequire, queueThunkForKernel) {
require: pluginRequire,
queueThunkForKernel,
registerResetter,
getPluginDir,
},
reset,
};
Expand Down
4 changes: 3 additions & 1 deletion packages/cosmic-swingset/lib/ag-solo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ start
}
case 'start': {
const basedir = insistIsBasedir();
await start(basedir, { ROLE: 'client' });
await start(basedir, {
ROLE: 'client',
});
break;
}
case 'reset-state': {
Expand Down
7 changes: 4 additions & 3 deletions packages/cosmic-swingset/lib/ag-solo/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ async function buildSwingset(
await processKernel();
});

const pluginsPrefix = `${path.resolve('./plugins')}${path.sep}`;
const pluginDir = path.resolve('./plugins');
const pluginsPrefix = `${pluginDir}${path.sep}`;
const pluginRequire = mod => {
// Ensure they can't traverse out of the plugins prefix.
const pluginFile = path.resolve(pluginsPrefix, mod);
assert(
pluginFile.startsWith(pluginsPrefix),
details`Cannot load ${pluginFile} plugin; outside of ./plugins`,
details`Cannot load ${pluginFile} plugin; outside of ${pluginDir}`,
);

// eslint-disable-next-line import/no-dynamic-require,global-require
return require(pluginFile);
};

const plugin = buildPlugin(pluginRequire, queueThunkForKernel);
const plugin = buildPlugin(pluginDir, pluginRequire, queueThunkForKernel);

let config = loadSwingsetConfigFile(`${vatsDir}/solo-config.json`);
if (config === null) {
Expand Down
8 changes: 6 additions & 2 deletions packages/cosmic-swingset/lib/ag-solo/vats/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ export function buildRootObject(vatPowers, vatParameters) {
// Needed for DApps, maybe for user clients.
const uploads = E(vats.uploads).getUploads();

const plugin = makePluginManager(devices.plugin, vatPowers);
// Only create the plugin manager if the device exists.
let plugin;
if (devices.plugin) {
plugin = makePluginManager(devices.plugin, vatPowers);
}

// This will allow dApp developers to register in their api/deploy.js
const httpRegCallback = {
Expand All @@ -243,9 +247,9 @@ export function buildRootObject(vatPowers, vatParameters) {

return allComparable(
harden({
...(plugin ? { plugin } : {}),
uploads,
spawner,
plugin,
network: vats.network,
http: httpRegCallback,
vattp: makeVattpFrom(vats),
Expand Down
4 changes: 4 additions & 0 deletions packages/cosmic-swingset/lib/ag-solo/vats/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { E, HandledPromise } from '@agoric/eventual-send';
* @property {(mod: string) => number} connect
* @property {(receiver: Receiver) => void} registerReceiver
* @property {(index: number, obj: Record<string, any>) => void} send
* @property {() => string} getPluginDir
*/

/**
Expand Down Expand Up @@ -63,6 +64,9 @@ export function makePluginManager(pluginDevice, { D, ...vatPowers }) {
);

return harden({
getPluginDir() {
return D(pluginDevice).getPluginDir();
},
/**
* Load a module, and call resetter.onReset(bootP) every time it is instantiated.
*/
Expand Down

0 comments on commit 94e7016

Please sign in to comment.