/
runtime.ts
242 lines (207 loc) · 6.32 KB
/
runtime.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as base64 from 'https://deno.land/x/base64/mod.ts';
import { TextProtoReader } from 'https://deno.land/std@0.63.0/textproto/mod.ts';
import { BufReader, BufWriter } from 'https://deno.land/std@0.63.0/io/bufio.ts';
import {
ServerRequest,
Response,
} from 'https://deno.land/std@0.63.0/http/server.ts';
import { Context } from 'https://denopkg.com/DefinitelyTyped/DefinitelyTyped/types/aws-lambda/handler.d.ts';
type Handler = (req: ServerRequest) => Promise<Response | void>;
const RUNTIME_PATH = '2018-06-01/runtime';
const {
AWS_LAMBDA_FUNCTION_NAME,
AWS_LAMBDA_FUNCTION_VERSION,
AWS_LAMBDA_FUNCTION_MEMORY_SIZE,
AWS_LAMBDA_LOG_GROUP_NAME,
AWS_LAMBDA_LOG_STREAM_NAME,
LAMBDA_TASK_ROOT,
_HANDLER,
AWS_LAMBDA_RUNTIME_API,
} = Deno.env.toObject();
Deno.env.delete('SHLVL');
async function start(): Promise<void> {
try {
await processEvents();
} catch (err) {
console.error(err);
Deno.exit(1);
}
}
async function processEvents(): Promise<void> {
let handler: Handler | null = null;
while (true) {
const { event, context } = await nextInvocation();
let result;
try {
if (!handler) {
const mod = await import(`./${_HANDLER}`);
handler = mod.default;
if (typeof handler !== 'function') {
throw new Error('Failed to load handler function');
}
}
const data = JSON.parse(event.body);
const input = new Deno.Buffer(base64.toUint8Array(data.body || ''));
const output = new Deno.Buffer(new Uint8Array(6000000)); // 6 MB
const req = new ServerRequest();
// req.conn
req.r = new BufReader(input, input.length);
req.method = data.method;
req.url = data.path;
req.proto = 'HTTP/1.1';
req.protoMinor = 1;
req.protoMajor = 1;
req.headers = new Headers();
for (const [name, value] of Object.entries(data.headers)) {
if (typeof value === 'string') {
req.headers.set(name, value);
}
// TODO: handle multi-headers?
}
req.w = new BufWriter(output);
// Run user code
const res = await handler(req);
if (res) {
req.respond(res);
}
const responseError = await req.done;
if (responseError) {
throw responseError;
}
const bufr = new BufReader(output, output.length);
const tp = new TextProtoReader(bufr);
const firstLine = await tp.readLine(); // e.g. "HTTP/1.1 200 OK"
if (firstLine === null) throw new Deno.errors.UnexpectedEof();
const headers = await tp.readMIMEHeader();
if (headers === null) throw new Deno.errors.UnexpectedEof();
const headersObj: { [name: string]: string } = {};
for (const [name, value] of headers.entries()) {
headersObj[name] = value;
}
const body = await bufr.readFull(new Uint8Array(bufr.buffered()));
if (!body) throw new Deno.errors.UnexpectedEof();
await req.finalize();
result = {
statusCode: parseInt(firstLine.split(' ', 2)[1], 10),
headers: headersObj,
encoding: 'base64',
body: base64.fromUint8Array(body),
};
} catch (e) {
console.error('Invoke Error:', e);
await invokeError(e, context);
continue;
}
await invokeResponse(result, context);
}
}
async function initError(err: Error) {
return postError('init/error', err);
}
async function nextInvocation() {
const res = await request('invocation/next');
if (res.status !== 200) {
throw new Error(
`Unexpected "/invocation/next" response: ${JSON.stringify(res)}`
);
}
const deadlineMs = Number(res.headers.get('lambda-runtime-deadline-ms'));
const traceId = res.headers.get('lambda-runtime-trace-id');
if (typeof traceId === 'string') {
Deno.env.set('_X_AMZN_TRACE_ID', traceId);
} else {
Deno.env.delete('_X_AMZN_TRACE_ID');
}
const awsRequestId = res.headers.get('lambda-runtime-aws-request-id');
if (typeof awsRequestId !== 'string') {
throw new Error(
'Did not receive "lambda-runtime-aws-request-id" header'
);
}
const invokedFunctionArn = res.headers.get(
'lambda-runtime-invoked-function-arn'
);
if (typeof invokedFunctionArn !== 'string') {
throw new Error(
'Did not receive "lambda-runtime-invoked-function-arn" header'
);
}
const context: Context = {
callbackWaitsForEmptyEventLoop: false,
logGroupName: AWS_LAMBDA_LOG_GROUP_NAME,
logStreamName: AWS_LAMBDA_LOG_STREAM_NAME,
functionName: AWS_LAMBDA_FUNCTION_NAME,
memoryLimitInMB: AWS_LAMBDA_FUNCTION_MEMORY_SIZE,
functionVersion: AWS_LAMBDA_FUNCTION_VERSION,
awsRequestId,
invokedFunctionArn,
getRemainingTimeInMillis: () => deadlineMs - Date.now(),
done: (error?: Error, result?: any): void => {},
fail: (error: Error | string): void => {},
succeed: (messageOrObject: any, obj?: any): void => {},
};
const clientContext = res.headers.get('lambda-runtime-client-context');
if (clientContext) {
context.clientContext = JSON.parse(clientContext);
}
const cognitoIdentity = res.headers.get('lambda-runtime-cognito-identity');
if (cognitoIdentity) {
context.identity = JSON.parse(cognitoIdentity);
}
const event = JSON.parse(res.body);
return { event, context };
}
async function invokeResponse(result: any, context: Context) {
const res = await request(`invocation/${context.awsRequestId}/response`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(result),
});
if (res.status !== 202) {
throw new Error(
`Unexpected "/invocation/response" response: ${JSON.stringify(res)}`
);
}
}
async function invokeError(err: Error, context: Context) {
return postError(`invocation/${context.awsRequestId}/error`, err);
}
async function postError(path: string, err: Error): Promise<void> {
const lambdaErr = toLambdaErr(err);
const res = await request(path, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Lambda-Runtime-Function-Error-Type': 'Unhandled',
},
body: JSON.stringify(lambdaErr),
});
if (res.status !== 202) {
throw new Error(
`Unexpected "${path}" response: ${JSON.stringify(res)}`
);
}
}
async function request(path: string, options?: RequestInit) {
const url = `http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/${path}`;
const res = await fetch(url, options);
const body = await res.text();
return {
status: res.status,
headers: res.headers,
body,
};
}
function toLambdaErr({ name, message, stack }: Error) {
return {
errorType: name,
errorMessage: message,
stackTrace: (stack || '').split('\n').slice(1),
};
}
start().catch((err) => {
console.error(err);
Deno.exit(1);
});