forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.ts
214 lines (191 loc) · 5.2 KB
/
lambda.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { AWS_LAMBDA_RUNTIME_API, LAMBDA_TASK_ROOT, _HANDLER } = process.env;
if (!AWS_LAMBDA_RUNTIME_API || AWS_LAMBDA_RUNTIME_API === "") {
throw new Error("AWS_LAMBDA_RUNTIME_API is not set");
}
const nextURL = `http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next`;
const sourceDir = LAMBDA_TASK_ROOT;
if (!sourceDir) {
throw new Error("handler is not set");
}
// don't care if this fails
if (process.cwd() !== sourceDir) {
try {
process.chdir(sourceDir);
} catch (e) {}
}
var handlerDot = _HANDLER.lastIndexOf(".");
var sourcefile = handlerDot > 0 ? _HANDLER.substring(0, handlerDot) : _HANDLER;
if (sourcefile.length === 0) {
throw new Error("handler is not set");
}
if (!sourcefile.startsWith("/")) {
sourcefile = `./${sourcefile}`;
}
function noop() {}
const method = (handlerDot > 0 ? _HANDLER.substring(handlerDot) : "") || "GET";
if (typeof process.env.VERBOSE !== "undefined") {
console.time(`Loaded ${sourcefile}`);
}
var Handler;
try {
Handler = await import(sourcefile);
} catch (e) {
console.error("Error loading sourcefile:", e);
try {
await fetch(
new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`)
.href,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
errorMessage: e.message,
errorType: e.name,
stackTrace: e?.stack?.split("\n") ?? [],
}),
}
);
} catch (e2) {
console.error("Error sending error to runtime:", e2);
}
process.exit(1);
}
if (typeof process.env.VERBOSE !== "undefined") {
console.timeEnd(`Loaded ${sourcefile}`);
}
const handlerFunction = Handler.default?.fetch;
if (typeof handlerFunction !== "function") {
const e = new Error(`${sourcefile} must export default a function called fetch
Here is an example:
export default {
fetch(req) {
return new Response("Hello World");
}
}
`);
console.error(e);
try {
await fetch(
new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`)
.href,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
errorMessage: e.message,
errorType: e.name,
stackTrace: e?.stack?.split("\n") ?? [],
}),
}
);
} catch (e2) {
console.error("Error sending error to runtime:", e2);
}
process.exit(1);
}
var baseURLString = AWS_LAMBDA_RUNTIME_API;
if ("baseURI" in Handler.default) {
baseURLString = Handler.default.baseURI?.toString();
}
var baseURL;
try {
baseURL = new URL(baseURLString);
} catch (e) {
console.error("Error parsing baseURI:", e);
try {
await fetch(
new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`)
.href,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
errorMessage: e.message,
errorType: e.name,
stackTrace: e?.stack?.split("\n") || [],
}),
}
);
} catch (e2) {
console.error("Error sending error to runtime:", e2);
}
process.exit(1);
}
async function runHandler(response: Response) {
const traceID = response.headers.get("Lambda-Runtime-Trace-Id");
const requestID = response.headers.get("Lambda-Runtime-Aws-Request-Id");
var request = new Request(baseURL.href, {
method,
headers: response.headers,
body:
parseInt(response.headers.get("Content-Length") || "0", 10) > 0
? await response.blob()
: undefined,
});
// we are done with the Response object here
// allow it to be GC'd
response = undefined;
var result: Response;
try {
if (typeof process.env.VERBOSE !== "undefined") {
console.time(`[${traceID}] Run ${request.url}`);
}
result = handlerFunction(request, {});
if (result && result.then) {
await result;
}
} catch (e1) {
if (typeof process.env.VERBOSE !== "undefined") {
console.error(`[${traceID}] Error running handler:`, e1);
}
fetch(
`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/error`,
{
method: "POST",
body: JSON.stringify({
errorMessage: e1.message,
errorType: e1.name,
stackTrace: e1?.stack?.split("\n") ?? [],
}),
}
).finally(noop);
return;
} finally {
if (typeof process.env.VERBOSE !== "undefined") {
console.timeEnd(`[${traceID}] Run ${request.url}`);
}
}
if (!result || !("headers" in result)) {
await fetch(
`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/error`,
{
method: "POST",
body: JSON.stringify({
errorMessage: "Expected Response object",
errorType: "ExpectedResponseObject",
stackTrace: [],
}),
}
);
return;
}
await fetch(
`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/response`,
{
method: "POST",
headers: result.headers,
body: await result.blob(),
}
);
result = undefined;
}
while (true) {
fetch(nextURL).then(runHandler, console.error);
}
export {};