another WSL matching fix
[webi-installers/.git] / _webi / ua-detect.js
1 'use strict';
2
3 function getOs(ua) {
4   if ('-' === ua) {
5     return '-';
6   }
7
8   if (/Android/i.test(ua)) {
9     // android must be tested before linux
10     return 'android';
11   } else if (/iOS|iPhone|Macintosh|Darwin|OS\s*X|macOS|mac/i.test(ua)) {
12     return 'macos';
13   } else if (/Microsoft\s*Linux/i.test(ua)) {
14     // It's the year of the Linux Desktop!
15     // WSL / WSL2
16     // (checking linux twice because I'm not sure about cygwin / msysgit)
17     // See also http://www.mslinux.org/
18     return 'linux';
19   } else if (/^ms$|Microsoft|Windows|win32|win|PowerShell/i.test(ua)) {
20     // 'win' must be tested after 'darwin'
21     return 'windows';
22   } else if (/Linux|curl|wget/i.test(ua)) {
23     return 'linux';
24   } else {
25     return 'error';
26   }
27 }
28
29 function getArch(ua) {
30   if ('-' === ua) {
31     return '-';
32   }
33
34   if (/arm64|arm8|armv8/i.test(ua)) {
35     return 'arm64';
36   } else if (/arm7|armv7/i.test(ua)) {
37     return 'armv7l';
38   } else if (/arm6|armv6/i.test(ua)) {
39     return 'armv6l';
40   } else if (/ppc64/i.test(ua)) {
41     return 'ppc64';
42   } else if (/mips64/i.test(ua)) {
43     return 'mips64';
44   } else if (/mips/i.test(ua)) {
45     return 'mips';
46   } else if (/(amd64|x64|_64)\b/i.test(ua)) {
47     // must come after ppc64/mips64
48     return 'amd64';
49   } else if (/(3|6|x|_)86\b/i.test(ua)) {
50     // must come after x86_64
51     return 'x86';
52   } else {
53     // TODO handle explicit invalid different
54     return 'error';
55   }
56 }
57
58 module.exports.os = getOs;
59 module.exports.arch = getArch;