import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import { realpathSync } from 'node:fs'; import { lstat, mkdir, readFile, writeFile } from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseArgs } from 'node:util'; const documentLimit = 1_000_000; const imageLimit = 700_000; const escapeHtml = value => value.replace(/[&<>"']/g, character => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', })[character]); const readSource = relative => readFile(new URL(relative, import.meta.url), 'utf8'); function label(value, field, maxLength, optional = false) { assert(typeof value === 'string' && value.length <= maxLength && (optional || value.trim()), 'Invalid ' + field); return escapeHtml(value.trim()); } function rasterMime(bytes) { if (bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) return 'image/png'; if (bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255) return 'image/jpeg'; if (bytes.toString('ascii', 0, 4) === 'RIFF' && bytes.toString('ascii', 8, 12) === 'WEBP') return 'image/webp'; throw new Error('Storyboard frames must be PNG, JPEG or WebP'); } async function localImage(directory, name) { assert(typeof name === 'string' && !/[\\/]/.test(name) && /\.(?:png|jpe?g|webp)$/i.test(name), 'Frame image must be a raster filename beside the manifest'); const filename = path.join(directory, name); const info = await lstat(filename); assert(info.isFile() && info.size > 0 && info.size <= imageLimit, 'Frame image must be a regular file within the size limit'); const bytes = await readFile(filename); return 'data:' + rasterMime(bytes) + ';base64,' + bytes.toString('base64'); } export async function renderInlineStoryboard({ manifestPath, output }) { assert(typeof manifestPath === 'string' && path.isAbsolute(manifestPath), 'Pass an absolute storyboard manifest path'); assert(path.isAbsolute(output) && output.endsWith('.html'), 'Pass an absolute HTML output path in the host visualization directory'); const manifestInfo = await lstat(manifestPath); assert(manifestInfo.isFile() && manifestInfo.size <= 64_000, 'Storyboard manifest must be a regular file within the size limit'); const manifest = JSON.parse(await readFile(manifestPath, 'utf8')); const title = label(manifest.title, 'title', 120); const openLabel = label(manifest.openLabel, 'openLabel', 60); const backLabel = label(manifest.backLabel, 'backLabel', 60); const previewType = manifest.previewType ?? 'storyboard'; assert(['storyboard', 'creative_directions'].includes(previewType), 'Unsupported still-image preview type'); const isDirections = previewType === 'creative_directions'; const entries = isDirections ? manifest.directions : manifest.frames; if (isDirections) { assert(Array.isArray(entries) && [2, 3].includes(entries.length), 'Provide two or three creative directions'); const ids = entries.map(entry => entry?.id); assert(ids.every(id => typeof id === 'string' && id.length > 0 && id.length <= 100 && id.trim() === id) && new Set(ids).size === ids.length, 'Creative directions require distinct candidate IDs'); } else { assert(Array.isArray(entries) && entries.length >= 1 && entries.length <= 12, 'Provide one to twelve frames in storyboard order'); } const root = 'openmotion-storyboard-' + randomUUID(); const directory = path.dirname(manifestPath); const cards = await Promise.all(entries.map(async (entry, index) => { const heading = label(entry.title, 'card title', 100); const meta = isDirections ? 'ABC'[index] : label(entry.time, 'frame time', 48); const note = label(entry.note ?? '', 'card note', 360, !isDirections); const alt = label(entry.alt, 'image alt text', 240); const src = await localImage(directory, entry.image); const headingId = root + '-card-' + index; return `
${note}