b174fd4acf2d5bf59099876fb91e1cb50b56fea0
[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 (/^ms$|Microsoft|Windows|win32|win|PowerShell/i.test(ua)) {
14     // It's the year of the Linux Desktop!
15     // (TODO: what about cygwin / msysgit?)
16     // See also http://www.mslinux.org/
17     // 'linux' must be tested before 'Microsoft' because WSL
18     // 'win' must be tested after 'darwin'
19     return 'windows';
20   } else if (/Linux|curl|wget/i.test(ua)) {
21     return 'linux';
22   } else {
23     return 'error';
24   }
25 }
26
27 function getArch(ua) {
28   if ('-' === ua) {
29     return '-';
30   }
31
32   if (/arm64|arm8|armv8/i.test(ua)) {
33     return 'arm64';
34   } else if (/arm7|armv7/i.test(ua)) {
35     return 'armv7l';
36   } else if (/arm6|armv6/i.test(ua)) {
37     return 'armv6l';
38   } else if (/ppc64/i.test(ua)) {
39     return 'ppc64';
40   } else if (/mips64/i.test(ua)) {
41     return 'mips64';
42   } else if (/mips/i.test(ua)) {
43     return 'mips';
44   } else if (/(amd64|x64|_64)\b/i.test(ua)) {
45     // must come after ppc64/mips64
46     return 'amd64';
47   } else if (/(3|6|x|_)86\b/i.test(ua)) {
48     // must come after x86_64
49     return 'x86';
50   } else {
51     // TODO handle explicit invalid different
52     return 'error';
53   }
54 }
55
56 module.exports.os = getOs;
57 module.exports.arch = getArch;