Skip to content

Commit b70dd05

Browse files
committed
chore: detect display
1 parent b5d01b5 commit b70dd05

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/browser.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
import {spawnSync} from 'node:child_process';
78
import fs from 'node:fs';
89
import os from 'node:os';
910
import path from 'node:path';
@@ -148,6 +149,19 @@ interface McpLaunchOptions {
148149
enableExtensions?: boolean;
149150
}
150151

152+
export function detectDisplay(): void {
153+
if (!process.env['DISPLAY']) {
154+
try {
155+
const result = spawnSync(
156+
`ps -u $(id -u) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\0' '\n' | grep -m1 '^DISPLAY=' | cut -d= -f2`,
157+
);
158+
process.env['DISPLAY'] = result.stdout?.toString('utf8');
159+
} catch {
160+
// no-op
161+
}
162+
}
163+
}
164+
151165
export async function launch(options: McpLaunchOptions): Promise<Browser> {
152166
const {channel, executablePath, headless, isolated} = options;
153167
const profileDirName =
@@ -189,6 +203,10 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
189203
: 'chrome';
190204
}
191205

206+
if (!headless) {
207+
detectDisplay();
208+
}
209+
192210
try {
193211
const browser = await puppeteer.launch({
194212
channel: puppeteerChannel,

tests/browser.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import {describe, it} from 'node:test';
1111

1212
import {executablePath} from 'puppeteer';
1313

14-
import {ensureBrowserConnected, launch} from '../src/browser.js';
14+
import {detectDisplay, ensureBrowserConnected, launch} from '../src/browser.js';
1515

1616
describe('browser', () => {
17+
it('detects display does not crash', () => {
18+
detectDisplay();
19+
});
20+
1721
it('cannot launch multiple times with the same profile', async () => {
1822
const tmpDir = os.tmpdir();
1923
const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);

0 commit comments

Comments
 (0)