fix(cli): cloud render finds media whose file name is stored in another Unicode form than the page's (#7419)

* fix(cli): cloud render finds media whose file name is stored in another Unicode form than the page's

* fix(cli): keep the archive-key change from raising publishProject's comment share

* fix(cli): a page that names a decomposed file as the disk lists it still matches its archive entry
This commit is contained in:
Miguel Ángel
2026-11-08 22:51:44 +00:00
committed by GitHub
parent 2927fe6155
commit 91ad7fb6ee
5 changed files with 129 additions and 11 deletions
+28
View File
@@ -1077,4 +1077,32 @@ describe("offloadHeavyMedia", () => {
expect(gifPrep).not.toHaveBeenCalled();
expect(fileMap.has("logo.png")).toBe(true);
});
it("offloads a file stored decomposed when the page names it composed, and the reverse", async () => {
const composed = "media/vid\u00e9o.mp4";
const decomposed = "media/vide\u0301o.mp4";
const dir = project({
"index.html": html(`<video src="${composed}"></video>`),
[decomposed]: HEAVY,
});
const fileMap = buildPublishFileMap(dir);
const report = await offloadHeavyMedia(dir, fileMap, {
apiBaseUrl: "https://api.example",
upload: fakeUpload(),
cachePath: cachePath(),
});
expect(report.map((item) => item.path)).toEqual([composed]);
expect(fileMap.get("index.html")!.toString()).toContain("https://assets.example/");
const other = project({
"index.html": html(`<video src="${decomposed}"></video>`),
[composed]: HEAVY,
});
const otherMap = buildPublishFileMap(other);
await offloadHeavyMedia(other, otherMap, {
apiBaseUrl: "https://api.example",
upload: fakeUpload(),
cachePath: cachePath(),
});
expect(otherMap.get("index.html")!.toString()).toContain("https://assets.example/");
});
});
+9 -4
View File
@@ -35,6 +35,7 @@ import { formatBytes } from "../ui/format.js";
import { bytesMatchExtension } from "./offloadSniff.js";
import { type PlayedSegment, planTrim } from "./offloadTrim.js";
import {
archiveKey,
assertUnchanged,
type HtmlAttributeResolver,
readChecked,
@@ -929,9 +930,9 @@ function htmlSrcKey(
if (!clean || isRemoteOrInlineUrl(clean)) return null;
const rootRelative = clean.startsWith("/")
? clean
: rewriteAssetPath(entryPath, clean, (p) => fileMap.has(p));
: rewriteAssetPath(entryPath, clean, (p) => fileMap.has(archiveKey(p)));
for (const candidate of resolveLocalAssetCandidates(absDir, rootRelative)) {
const key = relative(absDir, candidate).replaceAll("\\", "/");
const key = archiveKey(relative(absDir, candidate).replaceAll("\\", "/"));
if (fileMap.has(key)) return key;
}
return null;
@@ -956,7 +957,7 @@ function rewriteReferences(
): void {
const urlFor: UrlFor = (rootRelative) => {
for (const candidate of resolveLocalAssetCandidates(absDir, rootRelative)) {
const url = urlForKey(relative(absDir, candidate).replaceAll("\\", "/"));
const url = urlForKey(archiveKey(relative(absDir, candidate).replaceAll("\\", "/")));
if (url) return url;
}
return null;
@@ -1032,7 +1033,11 @@ function rewriteHtmlDocument(
const referrerAbsDir = resolve(absDir, dirname(entryPath));
// Sub-composition srcs resolve the way the renderer inlines them.
const resolveTarget: HtmlAttributeResolver = (raw) =>
localTarget(raw, (clean) => rewriteAssetPath(entryPath, clean, (p) => fileMap.has(p)), urlFor);
localTarget(
raw,
(clean) => rewriteAssetPath(entryPath, clean, (p) => fileMap.has(archiveKey(p))),
urlFor,
);
const { document } = parseHTML(html);
const attrs = rewriteHtmlAttributes(document, referrerAbsDir, entryPath, resolveTarget, {
attrs: ["src", "poster", "href"],
+7 -4
View File
@@ -49,6 +49,7 @@ import { resolveProject, type ProjectDir } from "../../utils/project.js";
import { readProjectConfig } from "../../utils/projectConfig.js";
import { normalizeErrorMessage } from "../../utils/errorMessage.js";
import {
archiveKey,
readStaged,
type StagedFile,
stagedSize,
@@ -729,10 +730,12 @@ function stageProjectFiles(
): { fileMap: Map<string, StagedFile>; entryPath: string } {
try {
const fileMap = stagePublishFiles(project.dir, OFFLOAD_MIN_BYTES);
const entryPath = relative(
project.dir,
resolvePath(project.dir, composition ?? "index.html"),
).replaceAll("\\", "/");
const entryPath = archiveKey(
relative(project.dir, resolvePath(project.dir, composition ?? "index.html")).replaceAll(
"\\",
"/",
),
);
if (!fileMap.has(entryPath)) {
throw new Error(
`Composition "${composition ?? "index.html"}" is excluded from the archive. Check .hyperframesignore.`,
@@ -1560,3 +1560,55 @@ describe("createPublishArchive referenced dot-paths", () => {
expect(names(dir)).not.toContain(".env");
});
});
describe("archive keys for names written in either Unicode form", () => {
// "é" as one code point (composed) and as e + a combining accent (decomposed): macOS file pickers store the latter.
const composed = "assets/Photos et vidéos 1.mp4";
const decomposed = "assets/Photos et vidéos 1.mp4";
it("keys a file stored decomposed by the composed name a page uses, so the renderer finds it", () => {
const dir = makeProjectDir();
mkdirSync(join(dir, "assets"), { recursive: true });
writeFileSync(join(dir, decomposed), "video");
writeFileSync(join(dir, "index.html"), `<video src="${composed}"></video>`);
const map = buildPublishFileMap(dir);
expect([...map.keys()].sort()).toEqual([composed, "index.html"]);
expect(map.get(composed)?.toString()).toBe("video");
const entries = new AdmZip(zipPublishFileMap(map).buffer).getEntries().map((e) => e.entryName);
expect(entries).toContain(composed);
});
it("rewrites a page and a stylesheet that name a decomposed file the way the disk lists it", () => {
const dir = makeProjectDir();
mkdirSync(join(dir, "assets"), { recursive: true });
writeFileSync(join(dir, decomposed), "video");
writeFileSync(
join(dir, "assets/styles.css"),
`.a { background: url("${decomposed.slice(7)}"); }`,
);
writeFileSync(
join(dir, "index.html"),
`<video src="${decomposed}"></video><img src="${encodeURI(decomposed)}">`,
);
const map = buildPublishFileMap(dir);
expect([...map.keys()].sort()).toEqual([composed, "assets/styles.css", "index.html"]);
expect(map.get("index.html")?.toString()).toBe(
`<video src="${composed}"></video><img src="${encodeURI(composed)}">`,
);
expect(map.get("assets/styles.css")?.toString()).toContain(composed.slice(7));
});
it("refuses two files that differ only by Unicode form instead of letting one overwrite the other", () => {
const dir = makeProjectDir();
mkdirSync(join(dir, "assets"), { recursive: true });
writeFileSync(join(dir, composed), "first");
writeFileSync(join(dir, decomposed), "second");
writeFileSync(join(dir, "index.html"), "<html></html>");
expect(() => buildPublishFileMap(dir)).toThrow(/same name|differ only/i);
});
});
+33 -3
View File
@@ -398,7 +398,7 @@ function addExternalAsset(ctx: ExternalAssetContext, absPath: string): string {
const rel = relative(ctx.absProjectDir, absPath).replaceAll("\\", "/");
const stripped = rel.replace(/^(?:\.\.\/)+/, "");
let archivePath = `${EXT_ASSETS_PREFIX}/${stripped}`;
let archivePath = archiveKey(`${EXT_ASSETS_PREFIX}/${stripped}`);
if (ctx.usedArchivePaths.has(archivePath)) {
const ext = posix.extname(archivePath);
@@ -425,7 +425,7 @@ function includeReferencedProjectFile(
for (const candidate of [resolvedPath, fromRoot]) {
const absPath = candidate.replace(/[?#].*$/, "");
if (!isPathInside(absPath, ctx.absProjectDir)) continue;
const rel = relative(ctx.absProjectDir, absPath).replaceAll("\\", "/");
const rel = archiveKey(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
if (!rel || ctx.fileContents.has(rel) || rel.split("/").some(shouldIgnoreSegment)) return;
if (!existsSync(absPath)) continue;
if (statSync(absPath).isFile()) {
@@ -673,6 +673,26 @@ export function buildPublishFileMap(projectDir: string): Map<string, Buffer> {
return stagePublishFiles(projectDir, Infinity) as Map<string, Buffer>;
}
export const archiveKey = (path: string): string => path.normalize("NFC");
function composeNamesInText(fileContents: Map<string, StagedFile>, filePaths: string[]): void {
const names = new Map<string, string>();
for (const segment of filePaths.flatMap((path) => path.split("/"))) {
const composed = archiveKey(segment);
if (composed === segment) continue;
names.set(segment, composed);
names.set(encodeURIComponent(segment), encodeURIComponent(composed));
}
if (names.size === 0) return;
for (const [key, file] of fileContents) {
if (!Buffer.isBuffer(file) || !TEXT_FILE_EXTS.test(key)) continue;
let text = file.toString("utf-8");
const before = text;
for (const [from, to] of names) text = text.replaceAll(from, to);
if (text !== before) fileContents.set(key, Buffer.from(text, "utf-8"));
}
}
/** {@link buildPublishFileMap}, but non-text files of `onDiskMinBytes` or more stay on disk. */
export function stagePublishFiles(
projectDir: string,
@@ -688,10 +708,20 @@ export function stagePublishFiles(
}
const fileContents = new Map<string, StagedFile>();
const diskPathOf = new Map<string, string>();
for (const filePath of filePaths) {
fileContents.set(filePath, stageFile(join(absProjectDir, filePath), onDiskMinBytes));
const key = archiveKey(filePath);
const clash = diskPathOf.get(key);
if (clash !== undefined) {
throw new Error(
`"${clash}" and "${filePath}" have the same name written in two Unicode forms and would overwrite each other in the archive. Rename one of them.`,
);
}
diskPathOf.set(key, filePath);
fileContents.set(key, stageFile(join(absProjectDir, filePath), onDiskMinBytes));
}
composeNamesInText(fileContents, filePaths);
localizeExternalAssets(absProjectDir, fileContents, onDiskMinBytes);
return fileContents;
}