-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
49 lines (38 loc) · 1.22 KB
/
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
const runScript = (filename, botId) => {
const spawn = require('child_process').spawn;
const child = spawn('python', ['-u', filename, botId]);
const stdoutBytes = [];
const stderrBytes = [];
return new Promise((resolve, reject) => {
child.stdout.on('data', (chunk) => {
stdoutBytes.push(chunk);
});
child.stderr.on('data', (chunk) => {
stderrBytes.push(chunk);
});
child.on('close', (code) => {
if (code) {
return reject(filename + ' failed: ' + Buffer.concat(stderrBytes));
}
return resolve(Buffer.concat(stdoutBytes));
});
child.on('error', (err) => {
return reject(filename + ' failed: ' + err.message);
});
});
};
const applyScript = (request, response, filename) => {
return runScript(filename, request.botId)
.then(result => {
response.setHeader('content-type', "image/JPEG");
response.writeHead(200);
response.end(result, "binary");
})
.catch(error => {
response.writeHead(500);
response.end(error);
});
};
module.exports = {
applyScript: applyScript
};