From 94c9fdb4243fceb088697cdf7ef90ca81df3bc62 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Fri, 25 Mar 2016 20:10:45 +0800 Subject: [PATCH] Contain fallback user agent within inferUserAgent --- src/infer/inferUserAgent.js | 13 ++++++++++--- src/options/optionsMain.js | 9 ++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/infer/inferUserAgent.js b/src/infer/inferUserAgent.js index 3a3db0f..ab0355d 100644 --- a/src/infer/inferUserAgent.js +++ b/src/infer/inferUserAgent.js @@ -2,10 +2,11 @@ import axios from 'axios'; import _ from 'lodash'; const ELECTRON_VERSIONS_URL = 'https://atom.io/download/atom-shell/index.json'; +const DEFAULT_CHROME_VERSION = '47.0.2526.73'; function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) { - return axios.get(url) + return axios.get(url, {timeout: 5000}) .then(response => { if (response.status !== 200) { throw `Bad request: Status code ${response.status}`; @@ -40,9 +41,15 @@ export function getUserAgentString(chromeVersion, platform) { return userAgent; } -export function inferUserAgent(electronVersion, platform) { - return getChromeVersionForElectronVersion(electronVersion) +function inferUserAgent(electronVersion, platform, url = ELECTRON_VERSIONS_URL) { + return getChromeVersionForElectronVersion(electronVersion, url) .then(chromeVersion => { return getUserAgentString(chromeVersion, platform); + }) + .catch(() => { + // console.warn(`Unable to infer chrome version for user agent, using ${DEFAULT_CHROME_VERSION}`); + return getUserAgentString(DEFAULT_CHROME_VERSION, platform); }); } + +export default inferUserAgent; diff --git a/src/options/optionsMain.js b/src/options/optionsMain.js index dc126bb..3fdf14b 100644 --- a/src/options/optionsMain.js +++ b/src/options/optionsMain.js @@ -6,7 +6,7 @@ import sanitizeFilenameLib from 'sanitize-filename'; import inferIcon from './../infer/inferIcon'; import inferTitle from './../infer/inferTitle'; import inferOs from './../infer/inferOs'; -import {inferUserAgent, getUserAgentString} from './../infer/inferUserAgent'; +import inferUserAgent from './../infer/inferUserAgent'; import normalizeUrl from './normalizeUrl'; import packageJson from './../../package.json'; @@ -14,7 +14,6 @@ const {inferPlatform, inferArch} = inferOs; const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app'); const ELECTRON_VERSION = '0.36.4'; -const CHROME_VERSION = '47.0.2526.73'; const DEFAULT_APP_NAME = 'APP'; @@ -78,11 +77,7 @@ function optionsFactory(inpOptions, callback) { options.userAgent = userAgent; callback(); }) - .catch(error => { - console.warn('Cannot get user agent:', error); - options.userAgent = getUserAgentString(CHROME_VERSION, options.platform); - callback(); - }); + .catch(callback); }, callback => { if (options.icon) {