Skip to content

Commit 77bacbe

Browse files
committed
fix(cli): a page that names a decomposed file as the disk lists it still matches its archive entry
1 parent 704e644 commit 77bacbe

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,28 @@ describe("archive keys for names written in either Unicode form", () => {
15801580
expect(entries).toContain(composed);
15811581
});
15821582

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+
15831605
it("refuses two files that differ only by Unicode form instead of letting one overwrite the other", () => {
15841606
const dir = makeProjectDir();
15851607
mkdirSync(join(dir, "assets"), { recursive: true });

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,24 @@ export function buildPublishFileMap(projectDir: string): Map<string, Buffer> {
675675

676676
export const archiveKey = (path: string): string => path.normalize("NFC");
677677

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+
678696
/** {@link buildPublishFileMap}, but non-text files of `onDiskMinBytes` or more stay on disk. */
679697
export function stagePublishFiles(
680698
projectDir: string,
@@ -703,6 +721,7 @@ export function stagePublishFiles(
703721
fileContents.set(key, stageFile(join(absProjectDir, filePath), onDiskMinBytes));
704722
}
705723

724+
composeNamesInText(fileContents, filePaths);
706725
localizeExternalAssets(absProjectDir, fileContents, onDiskMinBytes);
707726
return fileContents;
708727
}

0 commit comments

Comments
 (0)