mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-06-03 05:10:47 +00:00
6fb3b92eb8
* Update deps except eslint * Update eslint and lint:fix (WIP, needs manual fixing for remaining 44 problems) * Manually fix remaining eslint errors * Document deprecation of `version-string` as of electron-packager 9.0.0 * Upgrade to Electron 1.7.9 (chrome-58, node-7.9.0, v8-5.8) * npm: Disable generation of package-lock.json and gitignore it --Trying this, package-lock is a pain in PRs. May not be a good idea (obviously we lose deps pinning), will revert if necessary.-- * npm tasks: add dev-up-win for Windows developers, and e2e for end-to-end tests. Update docs. * Move normalizeUrl test to a jest unit test, makes no sense to be in the mocha e2e tests * Switch from babel-preset-es2015 to babel-preset-env, with target.node=4.0. Seem like it's today's most convenient way to support the latest ES and let babel transpile to what makes sense for our currently minimal node version
57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
import axios from 'axios';
|
|
import _ from 'lodash';
|
|
import log from 'loglevel';
|
|
|
|
const ELECTRON_VERSIONS_URL = 'https://atom.io/download/atom-shell/index.json';
|
|
const DEFAULT_CHROME_VERSION = '58.0.3029.110';
|
|
|
|
function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) {
|
|
return axios.get(url, { timeout: 5000 })
|
|
.then((response) => {
|
|
if (response.status !== 200) {
|
|
throw new Error(`Bad request: Status code ${response.status}`);
|
|
}
|
|
|
|
const { data } = response;
|
|
const electronVersionToChromeVersion = _.zipObject(
|
|
data.map(d => d.version),
|
|
data.map(d => d.chrome),
|
|
);
|
|
|
|
if (!(electronVersion in electronVersionToChromeVersion)) {
|
|
throw new Error(`Electron version '${electronVersion}' not found in retrieved version list!`);
|
|
}
|
|
|
|
return electronVersionToChromeVersion[electronVersion];
|
|
});
|
|
}
|
|
|
|
export function getUserAgentString(chromeVersion, platform) {
|
|
let userAgent;
|
|
switch (platform) {
|
|
case 'darwin':
|
|
userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
|
|
break;
|
|
case 'win32':
|
|
userAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
|
|
break;
|
|
case 'linux':
|
|
userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
|
|
break;
|
|
default:
|
|
throw new Error('Error invalid platform specified to getUserAgentString()');
|
|
}
|
|
return userAgent;
|
|
}
|
|
|
|
function inferUserAgent(electronVersion, platform, url = ELECTRON_VERSIONS_URL) {
|
|
return getChromeVersionForElectronVersion(electronVersion, url)
|
|
.then(chromeVersion => getUserAgentString(chromeVersion, platform))
|
|
.catch(() => {
|
|
log.warn(`Unable to infer chrome version for user agent, using ${DEFAULT_CHROME_VERSION}`);
|
|
return getUserAgentString(DEFAULT_CHROME_VERSION, platform);
|
|
});
|
|
}
|
|
|
|
export default inferUserAgent;
|