Skip to content

Commit 91ad7fb

Browse files
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
1 parent 2927fe6 commit 91ad7fb

5 files changed

Lines changed: 129 additions & 11 deletions

File tree

‎packages/cli/src/cloud/offload.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,4 +1077,32 @@ describe("offloadHeavyMedia", () => {
10771077
expect(gifPrep).not.toHaveBeenCalled();
10781078
expect(fileMap.has("logo.png")).toBe(true);
10791079
});
1080+
it("offloads a file stored decomposed when the page names it composed, and the reverse", async () => {
1081+
const composed = "media/vid\u00e9o.mp4";
1082+
const decomposed = "media/vide\u0301o.mp4";
1083+
const dir = project({
1084+
"index.html": html(`<video src="${composed}"></video>`),
1085+
[decomposed]: HEAVY,
1086+
});
1087+
const fileMap = buildPublishFileMap(dir);
1088+
const report = await offloadHeavyMedia(dir, fileMap, {
1089+
apiBaseUrl: "https://api.example",
1090+
upload: fakeUpload(),
1091+
cachePath: cachePath(),
1092+
});
1093+
expect(report.map((item) => item.path)).toEqual([composed]);
1094+
expect(fileMap.get("index.html")!.toString()).toContain("https://assets.example/");
1095+
1096+
const other = project({
1097+
"index.html": html(`<video src="${decomposed}"></video>`),
1098+
[composed]: HEAVY,
1099+
});
1100+
const otherMap = buildPublishFileMap(other);
1101+
await offloadHeavyMedia(other, otherMap, {
1102+
apiBaseUrl: "https://api.example",
1103+
upload: fakeUpload(),
1104+
cachePath: cachePath(),
1105+
});
1106+
expect(otherMap.get("index.html")!.toString()).toContain("https://assets.example/");
1107+
});
10801108
});

‎packages/cli/src/cloud/offload.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { formatBytes } from "../ui/format.js";
3535
import { bytesMatchExtension } from "./offloadSniff.js";
3636
import { type PlayedSegment, planTrim } from "./offloadTrim.js";
3737
import {
38+
archiveKey,
3839
assertUnchanged,
3940
type HtmlAttributeResolver,
4041
readChecked,
@@ -929,9 +930,9 @@ function htmlSrcKey(
929930
if (!clean || isRemoteOrInlineUrl(clean)) return null;
930931
const rootRelative = clean.startsWith("/")
931932
? clean
932-
: rewriteAssetPath(entryPath, clean, (p) => fileMap.has(p));
933+
: rewriteAssetPath(entryPath, clean, (p) => fileMap.has(archiveKey(p)));
933934
for (const candidate of resolveLocalAssetCandidates(absDir, rootRelative)) {
934-
const key = relative(absDir, candidate).replaceAll("\\", "/");
935+
const key = archiveKey(relative(absDir, candidate).replaceAll("\\", "/"));
935936
if (fileMap.has(key)) return key;
936937
}
937938
return null;
@@ -956,7 +957,7 @@ function rewriteReferences(
956957
): void {
957958
const urlFor: UrlFor = (rootRelative) => {
958959
for (const candidate of resolveLocalAssetCandidates(absDir, rootRelative)) {
959-
const url = urlForKey(relative(absDir, candidate).replaceAll("\\", "/"));
960+
const url = urlForKey(archiveKey(relative(absDir, candidate).replaceAll("\\", "/")));
960961
if (url) return url;
961962
}
962963
return null;
@@ -1032,7 +1033,11 @@ function rewriteHtmlDocument(
10321033
const referrerAbsDir = resolve(absDir, dirname(entryPath));
10331034
// Sub-composition srcs resolve the way the renderer inlines them.
10341035
const resolveTarget: HtmlAttributeResolver = (raw) =>
1035-
localTarget(raw, (clean) => rewriteAssetPath(entryPath, clean, (p) => fileMap.has(p)), urlFor);
1036+
localTarget(
1037+
raw,
1038+
(clean) => rewriteAssetPath(entryPath, clean, (p) => fileMap.has(archiveKey(p))),
1039+
urlFor,
1040+
);
10361041
const { document } = parseHTML(html);
10371042
const attrs = rewriteHtmlAttributes(document, referrerAbsDir, entryPath, resolveTarget, {
10381043
attrs: ["src", "poster", "href"],

‎packages/cli/src/commands/cloud/render.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { resolveProject, type ProjectDir } from "../../utils/project.js";
4949
import { readProjectConfig } from "../../utils/projectConfig.js";
5050
import { normalizeErrorMessage } from "../../utils/errorMessage.js";
5151
import {
52+
archiveKey,
5253
readStaged,
5354
type StagedFile,
5455
stagedSize,
@@ -729,10 +730,12 @@ function stageProjectFiles(
729730
): { fileMap: Map<string, StagedFile>; entryPath: string } {
730731
try {
731732
const fileMap = stagePublishFiles(project.dir, OFFLOAD_MIN_BYTES);
732-
const entryPath = relative(
733-
project.dir,
734-
resolvePath(project.dir, composition ?? "index.html"),
735-
).replaceAll("\\", "/");
733+
const entryPath = archiveKey(
734+
relative(project.dir, resolvePath(project.dir, composition ?? "index.html")).replaceAll(
735+
"\\",
736+
"/",
737+
),
738+
);
736739
if (!fileMap.has(entryPath)) {
737740
throw new Error(
738741
`Composition "${composition ?? "index.html"}" is excluded from the archive. Check .hyperframesignore.`,

‎packages/cli/src/utils/publishProject.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,3 +1560,55 @@ describe("createPublishArchive referenced dot-paths", () => {
15601560
expect(names(dir)).not.toContain(".env");
15611561
});
15621562
});
1563+
1564+
describe("archive keys for names written in either Unicode form", () => {
1565+
// "é" as one code point (composed) and as e + a combining accent (decomposed): macOS file pickers store the latter.
1566+
const composed = "assets/Photos et vidéos 1.mp4";
1567+
const decomposed = "assets/Photos et vidéos 1.mp4";
1568+
1569+
it("keys a file stored decomposed by the composed name a page uses, so the renderer finds it", () => {
1570+
const dir = makeProjectDir();
1571+
mkdirSync(join(dir, "assets"), { recursive: true });
1572+
writeFileSync(join(dir, decomposed), "video");
1573+
writeFileSync(join(dir, "index.html"), `<video src="${composed}"></video>`);
1574+
1575+
const map = buildPublishFileMap(dir);
1576+
1577+
expect([...map.keys()].sort()).toEqual([composed, "index.html"]);
1578+
expect(map.get(composed)?.toString()).toBe("video");
1579+
const entries = new AdmZip(zipPublishFileMap(map).buffer).getEntries().map((e) => e.entryName);
1580+
expect(entries).toContain(composed);
1581+
});
1582+
1583+
it("rewrites a page and a stylesheet that name a decomposed file the way the disk lists it", () => {
1584+
const dir = makeProjectDir();
1585+
mkdirSync(join(dir, "assets"), { recursive: true });
1586+
writeFileSync(join(dir, decomposed), "video");
1587+
writeFileSync(
1588+
join(dir, "assets/styles.css"),
1589+
`.a { background: url("${decomposed.slice(7)}"); }`,
1590+
);
1591+
writeFileSync(
1592+
join(dir, "index.html"),
1593+
`<video src="${decomposed}"></video><img src="${encodeURI(decomposed)}">`,
1594+
);
1595+
1596+
const map = buildPublishFileMap(dir);
1597+
1598+
expect([...map.keys()].sort()).toEqual([composed, "assets/styles.css", "index.html"]);
1599+
expect(map.get("index.html")?.toString()).toBe(
1600+
`<video src="${composed}"></video><img src="${encodeURI(composed)}">`,
1601+
);
1602+
expect(map.get("assets/styles.css")?.toString()).toContain(composed.slice(7));
1603+
});
1604+
1605+
it("refuses two files that differ only by Unicode form instead of letting one overwrite the other", () => {
1606+
const dir = makeProjectDir();
1607+
mkdirSync(join(dir, "assets"), { recursive: true });
1608+
writeFileSync(join(dir, composed), "first");
1609+
writeFileSync(join(dir, decomposed), "second");
1610+
writeFileSync(join(dir, "index.html"), "<html></html>");
1611+
1612+
expect(() => buildPublishFileMap(dir)).toThrow(/same name|differ only/i);
1613+
});
1614+
});

‎packages/cli/src/utils/publishProject.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function addExternalAsset(ctx: ExternalAssetContext, absPath: string): string {
398398

399399
const rel = relative(ctx.absProjectDir, absPath).replaceAll("\\", "/");
400400
const stripped = rel.replace(/^(?:\.\.\/)+/, "");
401-
let archivePath = `${EXT_ASSETS_PREFIX}/${stripped}`;
401+
let archivePath = archiveKey(`${EXT_ASSETS_PREFIX}/${stripped}`);
402402

403403
if (ctx.usedArchivePaths.has(archivePath)) {
404404
const ext = posix.extname(archivePath);
@@ -425,7 +425,7 @@ function includeReferencedProjectFile(
425425
for (const candidate of [resolvedPath, fromRoot]) {
426426
const absPath = candidate.replace(/[?#].*$/, "");
427427
if (!isPathInside(absPath, ctx.absProjectDir)) continue;
428-
const rel = relative(ctx.absProjectDir, absPath).replaceAll("\\", "/");
428+
const rel = archiveKey(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
429429
if (!rel || ctx.fileContents.has(rel) || rel.split("/").some(shouldIgnoreSegment)) return;
430430
if (!existsSync(absPath)) continue;
431431
if (statSync(absPath).isFile()) {
@@ -673,6 +673,26 @@ export function buildPublishFileMap(projectDir: string): Map<string, Buffer> {
673673
return stagePublishFiles(projectDir, Infinity) as Map<string, Buffer>;
674674
}
675675

676+
export const archiveKey = (path: string): string => path.normalize("NFC");
677+
678+
function composeNamesInText(fileContents: Map<string, StagedFile>, filePaths: string[]): void {
679+
const names = new Map<string, string>();
680+
for (const segment of filePaths.flatMap((path) => path.split("/"))) {
681+
const composed = archiveKey(segment);
682+
if (composed === segment) continue;
683+
names.set(segment, composed);
684+
names.set(encodeURIComponent(segment), encodeURIComponent(composed));
685+
}
686+
if (names.size === 0) return;
687+
for (const [key, file] of fileContents) {
688+
if (!Buffer.isBuffer(file) || !TEXT_FILE_EXTS.test(key)) continue;
689+
let text = file.toString("utf-8");
690+
const before = text;
691+
for (const [from, to] of names) text = text.replaceAll(from, to);
692+
if (text !== before) fileContents.set(key, Buffer.from(text, "utf-8"));
693+
}
694+
}
695+
676696
/** {@link buildPublishFileMap}, but non-text files of `onDiskMinBytes` or more stay on disk. */
677697
export function stagePublishFiles(
678698
projectDir: string,
@@ -688,10 +708,20 @@ export function stagePublishFiles(
688708
}
689709

690710
const fileContents = new Map<string, StagedFile>();
711+
const diskPathOf = new Map<string, string>();
691712
for (const filePath of filePaths) {
692-
fileContents.set(filePath, stageFile(join(absProjectDir, filePath), onDiskMinBytes));
713+
const key = archiveKey(filePath);
714+
const clash = diskPathOf.get(key);
715+
if (clash !== undefined) {
716+
throw new Error(
717+
`"${clash}" and "${filePath}" have the same name written in two Unicode forms and would overwrite each other in the archive. Rename one of them.`,
718+
);
719+
}
720+
diskPathOf.set(key, filePath);
721+
fileContents.set(key, stageFile(join(absProjectDir, filePath), onDiskMinBytes));
693722
}
694723

724+
composeNamesInText(fileContents, filePaths);
695725
localizeExternalAssets(absProjectDir, fileContents, onDiskMinBytes);
696726
return fileContents;
697727
}

0 commit comments

Comments
 (0)