From 91ad7fb6ee3fb4d0098385abdea64a1d5acd0dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 8 Nov 2026 22:51:44 +0000 Subject: [PATCH] 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 --- packages/cli/src/cloud/offload.test.ts | 28 ++++++++++ packages/cli/src/cloud/offload.ts | 13 +++-- packages/cli/src/commands/cloud/render.ts | 11 ++-- packages/cli/src/utils/publishProject.test.ts | 52 +++++++++++++++++++ packages/cli/src/utils/publishProject.ts | 36 +++++++++++-- 5 files changed, 129 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/cloud/offload.test.ts b/packages/cli/src/cloud/offload.test.ts index fe6fbd9d16..f515e222f7 100644 --- a/packages/cli/src/cloud/offload.test.ts +++ b/packages/cli/src/cloud/offload.test.ts @@ -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(``), + [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(``), + [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/"); + }); }); diff --git a/packages/cli/src/cloud/offload.ts b/packages/cli/src/cloud/offload.ts index 570e8eff88..24a705bbe3 100644 --- a/packages/cli/src/cloud/offload.ts +++ b/packages/cli/src/cloud/offload.ts @@ -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"], diff --git a/packages/cli/src/commands/cloud/render.ts b/packages/cli/src/commands/cloud/render.ts index 16a406fd4a..0b7de37747 100644 --- a/packages/cli/src/commands/cloud/render.ts +++ b/packages/cli/src/commands/cloud/render.ts @@ -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; 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.`, diff --git a/packages/cli/src/utils/publishProject.test.ts b/packages/cli/src/utils/publishProject.test.ts index d93a6eca21..e729991128 100644 --- a/packages/cli/src/utils/publishProject.test.ts +++ b/packages/cli/src/utils/publishProject.test.ts @@ -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"), ``); + + 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"), + ``, + ); + + const map = buildPublishFileMap(dir); + + expect([...map.keys()].sort()).toEqual([composed, "assets/styles.css", "index.html"]); + expect(map.get("index.html")?.toString()).toBe( + ``, + ); + 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"), ""); + + expect(() => buildPublishFileMap(dir)).toThrow(/same name|differ only/i); + }); +}); diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts index cfe8f0f007..70f34c4ba7 100644 --- a/packages/cli/src/utils/publishProject.ts +++ b/packages/cli/src/utils/publishProject.ts @@ -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 { return stagePublishFiles(projectDir, Infinity) as Map; } +export const archiveKey = (path: string): string => path.normalize("NFC"); + +function composeNamesInText(fileContents: Map, filePaths: string[]): void { + const names = new Map(); + 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(); + const diskPathOf = new Map(); 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; }