Skip to content

Commit

Permalink
fix: do not pass --{enable,disable}-features twice when user-provided (
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagowfx committed Oct 23, 2023
1 parent 014c72a commit edec7d5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
24 changes: 23 additions & 1 deletion packages/puppeteer-core/src/node/ChromeLauncher.test.ts
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
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;
}

0 comments on commit edec7d5

Please sign in to comment.