import { dirname, extname, join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; import { parse as parseToml, stringify as stringifyToml } from "@std/toml"; export type CacheMap = Record; const RESOURCE_REFERENCE_RE = /(?]*?\bhref\s*=\s*["']|\bsrc\s*=\s*["']|\bhref\s*=\s*["']|url\(\s*["']?|@import\s+["']|!\[[^\]]*\]\(\s*["']?|\bcontent\s*=\s*["'][^"']*\burl\s*=\s*["']?)(?https?:\/\/[^\s"'<>()[\]{}]+)(?["']?\s*\)?)/gi; const ASSET_EXTENSIONS = new Set([ ".avif", ".css", ".gif", ".htm", ".html", ".jpeg", ".jpg", ".js", ".mjs", ".otf", ".png", ".svg", ".ttf", ".webp", ".woff", ".woff2", ]); function isLocalUrl(url: string): boolean { return /^(?:data:|blob:|file:|\.\.?\/|\/|#)/i.test(url); } function isHttpUrl(url: string): boolean { return /^https?:\/\//i.test(url); } function isAssetUrl( url: string, prefix: string, sourceExtension: string, ): boolean { if (isLocalUrl(url)) return false; let parsed: URL; try { parsed = new URL(url); } catch { return false; } if (!/^https?:$/.test(parsed.protocol)) return false; const extension = extname(parsed.pathname).toLowerCase(); if (ASSET_EXTENSIONS.has(extension)) return true; if (/\.(?:css|html?)$/i.test(sourceExtension)) { return /^(?:src|url\(|@import|!\[)/i.test(prefix.trim()); } return false; } function isResourceReference(prefix: string, url: string): boolean { const extension = extname(new URL(url).pathname).toLowerCase(); if (prefix.toLowerCase().startsWith(" { const parsed = new URL(url); const name = parsed.pathname.split("/").filter(Boolean).pop() || "asset"; const digest = await crypto.subtle.digest( "SHA-256", new TextEncoder().encode(url), ); const hash = Array.from(new Uint8Array(digest)).slice(0, 12).map((byte) => byte.toString(16).padStart(2, "0") ).join(""); return `${hash}-${name.replace(/[^\p{L}\p{N}._-]/gu, "_")}`; } async function readCache(path: string): Promise { try { return parseToml(await Deno.readTextFile(path)) as CacheMap; } catch (error) { if (error instanceof Deno.errors.NotFound) return {}; throw error; } } export async function prepareLocalCache( publicationDir: string, templateDir?: string, ): Promise { const pubDir = resolve(publicationDir); if (!existsSync(pubDir)) { throw new Error(`Publication directory does not exist: ${pubDir}`); } const cacheToml = join(pubDir, ".cache.toml"); const cacheDir = join(pubDir, ".local-cache"); const map = await readCache(cacheToml); const initialCachedCount = Object.keys(map).length; const files = new Set(sourceFiles(pubDir)); if (templateDir) { for (const file of sourceFiles(resolve(templateDir))) files.add(file); } const urls = new Set(Object.keys(map)); for (const file of files) { const content = await Deno.readTextFile(file); for (const match of content.matchAll(RESOURCE_REFERENCE_RE)) { const url = match.groups?.url; const prefix = match.groups?.prefix ?? ""; if ( url && isAssetUrl(url, prefix, extname(file).toLowerCase()) && isResourceReference(prefix, url) ) { urls.add(url); } } } await Deno.mkdir(cacheDir, { recursive: true }); const pending = [...urls]; const processed = new Set(); while (pending.length > 0) { const url = pending.shift()!; if (processed.has(url)) continue; processed.add(url); const fileName = map[url]?.replace(/^\.local-cache\//, "") ?? await cacheFileName(url); const target = join(cacheDir, fileName); if (!(await exists(target))) { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to cache ${url}: HTTP ${response.status}`); } const bytes = new Uint8Array(await response.arrayBuffer()); await Deno.writeFile(target, bytes); } map[url] = `.local-cache/${fileName}`; } await Deno.writeTextFile(cacheToml, stringifyToml(map)); for (const file of files) await rewriteFile(file, pubDir, map); const inspected = new Set(); for (const url of [...urls]) { if (/\.(?:css|html?)$/i.test(new URL(url).pathname)) { const localFile = resolve(pubDir, map[url]); if (await exists(localFile)) { await scanDownloadedReferences(url, pubDir, map, inspected); } } } return Object.keys(map).length - initialCachedCount; } async function scanDownloadedReferences( sourceUrl: string, pubDir: string, map: CacheMap, processed: Set, ): Promise { const file = resolve(pubDir, map[sourceUrl]); if (processed.has(sourceUrl)) return; processed.add(sourceUrl); const content = await Deno.readTextFile(file); const sourceExtension = extname(new URL(sourceUrl).pathname).toLowerCase(); for (const match of content.matchAll(RESOURCE_REFERENCE_RE)) { const prefix = match.groups?.prefix ?? ""; const reference = match.groups?.url; if ( !reference || !isAssetUrl(reference, prefix, sourceExtension) || !isResourceReference(prefix, reference) ) continue; let dependency: string; try { dependency = new URL(reference, sourceUrl).href; } catch { continue; } if (!isHttpUrl(dependency) || !isResourceReference(prefix, dependency)) { continue; } if (!map[dependency] || !(await exists(resolve(pubDir, map[dependency])))) { const response = await fetch(dependency); if (!response.ok) { throw new Error( `Failed to cache ${dependency}: HTTP ${response.status}`, ); } const bytes = new Uint8Array(await response.arrayBuffer()); const fileName = await cacheFileName(dependency); await Deno.writeFile(join(pubDir, ".local-cache", fileName), bytes); map[dependency] = `.local-cache/${fileName}`; } processed.add(dependency); if (/\.(?:css|html?)$/i.test(new URL(dependency).pathname)) { await scanDownloadedReferences(dependency, pubDir, map, processed); } } await rewriteFile(file, pubDir, map, sourceUrl); await Deno.writeTextFile(join(pubDir, ".cache.toml"), stringifyToml(map)); } async function rewriteFile( file: string, pubDir: string, map: CacheMap, sourceUrl?: string, ): Promise { const original = await Deno.readTextFile(file); const rewritten = original.replace( RESOURCE_REFERENCE_RE, (whole, prefix: string, url: string) => { let resolvedUrl = url; try { if (sourceUrl) resolvedUrl = new URL(url, sourceUrl).href; } catch { return whole; } const localPath = map[resolvedUrl] ?? map[url]; if ( !localPath || !isHttpUrl(resolvedUrl) || !isResourceReference(prefix, resolvedUrl) ) return whole; return whole.replace( url, pathToFileURL(resolve(pubDir, localPath)).href, ); }, ); if (rewritten !== original) await Deno.writeTextFile(file, rewritten); } async function exists(path: string): Promise { try { await Deno.stat(path); return true; } catch (error) { if (error instanceof Deno.errors.NotFound) return false; throw error; } }