-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
entry.js
57 lines (49 loc) · 1.27 KB
/
entry.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
53
54
55
56
57
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
init();
export async function handler(event) {
const { path, httpMethod, headers, rawQuery, body, isBase64Encoded } = event;
const query = new URLSearchParams(rawQuery);
const encoding = isBase64Encoded ? 'base64' : headers['content-encoding'] || 'utf-8';
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;
const rendered = await render({
method: httpMethod,
headers,
path,
query,
rawBody
});
if (rendered) {
return {
isBase64Encoded: false,
statusCode: rendered.status,
...splitHeaders(rendered.headers),
body: rendered.body
};
}
return {
statusCode: 404,
body: 'Not found'
};
}
/**
* Splits headers into two categories: single value and multi value
* @param {Record<string, string | string[]>} headers
* @returns {{
* headers: Record<string, string>,
* multiValueHeaders: Record<string, string[]>
* }}
*/
function splitHeaders(headers) {
const h = {};
const m = {};
for (const key in headers) {
const value = headers[key];
const target = Array.isArray(value) ? m : h;
target[key] = value;
}
return {
headers: h,
multiValueHeaders: m
};
}