1
0
mirror of https://github.com/jiahaog/Nativefier synced 2024-06-27 07:45:03 +02:00
Nativefier/src/infer/browsers/inferChromeVersion.ts
Adam Weeden 7a08a2d676
Enable TypeScript strict:true, and more typescript-eslint rules (#1223)
* Catch promise errors better

* Move subFunctions to bottom of createNewWindow

* Use parents when creating child BrowserWindow instances

* Some about:blank pages have an anchor (for some reason)

* Inject browserWindowOptions better

* Interim refactor to MainWindow object

* Split up the window functions/helpers/events some

* Further separate out window functions + tests

* Add a mock for unit testing functions that access electron

* Add unit tests for onWillPreventUnload

* Improve windowEvents tests

* Add the first test for windowHelpers

* Move WebRequest event handling to node

* insertCSS completely under test

* clearAppData completely under test

* Fix contextMenu require bug

* More tests + fixes

* Fix + add to createNewTab tests

* Convert createMainWindow back to func + work out gremlins

* Move setupWindow away from main since its shared

* Make sure contextMenu is handling promises

* Fix issues with fullscreen not working + menu refactor

* Run jest against app/dist so that we can hit app unit tests as well

* Requested PR changes

* Add strict typing; tests currently failing

* Fix failing unit tests

* Add some more eslint warnings and fixes

* More eslint fixes

* Strict typing on (still issues with the lib dir)

* Fix the package.json import/require

* Fix some funky test errors

* Warn -> Error for eslint rules

* @ts-ignore -> @ts-expect-error

* Add back the ext code I removed
2021-06-15 22:20:49 -04:00

59 lines
1.8 KiB
TypeScript

import axios from 'axios';
import * as log from 'loglevel';
import {
DEFAULT_CHROME_VERSION,
DEFAULT_ELECTRON_VERSION,
} from '../../constants';
type ElectronRelease = {
version: string;
date: string;
node: string;
v8: string;
uv: string;
zlib: string;
openssl: string;
modules: string;
chrome: string;
files: string[];
};
const ELECTRON_VERSIONS_URL = 'https://atom.io/download/atom-shell/index.json';
export async function getChromeVersionForElectronVersion(
electronVersion: string,
url = ELECTRON_VERSIONS_URL,
): Promise<string> {
if (!electronVersion || electronVersion === DEFAULT_ELECTRON_VERSION) {
// Exit quickly for the scenario that we already know about
return DEFAULT_CHROME_VERSION;
}
try {
log.debug('Grabbing electron<->chrome versions file from', url);
const response = await axios.get<ElectronRelease[]>(url, { timeout: 5000 });
if (response.status !== 200) {
throw new Error(`Bad request: Status code ${response.status}`);
}
const electronReleases: ElectronRelease[] = response.data;
const electronVersionToChromeVersion: { [key: string]: string } = {};
for (const release of electronReleases) {
electronVersionToChromeVersion[release.version] = release.chrome;
}
if (!(electronVersion in electronVersionToChromeVersion)) {
throw new Error(
`Electron version '${electronVersion}' not found in retrieved version list!`,
);
}
const chromeVersion = electronVersionToChromeVersion[electronVersion];
log.debug(
`Associated electron v${electronVersion} to chrome v${chromeVersion}`,
);
return chromeVersion;
} catch (err: unknown) {
log.error('getChromeVersionForElectronVersion ERROR', err);
log.debug('Falling back to default Chrome version', DEFAULT_CHROME_VERSION);
return DEFAULT_CHROME_VERSION;
}
}