Skip to content

Commit 3379222

Browse files
committed
rpc connection to host with native messaging
1 parent 76aa130 commit 3379222

File tree

10 files changed

+176
-44
lines changed

10 files changed

+176
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ webext/content-script.js.LICENSE.txt
66
webext/content-script.js.map
77
webext/background.js
88
webext/background.js.map
9+
webext/ssb-client.js

host/host-script.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/local/bin/node
2+
3+
import MRPC from 'muxrpc'
4+
import { pull } from 'pull-stream'
5+
import { Buffer } from 'buffer'
6+
import { inspect } from 'util'
7+
8+
import {Input, Output} from 'web-ext-native-msg'
9+
10+
const handleReject = e => {
11+
e = (new Output()).encode(e);
12+
e && process.stdout.write(e);
13+
return false;
14+
};
15+
16+
const writeStdout = async msg => {
17+
msg = await (new Output()).encode(msg);
18+
return msg && process.stdout.write(msg);
19+
};
20+
21+
22+
const input = new Input();
23+
24+
let messageDataCallback = null
25+
let messageDataBuffer = []
26+
27+
const readStdin = chunk => {
28+
const arr = input.decode(chunk);
29+
const func = [];
30+
Array.isArray(arr) && arr.length && arr.forEach(msg => {
31+
msg && func.push(handleMsg(msg));
32+
});
33+
return Promise.all(func).catch(handleReject);
34+
};
35+
36+
process.stdin.on("data", readStdin);
37+
38+
const fromWebExt = function read(abort, cb) {
39+
if (messageDataBuffer.length > 0) {
40+
const data = messageDataBuffer[0]
41+
messageDataBuffer = messageDataBuffer.splice(1)
42+
cb(null, data)
43+
} else {
44+
messageDataCallback = cb
45+
}
46+
}
47+
48+
49+
const handleMsg = async message => {
50+
// console.error('msg', message, inspect(message))
51+
const asBuffer = Buffer.from(message)
52+
if (messageDataCallback) {
53+
const _messageDataCallback = messageDataCallback
54+
messageDataCallback = null
55+
_messageDataCallback(null, asBuffer)
56+
} else {
57+
console.log('buffering....')
58+
messageDataBuffer.push(asBuffer)
59+
}
60+
}
61+
62+
const toWebExt = function(read) {
63+
read(null, function more (end,data) {
64+
if (end) return
65+
writeStdout(data);
66+
read(null, more)
67+
})
68+
}
69+
70+
71+
72+
73+
74+
const manifest = {
75+
//async is a normal async function
76+
hello: 'async',
77+
78+
//source is a pull-stream (readable)
79+
stuff: 'source'
80+
}
81+
82+
const api = {
83+
hello(name, cb) {
84+
cb(null, 'hello, ' + name + '!')
85+
},
86+
stuff() {
87+
return pull.values([1, 2, 3, 4, 5])
88+
}
89+
}
90+
91+
const onClose = () => {
92+
console.log('muxrpc server closed')
93+
}
94+
95+
const server = MRPC(null, manifest) (api)
96+
const serverStream = server.createStream(onClose)
97+
98+
pull(fromWebExt, serverStream, toWebExt)

host/ssb4all.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "ssb4all",
3+
"description": "A Native Messaging host that gives access to ssb to e ssb4all web extension",
4+
"path": "/home/reto/Documents/retog/ssb4all/host/host-script.js",
5+
"type": "stdio",
6+
"allowed_extensions": [ "ssb4all@example.org" ]
7+
}

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"build": "rollup -c",
88
"browserify:ssb": "browserify -r ssb-client -i bufferutil -i utf-8-validate -s ssb -o webext/ssb-client.js",
9-
"start:ext": "web-ext run --no-reload --source-dir ./webext/",
9+
"start:ext": "web-ext run --no-reload --bc -v -u \"about:debugging#/runtime/this-firefox\"--source-dir ./webext/",
1010
"serve": "http-server site -p 9090"
1111
},
1212
"webExt": {
@@ -19,7 +19,8 @@
1919
"muxrpc": "^6.5.1",
2020
"pull-paramap": "^1.2.2",
2121
"pull-stream": "^3.6.14",
22-
"ssb-client": "^4.9.0"
22+
"ssb-client": "^4.9.0",
23+
"web-ext-native-msg": "^4.8.1"
2324
},
2425
"devDependencies": {
2526
"@rollup/plugin-commonjs": "^17.0.0",
@@ -34,5 +35,6 @@
3435
"rollup-plugin-node-globals": "^1.4.0",
3536
"rollup-plugin-terser": "^7.0.2",
3637
"web-ext": "^5.4.0"
37-
}
38+
},
39+
"type": "module"
3840
}

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default [{
1414
sourcemap: true,
1515
format: 'iife',
1616
name: 'app',
17-
file: 'webext/background-script.js',
17+
file: 'webext/background.js',
1818
intro: 'const global = window;',
1919
globals: {
2020
'ssb-client': 'ssb'

site/page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
55
</head>
66
<body>
7-
<p>RPC conextion with the plugin, see the console for some output.</p>
7+
<p>RPC conection with the plugin, see the console for some output.</p>
88

99
<script src="page-script.js"></script>
1010
</body>

src/background-script.js

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@ import { pull } from 'pull-stream'
33
import { Buffer } from 'buffer'
44

55

6-
const manifest = {
7-
//async is a normal async function
8-
hello: 'async',
9-
10-
//source is a pull-stream (readable)
11-
stuff: 'source'
12-
}
13-
14-
const api = {
15-
hello(name, cb) {
16-
cb(null, 'hello, ' + name + '!')
17-
},
18-
stuff() {
19-
return pull.values([1, 2, 3, 4, 5])
20-
}
21-
}
22-
236

247

25-
26-
let connections = []
278
let ports = []
289

2910
browser.runtime.onConnect.addListener(function connected(p) {
3011
ports[p.sender.tab.id] = p
31-
connections.push(createConnection(p))
12+
//p.sender.tab.onClose(() => console.log('tab closed, should close connection'))
13+
const [fromContentScript, toContentScript] = createConnection(p)
14+
const [fromNativeScript, toNativeScript] = createNativeConnection(p)
15+
16+
pull(
17+
fromContentScript,
18+
//logger('from content to native'),
19+
toNativeScript
20+
)
21+
pull(
22+
fromNativeScript,
23+
//logger('from native to content'),
24+
toContentScript
25+
)
3226
})
3327

28+
function createNativeConnection() {
29+
const port = browser.runtime.connectNative("ssb4all")
30+
return createConnection(port)
31+
}
32+
33+
3434
function createConnection(port) {
3535
let messageDataCallback = null
3636
let messageDataBuffer = []
37-
38-
const fromContentScript = function read(abort, cb) {
37+
38+
const fromPort = function read(abort, cb) {
3939
if (messageDataBuffer.length > 0) {
4040
const data = messageDataBuffer[0]
4141
messageDataBuffer = messageDataBuffer.splice(1)
@@ -57,28 +57,13 @@ function createConnection(port) {
5757
}
5858
});
5959

60-
const toContentScript = function(read) {
60+
const toPort = function(read) {
6161
read(null, function more (end,data) {
6262
if (end) return
6363
port.postMessage(data);
6464
read(null, more)
6565
})
6666
}
6767

68-
const onClose = () => {
69-
console.log('muxrpc server closed')
70-
}
71-
72-
const server = MRPC(null, manifest) (api)
73-
const serverStream = server.createStream(onClose)
74-
75-
pull(
76-
fromContentScript,
77-
serverStream,
78-
toContentScript
79-
)
80-
81-
console.log('created server for', port)
82-
83-
return {fromContentScript, toContentScript}
68+
return [fromPort, toPort]
8469
}

webext/icons/message.svg

Lines changed: 8 additions & 0 deletions
Loading

webext/manifest.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111

1212
"permissions": [
13+
"nativeMessaging",
1314
"<all_urls>"
1415
],
1516

@@ -20,6 +21,17 @@
2021
}
2122
],
2223

24+
"browser_action": {
25+
"default_icon": "icons/message.svg"
26+
},
27+
28+
"browser_specific_settings": {
29+
"gecko": {
30+
"id": "ssb4all@example.org",
31+
"strict_min_version": "50.0"
32+
}
33+
},
34+
2335
"background": {
2436
"page": "background-page.html"
2537
}

0 commit comments

Comments
 (0)