runloopable / web /src /apps /code /frameDocument.ts
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
dec006b verified
Raw History Blame Contribute Delete
1.03 kB
import { bridgeScript } from "./bridge";
export const CODE_APP_SANDBOX = "allow-scripts";
export const CODE_APP_MAX_BYTES = 512 * 1024;
const POLICY = "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'none'; img-src 'none'; font-src data:; media-src 'none'; object-src 'none'; frame-src 'none'; worker-src 'none'; base-uri 'none'; form-action 'none'";
const POLICY_META = `<meta http-equiv="Content-Security-Policy" content="${POLICY}">`;
export function checkCodeAppSize(html: string): void {
if (new TextEncoder().encode(html).byteLength > CODE_APP_MAX_BYTES) {
throw new RangeError("This App is too large to open.");
}
}
export function appDocument(html: string, css: string): string {
checkCodeAppSize(html);
// Escape stylesheet markup so the kit cannot close its host-owned style element.
const stylesheet = css.replace(/</g, "\\3c ");
return `<!doctype html>${POLICY_META}<meta charset="utf-8"><style>${stylesheet}</style><script>${bridgeScript()}</script>${html}`;
}