|
| 1 | +import { |
| 2 | + parse as parseCookies, |
| 3 | + serialize as serializeCookie, |
| 4 | + type CookieSerializeOptions, |
| 5 | +} from "cookie-es"; |
| 6 | +import type { CompiledTemplate } from "./compiler.ts"; |
| 7 | + |
| 8 | +export interface RenderOptions { |
| 9 | + request?: Request; |
| 10 | + context?: Record<string, unknown>; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Renders an HTML template to a Response object. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * ```ts |
| 18 | + * import { compileTemplate, renderToResponse } from "rendu"; |
| 19 | + * |
| 20 | + * const render = compileTemplate(template, { stream: true }); |
| 21 | + * |
| 22 | + * const response = await renderToResponse(render, { request }); |
| 23 | + * ``` |
| 24 | + * @param htmlTemplate The compiled HTML template. |
| 25 | + * @param opts Options for rendering. |
| 26 | + * @returns A Response object. |
| 27 | + */ |
| 28 | +export async function renderToResponse( |
| 29 | + htmlTemplate: CompiledTemplate<any>, |
| 30 | + opts: RenderOptions, |
| 31 | +): Promise<Response> { |
| 32 | + const ctx = createRenderContext(opts); |
| 33 | + const body = await htmlTemplate(ctx); |
| 34 | + if (body instanceof Response) { |
| 35 | + return body; |
| 36 | + } |
| 37 | + return new Response(body, { |
| 38 | + status: ctx.$RESPONSE.status, |
| 39 | + statusText: ctx.$RESPONSE.statusText, |
| 40 | + headers: ctx.$RESPONSE.headers, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +export type RenderContext = { |
| 45 | + globalThis: typeof globalThis; |
| 46 | + htmlspecialchars: (s: string) => string; |
| 47 | + setCookie: ( |
| 48 | + name: string, |
| 49 | + value: string, |
| 50 | + options?: CookieSerializeOptions, |
| 51 | + ) => void; |
| 52 | + redirect?: (url: string, status?: number) => void; |
| 53 | + $REQUEST?: Request; |
| 54 | + $METHOD?: string; |
| 55 | + $URL?: URL; |
| 56 | + $HEADERS?: Headers; |
| 57 | + $COOKIES: Readonly<Record<string, string>>; |
| 58 | + $RESPONSE: { |
| 59 | + status: number; |
| 60 | + statusText: string; |
| 61 | + headers: Headers; |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +export function createRenderContext(options: RenderOptions): RenderContext { |
| 66 | + // URL |
| 67 | + const url = new URL(options.request?.url || "http://_"); |
| 68 | + |
| 69 | + // Prepared response |
| 70 | + const response = { |
| 71 | + status: 200, |
| 72 | + statusText: "OK", |
| 73 | + headers: new Headers({ "Content-Type": "text/html ; charset=utf-8" }), |
| 74 | + }; |
| 75 | + |
| 76 | + // Cookies |
| 77 | + const $COOKIES = lazyCookies(options.request!); |
| 78 | + const setCookie = ( |
| 79 | + name: string, |
| 80 | + value: string, |
| 81 | + sOpts: CookieSerializeOptions = {}, |
| 82 | + ) => { |
| 83 | + response.headers.append("Set-Cookie", serializeCookie(name, value, sOpts)); |
| 84 | + }; |
| 85 | + |
| 86 | + // Redirect |
| 87 | + const redirect = (to: string, status = 302) => { |
| 88 | + response.status = status; |
| 89 | + response.headers.set("Location", to); |
| 90 | + }; |
| 91 | + |
| 92 | + return { |
| 93 | + ...options.context, |
| 94 | + globalThis, |
| 95 | + htmlspecialchars, |
| 96 | + setCookie, |
| 97 | + redirect, |
| 98 | + $REQUEST: options.request, |
| 99 | + $METHOD: options.request?.method, |
| 100 | + $URL: url, |
| 101 | + $HEADERS: options.request?.headers, |
| 102 | + $COOKIES, |
| 103 | + $RESPONSE: response, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +function lazyCookies(req: Request | undefined) { |
| 108 | + if (!req) { |
| 109 | + return {}; |
| 110 | + } |
| 111 | + let parsed: Record<string, string> | undefined; |
| 112 | + return new Proxy(Object.freeze(Object.create(null)), { |
| 113 | + get(_, prop: string) { |
| 114 | + if (typeof prop !== "string") return undefined; |
| 115 | + parsed ??= parseCookies(req.headers.get("cookie") || ""); |
| 116 | + return parsed[prop]; |
| 117 | + }, |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +function htmlspecialchars(s: string): string { |
| 122 | + // prettier-ignore |
| 123 | + const htmlSpecialCharsMap: Record<string, string> = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }; |
| 124 | + return String(s).replace(/[&<>"']/g, (c) => htmlSpecialCharsMap[c] || c); |
| 125 | +} |
0 commit comments