-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathhost.js
More file actions
executable file
·209 lines (196 loc) · 4.73 KB
/
host.js
File metadata and controls
executable file
·209 lines (196 loc) · 4.73 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
function lazyRequire(lib, name) {
if (!name) {
name = lib;
}
global.__defineGetter__(name, function() {
return require(lib);
});
return global[name];
}
const spawn = require('child_process').spawn;
const fs = lazyRequire('fs');
const os = lazyRequire('os');
const path = lazyRequire('path');
let files = [];
const sprocess = [];
const config = {
version: '1.0.8'
};
// closing node when parent process is killed
process.stdin.resume();
process.stdin.on('end', () => {
files.forEach(file => {
try {
fs.unlink(file);
}
catch (e) {}
});
sprocess.forEach(ps => ps.kill());
process.exit();
});
// process.on('uncaughtException', e => console.error(e));
function observe(msg, push, done) {
if (msg.cmd === 'version') {
push({
version: config.version
});
done();
}
if (msg.cmd === 'spec') {
push({
version: config.version,
env: process.env,
separator: path.sep,
tmpdir: os.tmpdir()
});
done();
}
else if (msg.cmd === 'echo') {
push(msg);
done();
}
else if (msg.cmd === 'spawn') {
if (msg.env) {
msg.env.forEach(n => process.env.PATH += path.delimiter + n);
}
const p = Array.isArray(msg.command) ? path.join(...msg.command) : msg.command;
const sp = spawn(p, msg.arguments || [], Object.assign({env: process.env}, msg.properties));
if (msg.kill) {
sprocess.push(sp);
}
sp.stdout.on('data', stdout => push({stdout}));
sp.stderr.on('data', stderr => push({stderr}));
sp.on('close', code => {
push({
cmd: msg.cmd,
code
});
done();
});
sp.on('error', e => {
push({
code: 1007,
error: e.message
});
done();
});
if (Array.isArray(msg.stdin)) {
msg.stdin.forEach(c => sp.stdin.write(c));
sp.stdin.end();
}
}
else if (msg.cmd === 'clean-tmp') {
files.forEach(file => {
try {
fs.unlink(file);
}
catch (e) {}
});
files = [];
push({
code: 0
});
done();
}
else if (msg.cmd === 'exec') {
if (msg.env) {
msg.env.forEach(n => process.env.PATH += path.delimiter + n);
}
const p = Array.isArray(msg.command) ? path.join(...msg.command) : msg.command;
const sp = spawn(p, msg.arguments || [], Object.assign({
env: process.env,
detached: true
}, msg.properties));
if (msg.kill) {
sprocess.push(sp);
}
let stderr = '';
let stdout = '';
if (sp.stdout) {
sp.stdout.on('data', data => stdout += data);
}
if (sp.stderr) {
sp.stderr.on('data', data => stderr += data);
}
sp.on('close', code => {
push({
code,
stderr,
stdout
});
done();
});
if (sp.stdin) {
if (Array.isArray(msg.stdin)) {
msg.stdin.forEach(c => sp.stdin.write(c));
sp.stdin.end();
}
}
if (msg.unref) {
sp.unref();
}
}
else if (msg.cmd === 'env') {
push({
env: process.env
});
done();
}
// this is from openstyles/native-client
else if ('script' in msg) {
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);
const vm = require('vm');
const sandbox = {
version: config.version,
env: process.env,
push,
close,
setTimeout,
args: msg.args,
// only allow internal modules that extension already requested permission for
require: name => (msg.permissions || []).indexOf(name) === -1 ? null : require(name)
};
const script = new vm.Script(msg.script);
const context = vm.createContext(sandbox);
script.runInContext(context);
}
else {
let error = 'This version of the native client does not support "' + msg.cmd + '" command. Check for updates...';
// Display warning about old unsupported commands
if (['ifup', 'dir', 'save-data', 'net', 'copy', 'remove', 'move'].includes(msg.cmd)) {
error = 'The "' + msg.cmd + '" command is no longer supported in this version of the native client. ' +
'Downgrade to version 0.9.7 if your extension requires this command.';
}
push({
error,
cmd: msg.cmd,
code: 1000
});
done();
}
}
/* message passing */
const nativeMessage = require('./messaging');
const input = new nativeMessage.Input();
const transform = new nativeMessage.Transform(observe);
const output = new nativeMessage.Output();
process.stdin
.pipe(input)
.pipe(transform)
.pipe(output)
.pipe(process.stdout);