mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): cloud render no longer refuses a file only an unused page names (#7812)
* fix(cli): cloud render no longer refuses a file only an unused page names The video's pages are walked from the staged upload, starting at index.html, with the renderer's resolver; when a page it loads is not staged text, every page stays strict. * fix(cli): cloud render --composition checks missing files from the page it renders
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const cliEntry = resolve(fileURLToPath(import.meta.url), "..", "..", "..", "cli.ts");
|
||||
|
||||
const page = (body: string) =>
|
||||
`<!doctype html><html><body><div data-composition-id="main" data-width="1920" data-height="1080">${body}</div></body></html>`;
|
||||
|
||||
/** A project whose index.html and alternate.html do not mount each other. */
|
||||
function project(index: string, alternate: string): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-cloud-missing-entry-"));
|
||||
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
|
||||
writeFileSync(join(dir, "index.html"), page(index));
|
||||
writeFileSync(join(dir, "alternate.html"), page(alternate));
|
||||
return dir;
|
||||
}
|
||||
|
||||
function dryRunAlternate(dir: string) {
|
||||
return spawnSync(
|
||||
"bun",
|
||||
[
|
||||
"run",
|
||||
cliEntry,
|
||||
"cloud",
|
||||
"render",
|
||||
dir,
|
||||
"--composition",
|
||||
"alternate.html",
|
||||
"--dry-run",
|
||||
"--json",
|
||||
],
|
||||
{
|
||||
encoding: "utf-8",
|
||||
timeout: 30_000,
|
||||
env: {
|
||||
...process.env,
|
||||
CI: "1",
|
||||
HEYGEN_API_URL: "http://127.0.0.1:1",
|
||||
HYPERFRAMES_NO_UPDATE_CHECK: "1",
|
||||
HYPERFRAMES_NO_TELEMETRY: "1",
|
||||
HOME: dir,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const MISSING = 'The composition references "missing.png", which is not in the project folder.';
|
||||
|
||||
describe("cloud render --composition checks missing files from the page it renders", () => {
|
||||
it("refuses a missing image on the selected page", () => {
|
||||
const result = dryRunAlternate(project("", `<img src="missing.png">`));
|
||||
expect(result.status).not.toBe(0);
|
||||
expect(result.stdout + result.stderr).toContain(MISSING);
|
||||
});
|
||||
|
||||
it("allows a missing image on an index.html the selected page does not play", () => {
|
||||
const result = dryRunAlternate(project(`<img src="missing.png">`, ""));
|
||||
expect(result.stdout + result.stderr).not.toContain(MISSING);
|
||||
expect(result.status).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -790,13 +790,19 @@ function stageProjectFiles(
|
||||
onOutsideFile: OnOutsideFile,
|
||||
): { fileMap: Map<string, StagedFile>; entryPath: string } {
|
||||
try {
|
||||
const fileMap = stagePublishFiles(project.dir, OFFLOAD_MIN_BYTES, onLeftOut, onOutsideFile);
|
||||
const entryPath = archiveKey(
|
||||
relative(project.dir, resolvePath(project.dir, composition ?? "index.html")).replaceAll(
|
||||
"\\",
|
||||
"/",
|
||||
),
|
||||
);
|
||||
const fileMap = stagePublishFiles(
|
||||
project.dir,
|
||||
OFFLOAD_MIN_BYTES,
|
||||
onLeftOut,
|
||||
onOutsideFile,
|
||||
entryPath,
|
||||
);
|
||||
if (!fileMap.has(entryPath)) {
|
||||
throw new Error(
|
||||
`Composition "${composition ?? "index.html"}" is excluded from the archive. Check .hyperframesignore.`,
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { stagePublishFiles } from "./publishProject.js";
|
||||
|
||||
const dirs: string[] = [];
|
||||
afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })));
|
||||
|
||||
const FILM = `<div data-composition-id="main" data-width="1920" data-height="1080" data-duration="2">
|
||||
<div data-composition-src="compositions/intro.html"></div>
|
||||
</div>`;
|
||||
|
||||
/** Writes `files` under a fresh project folder and stages it as the cloud would upload it. */
|
||||
function stage(files: Record<string, string | Buffer>, prepare?: (dir: string) => void) {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-missing-"));
|
||||
dirs.push(dir);
|
||||
for (const [name, body] of Object.entries({ "index.html": FILM, ...files })) {
|
||||
mkdirSync(dirname(join(dir, name)), { recursive: true });
|
||||
writeFileSync(join(dir, name), body);
|
||||
}
|
||||
prepare?.(dir);
|
||||
return () => stagePublishFiles(dir, Infinity);
|
||||
}
|
||||
|
||||
const refusal = (name: string) =>
|
||||
`The composition references "${name}", which is not in the project folder.`;
|
||||
|
||||
describe("a file a page names that the folder lacks", () => {
|
||||
// fails without the fix: a storyboard page beside the video blocked the cloud render of a video that has every file
|
||||
it("is no refusal when the page is one the video never plays", () => {
|
||||
const staged = stage({
|
||||
"compositions/intro.html": `<video src="clip.mp4"></video>`,
|
||||
"compositions/clip.mp4": "mp4",
|
||||
"boards/storyboard.html": `<img src="stills/at-1.2s.png">`,
|
||||
"drafts/old.html": `<audio src="../music/old.mp3"></audio>`,
|
||||
})();
|
||||
expect(staged.has("boards/storyboard.html")).toBe(true);
|
||||
expect(staged.has("compositions/clip.mp4")).toBe(true);
|
||||
});
|
||||
|
||||
it("is still refused when the video's own page names it", () => {
|
||||
const staged = stage({
|
||||
"index.html": `<div data-composition-id="main"><video src="assets/gone.mp4"></video></div>`,
|
||||
});
|
||||
expect(staged).toThrow(refusal("assets/gone.mp4"));
|
||||
});
|
||||
|
||||
it("is still refused when a composition the video loads names it", () => {
|
||||
const staged = stage({ "compositions/intro.html": `<img src="assets/logo.png">` });
|
||||
expect(staged).toThrow(refusal("assets/logo.png"));
|
||||
});
|
||||
|
||||
it("is still refused on a played page however deep the compositions nest", () => {
|
||||
const chain: Record<string, string> = {};
|
||||
for (let i = 0; i < 300; i++)
|
||||
chain[`compositions/c${i}.html`] =
|
||||
`<div data-composition-src="compositions/c${i + 1}.html"></div>`;
|
||||
chain["compositions/c300.html"] = `<img src="assets/deep.png">`;
|
||||
const staged = stage({
|
||||
...chain,
|
||||
"index.html": `<div data-composition-src="compositions/c0.html"></div>`,
|
||||
});
|
||||
expect(staged).toThrow(refusal("assets/deep.png"));
|
||||
});
|
||||
|
||||
it("is still refused on a page mounted by one whose name is written in another Unicode form", () => {
|
||||
const staged = stage({
|
||||
"index.html": `<div data-composition-src="compositions/caf\u00e9.html"></div>`,
|
||||
"compositions/cafe\u0301.html": `<div data-composition-src="compositions/inner.html"></div>`,
|
||||
"compositions/inner.html": `<img src="assets/logo.png">`,
|
||||
});
|
||||
expect(staged).toThrow(refusal("assets/logo.png"));
|
||||
});
|
||||
|
||||
it("is still refused on a page a UTF-16 index.html mounts", () => {
|
||||
const index = `<div data-composition-src="compositions/intro.html"></div>`;
|
||||
const staged = stage({
|
||||
"index.html": Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(index, "utf16le")]),
|
||||
"compositions/intro.html": `<img src="assets/logo.png">`,
|
||||
});
|
||||
expect(staged).toThrow(refusal("assets/logo.png"));
|
||||
});
|
||||
|
||||
it("is refused on any page when a page the video loads is not in the upload", () => {
|
||||
const staged = stage({
|
||||
"compositions/intro.html": `<div></div>`,
|
||||
".hyperframesignore": "compositions/intro.html\n",
|
||||
"boards/storyboard.html": `<img src="stills/at-1.2s.png">`,
|
||||
});
|
||||
expect(staged).toThrow(refusal("stills/at-1.2s.png"));
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"is refused on any page, without waiting, when the video loads a named pipe",
|
||||
() => {
|
||||
const staged = stage(
|
||||
{ "boards/storyboard.html": `<img src="stills/at-1.2s.png">` },
|
||||
(dir) => {
|
||||
mkdirSync(join(dir, "compositions"));
|
||||
execFileSync("mkfifo", [join(dir, "compositions/intro.html")]);
|
||||
},
|
||||
);
|
||||
expect(staged).toThrow(refusal("stills/at-1.2s.png"));
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -39,6 +39,11 @@ import {
|
||||
outsideFileReference,
|
||||
} from "./leftOutReference.js";
|
||||
import { isUploadedOutsideFileType } from "./outsideFileType.js";
|
||||
import {
|
||||
collectSubCompositionSrcs,
|
||||
decodeTextHonoringBom,
|
||||
resolveCompositionSrc,
|
||||
} from "@hyperframes/parsers/asset-resolution";
|
||||
|
||||
const IGNORED_DIRS = new Set([".git", "node_modules", "dist", ".next", "coverage"]);
|
||||
const IGNORED_FILES = new Set([".DS_Store", "Thumbs.db"]);
|
||||
@@ -411,6 +416,7 @@ interface ExternalAssetContext {
|
||||
onLeftOut?: OnLeftOut;
|
||||
onOutsideFile?: OnOutsideFile;
|
||||
received: (path: string) => boolean;
|
||||
filmPages: Set<string> | null;
|
||||
}
|
||||
|
||||
function addExternalAsset(
|
||||
@@ -488,7 +494,7 @@ function includeReferencedProjectFile(
|
||||
throw new Error(`The composition references "${rawPath}", which is not in the project folder.`);
|
||||
}
|
||||
|
||||
/** References with no fallback: a missing file here cannot render. */
|
||||
/** References with no fallback: on a page the video plays, a missing file here cannot render. */
|
||||
const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[poster]"]);
|
||||
|
||||
const relativeToProject = (ctx: ExternalAssetContext, absPath: string) =>
|
||||
@@ -690,7 +696,9 @@ function localizeHtmlEntry(ctx: ExternalAssetContext, entryPath: string, content
|
||||
val,
|
||||
dir,
|
||||
entryPath,
|
||||
ref !== undefined && HARD_REFERENCES.has(`${ref.tag}[${ref.attr}]`),
|
||||
(ctx.filmPages?.has(entryPath) ?? true) &&
|
||||
ref !== undefined &&
|
||||
HARD_REFERENCES.has(`${ref.tag}[${ref.attr}]`),
|
||||
);
|
||||
const attrsChanged = rewriteHtmlAttributes(document, referrerAbsDir, entryPath, resolveTarget, {
|
||||
attrs: ["src", "href", "poster"],
|
||||
@@ -714,6 +722,27 @@ function localizeCssEntry(ctx: ExternalAssetContext, entryPath: string, content:
|
||||
}
|
||||
}
|
||||
|
||||
/** Staged pages played from `entry`, as the renderer mounts them; null (check every page) when one is not staged. */
|
||||
function filmPagesOf(
|
||||
absProjectDir: string,
|
||||
fileContents: Map<string, StagedFile>,
|
||||
entry: string,
|
||||
): Set<string> | null {
|
||||
const pages = new Set<string>();
|
||||
const queue = [entry];
|
||||
for (const page of queue) {
|
||||
if (pages.has(page)) continue;
|
||||
const file = fileContents.get(page);
|
||||
if (!Buffer.isBuffer(file)) return null;
|
||||
pages.add(page);
|
||||
for (const src of collectSubCompositionSrcs(decodeTextHonoringBom(file))) {
|
||||
const absPath = resolveCompositionSrc(absProjectDir, src);
|
||||
if (absPath) queue.push(archiveKey(relative(absProjectDir, absPath).replaceAll("\\", "/")));
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan HTML and CSS files for asset references that resolve outside the
|
||||
* project directory. Copy those files into the archive under `_ext/` and
|
||||
@@ -725,6 +754,7 @@ export function localizeExternalAssets(
|
||||
onDiskMinBytes = Infinity,
|
||||
onLeftOut?: OnLeftOut,
|
||||
onOutsideFile?: OnOutsideFile,
|
||||
entry = "index.html",
|
||||
): number {
|
||||
const ctx: ExternalAssetContext = {
|
||||
absProjectDir,
|
||||
@@ -736,6 +766,7 @@ export function localizeExternalAssets(
|
||||
onLeftOut,
|
||||
onOutsideFile,
|
||||
received: receivedFiles(absProjectDir),
|
||||
filmPages: filmPagesOf(absProjectDir, fileContents, entry),
|
||||
};
|
||||
|
||||
for (const [entryPath, content] of [...fileContents.entries()]) {
|
||||
@@ -805,12 +836,16 @@ function walkedFileLeftOut(
|
||||
return null;
|
||||
}
|
||||
|
||||
/** {@link buildPublishFileMap}, but non-text files of `onDiskMinBytes` or more stay on disk. */
|
||||
/**
|
||||
* {@link buildPublishFileMap}, but non-text files of `onDiskMinBytes` or more stay on disk.
|
||||
* `entry` is the archive key of the page that will render.
|
||||
*/
|
||||
export function stagePublishFiles(
|
||||
projectDir: string,
|
||||
onDiskMinBytes: number,
|
||||
onLeftOut?: OnLeftOut,
|
||||
onOutsideFile?: OnOutsideFile,
|
||||
entry = "index.html",
|
||||
): Map<string, StagedFile> {
|
||||
const absProjectDir = resolve(projectDir);
|
||||
const filePaths: string[] = [];
|
||||
@@ -847,7 +882,14 @@ export function stagePublishFiles(
|
||||
}
|
||||
|
||||
composeNamesInText(fileContents, filePaths);
|
||||
localizeExternalAssets(absProjectDir, fileContents, onDiskMinBytes, onLeftOut, onOutsideFile);
|
||||
localizeExternalAssets(
|
||||
absProjectDir,
|
||||
fileContents,
|
||||
onDiskMinBytes,
|
||||
onLeftOut,
|
||||
onOutsideFile,
|
||||
entry,
|
||||
);
|
||||
return fileContents;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user