2015-06-26 10:47:11 +11:00
|
|
|
var ipc = require('ipc')
|
|
|
|
var muxrpc = require('muxrpc')
|
|
|
|
var pull = require('pull-stream')
|
|
|
|
var pushable = require('pull-pushable')
|
|
|
|
var Api = require('scuttlebot/lib/api')
|
|
|
|
|
2015-06-27 05:30:34 +11:00
|
|
|
module.exports = function (window, sbot, params) {
|
2015-06-26 10:47:11 +11:00
|
|
|
// construct api
|
|
|
|
var api = Api(sbot)
|
|
|
|
for (var k in sbot.manifest) {
|
|
|
|
if (typeof sbot.manifest[k] == 'object')
|
|
|
|
api[k] = sbot[k] // copy over the plugin APIs
|
|
|
|
}
|
|
|
|
|
|
|
|
// create rpc object
|
|
|
|
var rpc = muxrpc(null, sbot.manifest, serialize)(api)
|
|
|
|
rpc.authorized = { id: sbot.feed.id, role: 'master' }
|
|
|
|
rpc.permissions({allow: null, deny: null})
|
2015-06-26 11:03:26 +11:00
|
|
|
function serialize (stream) { return stream }
|
2015-06-26 10:47:11 +11:00
|
|
|
|
|
|
|
// setup rpc stream over ipc
|
|
|
|
var rpcStream = rpc.createStream()
|
|
|
|
var ipcPush = pushable()
|
|
|
|
ipc.on('muxrpc-ssb', function (e, msg) {
|
2015-07-03 03:47:35 +11:00
|
|
|
if (e.sender == window.webContents) {
|
|
|
|
if (msg.bvalue) {
|
|
|
|
msg.value = new Buffer(msg.bvalue, 'base64')
|
|
|
|
delete msg.bvalue
|
|
|
|
}
|
2015-06-26 10:47:11 +11:00
|
|
|
ipcPush.push(msg)
|
2015-07-03 03:47:35 +11:00
|
|
|
}
|
2015-06-26 10:47:11 +11:00
|
|
|
})
|
2015-07-02 05:02:59 +11:00
|
|
|
pull(ipcPush, rpcStream, pull.drain(
|
2015-07-03 03:47:35 +11:00
|
|
|
function (msg) {
|
|
|
|
if (msg.value && Buffer.isBuffer(msg.value)) {
|
|
|
|
// convert buffers to base64
|
|
|
|
msg.bvalue = msg.value.toString('base64')
|
|
|
|
delete msg.value
|
|
|
|
}
|
|
|
|
window.webContents.send('muxrpc-ssb', msg)
|
|
|
|
},
|
2015-07-02 05:02:59 +11:00
|
|
|
function (err) { if (err) { console.error(err) } }
|
|
|
|
))
|
2015-06-26 10:53:06 +11:00
|
|
|
|
|
|
|
// setup helper messages
|
2015-07-02 14:07:27 +11:00
|
|
|
ipc.on('fetch-manifest', function(e) {
|
2015-06-26 10:53:06 +11:00
|
|
|
if (e.sender == window.webContents)
|
2015-07-02 14:07:27 +11:00
|
|
|
e.returnValue = sbot.manifest
|
2015-06-26 10:53:06 +11:00
|
|
|
});
|
2015-06-27 05:30:34 +11:00
|
|
|
ipc.on('fetch-params', function(e) {
|
|
|
|
if (e.sender == window.webContents)
|
|
|
|
e.returnValue = params
|
|
|
|
});
|
2015-06-26 10:47:11 +11:00
|
|
|
}
|