File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { pathRoutes } from './routes/path.routes.js';
1010import { indexerRoutes } from './routes/indexer.routes.js' ;
1111import { exportRoutes } from './routes/export.routes.js' ;
1212import { browserRouter } from './routes/browser.routes.js' ;
13+ import { browserService } from './services/browser.service.js' ;
1314import { augmentationRouter } from './routes/augmentation.routes.js' ;
1415import { genealogyProviderRouter } from './routes/genealogy-provider.routes.js' ;
1516import { providerRouter } from './routes/provider.routes.js' ;
@@ -79,4 +80,7 @@ app.use(errorHandler);
7980
8081httpServer . listen ( PORT , '0.0.0.0' , ( ) => {
8182 console . log ( `🚀 Server running on http://localhost:${ PORT } ` ) ;
83+
84+ // Auto-connect to browser if enabled and browser is running
85+ browserService . autoConnectIfEnabled ( ) ;
8286} ) ;
Original file line number Diff line number Diff line change @@ -248,6 +248,45 @@ export const browserService = {
248248 return checkBrowserProcessRunning ( ) ;
249249 } ,
250250
251+ async autoConnectIfEnabled ( ) : Promise < void > {
252+ if ( ! browserConfig . autoConnect ) {
253+ console . log ( '[browser] Auto-connect disabled in config' ) ;
254+ return ;
255+ }
256+
257+ if ( connectedBrowser ?. isConnected ( ) ) {
258+ console . log ( '[browser] Already connected' ) ;
259+ return ;
260+ }
261+
262+ // Wait briefly for browser process to be ready (handles concurrent startup)
263+ const maxAttempts = 5 ;
264+ const delayMs = 2000 ;
265+
266+ for ( let attempt = 1 ; attempt <= maxAttempts ; attempt ++ ) {
267+ const isRunning = await checkBrowserProcessRunning ( ) ;
268+
269+ if ( isRunning ) {
270+ console . log ( '[browser] Auto-connecting to CDP...' ) ;
271+ await this . connect ( ) . catch ( err => {
272+ console . log ( `[browser] Auto-connect attempt ${ attempt } failed: ${ err . message } ` ) ;
273+ } ) ;
274+
275+ if ( connectedBrowser ?. isConnected ( ) ) {
276+ console . log ( '[browser] Auto-connect successful' ) ;
277+ return ;
278+ }
279+ }
280+
281+ if ( attempt < maxAttempts ) {
282+ console . log ( `[browser] Browser not ready, retrying in ${ delayMs } ms (attempt ${ attempt } /${ maxAttempts } )` ) ;
283+ await new Promise ( resolve => setTimeout ( resolve , delayMs ) ) ;
284+ }
285+ }
286+
287+ console . log ( '[browser] Auto-connect: browser process not available after retries' ) ;
288+ } ,
289+
251290 async getFamilySearchToken ( ) : Promise < { token : string | null ; cookies : Array < { name : string ; value : string } > } > {
252291 if ( ! connectedBrowser ?. isConnected ( ) ) {
253292 return { token : null , cookies : [ ] } ;
You can’t perform that action at this time.
0 commit comments