import { createHash } from 'node:crypto'; import { lstat, readFile, readdir } from 'node:fs/promises'; import { dirname, relative, resolve, sep } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { loadProductionInputs } from '../src/content/load-production-inputs.ts'; import { parseExactConfigArgument } from './prerender.mjs'; const scriptPath = fileURLToPath(import.meta.url); const repositoryRoot = resolve(dirname(scriptPath), '..'); const canonicalSectionIds = [ 'start', 'profile', 'experience', 'infrastructure', 'selected-builds', 'additional-work', 'toolkit', 'contact', ]; const prohibitedSurfaces = [ /__PORTFOLIO_INPUTS__/iu, /(?:evidenceId|sourcePath|sourceFingerprint|approvalOwner|privacyClassification|denylist|originalFilename)/iu, /sourceMappingURL/iu, ]; const escapeRegularExpression = (value) => value.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&'); const escapeHtml = (value) => value .replace(/&/gu, '&') .replace(//gu, '>') .replace(/"/gu, '"'); const readRequiredText = async (root, name) => { try { return await readFile(resolve(root, name), 'utf8'); } catch (error) { if (typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT') { throw new Error(`Missing required artifact: ${name}`); } throw error; } }; const listArtifacts = async (root) => { const rootStats = await lstat(root); if (rootStats.isSymbolicLink() || !rootStats.isDirectory()) { throw new Error('Artifact root must be a non-symlink directory.'); } const files = []; for (const entry of await readdir(root, { withFileTypes: true })) { const entryPath = resolve(root, entry.name); if (entry.isSymbolicLink()) throw new Error(`Unexpected linked artifact: ${entry.name}`); if (entry.isDirectory()) { if (entry.name !== 'assets') throw new Error(`Unexpected artifact directory: ${entry.name}`); for (const asset of await readdir(entryPath, { withFileTypes: true })) { if (asset.isSymbolicLink() || !asset.isFile()) { throw new Error(`Unexpected linked or nested artifact: assets/${asset.name}`); } files.push(`assets/${asset.name}`); } continue; } if (!entry.isFile()) throw new Error(`Unexpected special artifact: ${entry.name}`); files.push(entry.name); } return files.sort(); }; const assertArtifactInventory = (files, html, inputs) => { const expectedRootFiles = new Set(['index.html', 'robots.txt', '_headers.example', '_redirects.example']); if (inputs.site.environment === 'production') expectedRootFiles.add('sitemap.xml'); const referencedAssets = new Set( [...html.matchAll(/(?:src|href)="\/assets\/([a-zA-Z0-9._-]+)"/gu)].map((match) => `assets/${match[1]}`), ); for (const asset of inputs.manifest.assets) referencedAssets.add(`assets/${asset.fileName}`); for (const file of files) { if (file.endsWith('.map')) throw new Error(`Source map artifact is prohibited: ${file}`); if (file.startsWith('assets/')) { if (!referencedAssets.has(file)) throw new Error(`Unexpected artifact: ${file}`); } else if (!expectedRootFiles.has(file)) { if (inputs.site.environment === 'preview' && file === 'sitemap.xml') { throw new Error('Preview artifacts must not publish a sitemap.'); } throw new Error(`Unexpected artifact: ${file}`); } } for (const required of [...expectedRootFiles, ...referencedAssets]) { if (!files.includes(required)) throw new Error(`Missing required artifact: ${required}`); } }; const assertNoProhibitedSurfaces = async (root, files) => { for (const file of files) { const bytes = await readFile(resolve(root, file)); const text = bytes.toString('utf8'); for (const pattern of prohibitedSurfaces) { if (pattern.test(text)) throw new Error(`Prohibited surface detected in ${file}.`); } } }; const exactlyOneMatch = (text, expression, label) => { const matches = [...text.matchAll(expression)]; if (matches.length !== 1) throw new Error(`Expected exactly one ${label}.`); return matches[0]; }; const assertSemanticHtml = (html, inputs) => { if (!//u.test(html)) throw new Error('Final HTML must declare lang=en.'); const h1 = exactlyOneMatch(html, /]*>([\s\S]*?)<\/h1>/gu, 'H1'); if (h1[1] !== escapeHtml(inputs.content.identity.name)) throw new Error('H1 identity does not match public content.'); const adjacentIdentity = `

${escapeHtml(inputs.content.identity.name)}

${escapeHtml(inputs.content.identity.role)}

`; if (!html.includes(adjacentIdentity)) throw new Error('The canonical role must be immediately adjacent to the H1.'); for (const id of canonicalSectionIds) { const sectionMatches = html.match(new RegExp(`
)`, 'gu')) ?? []; if (sectionMatches.length !== 1) throw new Error(`Expected exactly one section: ${id}`); const navigationMatches = html.match(new RegExp(`href="#${escapeRegularExpression(id)}"`, 'gu')) ?? []; if (navigationMatches.length !== 1) throw new Error(`Expected one native navigation link for section: ${id}`); const label = inputs.content.navigation.find((item) => item.id === id)?.label; if (label === undefined || !html.includes(`

${escapeHtml(label.toUpperCase())}

`)) { throw new Error(`Missing canonical section heading: ${id}`); } } const requiredVisibleCopy = [ inputs.content.systemsOperations.title, inputs.content.infrastructure.title, ...inputs.content.infrastructure.bands.flatMap((band) => [band.title, band.summary]), ...inputs.content.projects.selected.flatMap((project) => [project.title, project.summary]), ...inputs.content.toolkit.flatMap((group) => [group.title, ...group.technologies.map((technology) => technology.name)]), inputs.content.contact.email, ]; for (const copy of requiredVisibleCopy) { if (!html.includes(escapeHtml(copy))) throw new Error(`Missing required semantic evidence: ${copy}`); } if (!html.includes(`href="mailto:${escapeHtml(inputs.content.contact.email)}"`)) { throw new Error('Missing visible mailto contact link.'); } const moduleScript = exactlyOneMatch(html, /