-
Notifications
You must be signed in to change notification settings - Fork 7
/
handler.js
32 lines (28 loc) · 839 Bytes
/
handler.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
const spawn = require('child_process').spawn;
module.exports.hello = function(event, context, callback) {
var child = spawn('./index.rb', [JSON.stringify(event)]);
var stdout = '';
var stderr = '';
child.stdout.on('data', function (data) {
stdout += data.toString();
});
child.stderr.on('data', function (data) {
stderr += data.toString();
});
child.on('close', function(code) {
if (code !== 0) {
return callback(new Error(`Process exited with non-zero status code: ${code}`));
}
if (stderr) {
console.error(stderr);
}
// We expect the child process to output valid JSON with a body
try {
var response = JSON.parse(stdout);
response.body = JSON.stringify(response.body);
callback(null, response);
} catch (error) {
callback(error);
}
});
}