mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
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:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user