diff --git a/scripts/gallery-sync.mjs b/scripts/gallery-sync.mjs index 3ffa782..fee054a 100644 --- a/scripts/gallery-sync.mjs +++ b/scripts/gallery-sync.mjs @@ -211,6 +211,9 @@ const bytes = (text) => Buffer.byteLength(text); // The platform refuses a file, and a registry_add or edit_site call, over 128 KiB of source. const maxFileBytes = 128 * 1024; const maxBatchBytes = 120 * 1024; +// registry_add takes at most four items and writes at most eight files per call. +const maxAddItems = 4; +const maxAddWrites = 8; // The draft's file list and source budget. A partial list would hide files from the plan, so it stops the sync. async function draftListing(siteId) { @@ -292,15 +295,39 @@ async function sync() { // Install every block, the type pairings the canvases load and the font items the type specimens link; the draft // keeps its own tokens. Font items are text stylesheets only: their WOFF2 binaries come from the platform's pack. const installs = catalog.items.filter(({ name, usage }) => (usage && name !== "tokens") || /^(pairing|font)-/.test(name)); + // registry_add installs each item with its dependencies: at most four items and eight written files per call, and + // files already identical on the draft don't count. So dependencies go first (fonts before the pairings that load + // them), and a batch counts every file its items bring that no earlier batch wrote. + const byName = new Map(catalog.items.map((item) => [item.name, item])); + const closure = (name, seen = new Set()) => { + if (seen.has(name) || !byName.has(name)) return seen; + seen.add(name); + for (const dependency of byName.get(name).dependencies ?? []) closure(dependency, seen); + return seen; + }; + const installing = new Set(installs.map(({ name }) => name)); + const ordered = []; + const placed = new Set(); + const place = (name) => { + if (placed.has(name)) return; + placed.add(name); + for (const dependency of byName.get(name).dependencies ?? []) if (installing.has(dependency)) place(dependency); + ordered.push(name); + }; + for (const { name } of installs) place(name); const batches = []; - for (const { name, files } of installs) { - const bytes = files.reduce((sum, file) => sum + file.bytes, 0); + const written = new Set(); + for (const name of ordered) { + const fresh = [...closure(name)].flatMap((item) => byName.get(item).files).filter(({ target }) => !written.has(target)); const last = batches.at(-1); - if (last && last.items.length < 4 && last.bytes + bytes <= 100 * 1024) { + const merged = last && new Map([...last.files, ...fresh.map((file) => [file.target, file])]); + if (last && last.items.length < maxAddItems && merged.size <= maxAddWrites && [...merged.values()].reduce((sum, file) => sum + file.bytes, 0) <= 100 * 1024) { last.items.push(name); - last.bytes += bytes; - } else batches.push({ items: [name], bytes }); + last.files = merged; + } else batches.push({ items: [name], files: new Map(fresh.map((file) => [file.target, file])) }); + for (const { target } of fresh) written.add(target); } + console.log(`registry: ${installs.length} items in ${batches.length} registry_add calls, at most ${Math.max(...batches.map(({ files }) => files.size))} new files each.`); // Plan before writing anything. Draft writes check the file count and refuse a file or an install over // 128 KiB, but not the total: the build checks that, so a sync over the budget would write every file // and leave a draft that won't build, and an oversized file or item would fail after earlier writes @@ -332,17 +359,18 @@ async function sync() { `plan: read ${present.length} managed files${leaving.length ? ` and ${leaving.length} retired` : ""} (${current.reread} needed a second read); source ${listing.budget.bytes} of ${listing.budget.limitBytes} bytes now, ${projected} after this sync; ${files} of ${listing.budget.limitFiles} files${leaving.length ? ` (${peak} before the retired go)` : ""}.`, ); const edited = leaving.filter((path) => !released(path).has(current.get(path).content)); - // registry_add installs a whole item in one call; edit_site writes gallery files one by one at most. + // Each registry_add call writes its items' new closure files, and one item's closure can't be split across + // calls; edit_site writes gallery files one by one at most. const oversized = [ - ...installs.map(({ name, files }) => [`the ${name} item`, files.reduce((sum, file) => sum + file.bytes, 0)]), - ...paths.map((path) => [path, next.get(path)]), + ...batches.map(({ items, files }) => [`registry_add ${items.join(", ")}`, [...files.values()].reduce((sum, file) => sum + file.bytes, 0), files.size]), + ...paths.map((path) => [path, next.get(path), 1]), ] - .filter(([, size]) => size > maxFileBytes) - .map(([what, size]) => `${what} (${size} bytes)`); + .filter(([, size, count]) => size > maxFileBytes || count > maxAddWrites) + .map(([what, size, count]) => `${what} (${count > 1 ? `${count} files, ` : ""}${size} bytes)`); const problems = [ projected > listing.budget.limitBytes && `it needs ${projected - listing.budget.limitBytes} bytes more than the draft's ${listing.budget.limitBytes}-byte source budget`, peak > listing.budget.limitFiles && `it needs ${peak} files${leaving.length ? " before the retired ones go" : ""} and the draft allows ${listing.budget.limitFiles}`, - oversized.length && `the platform refuses a file or an install over ${maxFileBytes} bytes, so ${oversized.join(", ")} would fail after earlier writes landed`, + oversized.length && `the platform refuses a file over ${maxFileBytes} bytes and a registry_add call over ${maxAddWrites} files or ${maxFileBytes} bytes, so ${oversized.join(", ")} would fail after earlier writes landed`, edited.length && `${edited.join(", ")} changed on the draft after a release synced it, so the sync won't remove it; merge or delete it by hand`, ].filter(Boolean); if (problems.length) throw new Error(`This sync can't land: ${problems.join("; ")}. No registry or gallery files were written.`); @@ -374,6 +402,7 @@ async function sync() { if (dryRun) { for (const { path, expectedRevision } of writes) console.log(` ${expectedRevision ? "update" : "create"} ${path}`); for (const { path } of plan.retire) console.log(` delete ${path} (an untouched copy of a committed version)`); + for (const { items, files } of batches) console.log(` registry_add ${items.join(", ")} (${files.size} files not yet written this run)`); return; } await phase("write", async () => {