diff --git a/README.md b/README.md index 38b4746..a615c6b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ssb-plugins2 +# ssb-plugins proof of concept muxrpc plugins as separate process diff --git a/help.js b/help.js new file mode 100644 index 0000000..3aef82e --- /dev/null +++ b/help.js @@ -0,0 +1,41 @@ + +var Module = { + single: 'module', + args: { + module: { + type: 'string', + description: 'a module@version-range string' + } + } +} + +function desc(d, type) { + return Object.assign({ + description: d, + type: type + }, Module) +} + +module.exports = { + description: 'manage ssb plugins', + commands: { + install: desc('install a plugin', 'source'), + uninstall: desc('remove a plugin', 'source'), + enable: desc('enable an installed plugin, must restart ssb-server afterwards', 'async'), + disable: desc('disable an installed plugin (without uninstalling), must restart ssb-server afterwards', 'async'), + } +} + + + + + + + + + + + + + + diff --git a/index.js b/index.js index a51e6b7..94fa891 100644 --- a/index.js +++ b/index.js @@ -15,10 +15,21 @@ var mdm = require('mdmanifest') var explain = require('explain-error') var valid = require('muxrpc-validation')({}) +function isObject(o) { + return o && 'object' === typeof o +} + module.exports = { name: 'plugins', version: '1.0.0', - manifest: mdm.manifest(fs.readFileSync(path.join(__dirname, 'api.md'), 'utf8')), + manifest: { + install: 'source', + uninstall: 'source', + enable: 'async', + disable: 'async', + help: 'sync' + } +//mdm.manifest(fs.readFileSync(path.join(__dirname, 'api.md'), 'utf8')), permissions: { master: {allow: ['install', 'uninstall', 'enable', 'disable']} }, @@ -30,6 +41,7 @@ module.exports = { // helper to enable/disable plugins function configPluginEnabled (b) { return function (pluginName, cb) { + if(isObject(pluginName)) pluginName = pluginName.module checkInstalled(pluginName, function (err) { if (err) return cb(err) @@ -58,6 +70,7 @@ module.exports = { // write the plugin config to ~/.ssb/config function writePluginConfig (pluginName, value) { + var cfgPath = path.join(config.path, 'config') // load ~/.ssb/config let existingConfig @@ -86,6 +99,7 @@ module.exports = { return { install: valid.source(function (pluginName, opts) { + if(isObject(pluginName)) pluginName = pluginName.module var p = pushable() var dryRun = opts && opts['dry-run'] var from = opts && opts.from @@ -157,6 +171,7 @@ module.exports = { ]) }, 'string', 'object?'), uninstall: valid.source(function (pluginName, opts) { + if(isObject(pluginName)) pluginName = pluginName.module var p = pushable() if (!pluginName || typeof pluginName !== 'string') return pull.error(new Error('plugin name is required')) @@ -174,7 +189,8 @@ module.exports = { return p }, 'string', 'object?'), enable: valid.async(configPluginEnabled(true), 'string'), - disable: valid.async(configPluginEnabled(false), 'string') + disable: valid.async(configPluginEnabled(false), 'string'), + help: function () { return require('./help') } } } } @@ -192,7 +208,14 @@ module.exports.loadUserPlugins = function (createSsbServer, config) { if (createSsbServer.plugins.some(plug => plug.name === name)) throw new Error('already loaded plugin named:'+name) - var plugin = require(path.join(nodeModulesPath, module_name)) + var pkg = require(path.join(nodeModulesPath, module_name, 'package.json')) + var plugin + if(pkg.ssb && pkg.ssb.outOfProcess) { + plugin = load(path.join(nodeModulesPath, module_name), name) + } else { + plugin = require(path.join(nodeModulesPath, module_name)) + } + if(!plugin || plugin.name !== name) throw new Error('plugin at:'+module_name+' expected name:'+name+' but had:'+(plugin||{}).name) assertSsbServerPlugin(plugin) @@ -225,3 +248,5 @@ function validatePluginName (name) { return true } + + diff --git a/package.json b/package.json index 5c5f302..fdf0878 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "ssb-plugins2", + "name": "ssb-plugins", "description": "", "version": "0.0.0", "homepage": "https://github.com/dominictarr/ssb-plugins2", @@ -15,7 +15,6 @@ "muxrpc": "^6.4.0", "muxrpc-validation": "^3.0.0", "mv": "^2.1.1", - "nimraf": "^1.1.0", "osenv": "^0.1.5", "pull-cat": "^1.1.11", "pull-many": "^1.0.8", @@ -35,3 +34,4 @@ "author": "Dominic Tarr (http://dominictarr.com)", "license": "MIT" } + diff --git a/run-standalone.js b/run-standalone.js deleted file mode 100755 index e63f547..0000000 --- a/run-standalone.js +++ /dev/null @@ -1,31 +0,0 @@ -#! /usr/bin/env node - -var path = require('path') -var fs = require('fs') - -if (process.argv.length !== 3) { - console.error(`usage: ${process.argv[1]} pluginPath`) - process.exit(1) -} - -const pluginPath = process.argv[2] -try { - fs.statSync(pluginPath) -} catch (e) { - console.error('could not locate specified pluginPath') - console.warn('exception:', e) - process.exit(1) -} - -var {child, manifest} = require('./run')(pluginPath) -var api = require('muxrpc/api')({}, manifest, child) - -api.callback('bob', function (err, value) { - if (err) throw err - console.error('from cb', value) - api.hello('DARRYL', function (err, value) { - if (err) throw err - console.error('from hello', value) - process.exit() - }) -})