268 lines
8.0 KiB
TypeScript
268 lines
8.0 KiB
TypeScript
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<string, string>;
|
|
|
|
const RESOURCE_REFERENCE_RE =
|
|
/(?<prefix><link\b[^>]*?\bhref\s*=\s*["']|\bsrc\s*=\s*["']|\bhref\s*=\s*["']|url\(\s*["']?|@import\s+["']|!\[[^\]]*\]\(\s*["']?|\bcontent\s*=\s*["'][^"']*\burl\s*=\s*["']?)(?<url>https?:\/\/[^\s"'<>()[\]{}]+)(?<suffix>["']?\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("<link")) {
|
|
return ASSET_EXTENSIONS.has(extension);
|
|
}
|
|
if (prefix.toLowerCase().startsWith("href")) return extension === ".css";
|
|
if (prefix.toLowerCase().startsWith("content")) {
|
|
return /\.(?:png|jpe?g|gif|webp|avif|svg)$/i.test(extension);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function sourceFiles(dir: string): string[] {
|
|
const result: string[] = [];
|
|
if (!existsSync(dir)) return result;
|
|
for (const entry of Deno.readDirSync(dir)) {
|
|
const path = join(dir, entry.name);
|
|
if (entry.isDirectory) {
|
|
if (entry.name !== ".local-cache") result.push(...sourceFiles(path));
|
|
} else if (entry.isFile && /\.(?:css|html?|js|mjs|md)$/i.test(entry.name)) {
|
|
result.push(path);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function existsSync(path: string): boolean {
|
|
try {
|
|
return Deno.statSync(path).isDirectory;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function cacheFileName(url: string): Promise<string> {
|
|
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<CacheMap> {
|
|
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<number> {
|
|
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<string>(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<string>();
|
|
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<string>();
|
|
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<string>,
|
|
): Promise<void> {
|
|
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<void> {
|
|
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<boolean> {
|
|
try {
|
|
await Deno.stat(path);
|
|
return true;
|
|
} catch (error) {
|
|
if (error instanceof Deno.errors.NotFound) return false;
|
|
throw error;
|
|
}
|
|
}
|