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

Commit 6f59b39

Browse files
authored
fix: map channel for resolveDefaultUserDataDir (#658)
- delegate to Puppeteer for channel lookup - handle lookup in a custom user data dir on the MCP side
1 parent 09111cc commit 6f59b39

File tree

2 files changed

+37
-51
lines changed

2 files changed

+37
-51
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+
// 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(

tests/browser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('browser', () => {
9090
devtools: false,
9191
});
9292
assert.ok(connectedBrowser);
93-
assert.ok(connectedBrowser.isConnected());
93+
assert.ok(connectedBrowser.connected);
9494
connectedBrowser.disconnect();
9595
} finally {
9696
await browser.close();

0 commit comments

Comments
 (0)