|
| 1 | +var http = require('http'); |
| 2 | +var Buffer = require('buffer').Buffer; |
| 3 | + |
| 4 | +function run() { |
| 5 | + request({ |
| 6 | + url: process.env.AWS_LAMBDA_RUNTIME_API + '/2018-06-01/runtime/invocation/next', |
| 7 | + }, function(err, invoke_result) { |
| 8 | + if (err) { |
| 9 | + return request({ |
| 10 | + url: process.env.AWS_LAMBDA_RUNTIME_API + '/2018-06-01/runtime/init/error', |
| 11 | + method: 'POST', |
| 12 | + data: err |
| 13 | + }, run); |
| 14 | + } |
| 15 | + var event_data = invoke_result.data; |
| 16 | + var request_id = invoke_result.resp.headers['lambda-runtime-aws-request-id']; |
| 17 | + |
| 18 | + var response = require(process.env.LAMBDA_TASK_ROOT + '/' + process.env._HANDLER.split('.')[0] + '.js')[process.env._HANDLER.split('.')[1]](JSON.parse(event_data), {}, function (err, result) { |
| 19 | + if (err) { |
| 20 | + failure(err); |
| 21 | + } else { |
| 22 | + success(result); |
| 23 | + } |
| 24 | + }); |
| 25 | + if (response && response.then && typeof response.then === 'function') { |
| 26 | + response.then(success); |
| 27 | + } |
| 28 | + if (response && response.catch && typeof response.catch === 'function') { |
| 29 | + response.catch(failure); |
| 30 | + } |
| 31 | + |
| 32 | + function success(result) { |
| 33 | + request({ |
| 34 | + url: process.env.AWS_LAMBDA_RUNTIME_API + '/2018-06-01/runtime/invocation/' + request_id + '/response', |
| 35 | + method: 'POST', |
| 36 | + data: result |
| 37 | + }, run); |
| 38 | + } |
| 39 | + function failure(err) { |
| 40 | + request({ |
| 41 | + url: process.env.AWS_LAMBDA_RUNTIME_API + '/2018-06-01/runtime/invocation/' + request_id + '/error', |
| 42 | + method: 'POST', |
| 43 | + data: err |
| 44 | + }, run); |
| 45 | + } |
| 46 | + }); |
| 47 | +} |
| 48 | +run(); |
| 49 | + |
| 50 | +function request(options, cb) { |
| 51 | + if (!cb) { |
| 52 | + cb = function(){}; |
| 53 | + } |
| 54 | + if (options.data && typeof options.data === 'object') { |
| 55 | + options.data = JSON.stringify(options.data); |
| 56 | + } |
| 57 | + if (options.data && !options.headers) { |
| 58 | + options.headers = {}; |
| 59 | + } |
| 60 | + if (options.data && !options.headers['Content-Length']) { |
| 61 | + options.headers['Content-Length'] = Buffer.byteLength(options.data); |
| 62 | + } |
| 63 | + if (options.data && !options.headers['Content-Type']) { |
| 64 | + options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 65 | + } |
| 66 | + options.timeout = 1000; |
| 67 | + var req = http.request('http://' + options.url, options, function(resp) { |
| 68 | + var data = ''; |
| 69 | + resp.on('data', function(chunk) { |
| 70 | + data += chunk; |
| 71 | + }); |
| 72 | + resp.on('end', function() { |
| 73 | + cb(null, {data: data, resp: resp}); |
| 74 | + }); |
| 75 | + }).on('error', function(err) { |
| 76 | + cb(err); |
| 77 | + }); |
| 78 | + |
| 79 | + if (options.data) { |
| 80 | + req.write(options.data); |
| 81 | + } |
| 82 | + req.end(); |
| 83 | +} |
0 commit comments