Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add portStrictMode option #319

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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