import { type BuildCache, type FileSnapshot, IslandPreparer, type StaticFile, } from "../build_cache.ts"; import * as path from "@std/path"; import { SEPARATOR as WINDOWS_SEPARATOR } from "@std/path/windows/constants"; import { encodeHex } from "@std/encoding/hex"; import { fsAdapter } from "../fs.ts"; import type { FileTransformer } from "./file_transformer.ts"; import { assertInDir, pathToSpec } from "../utils.ts"; import type { ResolvedBuildConfig } from "./builder.ts"; import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts"; import type { Command } from "../commands.ts"; import type { ServerIslandRegistry } from "../context.ts"; import { contentType as getStdContentType } from "@std/media-types/content-type"; export interface MemoryFile { hash: string | null; contentType: string; content: Uint8Array; } export interface IslandModChunk { name: string; server: string; browser: string | null; } export type FsRouteFileNoMod = Omit, "mod"> & { lazy: boolean; }; export interface FsRoute { id: string; dir: string; files: FsRouteFileNoMod[]; } export interface DevBuildCache extends BuildCache { islandModNameToChunk: Map; addUnprocessedFile(pathname: string, dir: string): void; addProcessedFile( pathname: string, content: Uint8Array, hash: string | null, ): Promise; flush(): Promise; prepare(): Promise; } export class MemoryBuildCache implements DevBuildCache { #processedFiles = new Map(); #unprocessedFiles = new Map(); #config: ResolvedBuildConfig; #transformer: FileTransformer; islandModNameToChunk = new Map(); #fsRoutes: FsRoute; #commands: Command[] = []; root: string; islandRegistry: ServerIslandRegistry = new Map(); constructor( config: ResolvedBuildConfig, fsRoutes: FsRoute, transformer: FileTransformer, ) { this.#config = config; this.#fsRoutes = fsRoutes; this.#transformer = transformer; this.root = config.root; } getFsRoutes(): Command[] { return this.#commands; } async readFile(pathname: string): Promise { const processed = this.#processedFiles.get(pathname); if (processed !== undefined) { return { hash: processed.hash, readable: processed.content, size: processed.content.byteLength, contentType: processed.contentType, close: () => {}, }; } const unprocessed = this.#unprocessedFiles.get(pathname); if (unprocessed !== undefined) { try { const [stat, file] = await Promise.all([ Deno.stat(unprocessed), Deno.open(unprocessed, { read: true }), ]); return { hash: null, size: stat.size, readable: file.readable, contentType: getContentType(unprocessed), close: () => file.close(), }; } catch { return null; } } let entry = pathname.startsWith("/") ? pathname.slice(1) : pathname; entry = path.join(this.#config.staticDir, entry); const relative = path.relative(this.#config.staticDir, entry); if (relative.startsWith("..")) { throw new Error( `Processed file resolved outside of static dir ${entry}`, ); } // Might be a file that we still need to process const transformed = await this.#transformer.process( entry, "development", this.#config.target, ); if (transformed !== null) { for (let i = 0; i < transformed.length; i++) { const file = transformed[i]; const relative = path.relative(this.#config.staticDir, file.path); if (relative.startsWith(".")) { throw new Error( `Processed file resolved outside of static dir ${file.path}`, ); } const pathname = `/${relative}`; this.addProcessedFile(pathname, file.content, null); } if (this.#processedFiles.has(pathname)) { return this.readFile(pathname); } } else { try { const filePath = path.join(this.#config.staticDir, pathname); const relative = path.relative(this.#config.staticDir, filePath); if (!relative.startsWith(".") && (await Deno.stat(filePath)).isFile) { this.addUnprocessedFile(pathname, this.#config.staticDir); return this.readFile(pathname); } } catch (err) { if (!(err instanceof Deno.errors.NotFound)) { throw err; } } } return null; } addUnprocessedFile(pathname: string, dir: string): void { this.#unprocessedFiles.set( pathname, path.join(dir, pathname), ); } // deno-lint-ignore require-await async addProcessedFile( pathname: string, content: Uint8Array, hash: string | null, ): Promise { this.#processedFiles.set(pathname, { content, hash, contentType: getContentType(pathname), }); } async flush(): Promise { const preparer = new IslandPreparer(); // Load islands await Promise.all( Array.from(this.islandModNameToChunk.entries()).map( async ([name, chunk]) => { const fileUrl = maybeToFileUrl(chunk.server); const mod = await import(fileUrl); if (chunk.browser === null) { throw new Error(`Unexpected missing browser chunk`); } preparer.prepare(this.islandRegistry, mod, chunk.browser, name); }, ), ); } async prepare(): Promise { // Load FS routes const files = await Promise.all(this.#fsRoutes.files.map(async (file) => { const fileUrl = maybeToFileUrl(file.filePath); return { ...file, mod: file.lazy ? () => import(fileUrl) : await import(fileUrl), }; })); this.#commands = fsItemsToCommands(files); } } export class DiskBuildCache implements DevBuildCache { #processedFiles = new Map(); #unprocessedFiles = new Map(); #transformer: FileTransformer; #config: ResolvedBuildConfig; islandModNameToChunk = new Map(); #fsRoutes: FsRoute; root: string; islandRegistry: ServerIslandRegistry = new Map(); constructor( config: ResolvedBuildConfig, fsRoutes: FsRoute, transformer: FileTransformer, ) { this.#fsRoutes = fsRoutes; this.#transformer = transformer; this.#config = config; this.root = config.root; } getFsRoutes(): Command[] { return []; } addUnprocessedFile(pathname: string, dir: string): void { this.#unprocessedFiles.set( pathname.replaceAll(WINDOWS_SEPARATOR, "/"), path.join(dir, pathname), ); } async addProcessedFile( pathname: string, content: Uint8Array, hash: string | null, ) { this.#processedFiles.set(pathname, hash); const outDir = pathname === "/metafile.json" ? this.#config.outDir : path.join(this.#config.outDir, "static"); const filePath = path.join(outDir, pathname); assertInDir(filePath, outDir); await fsAdapter.mkdirp(path.dirname(filePath)); await Deno.writeFile(filePath, content); } // deno-lint-ignore require-await async readFile(_pathname: string): Promise { throw new Error("Not implemented in build mode"); } async prepare(): Promise { // not needed } async flush(): Promise { const { staticDir, outDir, target, root } = this.#config; if (await fsAdapter.isDirectory(staticDir)) { const entries = fsAdapter.walk(staticDir, { includeDirs: false, includeFiles: true, followSymlinks: false, // Skip any folder or file starting with a "." skip: [/\/\.[^/]+(\/|$)/], }); for await (const entry of entries) { // OutDir might be inside static dir if (!path.relative(outDir, entry.path).startsWith("..")) { continue; } const result = await this.#transformer.process( entry.path, "production", target, ); if (result !== null) { for (let i = 0; i < result.length; i++) { const file = result[i]; assertInDir(file.path, staticDir); const pathname = `/${path.relative(staticDir, file.path)}`; await this.addProcessedFile(pathname, file.content, null); } } else { const relative = path.relative(staticDir, entry.path); const pathname = `/${relative}`; this.addUnprocessedFile(pathname, staticDir); } } } const staticFiles = new Map(); for (const [name, filePath] of this.#unprocessedFiles.entries()) { const file = await Deno.open(filePath); const hash = await hashContent(file.readable); staticFiles.set(name, { name, hash, filePath: path.relative(root, filePath), contentType: getContentType(filePath), }); } for (const [name, maybeHash] of this.#processedFiles.entries()) { let hash = maybeHash; // Ignore esbuild meta file. It's not intended for serving if (name === "/metafile.json") { continue; } const filePath = path.join(outDir, "static", name); if (maybeHash === null) { const file = await Deno.open(filePath); hash = await hashContent(file.readable); } staticFiles.set(name, { name, hash, filePath: path.relative(root, filePath), contentType: getContentType(filePath), }); } await Deno.writeTextFile( path.join(outDir, "static-files.json"), JSON.stringify(Array.from(staticFiles.values()), null, 2), ); const islandSpecifiers: string[] = []; for (const spec of this.islandModNameToChunk.keys()) { islandSpecifiers.push(spec); } const editWarning = `// WARNING: DO NOT EDIT THIS FILE. It is autogenerated by Fresh.`; const islands = Array.from(this.islandModNameToChunk.values()); await Deno.writeTextFile( path.join(outDir, "snapshot.js"), `${editWarning} import { IslandPreparer } from "fresh/do-not-use"; import staticFileData from "./static-files.json" with { type: "json" }; // Import islands ${ islands .map((item) => { const spec = pathToSpec(outDir, item.server); return `import * as ${item.name} from "${spec}";`; }) .join("\n") } // Import routes ${ this.#fsRoutes.files .map((item, i) => { if (item.lazy) return null; const spec = pathToSpec(outDir, item.filePath); return `import * as fsRoute_${i} from "${spec}"`; }) .filter(Boolean) .join("\n") } export const version = ${JSON.stringify(this.#config.buildId)}; const prefix = \`/_fresh/js/\${version}\`; export const islands = new Map(); const islandPreparer = new IslandPreparer(); ${ islands.map((item) => { // Strip prefix const prefix = `/_fresh/js/${this.#config.buildId}`; const chunkName = item.browser ? item.browser.slice(prefix.length) : item.browser; return `islandPreparer.prepare(islands, ${item.name}, \`\${prefix}${chunkName}\`, ${ JSON.stringify(item.name) });`; }).join("\n") } export const staticFiles = new Map(); for (let i = 0; i < staticFileData.length; i++) { const data = staticFileData[i]; staticFiles.set(data.name, data); } export const fsRoutes = [ ${ this.#fsRoutes.files .map((item, i) => { const id = JSON.stringify(item.id); const pattern = JSON.stringify(item.pattern); const type = JSON.stringify(item.type); const routePattern = JSON.stringify(item.routePattern); let mod = ""; if (item.lazy) { const spec = pathToSpec(outDir, item.filePath); mod = `() => import(${JSON.stringify(spec)})`; } else { mod = `fsRoute_${i}`; } return ` { id: ${id}, mod: ${mod}, type: ${type}, pattern: ${pattern}, routePattern: ${routePattern} },`; }) .join("\n") } ]; `, ); // TODO: Make main file configurable const appPath = path.relative(outDir, root); await Deno.writeTextFile( path.join(outDir, "server.js"), `${editWarning} import { setBuildCache, ProdBuildCache, path } from "fresh/do-not-use"; import * as snapshot from "./snapshot.js"; import { app } from "${appPath}/main.ts"; const root = path.join(import.meta.dirname, ${JSON.stringify(appPath)}); setBuildCache(app, new ProdBuildCache(root, snapshot)); export default { fetch: app.handler() }`, ); await Deno.writeTextFile( path.join(outDir, "compiled-entry.js"), `import fetcher from "./server.js"; Deno.serve( { port: Deno.env.get("PORT"), hostname: Deno.env.get("HOSTNAME") }, fetcher.fetch );`, ); } } async function hashContent( content: Uint8Array | ReadableStream, ): Promise { const buffer = await new Response(content).arrayBuffer(); const hashBuf = await crypto.subtle.digest( "SHA-256", buffer, ); return encodeHex(hashBuf); } export function getContentType(filePath: string): string { const ext = path.extname(filePath); return getStdContentType(ext) ?? "text/plain"; } function maybeToFileUrl(file: string) { return file.startsWith("file://") ? file : path.toFileUrl(file).href; }