Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

9.0 KB
Raw Blame
import type { ResumeMatch } from "@reactive-resume/api/features/resume/web-page";
import type { ResumeWebPage } from "@reactive-resume/pdf/web-page";
import type { ResumeData } from "@reactive-resume/schema/resume/data";
import { createHash } from "node:crypto";
import { loadPublicWebPage, verifyWebPagePassword } from "@reactive-resume/api/features/resume/web-page";
import { env } from "@reactive-resume/env/server";
import { renderResumeWebPage, webPageImageUrl } from "@reactive-resume/pdf/web-page";
import {
	localeFromAcceptLanguage,
	renderPasswordDocument,
	renderUnavailableDocument,
	renderWebPageDocument,
	WEB_PAGE_SCRIPT,
	webPageMessages,
} from "@reactive-resume/pdf/web-page-document";
import { publicResumePath } from "@reactive-resume/resume/sharing";
import { getResumeSocialMeta } from "@reactive-resume/resume/social-meta";
import { mistypedAppPath } from "@reactive-resume/utils/app-paths";
import { getPageLocale } from "./locale";
import { BASE_SECURITY_HEADERS, contentSecurityPolicy } from "./web";

const origin = () => new URL(env.APP_URL).origin;

function html(body: string, status: number, headers: Record<string, string> = {}) {
	return new Response(body, {
		status,
		headers: {
			...BASE_SECURITY_HEADERS,
			"Content-Type": "text/html; charset=UTF-8",
			// Views are counted per request and the page follows every edit, so no cache may keep it.
			"Cache-Control": "private, no-store",
			"Content-Security-Policy": contentSecurityPolicy(`<script>${WEB_PAGE_SCRIPT}</script>`),
			...headers,
		},
	});
}

// Rendered sheets by the content they draw (already redacted), so repeat visits skip React, HTML and CSS.
// ponytail: per process, cleared at 100 entries; a shared cache if renders show up under load.
const rendered = new Map<string, ResumeWebPage>();

async function renderSheet(data: ResumeData) {
	const key = createHash("sha256").update(JSON.stringify(data)).digest("hex");
	const hit = rendered.get(key);
	if (hit) return hit;
	const page = await renderResumeWebPage({ data, imageUrl: webPageImageUrl(origin()) });
	if (rendered.size >= 100) rendered.clear();
	rendered.set(key, page);
	return page;
}

/** The resume as a person, for search engines: who it is, what they do, and where else they're found. */
function structuredData(data: ResumeData, url: string, updatedAt: Date) {
	const sameAs = [data.basics.website.url, ...data.sections.profiles.items.map((item) => item.website.url)].filter(
		(link) => /^https?:\/\//i.test(link),
	);
	const image = data.picture.url ? webPageImageUrl(origin())(data.picture.url) : undefined;
	return {
		"@context": "https://schema.org",
		"@type": "ProfilePage",
		url,
		dateModified: updatedAt.toISOString(),
		mainEntity: {
			"@type": "Person",
			name: data.basics.name,
			...(data.basics.headline ? { jobTitle: data.basics.headline } : {}),
			...(data.basics.location ? { homeLocation: { "@type": "Place", name: data.basics.location } } : {}),
			...(image && !image.startsWith("data:") ? { image } : {}),
			...(sameAs.length > 0 ? { sameAs } : {}),
		},
	};
}

type PageOptions = {
	/** Set on a custom domain: the page lives at its root there. */
	customDomain?: string | undefined;
};

/**
 * Off, unknown or trashed resumes read the same, in the visitor's language; the owner of an unpublished one is told
 * it's theirs, with a link to its Share panel.
 */
function unavailableResponse(request: Request, ownerResumeId?: string) {
	const lang = getPageLocale(request)?.locale ?? localeFromAcceptLanguage(request.headers.get("accept-language"));
	const shareUrl = ownerResumeId ? `${origin()}/builder/${encodeURIComponent(ownerResumeId)}?mode=share` : undefined;
	// `html` sends it private and no-store, so the owner's version is never cached for anyone else.
	return html(
		renderUnavailableDocument({ lang, messages: webPageMessages(lang), homeUrl: `${origin()}/`, shareUrl }),
		404,
	);
}

/**
 * A resume's web page, wherever it's served: `/<username>/<slug>`, `/<username>` (the primary resume), the instance's
 * root (`ROOT_RESUME_ID`, matched by ID) or a custom domain. GET shows the page, its password form, or the page saying
 * it isn't shared; POST is the password form, whose success sets the access cookie and shows the page.
 */
export function serveResumePage(
	request: Request,
	match: ResumeMatch,
	trustedClient: string,
	options: PageOptions = {},
): Promise<Response> {
	return request.method === "POST"
		? submitPassword(request, match, trustedClient, options)
		: showPage(request, match, trustedClient, options);
}

/**
 * A moved public page. A slug's old address is a stable redirect to the same document, so it stays a permanent 301;
 * a username's old address stops working after 30 days and the name can then be claimed by another account, so its
 * redirect must not be cached as permanent (a browser-cached 301 would skip the server and land on the new owner).
 * Slug mappings expire too; no-store lets a reused slug resolve to its current document.
 */
export function movedResponse(
	request: Request,
	moved: { username: string; slug: string; isPrimary: boolean; movedBy: "username" | "slug" },
) {
	const target = new URL(request.url);
	target.pathname = publicResumePath(moved);
	const permanent = moved.movedBy === "slug";
	return new Response(null, {
		status: permanent ? 301 : 302,
		headers: {
			Location: `${target.pathname}${target.search}`,
			"Cache-Control": "no-store",
		},
	});
}

async function showPage(request: Request, match: ResumeMatch, trustedClient: string, options: PageOptions) {
	const previewToken = new URL(request.url).searchParams.get("preview") ?? undefined;
	const result = await loadPublicWebPage({ match, requestHeaders: request.headers, trustedClient, previewToken });

	if (result.status === "unavailable") {
		// `/applications` typed without `/dashboard` is the app's page, not a resume that isn't shared.
		const appPath =
			!options.customDomain && "username" in match ? mistypedAppPath(new URL(request.url).pathname) : null;
		if (appPath)
			return new Response(null, {
				status: 302,
				headers: { Location: `${appPath}${new URL(request.url).search}`, "Cache-Control": "no-store" },
			});
		return unavailableResponse(request, result.ownerResumeId);
	}
	if (result.status === "moved") return movedResponse(request, result);
	if (result.status === "password") {
		const lang = result.locale;
		const wrongPassword = new URL(request.url).searchParams.has("wrong-password");
		return html(renderPasswordDocument({ lang, messages: webPageMessages(lang), wrongPassword }), 401);
	}

	const { data } = result;
	const page = await renderSheet(data);
	const own = publicResumePath({ username: result.username, slug: result.slug });
	// One address for search engines, whichever domain or path served it: the custom domain, the root, the primary
	// resume's `/<username>`, or its own address, always on APP_URL.
	const domain = options.customDomain ?? result.customDomain;
	const canonicalUrl = domain
		? `https://${domain}/`
		: "id" in match
			? `${origin()}/`
			: `${origin()}${publicResumePath(result)}`;
	const social = getResumeSocialMeta(data);

	const body = renderWebPageDocument({
		page,
		messages: webPageMessages(data.metadata.page.locale),
		head: {
			title: social.title,
			description: social.description,
			canonicalUrl,
			indexable: result.indexable,
			// Password-protected pages show the generic banner; the card itself refuses them too.
			imageUrl: result.hasPassword ? `${origin()}/opengraph/banner.jpg` : `${origin()}/cards${own}.png`,
			structuredData: result.indexable ? structuredData(data, canonicalUrl, result.updatedAt) : undefined,
		},
		downloadUrl: result.showDownloadButtons ? `/api/resumes${own}/pdf?download` : undefined,
		wordDownloadUrl: result.allowWordDownload ? `/api/resumes${own}/docx` : undefined,
		creditUrl: data.metadata.web?.showCredit === false ? undefined : origin(),
	});
	return html(
		body,
		200,
		result.indexable ? {} : { "X-Robots-Tag": previewToken ? "noindex, nofollow" : "noindex, follow" },
	);
}

// The password form is one short field. Checked here, not by the route, because a custom domain's page is served by
// middleware that runs before any route's body limit.
const MAX_FORM_BYTES = 16 * 1024;

async function submitPassword(request: Request, match: ResumeMatch, trustedClient: string, options: PageOptions) {
	const length = request.headers.get("content-length");
	if (length === null || !(Number(length) <= MAX_FORM_BYTES))
		return new Response("Payload Too Large", { status: 413, headers: { "Cache-Control": "no-store" } });
	const form = await request.formData().catch(() => null);
	const password = form?.get("password");
	const responseHeaders = new Headers();
	const valid =
		typeof password === "string" && (await verifyWebPagePassword({ match, password, trustedClient, responseHeaders }));

	// Back to where the form was served.
	const page =
		options.customDomain || "id" in match
			? "/"
			: publicResumePath({ slug: "", ...match, isPrimary: "primary" in match });
	responseHeaders.set("Location", valid ? page : `${page}?wrong-password`);
	responseHeaders.set("Cache-Control", "no-store");
	return new Response(null, { status: 303, headers: responseHeaders });
}