Repository navigation
Expand file tree
/
Copy pathhost.js
More file actions
executable file
·240 lines (230 loc) · 5.68 KB
/
Copy pathhost.js
File metadata and controls
executable file
·240 lines (230 loc) · 5.68 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
// Deprecated commands
const DEPRECATED = ['ifup', 'dir', 'save-data', 'net', 'copy', 'remove', 'move', 'clean-tmp', 'echo', 'version'];
const sprocess = [];
const sessions = {};
const config = {
version: '1.1.5'
};
// closing node when parent process is killed
process.stdin.resume();
process.stdin.on('end', () => {
for (const ps of sprocess) {
try {
ps.kill();
}
catch (e) {}
}
process.exit();
});
// process.on('uncaughtException', e => console.error(e));
function observe(msg, push, done) {
if (msg.cmd === 'spec') {
const os = require('os');
const path = require('path');
push({
version: config.version,
env: process.env,
separator: path.sep,
tmpdir: os.tmpdir()
});
done();
}
else if (msg.cmd === 'env') {
push({
env: process.env
});
done();
}
else if (msg.cmd === 'spawn') {
const path = require('path');
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 spawn = require('child_process').spawn;
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 === 'exec') {
const path = require('path');
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 spawn = require('child_process').spawn;
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();
}
}
// 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);
if (msg.uuid) {
delete sessions[msg.uuid];
}
close = () => {};
};
process.addListener('uncaughtException', exception);
const vm = require('vm');
// only install observer if there is a uuid
if (msg.uuid) {
sessions[msg.uuid] = [];
}
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 || []).includes(name) ? require(name) : null,
// install a new session listener
connect: handle => sessions[msg.uuid].push(handle)
};
try {
const script = new vm.Script(msg.script);
const context = vm.createContext(sandbox);
script.runInContext(context);
}
catch (e) {
push({
code: -1001,
type: 'exception',
error: e.message
});
close();
}
// release the message pipeline; the script context stays alive to receive post-messages
done();
}
// communicate with sandboxed sessions later
else if (msg.cmd === 'post-message') {
if (msg.uuid in sessions) {
if (sessions[msg.uuid]) {
for (const f of sessions[msg.uuid]) {
try {
f(msg.data);
}
catch (e) {
push({
code: -1002,
type: 'exception',
error: e.message
});
}
}
push({
type: 'report',
sent: sessions[msg.uuid].length
});
}
else {
push({
code: -1003,
type: 'exception',
error: 'no session with "' + msg.uuid + '" id'
});
}
}
else {
push({
code: -1004,
type: 'exception',
error: 'no listener of this uuid'
});
}
done();
}
else {
// Display warning about old unsupported commands
if (DEPRECATED.includes(msg.cmd)) {
const 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: -1005
});
}
else {
const error = 'This version of the native client does not support "' + msg.cmd + '" command. ' +
'Check for updates...';
push({
error,
cmd: msg.cmd,
code: -1006
});
}
done();
}
}
/* message passing */
const {Input, Transform, Output} = require('./messaging');
process.stdin
.pipe(new Input())
.pipe(new Transform(observe))
.pipe(new Output())
.pipe(process.stdout);