git.iko.soy/gigawork-coworking/gigawork-coworking.github.io
on master help sign in
74 lines 2.0 kB history raw
1import * as elmPages from "./elm-pages-cli.mjs";
2import * as busboy from "busboy";
6export const handler = render;
9/**
10 * @param {import('aws-lambda').APIGatewayProxyEvent} event
11 * @param {any} context
12 */
13async function render(event, context) {
14 try {
15 const renderResult = await elmPages.render(await reqToJson(event));
16 const { headers, statusCode } = renderResult;
18 if (renderResult.kind === "bytes") {
19 return {
20 body: Buffer.from(renderResult.body).toString("base64"),
21 isBase64Encoded: true,
22 multiValueHeaders: {
23 "Content-Type": ["application/octet-stream"],
24 "x-powered-by": ["elm-pages"],
25 ...headers,
26 },
27 statusCode,
28 };
29 } else if (renderResult.kind === "api-response") {
30 return {
31 body: renderResult.body,
32 multiValueHeaders: headers,
33 statusCode,
34 isBase64Encoded: renderResult.isBase64Encoded,
35 };
36 } else {
37 return {
38 body: renderResult.body,
39 multiValueHeaders: {
40 "Content-Type": ["text/html"],
41 "x-powered-by": ["elm-pages"],
42 ...headers,
43 },
44 statusCode,
45 };
46 }
47 } catch (error) {
48 console.error(error);
49 console.error(JSON.stringify(error, null, 2));
50 return {
51 body: `<body><h1>Error</h1><pre>${JSON.stringify(error, null, 2)}</pre></body>`,
52 statusCode: 500,
53 multiValueHeaders: {
54 "Content-Type": ["text/html"],
55 "x-powered-by": ["elm-pages"],
56 },
57 };
58 }
61/**
62 * @param {import('aws-lambda').APIGatewayProxyEvent} req
63 * @returns {{method: string; rawUrl: string; body: string?; headers: Record<string, string>; multiPartFormData: unknown }}
64 */
65function reqToJson(req) {
66 return {
67 requestTime: Math.round(new Date().getTime()),
68 method: req.httpMethod,
69 headers: req.headers,
70 rawUrl: req.rawUrl,
71 body: req.body || null,
72 multiPartFormData: null,
73 };