feat(views): @Html string/file/template modes with boot validation
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+163
-6
No files matched your search
@@ -1,6 +1,9 @@
|
||||
// src/views.ts — HTML views: built-in templating, content-type map, and boot-time builders.
|
||||
// fs/path are lazy-required inside functions so the barrel stays edge-loadable (workerd has no filesystem).
|
||||
|
||||
import type { TransformerFn } from './metadata';
|
||||
import type { HtmlMeta } from './metadata';
|
||||
|
||||
/** Escape the five HTML-significant characters so interpolated data can't inject markup. */
|
||||
export function escapeHtml(s: string): string {
|
||||
const map: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
||||
@@ -44,3 +47,47 @@ const CONTENT_TYPES: Record<string, string> = {
|
||||
export function contentTypeFor(ext: string): string {
|
||||
return CONTENT_TYPES[ext.toLowerCase()] ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
const HTML_HEADERS = { 'content-type': 'text/html; charset=utf-8' } as const;
|
||||
|
||||
/** Boot-time context for building `@Html` transformers: the views base dir and an optional BYO engine. */
|
||||
export interface ViewsContext {
|
||||
views?: string;
|
||||
viewEngine?: (source: string, data: unknown) => string;
|
||||
}
|
||||
|
||||
/** Reads a view file at boot (lazy fs). Relative paths resolve against `views` (default cwd); absolute as-is.
|
||||
* node:fs/node:path are loaded lazily so importing the barrel stays edge/workerd-safe
|
||||
* (workerd's nodejs_compat provides no filesystem). This path only runs for path/template `@Html` modes. */
|
||||
function readViewFile(views: string | undefined, path: string): string {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const nodePath = require('node:path') as typeof import('node:path');
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const fs = require('node:fs') as typeof import('node:fs');
|
||||
const base = views ?? process.cwd();
|
||||
const full = nodePath.isAbsolute(path) ? path : nodePath.resolve(base, path);
|
||||
|
||||
try {
|
||||
return fs.readFileSync(full, 'utf8');
|
||||
} catch (err) {
|
||||
throw new Error(
|
||||
`@Html('${path}') could not read '${full}' — on a runtime without a filesystem (edge), return a string from @Html instead. (${(err as Error).message})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Turns `@Html` metadata into a response transformer at boot. String mode is fs-free; path/template modes read+cache the file. */
|
||||
export function buildHtmlTransformer(meta: HtmlMeta, ctx: ViewsContext): TransformerFn {
|
||||
if (!meta.path) {
|
||||
return (value) => ({ status: 200, headers: { ...HTML_HEADERS }, body: String(value) });
|
||||
}
|
||||
|
||||
const source = readViewFile(ctx.views, meta.path);
|
||||
|
||||
if (meta.template) {
|
||||
const engine = ctx.viewEngine ?? render;
|
||||
return (value) => ({ status: 200, headers: { ...HTML_HEADERS }, body: engine(source, value) });
|
||||
}
|
||||
|
||||
return () => ({ status: 200, headers: { ...HTML_HEADERS }, body: source });
|
||||
}
|
||||
Reference in new issue
Block a user