mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): cloud render zip carries files the composition references under dot-folders (#7353)
* fix(cli): cloud render zip carries files the composition references under dot-folders * fix(cli): a missing link href no longer fails the cloud zip; media refs still do * chore(cli): shorten the referenced-file comment * fix(cli): only a missing video, audio, img src or poster stops the cloud zip * fix(cli): a stylesheet url() never stops the cloud zip; only the no-fallback media refs do
This commit is contained in:
@@ -1504,3 +1504,59 @@ describe("readStaged", () => {
|
||||
expect(() => readStaged(staged)).toThrow(/changed on disk/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createPublishArchive referenced dot-paths", () => {
|
||||
const clip = ".hyperframes/prepared-assets/clip.mp4";
|
||||
|
||||
function projectWith(html: string, files: Record<string, string>): string {
|
||||
const dir = makeProjectDir();
|
||||
writeFileSync(join(dir, "index.html"), html, "utf-8");
|
||||
for (const [name, body] of Object.entries(files)) {
|
||||
mkdirSync(dirname(join(dir, name)), { recursive: true });
|
||||
writeFileSync(join(dir, name), body, "utf-8");
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
const names = (dir: string) =>
|
||||
new AdmZip(createPublishArchive(dir).buffer).getEntries().map((e) => e.entryName);
|
||||
|
||||
it("carries a referenced file under a dot-folder", () => {
|
||||
const dir = projectWith(`<video src="${clip}"></video>`, { [clip]: "v" });
|
||||
expect(names(dir)).toContain(clip);
|
||||
});
|
||||
|
||||
it("fails before upload when a referenced file is missing", () => {
|
||||
const dir = projectWith(`<video src="${clip}"></video>`, {});
|
||||
expect(() => createPublishArchive(dir)).toThrow(clip);
|
||||
});
|
||||
|
||||
it("does not fail on a missing href, source fallback, srcset candidate or CSS url", () => {
|
||||
const dir = projectWith(
|
||||
`<link rel="stylesheet" href="s.css"><a href="notes.html">n</a><video><source src="a.webm"><source src="a.mp4"></video>` +
|
||||
`<img src="a.png" srcset="a.png 1x, a@2x.png 2x"><style>.a{background:url(gone.jpg)}</style>`,
|
||||
{ "a.mp4": "v", "a.png": "p", "s.css": ".a{background:url(gone.jpg)}" },
|
||||
);
|
||||
expect(() => createPublishArchive(dir)).not.toThrow();
|
||||
expect(names(dir)).not.toContain("notes.html");
|
||||
});
|
||||
|
||||
it("fails on a missing video, audio, img src or poster, naming the file", () => {
|
||||
for (const tag of [
|
||||
'video src="gone.mp4"',
|
||||
'audio src="gone.mp3"',
|
||||
'img src="gone.png"',
|
||||
'video poster="gone.jpg"',
|
||||
]) {
|
||||
const name = /"(gone\.[a-z0-9]+)"/.exec(tag)![1]!;
|
||||
expect(() =>
|
||||
createPublishArchive(projectWith(`<${tag}></${tag.split(" ")[0]}>`, {})),
|
||||
).toThrow(name);
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves out an unreferenced .env", () => {
|
||||
const dir = projectWith(`<video src="${clip}"></video>`, { [clip]: "v", ".env": "SECRET=1" });
|
||||
expect(names(dir)).not.toContain(".env");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -414,15 +414,45 @@ function addExternalAsset(ctx: ExternalAssetContext, absPath: string): string {
|
||||
return archivePath;
|
||||
}
|
||||
|
||||
/** Ships a referenced in-project file the ignore rules skipped, resolved from its folder or the root. */
|
||||
function includeReferencedProjectFile(
|
||||
ctx: ExternalAssetContext,
|
||||
resolvedPath: string,
|
||||
rawPath: string,
|
||||
requireOnDisk: boolean,
|
||||
): void {
|
||||
const fromRoot = resolve(ctx.absProjectDir, rawPath);
|
||||
for (const candidate of [resolvedPath, fromRoot]) {
|
||||
const absPath = candidate.replace(/[?#].*$/, "");
|
||||
if (!isPathInside(absPath, ctx.absProjectDir)) continue;
|
||||
const rel = relative(ctx.absProjectDir, absPath).replaceAll("\\", "/");
|
||||
if (!rel || ctx.fileContents.has(rel) || rel.split("/").some(shouldIgnoreSegment)) return;
|
||||
if (!existsSync(absPath)) continue;
|
||||
if (statSync(absPath).isFile()) {
|
||||
ctx.fileContents.set(rel, stageFile(absPath, ctx.onDiskMinBytes));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!requireOnDisk) return;
|
||||
throw new Error(`The composition references "${rawPath}", which is not in the project folder.`);
|
||||
}
|
||||
|
||||
/** References with no fallback: a missing file here cannot render. */
|
||||
const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[poster]"]);
|
||||
|
||||
function tryResolveExternal(
|
||||
ctx: ExternalAssetContext,
|
||||
rawPath: string,
|
||||
referrerAbsDir: string,
|
||||
requireOnDisk = false,
|
||||
): string | null {
|
||||
if (isNonRelativeUrl(rawPath)) return null;
|
||||
for (const path of decodeUrlPathVariants(rawPath)) {
|
||||
const absPath = resolve(referrerAbsDir, path);
|
||||
if (isPathInside(absPath, ctx.absProjectDir)) return null;
|
||||
if (isPathInside(absPath, ctx.absProjectDir)) {
|
||||
includeReferencedProjectFile(ctx, absPath, rawPath, requireOnDisk);
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if (!existsSync(absPath) || !statSync(absPath).isFile()) continue;
|
||||
} catch {
|
||||
@@ -435,7 +465,11 @@ function tryResolveExternal(
|
||||
|
||||
/** Resolves a raw attribute or CSS `url()` value to the archive path or absolute
|
||||
* URL it should point at, or `null` to leave it untouched. */
|
||||
export type HtmlAttributeResolver = (rawValue: string, referrerAbsDir: string) => string | null;
|
||||
export type HtmlAttributeResolver = (
|
||||
rawValue: string,
|
||||
referrerAbsDir: string,
|
||||
ref?: { attr: string; tag: string },
|
||||
) => string | null;
|
||||
|
||||
function referenceFrom(entryPath: string, target: string): string {
|
||||
return isNonRelativeUrl(target) ? target : posix.relative(posix.dirname(entryPath), target);
|
||||
@@ -490,7 +524,7 @@ export function rewriteHtmlAttributes(
|
||||
for (const attr of attrs) {
|
||||
const val = (el.getAttribute(attr) || "").trim();
|
||||
if (!val) continue;
|
||||
const target = resolveTarget(val, referrerAbsDir);
|
||||
const target = resolveTarget(val, referrerAbsDir, { attr, tag: el.localName });
|
||||
if (!target) continue;
|
||||
el.setAttribute(attr, referenceFrom(entryPath, target));
|
||||
modified = true;
|
||||
@@ -551,7 +585,13 @@ function rewriteHtmlSrcsets(
|
||||
function localizeHtmlEntry(ctx: ExternalAssetContext, entryPath: string, content: Buffer): void {
|
||||
const referrerAbsDir = resolve(ctx.absProjectDir, dirname(entryPath));
|
||||
const { document } = parseHTML(content.toString("utf-8"));
|
||||
const resolveTarget: HtmlAttributeResolver = (val, dir) => tryResolveExternal(ctx, val, dir);
|
||||
const resolveTarget: HtmlAttributeResolver = (val, dir, ref) =>
|
||||
tryResolveExternal(
|
||||
ctx,
|
||||
val,
|
||||
dir,
|
||||
ref !== undefined && HARD_REFERENCES.has(`${ref.tag}[${ref.attr}]`),
|
||||
);
|
||||
const attrsChanged = rewriteHtmlAttributes(document, referrerAbsDir, entryPath, resolveTarget, {
|
||||
attrs: ["src", "href", "poster"],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user