git ssb

1+

mixmix / scuttle-shell



Tree: 13ce65e0e17ef86467d82e99ecce021463cc9485

Files: 13ce65e0e17ef86467d82e99ecce021463cc9485 / host-app.js

2427 bytesRaw
1
2const nativeMessage = require('chrome-native-messaging')
3const { spawn } = require('child_process')
4const path = require('path')
5const fs = require('fs')
6const home = require('os').homedir()
7
8const input = new nativeMessage.Input()
9const transform = new nativeMessage.Transform((msg, push, done) => {
10 getReplyFor(msg, data => {
11 push(data)
12 done()
13 })
14})
15const output = new nativeMessage.Output()
16
17const getConfig = () => {
18 try {
19 let secret = fs.readFileSync(`${home}/.ssb/secret`, "utf8")
20 let keys = JSON.parse(secret.replace(/#[^\n]*/g, ''))
21 let manifest = JSON.parse(fs.readFileSync(`${home}/.ssb/manifest.json`))
22 let remote = "ws://localhost:8989~shs:" + keys.id.substring(1, keys.id.indexOf('.'))
23 return { type: 'config', keys: keys, manifest: manifest, remote: remote, secret: secret }
24 } catch (n) {
25 return { type: 'exception', msg: n.message }
26 }
27}
28
29const startServer = (cb) => {
30 output.write({ type: "debug", msg: `starting scuttlebutt` })
31
32 var scriptPath = path.join(__dirname, 'server.js')
33 var child = spawn(process.execPath, [scriptPath])
34
35 child.stdout.on('data', (data) => {
36 output.write({ type: 'debug', msg: `stdout: ${data}` })
37 })
38
39 child.stderr.on('data', (data) => {
40 output.write({ type: 'error', msg: `stderr: ${data}` })
41 })
42
43 child.on('close', (code) => {
44 output.write({ type: 'debug', msg: `child process exited with code ${code}` })
45 })
46
47 output.write(getConfig())
48}
49
50const getReplyFor = (msg, cb) => {
51 output.write({ type: "debug", msg: `trying to get reply for ${msg.cmd}` })
52 switch (msg.cmd) {
53 case 'start-server':
54 startServer(cb)
55 break
56 case 'stop-server': {
57 clearInterval(timer)
58 cb({ type: 'shutdown', msg: 'stopping server' })
59 process.exit(1)
60 }
61 case 'get-config': {
62 let secretRaw = fs.readFileSync(`${home}/.ssb/secret`)
63 let keys = JSON.parse(secret.replace(/#[^\n]*/g, ''))
64 let manifest = JSON.parse(fs.readFileSync(`${home}/.ssb/manifest.json`))
65 let remote = "ws://localhost:8989~shs:" + keys.id.substring(1, keys.id.indexOf('.'))
66
67 cb({ type: 'config', keys, manifest, remote })
68 }
69 }
70}
71
72var timer = setInterval(function () {
73 output.write({ type: "ping", time: new Date().toISOString() })
74}, 120000)
75
76input.on('end', function () {
77 clearInterval(timer)
78})
79
80process.stdin
81 .pipe(input)
82 .pipe(transform)
83 .pipe(output)
84 .pipe(process.stdout)
85

Built with git-ssb-web