Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960interface HtmlResponseOptions { status?: number; edgeCacheSeconds?: number;}
/** * RFC 6266 attachment header. Note slugs may be Japanese, and a raw non-ASCII * byte in a header makes Response throw — so send an ASCII-only `filename` for * old clients plus the real name in `filename*`. */export function attachmentDisposition(filename: string): string { const ascii = filename.replace(/[^\x20-\x7E]/g, "_").replace(/["\\]/g, "_"); return `attachment; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(filename)}`;}
// Strict CSP: no inline scripts; inline styles allowed for the layout's per-theme <style> block.const CSP_DIRECTIVES = [ "default-src 'self'", "script-src 'self' https://stats.lichen.wiki https://static.cloudflareinsights.com https://cdn.jsdelivr.net", "style-src 'self' 'unsafe-inline'", "img-src 'self' data: https://cdn.bsky.app", "font-src 'self' data:", "connect-src 'self' https://stats.lichen.wiki https://cloudflareinsights.com", "frame-src https://www.youtube-nocookie.com", "frame-ancestors 'none'", "base-uri 'self'", "form-action 'self'", "object-src 'none'",].join("; ");
export function htmlResponse( body: string, statusOrOptions: number | HtmlResponseOptions = 200,): Response { const opts: HtmlResponseOptions = typeof statusOrOptions === "number" ? { status: statusOrOptions } : statusOrOptions; const headers: Record<string, string> = { "Content-Type": "text/html", "Content-Security-Policy": CSP_DIRECTIVES, "X-Content-Type-Options": "nosniff", "Referrer-Policy": "strict-origin-when-cross-origin", }; // Every response states its own policy: an HTML page that says nothing is // left to whatever a proxy in front of us appends, and access control here // is per-request, so the page one visitor gets is not the page for the next. if (opts.edgeCacheSeconds && opts.edgeCacheSeconds > 0) { headers["Cache-Control"] = `public, s-maxage=${opts.edgeCacheSeconds}, stale-while-revalidate=${opts.edgeCacheSeconds * 5}`; // Honoured by standards-compliant shared caches, but Cloudflare varies on // Accept-Encoding alone — so an HTML "Cache Everything" rule would need // locale and theme folded into the cache key before it could be safe. headers["Vary"] = "Cookie, Accept-Language"; } else { headers["Cache-Control"] = "private, no-store"; } return new Response(body, { status: opts.status ?? 200, headers });}