-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (50 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import browserLauncher from '@httptoolkit/browser-launcher';
import { parseVersion } from './utilities.js';
const installedWebBrowsersExpiryMillis = 5 * 60_000;
let installedWebBrowsers = [];
let installedWebBrowsersMillis = 0;
export const possibleWebBrowserTypes = [
'chrome',
'chromium',
'firefox',
'phantomjs',
'safari',
'ie',
'msedge',
'brave',
'opera',
'arc'
];
export const chromeWebBrowserTypes = [
'chrome',
'chromium'
];
async function _loadInstalledWebBrowsers() {
if (Date.now() - installedWebBrowsersMillis <=
installedWebBrowsersExpiryMillis ||
installedWebBrowsers.length > 0) {
return installedWebBrowsers;
}
return await new Promise((resolve) => {
browserLauncher.detect((browsers) => {
const installedWebBrowsersTemp = [];
for (const browser of browsers) {
installedWebBrowsersTemp.push(Object.assign(browser, parseVersion(browser.version)));
}
installedWebBrowsers = installedWebBrowsersTemp;
installedWebBrowsersMillis = Date.now();
resolve(installedWebBrowsersTemp);
});
});
}
export async function getInstalledWebBrowsers(webBrowserTypes, minimumMajorVersion = 0) {
const browsers = await _loadInstalledWebBrowsers();
if (webBrowserTypes === undefined) {
return browsers;
}
const webBrowserTypesToSearch = typeof webBrowserTypes === 'string' ? [webBrowserTypes] : webBrowserTypes;
return browsers.filter((possibleBrowser) => {
return (webBrowserTypesToSearch.includes(possibleBrowser.type) &&
(possibleBrowser.majorVersion ?? 0) >= minimumMajorVersion);
});
}