// Build one reader manifest from render pixels, names and playback times. Every output byte has // a format reader and every image has a pixel oracle before anything is accepted as a delivery. import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { spawnSync } from 'node:child_process'; import { mkdir, writeFile } from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { createZip } from '../src/export.js'; import { embedRecipe, readEmbeddedRecipe, readShareLink, shareLink, recipeKind, EMBED_LIMIT, LINK_LIMIT } from '../src/share.js'; import { createRevisionStore } from '../src/revisions.js'; import { run } from './read-delivery-engines.mjs'; const parsedJS = new Set(); export const sha = data => createHash('sha256').update(data).digest('hex'); export function still(image) { return { size: [image.width, image.height], frames: [{ rgba: sha(image.data) }] }; } export function gifFrame(frame) { const data = Uint8Array.from(frame.data); for (let i = 0; i < data.length; i += 4) { if (data[i + 3] < 128) data.fill(0, i, i + 4); else data[i + 3] = 255; } return { rgba: sha(data) }; } export class Delivery { constructor(directory, version) { this.directory = directory; this.plan = { version, files: {}, images: {}, zips: {} }; this.restrictions = new Map(); } async put(name, data, kind = path.extname(name).slice(1)) { const { files } = this.plan; assert.ok(!Object.hasOwn(files, name), name); files[name] = kind; if (kind === 'js' && !parsedJS.has(sha(data))) { const result = spawnSync(process.execPath, ['--input-type=module', '--check'], { input: data, encoding: 'utf8', windowsHide: true }); assert.equal(result.status, 0, name + '\n' + result.stderr); parsedJS.add(sha(data)); } const file = path.join(this.directory, name); await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, data); } async image(name, bytes, expected) { assert.ok(expected.frames.length && expected.frames.every(f => typeof f.rgba === 'string'), name); this.plan.images[name] = expected; await this.put(name, bytes, 'image'); } async bundle(files, pictures) { for (const [name, data] of files) { if (name.endsWith('.png')) { assert.ok(Object.hasOwn(pictures, name), 'unclassified PNG: ' + name); await this.image(name, data, pictures[name]); } else await this.put(name, data); } for (const name of Object.keys(pictures)) assert.ok(files.has(name), 'missing PNG: ' + name); const zip = 'delivery.zip'; this.plan.zips[zip] = Object.fromEntries([...files].map(([name, bytes]) => [name, sha(bytes)])); await this.put(zip, createZip(files), 'zip'); } async interchange(original, compiled, bytes, expected) { const version = this.plan.version; const storeRoot = path.join(this.directory, '..', 'revisions'); const id = await createRevisionStore(storeRoot).remember(original); assert.deepEqual(await createRevisionStore(storeRoot).read(id), original); let source = original; if (source.format === 'pixelforge-stack') { await assert.rejects(shareLink(source, version), /sprite stacks as compiled recipes/); await assert.rejects(embedRecipe(bytes, source, version), /sprite stacks as compiled recipes/); this.restrictions.set('stack-source', 'Share its compiled recipe as the playground documents.'); source = compiled; } this.plan.source = source; // Compute the encoded size independently of the refusal by asking for the unrestricted URL. const link = await shareLink(source, version, { limit: Infinity }); if (link.length <= LINK_LIMIT) { assert.equal(await shareLink(source, version), link); assert.deepEqual((await readShareLink(link)).recipe, source); await this.put('share.url', link, 'link'); } else { await assert.rejects(shareLink(source, version), /character limit/); this.restrictions.set('share-link', 'The compressed URL exceeds the documented link length.'); } const envelope = { v: version, kind: recipeKind(source), recipe: source }; if (Buffer.byteLength(JSON.stringify(envelope)) > EMBED_LIMIT) { await assert.rejects(embedRecipe(bytes, source, version), { code: 'too-large' }); this.restrictions.set('embedded-png', 'The embedded recipe exceeds the Pillow text-chunk limit.'); } else { const embedded = await embedRecipe(bytes, source, version); assert.deepEqual((await readEmbeddedRecipe(embedded)).recipe, source); await this.image('embedded.png', embedded, { ...expected, envelope }); } } async read(python) { const file = path.join(this.directory, 'reader-plan.json'); await writeFile(file, JSON.stringify(this.plan)); run(python, [fileURLToPath(new URL('./read-example-delivery.py', import.meta.url)), file]); } }