Skip to content

Commit 189bead

Browse files
fix(studio-server): prefer Puppeteer's cached Chrome for thumbnails (heygen-com#4272)
* fix(studio-server): prefer Puppeteers cached Chrome for thumbnails * style(studio): format browser resolver test * chore(studio): refresh checks * chore(studio): refresh checks * chore(studio): refresh checks * fix(studio): resolve Puppeteer cache by platform
1 parent 94609da commit 189bead

2 files changed

Lines changed: 122 additions & 25 deletions

File tree

‎packages/studio/vite.browser.test.ts‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { join } from "node:path";
23

34
const browserMocks = vi.hoisted(() => {
45
const page = {
@@ -127,6 +128,64 @@ describe("findSystemChrome", () => {
127128
const chromePath = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
128129
const pathExists = vi.fn((path: string) => path === chromePath);
129130

130-
expect(findSystemChrome({}, pathExists, "win32")).toBe(chromePath);
131+
expect(findSystemChrome({}, pathExists, "win32", () => "Chrome/140.0.1")).toBe(chromePath);
132+
});
133+
134+
it("prefers the newest Puppeteer cache entry over a compatible system Chrome", () => {
135+
const cachePath = join(
136+
"/tmp/puppeteer",
137+
"chrome",
138+
"chrome-152.0.1",
139+
"chrome-linux64",
140+
"chrome",
141+
);
142+
const systemPath = "/usr/bin/google-chrome";
143+
const pathExists = vi.fn((path: string) => path === cachePath || path === systemPath);
144+
145+
expect(
146+
findSystemChrome(
147+
{ PUPPETEER_CACHE_DIR: "/tmp/puppeteer" },
148+
pathExists,
149+
"linux",
150+
() => "Chrome/113.0.1",
151+
() => ["chrome-152.0.1"],
152+
),
153+
).toBe(cachePath);
154+
});
155+
156+
it("uses Puppeteer's Windows cache executable layout", () => {
157+
const cachePath = join(
158+
"C:\\puppeteer",
159+
"chrome",
160+
"chrome-152.0.1",
161+
"chrome-win64",
162+
"chrome.exe",
163+
);
164+
const pathExists = vi.fn((path: string) => path === cachePath);
165+
166+
expect(
167+
findSystemChrome(
168+
{ PUPPETEER_CACHE_DIR: "C:\\puppeteer" },
169+
pathExists,
170+
"win32",
171+
() => "Chrome/113.0.1",
172+
() => ["chrome-152.0.1"],
173+
),
174+
).toBe(cachePath);
175+
});
176+
177+
it("rejects a system Chrome older than the Puppeteer-supported major", () => {
178+
const systemPath = "/usr/bin/google-chrome";
179+
const pathExists = vi.fn((path: string) => path === systemPath);
180+
181+
expect(
182+
findSystemChrome(
183+
{},
184+
pathExists,
185+
"linux",
186+
() => "Google Chrome 113.0.1",
187+
() => [],
188+
),
189+
).toBe(undefined);
131190
});
132191
});

‎packages/studio/vite.browser.ts‎

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Shared Puppeteer browser management and thumbnail generation for Studio dev server.
22

33
import { existsSync, readdirSync } from "node:fs";
4+
import { execFileSync } from "node:child_process";
45
import { homedir } from "node:os";
56
import { join, win32 as pathWin32 } from "node:path";
67
import { thumbnailDeviceScaleFactor } from "@hyperframes/studio-server";
@@ -9,6 +10,7 @@ import { seekThumbnailPreview } from "./vite.thumbnail";
910

1011
let browser: import("puppeteer-core").Browser | null = null;
1112
let browserLaunch: Promise<import("puppeteer-core").Browser | null> | null = null;
13+
let browserDescription = "unknown browser";
1214

1315
function systemChromePaths(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): string[] {
1416
if (platform === "win32") {
@@ -43,14 +45,16 @@ function systemChromePaths(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): s
4345
function findPuppeteerCacheChrome(
4446
env: NodeJS.ProcessEnv,
4547
pathExists: (path: string) => boolean,
48+
platform: NodeJS.Platform,
49+
readDirectories: (path: string) => string[] = readdirSync,
4650
): string | undefined {
4751
const chromeRoot = join(
4852
env["PUPPETEER_CACHE_DIR"] ?? join(homedir(), ".cache", "puppeteer"),
4953
"chrome",
5054
);
5155
let versionDirs: string[];
5256
try {
53-
versionDirs = readdirSync(chromeRoot);
57+
versionDirs = readDirectories(chromeRoot);
5458
} catch {
5559
return undefined;
5660
}
@@ -59,12 +63,14 @@ function findPuppeteerCacheChrome(
5963
.split(".")
6064
.map((part) => Number.parseInt(part, 10) || 0)
6165
.reduce((order, part) => order * 100000 + part, 0);
62-
const relativeCandidates = [
63-
"chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing",
64-
"chrome-mac-x64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing",
65-
"chrome-linux64/chrome",
66-
"chrome-win64/chrome.exe",
67-
];
66+
const relativeCandidates =
67+
platform === "win32"
68+
? ["chrome-win64/chrome.exe"]
69+
: [
70+
"chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing",
71+
"chrome-mac-x64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing",
72+
"chrome-linux64/chrome",
73+
];
6874
for (const directory of versionDirs.sort((left, right) => buildOrder(right) - buildOrder(left))) {
6975
for (const relativePath of relativeCandidates) {
7076
const executable = join(chromeRoot, directory, relativePath);
@@ -74,11 +80,34 @@ function findPuppeteerCacheChrome(
7480
return undefined;
7581
}
7682

77-
/** Resolve explicit CLI overrides before system installs and Puppeteer's cache. */
83+
function browserVersion(
84+
executable: string,
85+
run: (file: string) => string = (file) => execFileSync(file, ["--version"], { encoding: "utf8" }),
86+
): string | undefined {
87+
try {
88+
return run(executable).trim() || undefined;
89+
} catch {
90+
return undefined;
91+
}
92+
}
93+
94+
function supportedSystemChrome(
95+
candidates: string[],
96+
versionOf: (path: string) => string | undefined,
97+
): string | undefined {
98+
return candidates.find((candidate) => {
99+
const match = versionOf(candidate)?.match(/(?:Chrome|Chromium)[ /](\d+)/i);
100+
return match ? Number(match[1]) >= 134 : false;
101+
});
102+
}
103+
104+
/** Resolve explicit overrides, Puppeteer's managed browser, then compatible system installs. */
78105
export function findSystemChrome(
79106
env: NodeJS.ProcessEnv = process.env,
80107
pathExists: (path: string) => boolean = existsSync,
81108
platform: NodeJS.Platform = process.platform,
109+
versionOf: (path: string) => string | undefined = browserVersion,
110+
readDirectories: (path: string) => string[] = readdirSync,
82111
): string | undefined {
83112
const override = [
84113
env["HYPERFRAMES_BROWSER_PATH"],
@@ -89,8 +118,8 @@ export function findSystemChrome(
89118
].find((candidate): candidate is string => Boolean(candidate) && pathExists(candidate));
90119
return (
91120
override ??
92-
systemChromePaths(env, platform).find(pathExists) ??
93-
findPuppeteerCacheChrome(env, pathExists)
121+
findPuppeteerCacheChrome(env, pathExists, platform, readDirectories) ??
122+
supportedSystemChrome(systemChromePaths(env, platform).filter(pathExists), versionOf)
94123
);
95124
}
96125

@@ -101,19 +130,28 @@ async function getSharedBrowser(): Promise<import("puppeteer-core").Browser | nu
101130
const puppeteer = await import("puppeteer-core");
102131
const executablePath = findSystemChrome();
103132
if (!executablePath) return null;
104-
browser = await puppeteer.default.launch({
105-
headless: true,
106-
executablePath,
107-
args: [
108-
"--no-sandbox",
109-
"--disable-dev-shm-usage",
110-
"--enable-webgl",
111-
"--ignore-gpu-blocklist",
112-
"--use-gl=angle",
113-
"--use-angle=swiftshader",
114-
"--enable-unsafe-swiftshader",
115-
],
116-
});
133+
browserDescription = `${executablePath}, ${browserVersion(executablePath) ?? "unknown version"}`;
134+
try {
135+
browser = await puppeteer.default.launch({
136+
headless: true,
137+
executablePath,
138+
args: [
139+
"--no-sandbox",
140+
"--disable-dev-shm-usage",
141+
"--enable-webgl",
142+
"--ignore-gpu-blocklist",
143+
"--use-gl=angle",
144+
"--use-angle=swiftshader",
145+
"--enable-unsafe-swiftshader",
146+
],
147+
});
148+
} catch (error) {
149+
console.warn(
150+
`[Studio] Thumbnail browser launch failed (${browserDescription}):`,
151+
error instanceof Error ? error.message : error,
152+
);
153+
throw error;
154+
}
117155
return browser;
118156
})();
119157
browserLaunch = launch;
@@ -258,7 +296,7 @@ export async function generateThumbnail(opts: GenerateThumbnailOptions): Promise
258296
} catch (error) {
259297
if (!opts.signal.aborted) {
260298
console.warn(
261-
"[Studio] Thumbnail generation failed:",
299+
`[Studio] Thumbnail generation failed (${browserDescription}):`,
262300
error instanceof Error ? error.message : error,
263301
);
264302
}

0 commit comments

Comments
 (0)