11// Shared Puppeteer browser management and thumbnail generation for Studio dev server.
22
33import { existsSync , readdirSync } from "node:fs" ;
4+ import { execFileSync } from "node:child_process" ;
45import { homedir } from "node:os" ;
56import { join , win32 as pathWin32 } from "node:path" ;
67import { thumbnailDeviceScaleFactor } from "@hyperframes/studio-server" ;
@@ -9,6 +10,7 @@ import { seekThumbnailPreview } from "./vite.thumbnail";
910
1011let browser : import ( "puppeteer-core" ) . Browser | null = null ;
1112let browserLaunch : Promise < import ( "puppeteer-core" ) . Browser | null > | null = null ;
13+ let browserDescription = "unknown browser" ;
1214
1315function 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
4345function 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 ( / (?: C h r o m e | C h r o m i u m ) [ / ] ( \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. */
78105export 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