diff --git a/src/index.ts b/src/index.ts index eb4c42a..bc8a7ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,9 @@ export type { GraphView } from './graph-viz'; export { buildOpenApi } from './openapi'; export type { OpenApiDoc, OpenApiInfo, OpenApiRoute } from './openapi'; +// Views +export { render } from './views'; + // Routing & decorators export { Provider, Step, Route, Get, Module, Transformer, Sse, Ws, Stream, Post, Put, Patch, Delete } from './metadata'; export type { Transport, HttpMethod } from './metadata'; diff --git a/src/views.ts b/src/views.ts new file mode 100644 index 0000000..3f6aa21 --- /dev/null +++ b/src/views.ts @@ -0,0 +1,46 @@ +// 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). + +/** Escape the five HTML-significant characters so interpolated data can't inject markup. */ +export function escapeHtml(s: string): string { + const map: Record = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; + return s.replace(/[&<>"']/g, (c) => map[c]); +} + +function stringify(v: unknown): string { + return v === null || v === undefined ? '' : String(v); +} + +/** + * Minimal built-in template engine: `{{ key }}` interpolates HTML-escaped, `{{{ key }}}` raw, + * `{{ a.b }}` walks nested keys, a missing key renders empty. No loops/conditionals/partials. + */ +export function render(source: string, data: unknown): string { + const lookup = (key: string): unknown => + key.split('.').reduce((obj, k) => (obj === null || obj === undefined ? undefined : obj[k]), data); + return source + .replace(/\{\{\{\s*([\w.]+)\s*\}\}\}/g, (_, key) => stringify(lookup(key))) + .replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, key) => escapeHtml(stringify(lookup(key)))); +} + +const CONTENT_TYPES: Record = { + '.html': 'text/html; charset=utf-8', + '.css': 'text/css; charset=utf-8', + '.js': 'text/javascript; charset=utf-8', + '.mjs': 'text/javascript; charset=utf-8', + '.json': 'application/json; charset=utf-8', + '.svg': 'image/svg+xml', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.gif': 'image/gif', + '.webp': 'image/webp', + '.ico': 'image/x-icon', + '.woff2': 'font/woff2', + '.txt': 'text/plain; charset=utf-8', +}; + +/** Content-type for a file extension (case-insensitive, leading dot); unknown → `application/octet-stream`. */ +export function contentTypeFor(ext: string): string { + return CONTENT_TYPES[ext.toLowerCase()] ?? 'application/octet-stream'; +} diff --git a/test/views.test.ts b/test/views.test.ts new file mode 100644 index 0000000..2523385 --- /dev/null +++ b/test/views.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'vitest'; +import { render, escapeHtml, contentTypeFor } from '../src/views'; + +describe('escapeHtml', () => { + it('escapes the five HTML-significant characters', () => { + expect(escapeHtml(`&'`)).toBe('<a href="x">&''); + }); +}); + +describe('render', () => { + it('interpolates and HTML-escapes {{ x }} by default', () => { + expect(render('

{{ name }}

', { name: 'hi' })).toBe('

<b>hi</b>

'); + }); + it('leaves {{{ x }}} raw (unescaped)', () => { + expect(render('{{{ html }}}', { html: 'hi' })).toBe('hi'); + }); + it('resolves nested keys and renders missing keys as empty', () => { + expect(render('{{ user.name }}|{{ nope }}', { user: { name: 'Diego' } })).toBe('Diego|'); + }); +}); + +describe('contentTypeFor', () => { + it('maps known extensions and defaults unknown to octet-stream', () => { + expect(contentTypeFor('.css')).toBe('text/css; charset=utf-8'); + expect(contentTypeFor('.PNG')).toBe('image/png'); + expect(contentTypeFor('.xyz')).toBe('application/octet-stream'); + }); +});