Skip to content

Commit

Permalink
Merge pull request #384 from ulixee/useragent
Browse files Browse the repository at this point in the history
fix(plugins): improve user agent selector
  • Loading branch information
calebjclark authored Nov 23, 2021
2 parents bac8c9d + cd708bb commit d28822c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions commons/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function translateToPrintable(
value: true,
});
result.error = value;
continue;
}
const printable = translateValueToPrintable(value);
if (!printable) continue;
Expand Down
4 changes: 4 additions & 0 deletions plugins/default-browser-emulator/lib/BrowserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export default class BrowserData implements IBrowserData {
this.osDataDir = `${this.baseDataDir}/as-${os.name}-${os.version}`;
if (!this.dataLoader.isSupportedEmulator(this.osDataDir)) {
const otherVersions = this.dataLoader.getBrowserOperatingSystemVersions(browserId, os.name);
if (!otherVersions.length) {
throw new Error(`${browserId} has no emulation data for ${os.name}`);
}

const closestVersionMatch = findClosestVersionMatch(os.version, otherVersions);
this.osDataDir = `${this.baseDataDir}/as-${os.name}-${closestVersionMatch}`;
}
Expand Down
1 change: 1 addition & 0 deletions plugins/default-browser-emulator/lib/DataLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class DataLoader implements IDataCore {
}

public getBrowserOperatingSystemVersions(browserId: string, osName: string): string[] {
if (!this.browserOsEmulatorsByVersion[`as-${browserId}`]) return [];
return this.browserOsEmulatorsByVersion[`as-${browserId}`][osName];
}
}
Expand Down
1 change: 1 addition & 0 deletions plugins/default-browser-emulator/lib/VersionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function convertMacOsVersionString(versionString: string) {

export function findClosestVersionMatch(versionToMatch: string, versions: string[]) {
if (versions.length === 1 && versions[0] === 'ALL') return 'ALL';
if (!versions.length) return null;

// there is no guarantee we have an exact match, so let's get the closest
const versionTree = convertVersionsToTree(versions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export default function selectUserAgentOption(

if (!userAgentSelector) {
const filteredOptions = dataUserAgentOptions.filter(x => {
if (x.browserName !== 'chrome') return false;
if (x.browserVersion.major !== latestChromeBrowserVersion.major) return false;
if (x.browserVersion.minor !== latestChromeBrowserVersion.minor) return false;
return true;
return (
x.browserName === 'chrome' &&
x.browserVersion.major === latestChromeBrowserVersion.major &&
x.browserVersion.minor === latestChromeBrowserVersion.minor
);
});
return pickRandomUserAgentOption(filteredOptions);
}
Expand All @@ -45,6 +46,10 @@ function findUserAgentOption(
return isSelectorMatch(userAgentOption, selectors);
});

if (!filteredOptions.length) {
throw new Error(`No installed UserAgent Emulators match your criteria (${userAgentSelector})`);
}

const dataUserAgentOption = pickRandom<IDataUserAgentOption>(filteredOptions);
return convertToUserAgentOption(dataUserAgentOption);
}
Expand All @@ -69,14 +74,19 @@ function isSelectorMatch(userAgentOption: IDataUserAgentOption, selectors: ISele
version = browserVersion;
} else if (name === userAgentOption.operatingSystemName) {
version = operatingSystemVersion;
} else continue;
} else {
return false;
}

const isMatch = matches.every(match =>
compareVersions.compare(version, match.version, match.operator),
);
if (isMatch) return true;
for (const match of matches) {
if (match.version === '*.*.*') continue;
const isValid = compareVersions.compare(version, match.version, match.operator);

// must match every selector
if (!isValid) return false;
}
}
return false;
return true;
}

interface ISelectorMatch {
Expand Down Expand Up @@ -135,10 +145,12 @@ function cleanupName(name: string) {
}

function cleanupOperator(operator: string) {
if (!operator) return '=';
return operator.replace(/[^<>=]+/g, '');
}

function cleanupVersion(version: string) {
if (!version) return '*';
return version.trim().replace(/[^0-9x*]+/g, '.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import selectUserAgentOption from '../lib/helpers/selectUserAgentOption';
import DataLoader from '../lib/DataLoader';

const dataLoader = new DataLoader(`${__dirname}/..`);

test('should support choosing a specific useragent', async () => {
const options = selectUserAgentOption(
'~ chrome >= 88 && chrome < 89',
dataLoader.userAgentOptions,
);
expect(options.browserVersion.major).toBe('88');
});

test('should support choosing a specific OS', async () => {
const options = selectUserAgentOption('~ mac & chrome >= 88', dataLoader.userAgentOptions);
expect(parseInt(options.browserVersion.major, 10)).toBeGreaterThanOrEqual(88);
expect(options.operatingSystemName).toBe('mac-os');
});

test('should throw an error for a non-installed pattern', async () => {
try {
expect(
selectUserAgentOption('~ mac & chrome >= 500000', dataLoader.userAgentOptions),
).not.toBeTruthy();
} catch (err) {
// eslint-disable-next-line jest/no-try-expect
expect(err.message).toMatch('No installed UserAgent');
}
});

0 comments on commit d28822c

Please sign in to comment.