2023-01-27 21:18:19 +03:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2021-06-02 20:02:03 +03:00
|
|
|
|
2022-03-10 22:49:07 +03:00
|
|
|
export default async function run({
|
2021-12-20 22:26:12 +03:00
|
|
|
renderFunctionFilePath,
|
|
|
|
routePatterns,
|
|
|
|
apiRoutePatterns,
|
2022-03-03 20:29:50 +03:00
|
|
|
portsFilePath,
|
2022-04-06 18:54:23 +03:00
|
|
|
htmlTemplate,
|
2021-12-20 22:26:12 +03:00
|
|
|
}) {
|
2022-03-10 22:49:07 +03:00
|
|
|
console.log("Running adapter script");
|
2021-12-18 22:23:12 +03:00
|
|
|
ensureDirSync("functions/render");
|
|
|
|
ensureDirSync("functions/server-render");
|
|
|
|
|
2021-06-02 20:02:03 +03:00
|
|
|
fs.copyFileSync(
|
|
|
|
renderFunctionFilePath,
|
2023-01-25 20:00:19 +03:00
|
|
|
"./functions/render/elm-pages-cli.cjs"
|
2021-06-02 20:02:03 +03:00
|
|
|
);
|
2021-12-17 07:37:32 +03:00
|
|
|
fs.copyFileSync(
|
|
|
|
renderFunctionFilePath,
|
2023-01-25 20:00:19 +03:00
|
|
|
"./functions/server-render/elm-pages-cli.cjs"
|
2021-12-17 07:37:32 +03:00
|
|
|
);
|
2022-03-03 20:29:50 +03:00
|
|
|
|
2022-02-22 03:54:51 +03:00
|
|
|
fs.writeFileSync(
|
2023-01-23 02:36:11 +03:00
|
|
|
"./functions/render/index.mjs",
|
2022-04-06 18:54:23 +03:00
|
|
|
rendererCode(true, htmlTemplate)
|
2022-02-22 03:54:51 +03:00
|
|
|
);
|
|
|
|
fs.writeFileSync(
|
2023-01-23 02:36:11 +03:00
|
|
|
"./functions/server-render/index.mjs",
|
2022-04-06 18:54:23 +03:00
|
|
|
rendererCode(false, htmlTemplate)
|
2022-02-22 03:54:51 +03:00
|
|
|
);
|
2021-06-02 20:02:03 +03:00
|
|
|
// TODO rename functions/render to functions/fallback-render
|
2021-06-02 21:02:35 +03:00
|
|
|
// TODO prepend instead of writing file
|
2021-12-17 22:14:29 +03:00
|
|
|
|
2021-12-21 21:01:42 +03:00
|
|
|
const apiServerRoutes = apiRoutePatterns.filter(isServerSide);
|
|
|
|
|
|
|
|
ensureValidRoutePatternsForNetlify(apiServerRoutes);
|
2021-12-20 22:26:12 +03:00
|
|
|
|
|
|
|
// TODO filter apiRoutePatterns on is server side
|
|
|
|
// TODO need information on whether api route is odb or serverless
|
2021-12-21 21:01:42 +03:00
|
|
|
const apiRouteRedirects = apiServerRoutes
|
2021-12-20 22:26:12 +03:00
|
|
|
.map((apiRoute) => {
|
2021-12-21 00:35:27 +03:00
|
|
|
if (apiRoute.kind === "prerender-with-fallback") {
|
2021-12-21 20:08:17 +03:00
|
|
|
return `${apiPatternToRedirectPattern(
|
|
|
|
apiRoute.pathPattern
|
2021-12-23 21:53:43 +03:00
|
|
|
)} /.netlify/builders/render 200`;
|
2021-12-21 00:35:27 +03:00
|
|
|
} else if (apiRoute.kind === "serverless") {
|
|
|
|
return `${apiPatternToRedirectPattern(
|
|
|
|
apiRoute.pathPattern
|
|
|
|
)} /.netlify/functions/server-render 200`;
|
|
|
|
} else {
|
|
|
|
throw "Unhandled 2";
|
|
|
|
}
|
2021-12-20 22:26:12 +03:00
|
|
|
})
|
|
|
|
.join("\n");
|
|
|
|
|
2021-12-16 02:39:35 +03:00
|
|
|
const redirectsFile =
|
|
|
|
routePatterns
|
|
|
|
.filter(isServerSide)
|
|
|
|
.map((route) => {
|
2021-12-16 02:44:48 +03:00
|
|
|
if (route.kind === "prerender-with-fallback") {
|
2021-12-23 21:53:43 +03:00
|
|
|
return `${route.pathPattern} /.netlify/builders/render 200
|
2022-02-04 07:08:45 +03:00
|
|
|
${route.pathPattern}/content.dat /.netlify/builders/render 200`;
|
2021-12-16 02:39:35 +03:00
|
|
|
} else {
|
2021-12-16 23:05:18 +03:00
|
|
|
return `${route.pathPattern} /.netlify/functions/server-render 200
|
2022-02-04 07:08:45 +03:00
|
|
|
${route.pathPattern}/content.dat /.netlify/functions/server-render 200`;
|
2021-12-16 02:39:35 +03:00
|
|
|
}
|
|
|
|
})
|
2021-12-21 00:35:27 +03:00
|
|
|
.join("\n") +
|
|
|
|
"\n" +
|
|
|
|
apiRouteRedirects +
|
|
|
|
"\n";
|
2021-06-02 20:02:03 +03:00
|
|
|
|
2021-12-16 02:39:35 +03:00
|
|
|
fs.writeFileSync("dist/_redirects", redirectsFile);
|
2021-06-02 20:02:03 +03:00
|
|
|
}
|
|
|
|
|
2021-12-20 22:26:12 +03:00
|
|
|
function ensureValidRoutePatternsForNetlify(apiRoutePatterns) {
|
|
|
|
const invalidNetlifyRoutes = apiRoutePatterns.filter((apiRoute) =>
|
|
|
|
apiRoute.pathPattern.some(({ kind }) => kind === "hybrid")
|
|
|
|
);
|
|
|
|
if (invalidNetlifyRoutes.length > 0) {
|
|
|
|
throw (
|
|
|
|
"Invalid Netlify routes!\n" +
|
|
|
|
invalidNetlifyRoutes
|
|
|
|
.map((value) => JSON.stringify(value, null, 2))
|
|
|
|
.join(", ")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 02:39:35 +03:00
|
|
|
function isServerSide(route) {
|
|
|
|
return (
|
|
|
|
route.kind === "prerender-with-fallback" || route.kind === "serverless"
|
|
|
|
);
|
2021-06-02 20:02:03 +03:00
|
|
|
}
|
|
|
|
|
2021-12-17 22:14:29 +03:00
|
|
|
/**
|
|
|
|
* @param {boolean} isOnDemand
|
2022-04-06 18:54:23 +03:00
|
|
|
* @param {string} htmlTemplate
|
2021-12-17 22:14:29 +03:00
|
|
|
*/
|
2022-04-06 18:54:23 +03:00
|
|
|
function rendererCode(isOnDemand, htmlTemplate) {
|
2023-01-23 01:17:47 +03:00
|
|
|
return `import * as path from "path";
|
|
|
|
import * as busboy from "busboy";
|
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
import * as renderer from "../../../../generator/src/render.js";
|
|
|
|
import * as preRenderHtml from "../../../../generator/src/pre-render-html.js";
|
2023-01-27 21:18:19 +03:00
|
|
|
import * as customBackendTask from "${path.resolve(portsFilePath)}";
|
2022-04-06 18:54:23 +03:00
|
|
|
const htmlTemplate = ${JSON.stringify(htmlTemplate)};
|
2021-12-30 00:02:45 +03:00
|
|
|
|
2021-12-17 22:14:29 +03:00
|
|
|
${
|
|
|
|
isOnDemand
|
2023-01-23 01:17:47 +03:00
|
|
|
? `import { builder } from "@netlify/functions";
|
2021-12-17 22:14:29 +03:00
|
|
|
|
2023-01-23 01:17:47 +03:00
|
|
|
export const handler = builder(render);`
|
2021-12-17 22:14:29 +03:00
|
|
|
: `
|
|
|
|
|
2023-01-23 01:17:47 +03:00
|
|
|
export const handler = render;`
|
2021-12-17 22:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import('aws-lambda').APIGatewayProxyEvent} event
|
|
|
|
* @param {any} context
|
|
|
|
*/
|
|
|
|
async function render(event, context) {
|
2021-12-24 00:34:54 +03:00
|
|
|
const requestTime = new Date();
|
2021-12-17 22:14:29 +03:00
|
|
|
console.log(JSON.stringify(event));
|
2023-01-23 01:17:47 +03:00
|
|
|
|
2021-12-17 22:14:29 +03:00
|
|
|
try {
|
|
|
|
const basePath = "/";
|
|
|
|
const mode = "build";
|
|
|
|
const addWatcher = () => {};
|
|
|
|
|
2022-11-22 22:21:11 +03:00
|
|
|
const renderResult = await renderer.render(
|
2023-01-27 21:18:19 +03:00
|
|
|
customBackendTask,
|
2021-12-17 22:14:29 +03:00
|
|
|
basePath,
|
2023-01-25 20:00:19 +03:00
|
|
|
(await import("./elm-pages-cli.cjs")).default,
|
2021-12-17 22:14:29 +03:00
|
|
|
mode,
|
|
|
|
event.path,
|
2021-12-30 00:21:49 +03:00
|
|
|
await reqToJson(event, requestTime),
|
2021-12-30 04:18:37 +03:00
|
|
|
addWatcher,
|
|
|
|
false
|
2021-12-17 22:14:29 +03:00
|
|
|
);
|
2022-02-04 22:17:24 +03:00
|
|
|
console.log("@@@renderResult", JSON.stringify(renderResult, null, 2));
|
2021-12-17 22:14:29 +03:00
|
|
|
|
2022-01-18 03:56:40 +03:00
|
|
|
const statusCode = renderResult.is404 ? 404 : renderResult.statusCode;
|
2021-12-17 22:14:29 +03:00
|
|
|
|
2022-02-04 22:07:24 +03:00
|
|
|
if (renderResult.kind === "bytes") {
|
2021-12-17 22:14:29 +03:00
|
|
|
return {
|
2022-02-04 22:17:24 +03:00
|
|
|
body: Buffer.from(renderResult.contentDatPayload.buffer).toString("base64"),
|
|
|
|
isBase64Encoded: true,
|
2021-12-17 22:14:29 +03:00
|
|
|
headers: {
|
2022-02-04 22:07:24 +03:00
|
|
|
"Content-Type": "application/octet-stream",
|
2021-12-17 22:14:29 +03:00
|
|
|
"x-powered-by": "elm-pages",
|
2022-01-18 03:56:40 +03:00
|
|
|
...renderResult.headers,
|
2021-12-21 00:55:24 +03:00
|
|
|
},
|
|
|
|
statusCode,
|
|
|
|
};
|
|
|
|
} else if (renderResult.kind === "api-response") {
|
2021-12-21 01:45:18 +03:00
|
|
|
const serverResponse = renderResult.body;
|
2021-12-21 00:55:24 +03:00
|
|
|
return {
|
2021-12-21 01:45:18 +03:00
|
|
|
body: serverResponse.body,
|
2021-12-22 04:33:41 +03:00
|
|
|
multiValueHeaders: serverResponse.headers,
|
2021-12-21 01:45:18 +03:00
|
|
|
statusCode: serverResponse.statusCode,
|
2021-12-21 20:45:25 +03:00
|
|
|
isBase64Encoded: serverResponse.isBase64Encoded,
|
2021-12-17 22:14:29 +03:00
|
|
|
};
|
|
|
|
} else {
|
2022-04-06 18:54:23 +03:00
|
|
|
console.log('@rendering', preRenderHtml.replaceTemplate(htmlTemplate, renderResult.htmlString))
|
2021-12-17 22:14:29 +03:00
|
|
|
return {
|
2022-04-06 18:54:23 +03:00
|
|
|
body: preRenderHtml.replaceTemplate(htmlTemplate, renderResult.htmlString),
|
2021-12-17 22:14:29 +03:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
"x-powered-by": "elm-pages",
|
2022-01-18 03:56:40 +03:00
|
|
|
...renderResult.headers,
|
2021-12-17 22:14:29 +03:00
|
|
|
},
|
|
|
|
statusCode,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-01-27 20:43:37 +03:00
|
|
|
console.log('ERROR')
|
2021-12-17 22:14:29 +03:00
|
|
|
console.error(error);
|
2023-01-27 20:43:37 +03:00
|
|
|
console.error(JSON.stringify(error, null, 2));
|
2021-12-17 22:14:29 +03:00
|
|
|
return {
|
|
|
|
body: \`<body><h1>Error</h1><pre>\${error.toString()}</pre></body>\`,
|
|
|
|
statusCode: 500,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
"x-powered-by": "elm-pages",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import('aws-lambda').APIGatewayProxyEvent} req
|
2021-12-24 00:34:54 +03:00
|
|
|
* @param {Date} requestTime
|
2021-12-30 00:58:21 +03:00
|
|
|
* @returns {Promise<{ method: string; hostname: string; query: Record<string, string | undefined>; headers: Record<string, string>; host: string; pathname: string; port: number | null; protocol: string; rawUrl: string; }>}
|
2021-12-17 22:14:29 +03:00
|
|
|
*/
|
2021-12-24 00:34:54 +03:00
|
|
|
function reqToJson(req, requestTime) {
|
2021-12-30 00:02:45 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (
|
2021-12-30 00:37:47 +03:00
|
|
|
req.httpMethod && req.httpMethod.toUpperCase() === "POST" &&
|
2021-12-30 00:02:45 +03:00
|
|
|
req.headers["content-type"] &&
|
|
|
|
req.headers["content-type"].includes("multipart/form-data") &&
|
|
|
|
req.body
|
|
|
|
) {
|
|
|
|
try {
|
2021-12-30 00:37:47 +03:00
|
|
|
console.log('@@@1');
|
2021-12-30 00:02:45 +03:00
|
|
|
const bb = busboy({
|
|
|
|
headers: req.headers,
|
|
|
|
});
|
|
|
|
let fields = {};
|
|
|
|
|
|
|
|
bb.on("file", (fieldname, file, info) => {
|
2021-12-30 00:37:47 +03:00
|
|
|
console.log('@@@2');
|
2021-12-30 00:02:45 +03:00
|
|
|
const { filename, encoding, mimeType } = info;
|
|
|
|
|
|
|
|
file.on("data", (data) => {
|
|
|
|
fields[fieldname] = {
|
|
|
|
filename,
|
|
|
|
mimeType,
|
|
|
|
body: data.toString(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
bb.on("field", (fieldName, value) => {
|
2021-12-30 00:28:20 +03:00
|
|
|
console.log("@@@field", fieldName, value);
|
2021-12-30 00:02:45 +03:00
|
|
|
fields[fieldName] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO skip parsing JSON and form data body if busboy doesn't run
|
|
|
|
bb.on("close", () => {
|
2021-12-30 00:37:47 +03:00
|
|
|
console.log('@@@3');
|
2021-12-30 00:28:20 +03:00
|
|
|
console.log("@@@close", fields);
|
2021-12-30 00:02:45 +03:00
|
|
|
resolve(toJsonHelper(req, requestTime, fields));
|
|
|
|
});
|
2021-12-30 00:37:47 +03:00
|
|
|
console.log('@@@4');
|
2021-12-30 01:25:46 +03:00
|
|
|
|
|
|
|
if (req.isBase64Encoded) {
|
|
|
|
bb.write(Buffer.from(req.body, 'base64').toString('utf8'));
|
|
|
|
} else {
|
|
|
|
bb.write(req.body);
|
|
|
|
}
|
2021-12-30 00:02:45 +03:00
|
|
|
} catch (error) {
|
2021-12-30 00:37:47 +03:00
|
|
|
console.error('@@@5', error);
|
2021-12-30 00:02:45 +03:00
|
|
|
resolve(toJsonHelper(req, requestTime, null));
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-30 00:37:47 +03:00
|
|
|
console.log('@@@6');
|
2021-12-30 00:02:45 +03:00
|
|
|
resolve(toJsonHelper(req, requestTime, null));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-12-22 03:11:51 +03:00
|
|
|
|
2022-03-03 19:52:33 +03:00
|
|
|
/**
|
|
|
|
* @param {import('aws-lambda').APIGatewayProxyEvent} req
|
|
|
|
* @param {Date} requestTime
|
|
|
|
* @returns {{method: string; rawUrl: string; body: string?; headers: Record<string, string>; requestTime: number; multiPartFormData: unknown }}
|
|
|
|
*/
|
2021-12-30 00:02:45 +03:00
|
|
|
function toJsonHelper(req, requestTime, multiPartFormData) {
|
2021-12-17 22:14:29 +03:00
|
|
|
return {
|
|
|
|
method: req.httpMethod,
|
|
|
|
headers: req.headers,
|
2022-03-03 19:52:33 +03:00
|
|
|
rawUrl: req.rawUrl,
|
2021-12-23 02:28:19 +03:00
|
|
|
body: req.body,
|
2021-12-24 00:34:54 +03:00
|
|
|
requestTime: Math.round(requestTime.getTime()),
|
2021-12-30 00:02:45 +03:00
|
|
|
multiPartFormData: multiPartFormData,
|
2021-12-17 22:14:29 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
}
|
2021-12-18 22:23:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {fs.PathLike} dirpath
|
|
|
|
*/
|
|
|
|
function ensureDirSync(dirpath) {
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(dirpath, { recursive: true });
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "EEXIST") throw err;
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 22:26:12 +03:00
|
|
|
|
|
|
|
/** @typedef {{kind: 'dynamic'} | {kind: 'literal', value: string}} ApiSegment */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ApiSegment[]} pathPattern
|
|
|
|
*/
|
|
|
|
function apiPatternToRedirectPattern(pathPattern) {
|
2021-12-21 00:35:27 +03:00
|
|
|
return (
|
|
|
|
"/" +
|
|
|
|
pathPattern
|
2022-01-17 22:46:40 +03:00
|
|
|
.map((segment, index) => {
|
2021-12-21 00:35:27 +03:00
|
|
|
switch (segment.kind) {
|
|
|
|
case "literal": {
|
|
|
|
return segment.value;
|
|
|
|
}
|
|
|
|
case "dynamic": {
|
2022-03-11 22:11:50 +03:00
|
|
|
return `:dynamic${index}`;
|
2021-12-21 00:35:27 +03:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw "Unhandled segment: " + JSON.stringify(segment);
|
|
|
|
}
|
2021-12-20 22:26:12 +03:00
|
|
|
}
|
2021-12-21 00:35:27 +03:00
|
|
|
})
|
|
|
|
.join("/")
|
|
|
|
);
|
2021-12-20 22:26:12 +03:00
|
|
|
}
|