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

fix: do not pass --{enable,disable}-features twice when user-provided #11230

Merged
merged 3 commits into from
Oct 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/puppeteer-core/src/node/ChromeLauncher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {describe, it} from 'node:test';

import expect from 'expect';

import {getFeatures} from './ChromeLauncher.js';
import {getFeatures, removeMatchingFlags} from './ChromeLauncher.js';

describe('getFeatures', () => {
it('returns an empty array when no options are provided', () => {
Expand Down Expand Up @@ -45,3 +45,25 @@ describe('getFeatures', () => {
expect(result).toEqual(['bar', 'baz']);
});
});

describe('removeMatchingFlags', () => {
it('empty', () => {
const a: string[] = [];
expect(removeMatchingFlags(a, '--foo')).toEqual([]);
});

it('with one match', () => {
const a: string[] = ['--foo=1', '--bar=baz'];
expect(removeMatchingFlags(a, '--foo')).toEqual(['--bar=baz']);
});

it('with multiple matches', () => {
const a: string[] = ['--foo=1', '--foo=2', '--bar=baz'];
expect(removeMatchingFlags(a, '--foo')).toEqual(['--bar=baz']);
});

it('with no matches', () => {
const a: string[] = ['--foo=1', '--bar=baz'];
expect(removeMatchingFlags(a, '--baz')).toEqual(['--foo=1', '--bar=baz']);
});
});
36 changes: 34 additions & 2 deletions packages/puppeteer-core/src/node/ChromeLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ export class ChromeLauncher extends ProductLauncher {
override defaultArgs(options: BrowserLaunchArgumentOptions = {}): string[] {
// See https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md

const userDisabledFeatures = getFeatures(
'--disable-features',
options.args
);
if (options.args && userDisabledFeatures.length > 0) {
removeMatchingFlags(options.args, '--disable-features');
}

// Merge default disabled features with user-provided ones, if any.
const disabledFeatures = [
'Translate',
Expand All @@ -175,13 +183,18 @@ export class ChromeLauncher extends ProductLauncher {
'OptimizationHints',
// https://crbug.com/1492053
'ProcessPerSiteUpToMainFrameThreshold',
...getFeatures('--disable-features', options.args),
...userDisabledFeatures,
];

const userEnabledFeatures = getFeatures('--enable-features', options.args);
if (options.args && userEnabledFeatures.length > 0) {
removeMatchingFlags(options.args, '--enable-features');
}

// Merge default enabled features with user-provided ones, if any.
const enabledFeatures = [
'NetworkServiceInProcess2',
...getFeatures('--enable-features', options.args),
...userEnabledFeatures,
];

const chromeArguments = [
Expand Down Expand Up @@ -297,3 +310,22 @@ export function getFeatures(flag: string, options: string[] = []): string[] {
return s;
}) as string[];
}

/**
* Removes all elements in-place from the given string array
* that match the given command-line flag.
*
* @internal
*/
export function removeMatchingFlags(array: string[], flag: string): string[] {
const regex = new RegExp(`^${flag}=.*`);
let i = 0;
while (i < array.length) {
if (regex.test(array[i]!)) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}