Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions packages/producer/src/services/distributed/plan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1624,3 +1624,73 @@ describe("plan() — webm format (distributed VP9)", () => {
expect((caught as Error).message).toMatch(/codec.*only valid for format="mp4"/);
});
});

describe("plan() — template sub-composition entry", () => {
const TIMEOUT_MS = 30_000;
const INDEX_HTML = `<!doctype html>
<html>
<head><meta charset="utf-8"></head>
<body>
<div data-composition-id="root" data-width="320" data-height="240" data-duration="5">
<div id="sting" data-composition-id="sting" data-composition-src="compositions/sting.html" data-start="0" data-duration="3"></div>
</div>
</body>
</html>`;
const STING_TEMPLATE = `<template id="sting-template">
<div data-composition-id="sting" data-width="320" data-height="240" data-duration="3">
<p>sting</p>
</div>
</template>`;

function writeMountedProject(name: string): string {
const dir = join(runRoot, name);
mkdirSync(join(dir, "compositions"), { recursive: true });
writeFileSync(join(dir, "index.html"), INDEX_HTML, "utf-8");
writeFileSync(join(dir, "compositions", "sting.html"), STING_TEMPLATE, "utf-8");
return dir;
}

it(
"plans a template-wrapped sub-composition rendered alone with its own duration",
async () => {
const dir = writeMountedProject("template-entry-project");
const planDir = join(runRoot, "template-entry-plan");
mkdirSync(planDir, { recursive: true });
const result = await plan(
dir,
{ fps: 30, width: 320, height: 240, format: "mp4", entryFile: "compositions/sting.html" },
planDir,
);
expect(result.totalFrames).toBe(90);
},
TIMEOUT_MS,
);

it(
"rejects a template entry that index.html does not mount",
async () => {
const dir = writeMountedProject("template-entry-unmounted");
writeFileSync(join(dir, "compositions", "orphan.html"), STING_TEMPLATE, "utf-8");
const planDir = join(runRoot, "template-entry-unmounted-plan");
mkdirSync(planDir, { recursive: true });
let caught: unknown;
try {
await plan(
dir,
{
fps: 30,
width: 320,
height: 240,
format: "mp4",
entryFile: "compositions/orphan.html",
},
planDir,
);
} catch (err) {
caught = err;
}
expect((caught as Error).message).toMatch(/not mounted from index\.html/);
},
TIMEOUT_MS,
);
});
1 change: 0 additions & 1 deletion packages/producer/src/services/distributed/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,6 @@ async function compilePlanComposition(
urlByAbsentLocalPath,
projectDir,
workDir,
htmlPath,
entryFile,
job,
cfg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ describe("runCompileStage — asset media-type preflight", () => {
workDir = mkdtempSync(join(tmpdir(), "compile-stage-media-type-"));
const projectDir = join(workDir, "project");
mkdirSync(projectDir);
const htmlPath = join(projectDir, "index.html");
writeFileSync(
htmlPath,
join(projectDir, "index.html"),
`<!doctype html><html><body>
<div data-composition-id="root" data-width="320" data-height="180" data-duration="1">
<img id="hero" src="voice.asset" data-start="0" data-end="1" />
Expand Down Expand Up @@ -92,7 +91,6 @@ describe("runCompileStage — asset media-type preflight", () => {
runCompileStage({
projectDir,
workDir,
htmlPath,
entryFile: "index.html",
job: {
id: "media-type-test",
Expand Down
73 changes: 68 additions & 5 deletions packages/producer/src/services/render/stages/compileStage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/

import { afterEach, describe, expect, it } from "vitest";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import type { EngineConfig } from "@hyperframes/engine";
import type { CanvasResolution } from "@hyperframes/core";
import {
resolveEntryHtmlPath,
runCompileStage,
type CompileStageInput,
type CompileStageResult,
Expand Down Expand Up @@ -164,7 +165,6 @@ async function runWith(
const input: CompileStageInput = {
projectDir,
workDir: fixture.workDir,
htmlPath: fixture.htmlPath,
entryFile: "index.html",
job: createJob(),
cfg,
Expand Down Expand Up @@ -260,7 +260,6 @@ describe("runCompileStage — master bus", () => {
const result = await runCompileStage({
projectDir: join(fixture.workDir, "project"),
workDir: fixture.workDir,
htmlPath: fixture.htmlPath,
entryFile: "index.html",
job: createJob(),
cfg: createCfg(),
Expand Down Expand Up @@ -294,7 +293,6 @@ describe("runCompileStage — font fetch cancellation", () => {
const result = runCompileStage({
projectDir,
workDir: fixture.workDir,
htmlPath: fixture.htmlPath,
entryFile: "index.html",
job: createJob(),
cfg: createCfg(),
Expand Down Expand Up @@ -345,7 +343,6 @@ describe("runCompileStage — aspect-agnostic --resolution re-map", () => {
const stageInput: CompileStageInput = {
projectDir,
workDir: fixture.workDir,
htmlPath: fixture.htmlPath,
entryFile: "index.html",
job: createJob({
outputResolution: input.outputResolution,
Expand Down Expand Up @@ -472,3 +469,69 @@ describe("runCompileStage — aspect-agnostic --resolution re-map", () => {
).rejects.toThrow(/aspect ratio|--resolution portrait/i);
});
});

describe("resolveEntryHtmlPath", () => {
const TEMPLATE = `<template><div data-composition-id="scene" data-width="320" data-height="180" data-duration="2"></div></template>`;
let fixture: CompileFixture | null = null;

afterEach(() => {
fixture?.cleanup();
fixture = null;
});

function resolve(entryFile: string): string {
if (!fixture) throw new Error("fixture not set up");
return resolveEntryHtmlPath({
projectDir: join(fixture.workDir, "project"),
workDir: fixture.workDir,
entryFile,
log: noopLog,
});
}

it("returns a plain entry untouched and writes no wrapper", () => {
fixture = setupFixture(PLAIN_HTML);
expect(resolve("index.html")).toBe(fixture.htmlPath);
expect(existsSync(join(fixture.workDir, "standalone-entry.html"))).toBe(false);
});

it("returns a non-template sub-composition file untouched", () => {
fixture = setupFixture(PLAIN_HTML);
const subPath = join(fixture.workDir, "project", "full.html");
writeFileSync(subPath, PLAIN_HTML, "utf-8");
expect(resolve("full.html")).toBe(subPath);
});

it("isolates a mounted template entry inside the index shell", () => {
fixture = setupFixture(
`<!doctype html><html><body>
<div data-composition-id="root" data-width="320" data-height="180" data-duration="9">
<div id="scene" data-composition-id="scene" data-composition-src="compositions/scene.html"></div>
<div id="other" data-composition-id="other" data-composition-src="compositions/other.html"></div>
</div>
</body></html>`,
);
mkdirSync(join(fixture.workDir, "project", "compositions"));
writeFileSync(join(fixture.workDir, "project", "compositions", "scene.html"), TEMPLATE);
const wrapper = resolve("compositions/scene.html");
expect(wrapper).toBe(join(fixture.workDir, "standalone-entry.html"));
const html = readFileSync(wrapper, "utf-8");
expect(html).toContain('id="scene"');
expect(html).toContain('data-duration="2"');
expect(html).not.toContain('id="other"');
});

it("refuses a template entry that index.html does not mount", () => {
fixture = setupFixture(PLAIN_HTML);
mkdirSync(join(fixture.workDir, "project", "compositions"));
writeFileSync(join(fixture.workDir, "project", "compositions", "orphan.html"), TEMPLATE);
expect(() => resolve("compositions/orphan.html")).toThrow(/not mounted from index\.html/);
});

it("requires an index.html to extract a template entry's shell", () => {
fixture = setupFixture(PLAIN_HTML);
rmSync(fixture.htmlPath);
writeFileSync(join(fixture.workDir, "project", "scene.html"), TEMPLATE);
expect(() => resolve("scene.html")).toThrow(/requires a project index\.html/);
});
});
47 changes: 43 additions & 4 deletions packages/producer/src/services/render/stages/compileStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
* same behavior as before this PR.
*/

import { existsSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import type { EngineConfig } from "@hyperframes/engine";
import { suggestMatchingPreset, type CanvasResolution } from "@hyperframes/core";
import { extractStandaloneEntryFromIndex, isTemplateEntry } from "@hyperframes/core/compiler";
import { readTextFile } from "@hyperframes/parsers/asset-resolution";
import type { CompiledComposition } from "../../htmlCompiler.js";
import { compileForRender } from "../../htmlCompiler.js";
import type { ProducerLogger } from "../../../logger.js";
Expand All @@ -53,9 +56,7 @@ import { preflightCompositionAssetMediaTypes } from "../../assetMediaType.js";
export interface CompileStageInput {
projectDir: string;
workDir: string;
/** Absolute path to the entry HTML (already resolved to standalone-entry if needed). */
htmlPath: string;
/** The relative `entryFile` string, used only for log payloads. */
/** Entry HTML relative to `projectDir`; a `<template>` sub-composition is isolated inside the index shell. */
entryFile: string;
job: RenderJob;
/**
Expand Down Expand Up @@ -152,11 +153,48 @@ function adaptAspectAgnosticResolution(
return flipped;
}

/**
* The HTML file to compile for `entryFile`. A `<template>`-wrapped
* sub-composition has no live root, timeline or duration of its own, so it is
* rendered through the project's index.html shell with only its mount host kept.
*/
export function resolveEntryHtmlPath(input: {
projectDir: string;
workDir: string;
entryFile: string;
log: ProducerLogger;
}): string {
const { projectDir, workDir, entryFile, log } = input;
const htmlPath = join(projectDir, entryFile);
const rawEntry = readTextFile(htmlPath);
if (entryFile === "index.html" || !isTemplateEntry(rawEntry)) return htmlPath;

const projectIndexPath = join(projectDir, "index.html");
if (!existsSync(projectIndexPath)) {
throw new Error(
`Template entry file "${entryFile}" requires a project index.html to extract its render shell.`,
);
}
const standaloneHtml = extractStandaloneEntryFromIndex(
readTextFile(projectIndexPath),
entryFile,
rawEntry,
);
if (!standaloneHtml) {
throw new Error(
`Entry file "${entryFile}" is not mounted from index.html via data-composition-src, so it cannot be rendered independently.`,
);
}
const wrapperPath = join(workDir, "standalone-entry.html");
writeFileSync(wrapperPath, standaloneHtml, "utf-8");
log.info("Extracted standalone entry from index.html host context", { entryFile });
return wrapperPath;
}

export async function runCompileStage(input: CompileStageInput): Promise<CompileStageResult> {
const {
projectDir,
workDir,
htmlPath,
entryFile,
job,
cfg,
Expand All @@ -168,6 +206,7 @@ export async function runCompileStage(input: CompileStageInput): Promise<Compile
abortSignal,
} = input;

const htmlPath = resolveEntryHtmlPath({ projectDir, workDir, entryFile, log });
const compileStart = Date.now();
const compiled = await compileForRender(projectDir, htmlPath, join(workDir, "downloads"), {
log,
Expand Down
34 changes: 1 addition & 33 deletions packages/producer/src/services/renderOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ import {
toFps,
} from "@hyperframes/core";
import { HF_AUDIO_GROUP_TAG } from "@hyperframes/core/audio-groups";
import { extractStandaloneEntryFromIndex, isTemplateEntry } from "@hyperframes/core/compiler";
import { HTML_BODY_CSS_HEIGHT_FIRST_RE, HTML_BODY_CSS_WIDTH_FIRST_RE } from "@hyperframes/parsers";
import { readTextFile } from "@hyperframes/parsers/asset-resolution";
import {
type EngineConfig,
extractMediaMetadata,
Expand Down Expand Up @@ -3244,41 +3242,12 @@ async function executeRenderPipeline(input: {
});

const entryFile = job.config.entryFile || "index.html";
let htmlPath = join(projectDir, entryFile);
const htmlPath = join(projectDir, entryFile);
if (!existsSync(htmlPath)) {
throw new Error(`Entry file not found: ${htmlPath}`);
}
assertNotAborted();

// If entryFile is a sub-composition (<template> wrapper), reuse the real
// index.html shell and isolate the matching host instead of fabricating
// a new standalone document.
const rawEntry = readTextFile(htmlPath);
if (entryFile !== "index.html" && isTemplateEntry(rawEntry)) {
const wrapperPath = join(workDir, "standalone-entry.html");
const projectIndexPath = join(projectDir, "index.html");
if (!existsSync(projectIndexPath)) {
throw new Error(
`Template entry file "${entryFile}" requires a project index.html to extract its render shell.`,
);
}
const standaloneHtml = extractStandaloneEntryFromIndex(
readTextFile(projectIndexPath),
entryFile,
rawEntry,
);
if (!standaloneHtml) {
throw new Error(
`Entry file "${entryFile}" is not mounted from index.html via data-composition-src, so it cannot be rendered independently.`,
);
}
writeFileSync(wrapperPath, standaloneHtml, "utf-8");
htmlPath = wrapperPath;
log.info("Extracted standalone entry from index.html host context", {
entryFile,
});
}

// ── Stage 1: Compile ─────────────────────────────────────────────────
const stage1Start = Date.now();
updateJobStatus(job, "preprocessing", "Compiling composition", 5, onProgress, {
Expand All @@ -3289,7 +3258,6 @@ async function executeRenderPipeline(input: {
runCompileStage({
projectDir,
workDir,
htmlPath,
entryFile,
job,
cfg,
Expand Down
Loading