File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ import { spawnSync } from 'node:child_process' ;
78import fs from 'node:fs' ;
89import os from 'node:os' ;
910import 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+
151165export 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 ,
Original file line number Diff line number Diff line change @@ -11,9 +11,13 @@ import {describe, it} from 'node:test';
1111
1212import { executablePath } from 'puppeteer' ;
1313
14- import { ensureBrowserConnected , launch } from '../src/browser.js' ;
14+ import { detectDisplay , ensureBrowserConnected , launch } from '../src/browser.js' ;
1515
1616describe ( '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 ( ) } ` ) ;
You can’t perform that action at this time.
0 commit comments