Skip to content

Art and animation, written as code. Illustrations, loops, interactive web art, launch-videos and scored films in dozens of styles, identical on every render.

fix(gate,emit): a film's declared assets may sit in the manifest (and only there); gate scans the example tree; bakes decode in memory (no data: URL in the bundle)

Alex Greenshpun83f32a459fde
diff --git a/skills/anidoodle/engine/src/hosts/page.ts b/skills/anidoodle/engine/src/hosts/page.ts
index 0d4f726..8fff741 100644
--- a/skills/anidoodle/engine/src/hosts/page.ts
+++ b/skills/anidoodle/engine/src/hosts/page.ts
@@ -12,7 +12,7 @@ declare global { interface Window { FILM: unknown; __ASSETS__?: Record<string, s
// the fresh ones after; the page never touches a file. Only fully opaque frames are kept, because
// only those survive a PNG round trip bit for bit.
const bakeStore = () => {
- const loaded = new Map<string, HTMLImageElement>(), fresh = new Map<string, HTMLCanvasElement>(), stats = { hits: 0, misses: 0, kept: 0, skipped: 0 };
+ const loaded = new Map<string, ImageBitmap>(), fresh = new Map<string, HTMLCanvasElement>(), stats = { hits: 0, misses: 0, kept: 0, skipped: 0 };
let on = false; // off until an adapter loads the store: a shipped player keeps no copies
const keyOf = (film: object, frame: number, w: number, h: number) => { const mod = window.__ANIDOODLE_SRC__?.get(film), hash = mod ? window.__BAKE_SRC__?.[mod] : undefined; return hash ? `${hash}.${frame}.${w}x${h}` : null; };
const bake: NonNullable<Env["bake"]> = {
@@ -24,7 +24,7 @@ const bakeStore = () => {
const c = document.createElement("canvas"); c.width = w; c.height = h; c.getContext("2d")!.drawImage(layer.canvas, 0, 0); fresh.set(k, c); stats.kept++;
},
};
- const load = async (entries: Record<string, string>) => { on = true; await Promise.all(Object.entries(entries).map(async ([k, b64]) => { const img = new Image(); img.src = "data:image/png;base64," + b64; try { await img.decode(); if (`${img.naturalWidth}x${img.naturalHeight}` === k.split(".").pop()) loaded.set(k, img); } catch { /* unreadable: draw it cold */ } })); return loaded.size; };
+ const load = async (entries: Record<string, string>) => { on = true; await Promise.all(Object.entries(entries).map(async ([k, b64]) => { try { const bin = atob(b64), u8 = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) u8[i] = bin.charCodeAt(i); const img = await createImageBitmap(new Blob([u8], { type: "image/png" }), { premultiplyAlpha: "none", colorSpaceConversion: "none" }); if (`${img.width}x${img.height}` === k.split(".").pop()) loaded.set(k, img); } catch { /* unreadable: draw it cold */ } })); return loaded.size; }; // decoded in memory: no URL, no request
const take = () => { const out: Record<string, string> = {}; for (const [k, c] of fresh) out[k] = c.toDataURL("image/png").slice(22); fresh.clear(); return out; };
const hashes = () => [...new Set(Object.values(window.__BAKE_SRC__ ?? {}))];
return { bake, load, take, hashes, stats };
diff --git a/skills/anidoodle/engine/tools/adapters/html-player.mjs b/skills/anidoodle/engine/tools/adapters/html-player.mjs
index 1405f98..c902556 100644
--- a/skills/anidoodle/engine/tools/adapters/html-player.mjs
+++ b/skills/anidoodle/engine/tools/adapters/html-player.mjs
@@ -25,16 +25,19 @@ export const probe = () => {
};
// what makes a self-contained page worth shipping: nothing in it may reach outside itself
-export const pageChecks = (html) => {
- const has = (re) => { const m = html.match(re); return m ? m.length : 0; };
- const assets = html.match(/window\.__ASSETS__=([^;]*);/);
+// A film may declare assets (a photo the user brought): build-page inlines exactly those into the
+// manifest, and that is the ONLY place a binary may sit. `declared` = the film's asset names.
+export const pageChecks = (html, declared = []) => {
+ const assets = html.match(/window\.__ASSETS__=(\{.*?\});(?=window\.|<\/script>)/), rest = assets ? html.replace(assets[0], "") : html;
+ const has = (re) => { const m = rest.match(re); return m ? m.length : 0; };
+ let names = null; try { names = assets ? Object.keys(JSON.parse(assets[1])) : null; } catch { names = null; }
return [
{ ok: has(/https?:\/\//g) === 0, label: "no http(s) reference of any kind", detail: `${has(/https?:\/\//g)} found` },
{ ok: has(/<(img|audio|video|source|link|iframe)\b/gi) === 0, label: "no external element (img, audio, video, link)", detail: `${has(/<(img|audio|video|source|link|iframe)\b/gi)} found` },
{ ok: has(/@font-face/gi) === 0, label: "no web font", detail: `${has(/@font-face/gi)} found` },
- { ok: has(/data:[a-z]+\/[a-z0-9.+-]+;base64/gi) === 0, label: "no embedded binary asset", detail: `${has(/data:[a-z]+\/[a-z0-9.+-]+;base64/gi)} found` },
+ { ok: has(/data:[a-z]+\/[a-z0-9.+-]+;base64/gi) === 0, label: "no embedded binary outside the asset manifest", detail: `${has(/data:[a-z]+\/[a-z0-9.+-]+;base64/gi)} found` },
{ ok: has(/fetch\(|XMLHttpRequest|importScripts/g) === 0, label: "no network call in the bundle", detail: `${has(/fetch\(|XMLHttpRequest|importScripts/g)} found` },
- { ok: assets ? assets[1].trim() === "{}" : false, label: "the asset manifest is empty", detail: assets ? assets[1].trim() : "manifest absent" },
+ { ok: !!names && names.length === declared.length && declared.every((n) => names.includes(n)), label: declared.length ? `the asset manifest holds exactly the film's ${declared.length} declared asset(s)` : "the asset manifest is empty", detail: names ? (names.join(", ") || "{}") : "manifest absent or unreadable" },
{ ok: /window\.FILM\s*=/.test(html), label: "exposes window.FILM so any host can drive it", detail: "" },
{ ok: /audio\s*[:(]/.test(html), label: "any score is synthesized in the page, never loaded", detail: "" },
];
@@ -69,7 +72,7 @@ export const open = async (film, opts = {}) => {
frame: async (n, w = 0) => { const r = await pick(w).evaluate((f) => { const s = window.FILM.seek(f); return { ...s, png: window.FILM.png() }; }, n); return { png: Buffer.from(r.png, "base64"), shot: r.shot, drawMs: r.ms }; },
hash: (n, w = 0) => pick(w).evaluate((f) => { window.FILM.seek(f); return window.FILM.hash(); }, n),
audio: (sr) => pages[0].evaluate((s) => window.FILM.audio(s), sr),
- artifact: () => ({ path: out, kind: "self-contained HTML player", bytes: statSync(out).size, meta: built.meta, checks: pageChecks(html) }),
+ artifact: () => ({ path: out, kind: "self-contained HTML player", bytes: statSync(out).size, meta: built.meta, checks: pageChecks(html, built.assets ?? []) }),
close: async () => { if (useBakes) for (const p of pages) await saveBakes(p, bv).catch(() => 0); await browser.close(); },
};
};
diff --git a/skills/anidoodle/engine/tools/build-page.mjs b/skills/anidoodle/engine/tools/build-page.mjs
index 5662c01..cd772be 100644
--- a/skills/anidoodle/engine/tools/build-page.mjs
+++ b/skills/anidoodle/engine/tools/build-page.mjs
@@ -56,6 +56,6 @@ export const buildPage = async ({ entry, out, title, plugins: extra = [] }) => {
<style>html,body{margin:0;height:100%;background:#1b1a1a;display:grid;place-items:center}canvas{max-width:100vw;max-height:100vh;aspect-ratio:${film.meta.W}/${film.meta.H};cursor:pointer;background:#fff}</style></head>
<body><canvas id="film"></canvas><script>window.__ASSETS__=${JSON.stringify(assets)};window.__BAKE_SRC__=${JSON.stringify(bakeSrc)};</script><script>${js.replace(/<\/script/g, "<\\/script")}</script></body></html>`;
mkdirSync(join(out, ".."), { recursive: true }); writeFileSync(out, html);
- return { out, bytes: html.length, meta: film.meta };
+ return { out, bytes: html.length, meta: film.meta, assets: Object.keys(film.assets.images) };
};
if (import.meta.url === `file://${process.argv[1]}`) { const title = process.argv[2] ?? "fixtures"; const r = await buildPage({ entry: `src/hosts/page-${title}.ts`, out: `dist/${title}.html`, title }); console.log(`built ${r.out} (${(r.bytes / 1024).toFixed(0)} KB, self-contained)`); }
diff --git a/skills/anidoodle/engine/tools/emit.mjs b/skills/anidoodle/engine/tools/emit.mjs
index 2c2cc3d..ec4dd10 100644
--- a/skills/anidoodle/engine/tools/emit.mjs
+++ b/skills/anidoodle/engine/tools/emit.mjs
@@ -50,7 +50,7 @@ console.log(` ${out} (${(ship.bytes / 1024).toFixed(0)} KB, self-containe
// ---------------------------------------------------------------- 1. static
head(1, "SHIP-WORTHY the file reaches outside itself for nothing");
const html = readFileSync(out, "utf8");
-pageChecks(html).forEach((c) => say(c.ok, c.label, c.detail));
+pageChecks(html, ship.assets).forEach((c) => say(c.ok, c.label, c.detail));
say(statSync(out).size === ship.bytes, "the file on disk is the file that was built", `${statSync(out).size} bytes`);
// ---------------------------------------------------------------- 2 & 3. run it
diff --git a/skills/anidoodle/engine/tools/gate.mjs b/skills/anidoodle/engine/tools/gate.mjs
index f9372b5..3f9994c 100644
--- a/skills/anidoodle/engine/tools/gate.mjs
+++ b/skills/anidoodle/engine/tools/gate.mjs
@@ -60,7 +60,8 @@ const walk = (dir, out = []) => { for (const e of execFileSync("find", [dir, "-n
const contractScan = () => {
head(2, "CONTRACT the art core cannot reach a clock, the network or a file");
- const files = [...walk("src/canvas-core"), `src/hosts/page-${film}.ts`].filter((f) => existsSync(f));
+ // the worked example is part of the same tree at build time (tools/overlay.mjs): scan it too
+ const files = [...walk("src/canvas-core"), ...(existsSync("../example/src/canvas-core") ? walk("../example/src/canvas-core") : []), `src/hosts/page-${film}.ts`].filter((f) => existsSync(f));
const hits = [];
for (const f of files) { const src = stripComments(readFileSync(f, "utf8")); FORBIDDEN.forEach(([re, label]) => { const m = src.match(new RegExp(re.source, "g")); if (m) hits.push(`${f}: ${label} x${m.length}`); }); }
say(hits.length === 0, `no forbidden call in ${files.length} art-core modules`, hits.length ? hits.slice(0, 6).join(" | ") : "Math.random, Date, performance.now, ctx.filter, network, image/font loads: none");