// Browser-safe helpers shared by the preview, the session page and the Cloudflare deploy. export const COLLECTION_RE = /^[a-zA-Z0-9_-]{1,40}$/; /** * Injects Echo's tiny runtime into a generated page: * - window.EchoDB: shared cloud storage for the built app (backed by /api/public/apps/:appId/:collection) * - live testing: when shown inside Echo's preview iframe, reports runtime errors and a * "ready" health check to the parent window. */ export function withEchoRuntime(html: string, appId: string, apiBase: string) { const script = ``; html = pinLibraries(html); if (/]*>/i.test(html)) return html.replace(/]*>/i, (m) => `${m}${script}`); return script + html; } /** Pins CDN libraries whose "latest" builds break in-browser compiling (Babel 8 emits ES imports). */ export function pinLibraries(html: string) { return html .replace(/(unpkg\.com|cdn\.jsdelivr\.net\/npm)\/@babel\/standalone(@[^/"']*)?\//g, "$1/@babel/standalone@7/") .replace(/(unpkg\.com|cdn\.jsdelivr\.net\/npm)\/@babel\/standalone(@[^/"']*)?(["'])/g, "$1/@babel/standalone@7/babel.min.js$3") .replace(/(unpkg\.com|cdn\.jsdelivr\.net\/npm)\/lucide@latest(\/[^"']*)?(["'])/g, "$1/lucide@0.468.0/dist/umd/lucide.min.js$3"); }