Skip to content

Commit 3858698

Browse files
committed
perf(cli): take a thumbnail's version from the cached project signature
Each thumbnail walked the project and hashed every text file for its page version; the adapter already caches that signature and clears it from the watcher. An aborted request now also stops before its screenshot.
1 parent efce33b commit 3858698

2 files changed

Lines changed: 73 additions & 7 deletions

File tree

‎packages/cli/src/server/studioServer.test.ts‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
33
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
44
import { tmpdir } from "node:os";
55
import { basename, join } from "node:path";
6-
import { fileContentVersion } from "@hyperframes/studio-server";
6+
import { createProjectSignature, fileContentVersion } from "@hyperframes/studio-server";
77
import { loadHyperframeRuntimeSource } from "@hyperframes/core";
88
import { loadRuntimeSource } from "./runtimeSource.js";
99
import { findFFmpeg, findFFprobe } from "../browser/ffmpeg.js";
@@ -43,6 +43,10 @@ vi.mock("@hyperframes/engine", () => ({
4343
killTrackedProcesses: () => {},
4444
closeBrowserPool: () => engineState.closeBrowserPool(),
4545
}));
46+
vi.mock("@hyperframes/studio-server", async (importOriginal) => {
47+
const actual = await importOriginal<typeof import("@hyperframes/studio-server")>();
48+
return { ...actual, createProjectSignature: vi.fn(actual.createProjectSignature) };
49+
});
4650
vi.mock("../browser/gpuPolicy.js", () => ({
4751
resolveCaptureBrowserGpuMode: async () => "software",
4852
resolveLocalBrowserGpuMode: () => "software",
@@ -386,6 +390,66 @@ describe("createStudioServer shutdown", () => {
386390
});
387391
});
388392

393+
describe("Studio thumbnail capture", () => {
394+
function fakePageBrowser(onEvaluate = () => {}) {
395+
const screenshot = vi.fn(async () => Buffer.from("jpeg"));
396+
const evaluate = vi.fn(async () => onEvaluate());
397+
const page = new Proxy(
398+
{ screenshot, evaluate },
399+
{
400+
get: (target, key) =>
401+
key === "then"
402+
? undefined
403+
: key in target
404+
? target[key as keyof typeof target]
405+
: async () => {},
406+
},
407+
);
408+
engineState.acquireBrowser = async () => ({
409+
browser: { connected: true, newPage: async () => page, on: () => {} },
410+
release: async () => {},
411+
});
412+
return { screenshot };
413+
}
414+
const opts = (dir: string, signal = new AbortController().signal) => ({
415+
project: { id: "demo", dir, title: "demo" },
416+
compPath: "index.html",
417+
seekTime: 0.5,
418+
width: 640,
419+
height: 360,
420+
outputWidth: 640,
421+
outputHeight: 360,
422+
previewUrl: "http://localhost/preview",
423+
signal,
424+
});
425+
426+
it("stops a thumbnail whose request is aborted before its screenshot", async () => {
427+
let abortOnEvaluate: AbortController | undefined;
428+
const { screenshot } = fakePageBrowser(() => abortOnEvaluate?.abort());
429+
const dir = tmpProject();
430+
server = createStudioServer({ projectDir: dir });
431+
await expect(server.adapter.generateThumbnail?.(opts(dir))).resolves.toBeInstanceOf(Buffer);
432+
expect(screenshot).toHaveBeenCalledTimes(1);
433+
434+
const aborting = new AbortController();
435+
abortOnEvaluate = aborting;
436+
await expect(
437+
server.adapter.generateThumbnail?.(opts(dir, aborting.signal)),
438+
).resolves.toBeNull();
439+
expect(screenshot).toHaveBeenCalledTimes(1);
440+
});
441+
442+
it("reuses the cached project signature instead of walking the project per thumbnail", async () => {
443+
fakePageBrowser();
444+
const dir = tmpProject();
445+
server = createStudioServer({ projectDir: dir });
446+
await server.adapter.generateThumbnail?.(opts(dir));
447+
const walks = vi.mocked(createProjectSignature).mock.calls.length;
448+
for (let i = 0; i < 3; i++) await server.adapter.generateThumbnail?.(opts(dir));
449+
expect(vi.mocked(createProjectSignature).mock.calls.length).toBe(walks);
450+
});
451+
});
452+
389453
describe("Studio project lint endpoint", () => {
390454
it("surfaces findings that require the complete project graph", async () => {
391455
const projectDir = tmpProject();

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
414414
cachedProjectSignature = null;
415415
}
416416
});
417+
const projectSignature = (dir: string): string => {
418+
if (resolve(dir) !== resolve(projectDir)) return createProjectSignature(dir);
419+
cachedProjectSignature ??= createProjectSignature(projectDir);
420+
return cachedProjectSignature;
421+
};
417422

418423
const inFlightRenders = new Map<AbortController, Promise<void>>();
419424
// Set synchronously by shutdown() before any await, so a render or
@@ -483,11 +488,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
483488
return injectDeterministicFontFaces(prepared.html);
484489
},
485490

486-
getProjectSignature(dir: string): string {
487-
if (resolve(dir) !== resolve(projectDir)) return createProjectSignature(dir);
488-
cachedProjectSignature ??= createProjectSignature(projectDir);
489-
return cachedProjectSignature;
490-
},
491+
getProjectSignature: projectSignature,
491492

492493
async lint(html: string, opts?: { filePath?: string; isSubComposition?: boolean }) {
493494
const { lintHyperframeHtml } = await import("@hyperframes/lint");
@@ -659,7 +660,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
659660
return await thumbnailPages.withPage(
660661
session.browser,
661662
opts.previewUrl,
662-
createProjectSignature(opts.project.dir),
663+
projectSignature(opts.project.dir),
663664
async (page) => {
664665
await page.setViewport(viewport);
665666
await page.goto(opts.previewUrl, { waitUntil: "domcontentloaded", timeout: 10000 });
@@ -696,6 +697,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
696697
});
697698
await new Promise((r) => setTimeout(r, 200));
698699
await reapplyStudioManualEditsToThumbnailPage(page);
700+
if (opts.signal.aborted) return null;
699701
let clip: ScreenshotClip | undefined;
700702
if (opts.selector) {
701703
clip = await page.evaluate(

0 commit comments

Comments
 (0)