@@ -14,14 +14,8 @@ import type {
1414 ChromeReleaseChannel ,
1515 LaunchOptions ,
1616 Target ,
17- BrowsersChromeReleaseChannel ,
18- } from './third_party/index.js' ;
19- import {
20- puppeteer ,
21- resolveDefaultUserDataDir ,
22- detectBrowserPlatform ,
23- BrowserEnum ,
2417} from './third_party/index.js' ;
18+ import { puppeteer } from './third_party/index.js' ;
2519
2620let browser : Browser | undefined ;
2721
@@ -76,52 +70,44 @@ export async function ensureBrowserConnected(options: {
7670 } else if ( options . browserURL ) {
7771 connectOptions . browserURL = options . browserURL ;
7872 } else if ( channel || options . userDataDir ) {
79- let userDataDir = options . userDataDir ;
80- if ( ! userDataDir ) {
73+ const userDataDir = options . userDataDir ;
74+ if ( userDataDir ) {
75+ // TODO: re-expose this logic via Puppeteer.
76+ const portPath = path . join ( userDataDir , 'DevToolsActivePort' ) ;
77+ try {
78+ const fileContent = await fs . promises . readFile ( portPath , 'utf8' ) ;
79+ const [ rawPort , rawPath ] = fileContent
80+ . split ( '\n' )
81+ . map ( line => {
82+ return line . trim ( ) ;
83+ } )
84+ . filter ( line => {
85+ return ! ! line ;
86+ } ) ;
87+ if ( ! rawPort || ! rawPath ) {
88+ throw new Error ( `Invalid DevToolsActivePort '${ fileContent } ' found` ) ;
89+ }
90+ const port = parseInt ( rawPort , 10 ) ;
91+ if ( isNaN ( port ) || port <= 0 || port > 65535 ) {
92+ throw new Error ( `Invalid port '${ rawPort } ' found` ) ;
93+ }
94+ const browserWSEndpoint = `ws://127.0.0.1:${ port } ${ rawPath } ` ;
95+ connectOptions . browserWSEndpoint = browserWSEndpoint ;
96+ } catch ( error ) {
97+ throw new Error (
98+ `Could not connect to Chrome in ${ userDataDir } . Check if Chrome is running and remote debugging is enabled.` ,
99+ {
100+ cause : error ,
101+ } ,
102+ ) ;
103+ }
104+ } else {
81105 if ( ! channel ) {
82106 throw new Error ( 'Channel must be provided if userDataDir is missing' ) ;
83107 }
84- const platform = detectBrowserPlatform ( ) ;
85- if ( ! platform ) {
86- throw new Error ( 'Could not detect required browser platform' ) ;
87- }
88- userDataDir = resolveDefaultUserDataDir (
89- BrowserEnum . CHROME ,
90- platform ,
91- ( channel === 'stable'
92- ? 'chrome'
93- : `chrome-${ channel } ` ) as BrowsersChromeReleaseChannel ,
94- ) ;
95- }
96-
97- // TODO: re-expose this logic via Puppeteer.
98- const portPath = path . join ( userDataDir , 'DevToolsActivePort' ) ;
99- try {
100- const fileContent = await fs . promises . readFile ( portPath , 'utf8' ) ;
101- const [ rawPort , rawPath ] = fileContent
102- . split ( '\n' )
103- . map ( line => {
104- return line . trim ( ) ;
105- } )
106- . filter ( line => {
107- return ! ! line ;
108- } ) ;
109- if ( ! rawPort || ! rawPath ) {
110- throw new Error ( `Invalid DevToolsActivePort '${ fileContent } ' found` ) ;
111- }
112- const port = parseInt ( rawPort , 10 ) ;
113- if ( isNaN ( port ) || port <= 0 || port > 65535 ) {
114- throw new Error ( `Invalid port '${ rawPort } ' found` ) ;
115- }
116- const browserWSEndpoint = `ws://127.0.0.1:${ port } ${ rawPath } ` ;
117- connectOptions . browserWSEndpoint = browserWSEndpoint ;
118- } catch ( error ) {
119- throw new Error (
120- `Could not connect to Chrome in ${ userDataDir } . Check if Chrome is running and remote debugging is enabled.` ,
121- {
122- cause : error ,
123- } ,
124- ) ;
108+ connectOptions . channel = (
109+ channel === 'stable' ? 'chrome' : `chrome-${ channel } `
110+ ) as ChromeReleaseChannel ;
125111 }
126112 } else {
127113 throw new Error (
0 commit comments