Browse Source

setup muxrpc over ipc with the browserwindow

Paul Frazee 5 years ago
parent
commit
7f5bb1ffb2
4 changed files with 40 additions and 30 deletions
  1. 0
    29
      app/index.html
  2. 3
    1
      app/index.js
  3. 34
    0
      app/lib/muxrpc-ipc.js
  4. 3
    0
      package.json

+ 0
- 29
app/index.html View File

@@ -1,29 +0,0 @@
1
-<!DOCTYPE html>
2
-<html>
3
-  <head>
4
-    <title>Scuttlebutt</title>
5
-    <meta charset="utf-8">
6
-    <link rel="shortcut icon" type="image/png" href="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/img/icon.png"/>
7
-    <link rel="stylesheet" type="text/css" href="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/css/home.css">
8
-    <link rel="stylesheet" type="text/css" href="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/css/sweet-alert.css">
9
-  </head>
10
-  <body>
11
-    <div id="app-status"></div>
12
-    <div id="please-wait">
13
-      <div class="spinner">
14
-        <div class="cube1"></div>
15
-        <div class="cube2"></div>
16
-      </div>
17
-    </div>
18
-    <div id="page-nav"></div>
19
-    <div id="page-container"></div>
20
-    <script src="http://localhost:8008/manifest.js"></script>
21
-    <script src="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/js/sweet-alert.js"></script>
22
-    <script src="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/js/canvas-to-blob.js"></script>
23
-    <script src="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/js/sigma.min.js"></script>
24
-    <script src="../node_modules/scuttlebot/node_modules/ssbplug-phoenix/js/home.js"></script>
25
-    <script>
26
-      // require('../node_modules/scuttlebot/node_modules/ssbplug-phoenix/src/home')
27
-    </script>
28
-  </body>
29
-</html>

+ 3
- 1
app/index.js View File

@@ -5,6 +5,7 @@ var BrowserWindow = require('browser-window')
5 5
 var path = require('path')
6 6
 
7 7
 var config = require('ssb-config') 
8
+var setupRpc = require('./lib/muxrpc-ipc')
8 9
 
9 10
 // Report crashes to our server.
10 11
 //require('crash-reporter').start();
@@ -24,9 +25,10 @@ app.on('ready', function ready () {
24 25
 
25 26
     // open the web app
26 27
     mainWindow = new BrowserWindow({width: 1000, height: 720})
27
-    mainWindow.loadUrl('file://' + __dirname + '/index.html')
28
+    mainWindow.loadUrl('file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'))
28 29
     mainWindow.webContents.on('new-window', onNewWindow)
29 30
     mainWindow.on('closed', function() { mainWindow = null })
31
+    setupRpc(sbot, mainWindow)
30 32
 
31 33
     function onNewWindow (e, url) {
32 34
       e.preventDefault() // hell naw

+ 34
- 0
app/lib/muxrpc-ipc.js View File

@@ -0,0 +1,34 @@
1
+var ipc        = require('ipc')
2
+var muxrpc     = require('muxrpc')
3
+var pull       = require('pull-stream')
4
+var Serializer = require('pull-serializer')
5
+var pushable   = require('pull-pushable')
6
+var Api        = require('scuttlebot/lib/api')
7
+
8
+module.exports = function (sbot, window) {
9
+  // construct api
10
+  var api = Api(sbot)
11
+  for (var k in sbot.manifest) {
12
+    if (typeof sbot.manifest[k] == 'object')
13
+      api[k] = sbot[k] // copy over the plugin APIs
14
+  }
15
+
16
+  // create rpc object
17
+  var rpc = muxrpc(null, sbot.manifest, serialize)(api)
18
+  rpc.authorized = { id: sbot.feed.id, role: 'master' }
19
+  rpc.permissions({allow: null, deny: null})
20
+  function serialize (stream) {
21
+    return Serializer(stream, JSON, {split: '\n\n'})
22
+  }
23
+
24
+  // setup rpc stream over ipc
25
+  var rpcStream = rpc.createStream()
26
+  var ipcPush = pushable()
27
+  ipc.on('muxrpc-ssb', function (e, msg) {
28
+    if (e.sender == window.webContents)
29
+      ipcPush.push(msg)
30
+  })
31
+  pull(ipcPush, rpcStream, pull.drain(function (msg) {
32
+    window.webContents.send('muxrpc-ssb', msg)
33
+  }))
34
+}

+ 3
- 0
package.json View File

@@ -27,6 +27,9 @@
27 27
   "dependencies": {
28 28
     "multiblob": "^1.4.3",
29 29
     "multicb": "^1.1.0",
30
+    "muxrpc": "^5.0.1",
31
+    "pull-pushable": "^1.1.4",
32
+    "pull-serializer": "^0.3.2",
30 33
     "pull-stream": "^2.27.0",
31 34
     "scuttlebot": "^4.2.3",
32 35
     "ssb-config": "^1.0.3",

Loading…
Cancel
Save