fix(studio): stop a first open from reloading the preview once per scene file

Studio stamped hf-ids into a composition the first time that composition was served, so opening a
film of external scenes wrote to index.html and every scene file mid-session. Each write reached
Studio as an outside edit and reloaded the preview, which failed for a large film. The server now
stamps every composition once before it starts watching the project.
This commit is contained in:
Miguel Ángel
2026-09-23 06:21:20 -04:00
parent 8e2963c60c
commit bc4ff23560
5 changed files with 80 additions and 3 deletions
@@ -0,0 +1,55 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import * as fs from "node:fs";
import * as path from "node:path";
import { tmpdir } from "node:os";
import { createStudioServer, type StudioServer } from "./studioServer.js";
const SCENES = Array.from({ length: 24 }, (_, i) => `s${String(i).padStart(2, "0")}`);
const scene = (id: string) => `<template id="${id}-template">
<div data-composition-id="${id}" data-width="320" data-height="180">
<div class="clip" data-start="0" data-duration="1"><h1>${id}</h1><p>caption</p></div>
</div>
</template>`;
const root = (ids: string[]) => `<!doctype html>
<html><head></head><body><div data-composition-id="main" data-width="320" data-height="180">
${ids.map((id, i) => `<div data-composition-id="${id}" data-composition-src="compositions/${id}.html" data-start="${i}" data-duration="1"></div>`).join("\n")}
</div></body></html>`;
let dir: string;
let server: StudioServer | undefined;
beforeEach(() => {
dir = fs.mkdtempSync(path.join(tmpdir(), "hf-stamp-on-open-"));
fs.mkdirSync(path.join(dir, "compositions"));
fs.writeFileSync(path.join(dir, "index.html"), root(SCENES));
for (const id of SCENES)
fs.writeFileSync(path.join(dir, "compositions", `${id}.html`), scene(id));
});
afterEach(() => {
server?.watcher.close();
fs.rmSync(dir, { recursive: true, force: true });
});
describe("opening a film of external scenes", () => {
it("writes nothing to the project mid-session, so Studio sees no outside edit to reload for", async () => {
server = createStudioServer({ projectDir: dir, projectName: "film" });
const changed: string[] = [];
server.watcher.addListener((changedPath) => changed.push(changedPath));
expect((await server.app.request("/api/projects/film/preview")).status).toBe(200);
for (const id of SCENES) {
const comp = await server.app.request(
`/api/projects/film/preview/comp/compositions/${id}.html`,
);
expect(comp.status).toBe(200);
}
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(changed).toEqual([]);
const stamped = fs.readFileSync(path.join(dir, "compositions", "s23.html"), "utf-8");
expect(stamped).toContain("data-hf-id=");
});
});
+2
View File
@@ -50,6 +50,7 @@ import {
type ResolvedProject,
type RenderJobState,
type BackgroundRemovalRender,
stampProjectHfIds,
} from "@hyperframes/studio-server";
import { resolveAutoProxy } from "../utils/projectConfig.js";
import { getElementScreenshotClip } from "@hyperframes/studio-server/screenshot-clip";
@@ -400,6 +401,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
const browserGpuMode = options.browserGpuMode ?? resolveLocalBrowserGpuMode();
const studioDir = resolveDistDir();
const runtimePath = resolveRuntimePath();
stampProjectHfIds(projectDir);
const watcher = createProjectWatcher(projectDir);
// ── CLI adapter for the shared studio API ──────────────────────────────
@@ -1,6 +1,26 @@
import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
import { closeSync, constants, fstatSync, openSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { replaceFileAtomically } from "./atomicFile.js";
import { isInHiddenOrVendorDir, walkDir } from "./safePath.js";
export const isCompositionSource = (html: string): boolean => /data-composition-id\s*=/.test(html);
/** Pins hf-ids into every composition in the project. A host runs it once before it serves or
* watches the project: an id write during a session reaches Studio as an outside edit and reloads it. */
export function stampProjectHfIds(projectDir: string): void {
for (const file of walkDir(projectDir)) {
if (!file.endsWith(".html") || isInHiddenOrVendorDir(file)) continue;
const absPath = join(projectDir, file);
let html: string;
try {
html = readFileSync(absPath, "utf-8");
} catch {
continue; // unreadable now; the preview route stamps it when it is served
}
if (isCompositionSource(html)) stampFileHfIds(absPath);
}
}
/**
* Ensure `html` has `data-hf-id` attributes minted, and write the result back
+1
View File
@@ -11,6 +11,7 @@ export type {
StudioSelectionTextField,
} from "./types.js";
export { isSafePath, walkDir } from "./helpers/safePath.js";
export { stampProjectHfIds } from "./helpers/hfIdPersist.js";
export {
patchElementInHtml,
splitElementInHtml,
@@ -3,17 +3,16 @@ import { join } from "node:path";
import type { Hono } from "hono";
import type { StudioApiAdapter } from "../types.js";
import { isInHiddenOrVendorDir, walkDir } from "../helpers/safePath.js";
import { isCompositionSource } from "../helpers/hfIdPersist.js";
import { resolveProjectSignature } from "../helpers/projectSignature.js";
const COMPOSITION_ID_RE = /data-composition-id\s*=/;
async function filterCompositionFiles(projectDir: string, files: string[]): Promise<string[]> {
const htmlFiles = files.filter((f) => f.endsWith(".html") && !isInHiddenOrVendorDir(f));
const checks = await Promise.all(
htmlFiles.map(async (f) => {
try {
const content = await readFile(join(projectDir, f), "utf-8");
return COMPOSITION_ID_RE.test(content);
return isCompositionSource(content);
} catch {
return false;
}