Skip to content

Commit

Permalink
add portStrictMode option (#319)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Irish <[email protected]>
  • Loading branch information
connorjclark and paulirish authored Sep 29, 2023
1 parent 55020d3 commit f7b24a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ npm install chrome-launcher
// Default: an available port is autoselected
port: number;

// (optional) When `port` is specified *and* no Chrome is found at that port,
// * if `false` (default), chrome-launcher will launch a new Chrome with that port.
// * if `true`, throw an error
// This option is useful when you wish to explicitly connect to a running Chrome, such as on a mobile device via adb
// Default: false
portStrictMode: boolean;

// (optional) Additional flags to pass to Chrome, for example: ['--headless', '--disable-gpu']
// See: https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
// Do note, many flags are set by default: https://github.com/GoogleChrome/chrome-launcher/blob/main/src/flags.ts
Expand Down
7 changes: 7 additions & 0 deletions src/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface Options {
chromeFlags?: Array<string>;
prefs?: Record<string, JSONLike>;
port?: number;
portStrictMode?: boolean;
handleSIGINT?: boolean;
chromePath?: string;
userDataDir?: string|boolean;
Expand Down Expand Up @@ -119,6 +120,7 @@ class Launcher {
private chromeFlags: string[];
private prefs: Record<string, JSONLike>;
private requestedPort?: number;
private portStrictMode?: boolean;
private connectionPollInterval: number;
private maxConnectionRetries: number;
private fs: typeof fs;
Expand All @@ -142,6 +144,7 @@ class Launcher {
this.chromeFlags = defaults(this.opts.chromeFlags, []);
this.prefs = defaults(this.opts.prefs, {});
this.requestedPort = defaults(this.opts.port, 0);
this.portStrictMode = opts.portStrictMode;
this.chromePath = this.opts.chromePath;
this.ignoreDefaultFlags = defaults(this.opts.ignoreDefaultFlags, false);
this.connectionPollInterval = defaults(this.opts.connectionPollInterval, 500);
Expand Down Expand Up @@ -263,6 +266,10 @@ class Launcher {
`Found existing Chrome already running using port ${this.port}, using that.`);
return;
} catch (err) {
if (this.portStrictMode) {
throw new Error(`found no Chrome at port ${this.requestedPort}`);
}

log.log(
'ChromeLauncher',
`No debugging port found on port ${this.port}, launching a new Chrome.`);
Expand Down

0 comments on commit f7b24a0

Please sign in to comment.