forked from openstyles/native-client
-
Notifications
You must be signed in to change notification settings - Fork 112
/
host.js
executable file
·69 lines (65 loc) · 1.72 KB
/
host.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
'use strict';
const config = require('./config.js');
// closing node when parent process is killed
process.stdin.resume();
process.stdin.on('end', () => process.exit());
function observe(request, push, done) {
let close;
const exception = e => {
push({
code: -1,
type: 'exception',
error: e.stack
});
close();
};
close = () => {
process.removeListener('uncaughtException', exception);
done();
close = () => {};
};
process.addListener('uncaughtException', exception);
if (request.method === 'spec') {
push({
version: config.version,
env: process.env,
release: process.release,
platform: process.platform,
arch: process.arch,
versions: process.versions,
separator: require('path').sep,
tmpdir: require('os').tmpdir()
});
close();
}
else if ('script' in request) {
const vm = require('vm');
const sandbox = {
version: config.version,
env: process.env,
push,
close,
setTimeout,
args: request.args,
// only allow internal modules that extension already requested permission for
require: name => (request.permissions || []).indexOf(name) === -1 ? null : require(name)
};
const script = new vm.Script(request.script);
const context = new vm.createContext(sandbox);
script.runInContext(context);
}
else {
push({
type: 'context',
error: 'cannot find "script" key in your request. Closing connection...'
});
close();
}
}
/* message passing */
const nativeMessage = require('./messaging');
process.stdin
.pipe(new nativeMessage.Input())
.pipe(new nativeMessage.Transform(observe))
.pipe(new nativeMessage.Output())
.pipe(process.stdout);