-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.mjs
More file actions
131 lines (121 loc) · 3.3 KB
/
runtime.mjs
File metadata and controls
131 lines (121 loc) · 3.3 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
import http from "http";
import path from "path";
import { Buffer } from "buffer";
import { loadFile } from "/opt/lib/node_modules/nbb/index.mjs";
// TODO is this the only one to support?
const ext = 'cljs';
// The standard format of _HANDLER is file.method, where file is the
// name of the file without an extension, and method is the name of a
// method or function that's defined in the file.
const [filePathNoExt, handlerFuncName] = process.env._HANDLER.split('.');
const handlerFileName = `${filePathNoExt}.${ext}`;
const filePath = path.join(process.env.LAMBDA_TASK_ROOT, handlerFileName);
const file = await loadFile(filePath);
const handler = file[handlerFuncName];
function run() {
request(
{
url:
process.env.AWS_LAMBDA_RUNTIME_API +
"/2018-06-01/runtime/invocation/next",
},
function (err, invoke_result) {
if (err) {
return request(
{
url:
process.env.AWS_LAMBDA_RUNTIME_API +
"/2018-06-01/runtime/init/error",
method: "POST",
data: err,
},
run
);
}
var event_data = invoke_result.data;
var request_id =
invoke_result.resp.headers["lambda-runtime-aws-request-id"];
var response = handler(
JSON.parse(event_data),
{},
function (err, result) {
if (err) {
failure(err);
} else {
success(result);
}
}
);
if (response && response.then && typeof response.then === "function") {
response.then(success);
}
if (response && response.catch && typeof response.catch === "function") {
response.catch(failure);
}
function success(result) {
request(
{
url:
process.env.AWS_LAMBDA_RUNTIME_API +
"/2018-06-01/runtime/invocation/" +
request_id +
"/response",
method: "POST",
data: result,
},
run
);
}
function failure(err) {
request(
{
url:
process.env.AWS_LAMBDA_RUNTIME_API +
"/2018-06-01/runtime/invocation/" +
request_id +
"/error",
method: "POST",
data: err,
},
run
);
}
}
);
}
run();
function request(options, cb) {
if (!cb) {
cb = function () {};
}
if (options.data && typeof options.data === "object") {
options.data = JSON.stringify(options.data);
}
if (options.data && !options.headers) {
options.headers = {};
}
if (options.data && !options.headers["Content-Length"]) {
options.headers["Content-Length"] = Buffer.byteLength(options.data);
}
if (options.data && !options.headers["Content-Type"]) {
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
options.timeout = 1000;
var req = http
.request("http://" + options.url, options, function (resp) {
var data = "";
resp.on("data", function (chunk) {
data += chunk;
});
resp.on("end", function () {
cb(null, { data: data, resp: resp });
});
})
.on("error", function (err) {
cb(err);
});
if (options.data) {
req.write(options.data);
}
req.end();
}