import { escapeHtml, isText, renderFile, renderListing, renderPage, urlPath, type Link } from './render.ts'; import type { ContentIndex, Entry } from './walk.ts'; export type SitePage = { body: Buffer; type: string; headers: Record; }; export type Site = { pages: Map; hash: string; }; export type SiteOptions = { /** Absolute URL of the site, used by the sitemap. */ siteUrl: string; /** Route name to content file, for example impressum to impressum.txt. */ legal: Map; }; const RESERVED = ['ws', 'raw', 'ls', 'healthz', 'assets', 'index.html', 'sitemap.xml', 'robots.txt']; /** Route names the content cannot use at its top level, including the legal pages. */ export function reservedNames(legal: Map): Set { return new Set([...RESERVED, ...legal.keys()]); } const RAW_TYPES: [RegExp, string][] = [ [/\.png$/i, 'image/png'], [/\.jpe?g$/i, 'image/jpeg'], [/\.gif$/i, 'image/gif'], [/\.webp$/i, 'image/webp'], [/\.avif$/i, 'image/avif'], ]; /** * Raw files never run in the site's origin: text is served as plain text, * also for HTML and SVG, and a sandbox policy disables scripts entirely. */ const RAW_HEADERS = { 'Content-Security-Policy': "sandbox; default-src 'none'; img-src 'self'" }; function rawPage(entry: Entry & { content: Buffer }): SitePage { const image = RAW_TYPES.find(([pattern]) => pattern.test(entry.name))?.[1]; if (image) return { body: entry.content, type: image, headers: RAW_HEADERS }; if (isText(entry.content)) return { body: entry.content, type: 'text/plain; charset=utf-8', headers: RAW_HEADERS }; return { body: entry.content, type: 'application/octet-stream', headers: { ...RAW_HEADERS, 'Content-Disposition': `attachment; filename*=UTF-8''${encodeURIComponent(entry.name)}` }, }; } function describe(entry: Entry): string { if (entry.kind === 'dir') return `Contents of /${entry.path || ''} on security-command.org.`; if (!entry.content || !isText(entry.content)) return `${entry.name} on security-command.org.`; const text = entry.content.toString('utf8').replace(/[#*_`>\[\]()]/g, '').replace(/\s+/g, ' ').trim(); return text.length > 160 ? `${text.slice(0, 157)}...` : text; } function crumbs(entry: Entry): Link[] { const parts = entry.path ? entry.path.split('/') : []; return [ { label: '~', href: '/ls' }, ...parts.map((label, index) => ({ label, href: `/${urlPath(parts.slice(0, index + 1).join('/'))}` })), ]; } /** Builds every static route from the index. Paths are decoded, with a leading slash and no trailing one. */ export function buildSite(index: ContentIndex, options: SiteOptions): Site { const pages = new Map(); const nav: Link[] = [ { label: 'terminal', href: '/' }, { label: 'ls', href: '/ls' }, ...[...options.legal.keys()].map((route) => ({ label: route, href: `/${route}` })), ]; const html = (title: string, description: string, entryCrumbs: Link[], body: string): SitePage => ({ body: Buffer.from(renderPage({ title, description, crumbs: entryCrumbs, body, nav })), type: 'text/html; charset=utf-8', headers: {}, }); for (const entry of index.entries.values()) { const title = `${entry.path ? `/${entry.path}` : '~'} · security-command.org`; const route = entry.path ? `/${entry.path}` : '/ls'; if (entry.kind === 'dir') { const children = (entry.children ?? []).map((path) => index.entries.get(path)!); pages.set(route, html(title, describe(entry), crumbs(entry), renderListing(entry, children))); continue; } pages.set(route, html(title, describe(entry), crumbs(entry), renderFile(entry))); if (entry.content) pages.set(`/raw/${entry.path}`, rawPage(entry as Entry & { content: Buffer })); } for (const [routeName, file] of options.legal) { const entry = index.entries.get(file); const body = entry?.kind === 'file' ? renderFile(entry) : `

${escapeHtml(file)} is missing from the content directory.

`; pages.set(`/${routeName}`, html(`${routeName} · security-command.org`, `${routeName} of security-command.org.`, [], body)); } const base = options.siteUrl.replace(/\/$/, ''); const urls: [string, Date | undefined][] = [ ['/', undefined], ...[...index.entries.values()].map((entry): [string, Date] => [entry.path ? `/${urlPath(entry.path)}` : '/ls', entry.modified]), ...[...options.legal].map(([route, file]): [string, Date | undefined] => [`/${route}`, index.entries.get(file)?.modified]), ]; const sitemap = [ '', '', ...urls.map(([url, modified]) => `${escapeHtml(base + url)}${modified ? `${modified.toISOString().slice(0, 10)}` : ''}`), '', '', ].join('\n'); pages.set('/sitemap.xml', { body: Buffer.from(sitemap), type: 'application/xml; charset=utf-8', headers: {} }); const robots = ['User-agent: *', 'Disallow: /ws', 'Disallow: /raw/', `Sitemap: ${base}/sitemap.xml`, ''].join('\n'); pages.set('/robots.txt', { body: Buffer.from(robots), type: 'text/plain; charset=utf-8', headers: {} }); return { pages, hash: index.hash }; }