2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-03 13:20:47 +00:00
nativefier/src/infer/browsers/inferSafariVersion.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

78 lines
2.3 KiB
TypeScript

import axios from 'axios';
import * as log from 'loglevel';
import { DEFAULT_SAFARI_VERSION } from '../../constants';
export type SafariVersion = {
majorVersion: number;
version: string;
webkitVersion: string;
};
const SAFARI_VERSIONS_HISTORY_URL =
'https://en.wikipedia.org/wiki/Safari_version_history';
export async function getLatestSafariVersion(
url = SAFARI_VERSIONS_HISTORY_URL,
): Promise<SafariVersion> {
try {
log.debug('Grabbing apple version data from', url);
const response = await axios.get<string>(url, { timeout: 5000 });
if (response.status !== 200) {
throw new Error(`Bad request: Status code ${response.status}`);
}
// This would be easier with an HTML parser, but we're not going to include an extra dependency for something that dumb
const rawData: string = response.data;
const majorVersions = [
...rawData.matchAll(
/class="mw-headline" id="Safari_[0-9]*">Safari ([0-9]*)</g,
),
].map((match) => match[1]);
const majorVersion = parseInt(majorVersions[majorVersions.length - 1]);
const majorVersionTable = rawData
.split('>Release history<')[2]
.split('<table')
.filter((table) => table.includes(`Safari ${majorVersion}.x`))[0];
const versionRows = majorVersionTable.split('<tbody')[1].split('<tr');
let version: string | undefined = undefined;
let webkitVersion: string | undefined = undefined;
for (const versionRow of versionRows.reverse()) {
const versionMatch = [
...versionRow.matchAll(/>\s*(([0-9]*\.){2}[0-9])\s*</g),
];
if (versionMatch.length > 0 && !version) {
version = versionMatch[0][1];
}
const webkitVersionMatch = [
...versionRow.matchAll(/>\s*(([0-9]*\.){3,4}[0-9])\s*</g),
];
if (webkitVersionMatch.length > 0 && !webkitVersion) {
webkitVersion = webkitVersionMatch[0][1];
}
if (version && webkitVersion) {
break;
}
}
if (version && webkitVersion) {
return {
majorVersion,
version,
webkitVersion,
};
}
return DEFAULT_SAFARI_VERSION;
} catch (err: unknown) {
log.error('getLatestSafariVersion ERROR', err);
log.debug('Falling back to default Safari version', DEFAULT_SAFARI_VERSION);
return DEFAULT_SAFARI_VERSION;
}
}