@@ -19,7 +19,7 @@ function browserLaunchFailure(error) {
1919 const reason = / s a n d b o x | z y g o t e _ h o s t _ i m p l / i. test ( message ) ? 'sandbox'
2020 : / D I S P L A Y | X s e r v e r | X 1 1 | o z o n e _ p l a t f o r m _ x 1 1 / i. test ( message ) ? 'display'
2121 : / s h a r e d l i b r a r | c a n n o t o p e n s h a r e d o b j e c t / i. test ( message ) ? 'system_library'
22- : error ?. code === 'ENOENT' ? 'executable_missing' : 'browser_launch' ;
22+ : error ?. code === 'ENOENT' ? 'executable_missing' : error ?. name === 'TimeoutError' ? 'timeout' : 'browser_launch' ;
2323 return { ok : false , code : error ?. code === 'captcha_display_unavailable' ? error . code : 'captcha_browser_launch_failed' , stage : 'browser_launch' , browser_launch_reason : reason } ;
2424}
2525
@@ -111,7 +111,7 @@ function playwright(config) {
111111 return null ;
112112}
113113
114- function browserPath ( config , driver , headed = false ) {
114+ function browserPath ( config , driver , headed = false , preferHeadlessShell = false ) {
115115 const windows = process . platform === 'win32' ;
116116 const names = windows ? [ 'msedge.exe' , 'chrome.exe' , 'chromium.exe' ] : [ 'chromium' , 'chromium-browser' , 'google-chrome' , 'google-chrome-stable' , 'microsoft-edge' ] ;
117117 if ( config . browser_path ) {
@@ -151,6 +151,10 @@ function browserPath(config, driver, headed = false) {
151151 }
152152 }
153153 }
154+ if ( preferHeadlessShell ) {
155+ const shell = candidates . find ( file => headlessShell ( file ) && executable ( file ) ) ;
156+ if ( shell ) return shell ;
157+ }
154158 return candidates . find ( file => executable ( file ) && ( ! headed || ! headlessShell ( file ) ) ) || '' ;
155159}
156160
@@ -164,8 +168,10 @@ function browserPlan(config, nativeUnavailable = '') {
164168 : ! fullBrowser ? 'captcha_browser_missing'
165169 : proxy && ( ! [ 'http:' , 'https:' ] . includes ( proxy . protocol ) || proxy . username || proxy . password ) ? 'captcha_proxy_unsupported'
166170 : ! display . available ? 'captcha_display_unavailable' : '' ) ;
167- const backend = requested === 'native' || ( requested === 'auto' && ! nativeCode ) ? 'native' : 'playwright' ;
168- const file = backend === 'native' || ! headless ? fullBrowser : browserPath ( config , driver ) ;
171+ // Linux services avoid one X server and full desktop browser per worker when a headless driver is available.
172+ const preferNative = process . platform !== 'linux' || ! driver || ! headless ;
173+ const backend = requested === 'native' || ( requested === 'auto' && ! nativeCode && preferNative ) ? 'native' : 'playwright' ;
174+ const file = backend === 'native' || ! headless ? fullBrowser : browserPath ( config , driver , false , true ) ;
169175 const code = backend === 'native' ? nativeCode : ! driver ? 'captcha_playwright_missing' : ! file ? 'captcha_browser_missing' : ! headless && ! display . available ? 'captcha_display_unavailable' : '' ;
170176 return { requested, backend, headless : backend === 'playwright' && headless , driver, display, file, code, nativeCode, fallback_code : requested === 'auto' && backend === 'playwright' ? nativeCode : '' } ;
171177}
@@ -284,6 +290,7 @@ async function verify(browser, sessions, config) {
284290}
285291
286292async function worker ( ) {
293+ const { browserLaunchTimeout } = require ( './captcha.cjs' ) ;
287294 let browser , native , nativeUnavailable = '' , stopping = false , chain = Promise . resolve ( ) ;
288295 const display = new BrowserDisplay ( ) ;
289296 const sessions = new Map ( ) ;
@@ -321,11 +328,16 @@ async function worker() {
321328 }
322329 // Never switch runtimes after an SDK refusal, timeout or interaction request.
323330 if ( ! stopping && ! result . ok && [ 'captcha_browser_launch_failed' , 'captcha_display_unavailable' ] . includes ( result . code ) && ! result . sdk_executed && plan . requested === 'auto' ) {
324- nativeUnavailable = result . code ;
325331 await native . close ( ) ;
326- result = null ;
327- plan = browserPlan ( config , nativeUnavailable ) ;
328- if ( plan . code ) result = { ok : false , code : plan . code , stage : 'browser_selection' } ;
332+ const fallback = browserPlan ( config , result . code ) ;
333+ if ( fallback . code ) {
334+ // A missing fallback must not hide a transient native startup failure.
335+ plan = { ...plan , fallback_code : fallback . code } ;
336+ } else {
337+ nativeUnavailable = result . code ;
338+ result = null ;
339+ plan = fallback ;
340+ }
329341 }
330342 }
331343 if ( ! stopping && ! result && plan . backend === 'playwright' ) {
@@ -335,7 +347,7 @@ async function worker() {
335347 sessions . clear ( ) ;
336348 const env = plan . headless ? process . env : await display . environment ( config ) ;
337349 if ( stopping ) return ;
338- browser = await plan . driver . library . chromium . launch ( { executablePath : plan . file , headless : plan . headless , env, timeout : 20000 } ) ;
350+ browser = await plan . driver . library . chromium . launch ( { executablePath : plan . file , headless : plan . headless , env, timeout : browserLaunchTimeout ( config ) } ) ;
339351 if ( stopping ) { await browser . close ( ) ; return ; }
340352 }
341353 result = config . action === 'warm' ? { ok : true } : await verify ( browser , sessions , config ) ;
0 commit comments