-
Notifications
You must be signed in to change notification settings - Fork 22
/
handler.js
52 lines (41 loc) · 1.3 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var child_process = require('child_process');
module.exports.handle = (event, context, callback) => {
var response = '';
var php = './php';
// When using 'serverless invoke local' use the system PHP binary instead
if (typeof process.env.PWD !== "undefined") {
php = 'php';
}
// Build the context object data
var contextData = {};
Object.keys(context).forEach(function(key) {
if (typeof context[key] !== 'function') {
contextData[key] = context[key];
}
});
// Launch PHP
var args = ['handler.php', JSON.stringify(event), JSON.stringify(contextData)];
var options = {'stdio': ['pipe', 'pipe', 'pipe', 'pipe']};
var proc = child_process.spawn(php, args, options);
// Request for remaining time from context
proc.stdio[3].on('data', function (data) {
var remaining = context.getRemainingTimeInMillis();
proc.stdio[3].write(`${remaining}\n`);
});
// Output
proc.stdout.on('data', function (data) {
response += data.toString()
});
// Logging
proc.stderr.on('data', function (data) {
console.log(`${data}`);
});
// PHP script execution end
proc.on('close', function(code) {
if (code !== 0) {
return callback(new Error(`Process error code ${code}: ${response}`));
}
callback(null, JSON.parse(response));
});
};