-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost-script.js
executable file
·139 lines (112 loc) · 3.13 KB
/
host-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const MRPC = require('muxrpc')
const { pull } = require('pull-stream')
const { Buffer } = require('buffer')
const Client = require('ssb-client')
const {Input, Output} = require('web-ext-native-msg')
const handleReject = e => {
e = (new Output()).encode(e);
e && process.stdout.write(e);
return false;
};
const writeStdout = async msg => {
//TODO check msg length < 1048576 bytes.
//console.error('msg', msg, inspect(msg))
//console.error('msg.toString()', msg.toString())
//console.error('msg.length', msg.length)
msg = await (new Output()).encode(msg);
return msg && process.stdout.write(msg);
};
const input = new Input();
let messageDataCallback = null
let messageDataBuffer = []
const readStdin = chunk => {
const arr = input.decode(chunk);
const func = [];
Array.isArray(arr) && arr.length && arr.forEach(msg => {
msg && func.push(handleMsg(msg));
});
return Promise.all(func).catch(handleReject);
};
process.stdin.on("data", readStdin);
const fromWebExt = function read(abort, cb) {
if (messageDataBuffer.length > 0) {
const data = messageDataBuffer[0]
messageDataBuffer = messageDataBuffer.splice(1)
cb(null, data)
} else {
messageDataCallback = cb
}
}
const handleMsg = async message => {
//console.error('msg', message, inspect(message))
const asBuffer = Buffer.from(message)
if (messageDataCallback) {
const _messageDataCallback = messageDataCallback
messageDataCallback = null
_messageDataCallback(null, asBuffer)
} else {
//console.log('buffering....')
messageDataBuffer.push(asBuffer)
}
}
const toWebExt = function(read) {
read(null, function more (end,data) {
if (end) return
writeStdout(data);
read(null, more)
})
}
const manifest = {
//async is a normal async function
hello: 'async',
//source is a pull-stream (readable)
stuff: 'source',
manifest: 'sync'
}
const api = {
hello(name, cb) {
cb(null, 'hello, ' + name + '!')
},
stuff() {
return pull.values([1, 2, 3, 4, 5])
},
manifest: function () {
return manifest
}
}
const onClose = () => {
//console.log('muxrpc server closed')
}
/*const server = MRPC(null, manifest) (api)
const serverStream = server.createStream(onClose)
pull(fromWebExt, serverStream, toWebExt)*/
function asyncifyManifest(manifest) {
if (typeof manifest !== 'object' || manifest === null) return manifest
let asyncified = {}
for (let k in manifest) {
var value = manifest[k]
// Rewrite re-exported sync methods as async,
// except for manifest method, as we define it later
if (value === 'sync' && k !== 'manifest') {
value = 'async'
}
asyncified[k] = value
}
return asyncified
}
Client((err, sbot) => {
if (err) {
console.error("could not connect to ssb-server instance", err)
return
}
sbot.manifest().then(manifest => {
//console.error('manifest', inspect(manifest))
const asyncManifest = asyncifyManifest(manifest)
sbot.manifest = function () {
return manifest
}
const server = MRPC(null, asyncManifest) (sbot)
const serverStream = server.createStream(onClose)
pull(fromWebExt, serverStream, toWebExt)
})
})