Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import { spaceUpdatingTitle } from "#lib/api/load.ts";import type { WebConfig } from "#lib/server/config.ts";import { themeAttribute, themeFromCookies } from "#lib/theme.ts";
export type PageHosts = Pick<WebConfig, "avatarUrl" | "camoUrl">;
// fills the markers app.html leaves for what only the request or the deploy knowsexport const stampPage = (html: string, request: Request, { avatarUrl, camoUrl }: PageHosts) => { // the markers are all in the head, so replacing there avoids copying the body each time const end = html.indexOf("</head>"); const split = end === -1 ? html.length : end; const head = html .slice(0, split) .replace("%tangled.theme%", themeAttribute(themeFromCookies(request.headers.get("cookie")))) .replace("%tangled.avatar%", avatarUrl) .replace("%tangled.camo%", camoUrl); return head + html.slice(split);};
const mediaQuality = (accept: string, type: string) => { for (const range of accept.toLowerCase().split(",")) { const [media, ...params] = range.split(";").map((part) => part.trim()); if (media !== type) continue; const weight = params.find((param) => param.startsWith("q=")); return weight ? Number(weight.slice(2)) : 1; } return 0;};
const escaped: Record<string, string> = { "&": "&", "<": "<", ">": ">" };
export const errorPage = (template: string, request: Request, status: number, body: App.Error) => { const accept = request.headers.get("accept") ?? ""; const headers = { vary: "accept" }; if (mediaQuality(accept, "application/json") > mediaQuality(accept, "text/html")) { return Response.json(body, { status, headers }); } const message = body.message.replace(/[&<>]/g, (char) => escaped[char]); const updating = status === 503 && body.code === "SpaceUpdating"; const html = template .replaceAll("%sveltekit.status%", updating ? spaceUpdatingTitle : String(status)) .replaceAll("%sveltekit.error.message%", () => message) .replace("<!-- retry action -->", updating ? '<a href="">Try again</a>' : ""); return new Response(html, { status, headers: { ...headers, "content-type": "text/html; charset=utf-8" } });};