豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 8a3099f

Browse files
committed
fix: map channel for resolveDefaultUserDataDir
1 parent 09111cc commit 8a3099f

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

src/browser.ts

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2620
let 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+
connectOptions.channel = (
76+
channel === 'stable' ? 'chrome' : `chrome-${channel}`
77+
) as ChromeReleaseChannel;
78+
// TODO: re-expose this logic via Puppeteer.
79+
const portPath = path.join(userDataDir, 'DevToolsActivePort');
80+
try {
81+
const fileContent = await fs.promises.readFile(portPath, 'utf8');
82+
const [rawPort, rawPath] = fileContent
83+
.split('\n')
84+
.map(line => {
85+
return line.trim();
86+
})
87+
.filter(line => {
88+
return !!line;
89+
});
90+
if (!rawPort || !rawPath) {
91+
throw new Error(`Invalid DevToolsActivePort '${fileContent}' found`);
92+
}
93+
const port = parseInt(rawPort, 10);
94+
if (isNaN(port) || port <= 0 || port > 65535) {
95+
throw new Error(`Invalid port '${rawPort}' found`);
96+
}
97+
const browserWSEndpoint = `ws://127.0.0.1:${port}${rawPath}`;
98+
connectOptions.browserWSEndpoint = browserWSEndpoint;
99+
} catch (error) {
100+
throw new Error(
101+
`Could not connect to Chrome in ${userDataDir}. Check if Chrome is running and remote debugging is enabled.`,
102+
{
103+
cause: error,
104+
},
105+
);
106+
}
107+
} else {
81108
if (!channel) {
82109
throw new Error('Channel must be provided if userDataDir is missing');
83110
}
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-
);
125111
}
126112
} else {
127113
throw new Error(

0 commit comments

Comments
 (0)