// helpers for deploying a cross-origin sandbox environment // Node.js-only helper. Uses opaque dynamic require so bundlers can't resolve it. function readServiceWorkerSource(): string | null { try { // invisible to bundlers' static analysis const _req = globalThis['require' as keyof typeof globalThis] as typeof require | undefined; if (typeof _req !== 'function') return null; const fs = _req('fs' + ''); const path = _req('path' + ''); let dir: string; try { const url = _req('url' + ''); dir = path.dirname(url.fileURLToPath(import.meta.url)); } catch { dir = typeof __dirname !== 'undefined' ? __dirname : '.'; } let swFile = path.join(dir, '__sw__.js'); if (fs.existsSync(swFile)) return fs.readFileSync(swFile, 'utf-8'); swFile = path.join(dir, '../dist/__sw__.js'); if (fs.existsSync(swFile)) return fs.readFileSync(swFile, 'utf-8'); return null; } catch { return null; } } import { DEFAULT_NODEPOD_CDN } from "./constants/config"; export interface SandboxPageConfig { nodepodUrl?: string; enableServiceWorker?: boolean; /** parent page origin, e.g. 'https://myapp.com'. locks down who can * talk to the sandbox. defaults to '*' for backwards compat */ parentOrigin?: string; } export function getSandboxPageHtml(config: SandboxPageConfig | string = {}): string { const opts: SandboxPageConfig = typeof config === 'string' ? { nodepodUrl: config } : config; const nodepodUrl = opts.nodepodUrl ?? DEFAULT_NODEPOD_CDN; const withSW = opts.enableServiceWorker ?? true; const parentOriginJs = opts.parentOrigin ? JSON.stringify(opts.parentOrigin) : "'*'"; const swBlock = withSW ? ` if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/__sw__.js', { scope: '/' }) .then(() => console.log('[Sandbox] SW registered')) .catch(e => console.warn('[Sandbox] SW failed:', e)); } ` : ''; return `