chore: make Prettier + fix spelling, update upgrade/switch instructions, prefer ale...
[webi-installers/.git] / _webi / ua-detect.js
index 5455d6cf17bc11276f87d2b201747691ad6545f5..6b972cb914b6b27e610027c19ee18a0d38f0860a 100644 (file)
@@ -1,5 +1,27 @@
 'use strict';
 
+function getRequest(req) {
+  var ua = req.headers['user-agent'] || '';
+  var os = req.query.os;
+  var arch = req.query.arch;
+  var scheme = req.socket.encrypted ? 'https' : 'http';
+  var host = req.headers.host || 'beta.webinstall.dev';
+  var url = scheme + '://' + host + '/api/debug';
+  if (os && arch) {
+    ua = os + ' ' + arch;
+  } else if (os || arch) {
+    ua = os || arch;
+  }
+
+  return {
+    unix: 'curl -fsSA "$(uname -a)" ' + url,
+    windows: 'curl.exe -fsSA "MS $Env:PROCESSOR_ARCHITECTURE" ' + url,
+    ua: ua,
+    os: uaDetect.os(ua),
+    arch: uaDetect.arch(ua)
+  };
+}
+
 function getOs(ua) {
   if ('-' === ua) {
     return '-';
@@ -10,16 +32,17 @@ function getOs(ua) {
     return 'android';
   } else if (/iOS|iPhone|Macintosh|Darwin|OS\s*X|macOS|mac/i.test(ua)) {
     return 'macos';
-  } else if (/Microsoft\s*Linux/i.test(ua)) {
+  } else if (/Linux/i.test(ua) && !/cygwin|msysgit/i.test(ua)) {
     // It's the year of the Linux Desktop!
-    // WSL / WSL2
-    // (checking linux twice because I'm not sure about cygwin / msysgit)
     // See also http://www.mslinux.org/
+    // 'linux' must be tested before 'Microsoft' because WSL
+    // (TODO: does this affect cygwin / msysgit?)
     return 'linux';
   } else if (/^ms$|Microsoft|Windows|win32|win|PowerShell/i.test(ua)) {
     // 'win' must be tested after 'darwin'
     return 'windows';
   } else if (/Linux|curl|wget/i.test(ua)) {
+    // test 'linux' again, after 'win'
     return 'linux';
   } else {
     return 'error';
@@ -31,9 +54,13 @@ function getArch(ua) {
     return '-';
   }
 
-  if (/arm64|arm8|armv8/i.test(ua)) {
+  // quick hack for Apple Silicon M1
+  // Native:  Darwin boomer.local 20.2.0 Darwin Kernel Version 20.2.0: Wed Dec  2 20:40:21 PST 2020; root:xnu-7195.60.75~1/RELEASE_ARM64_T8101 arm64
+  // Resetta: Darwin boomer.local 20.2.0 Darwin Kernel Version 20.2.0: Wed Dec  2 20:40:21 PST 2020; root:xnu-7195.60.75~1/RELEASE_ARM64_T8101 x86_64
+  ua = ua.replace(/xnu-.*RELEASE_[^\s]*/, '');
+  if (/aarch64|arm64|arm8|armv8/i.test(ua)) {
     return 'arm64';
-  } else if (/arm7|armv7/i.test(ua)) {
+  } else if (/aarch|arm7|armv7/i.test(ua)) {
     return 'armv7l';
   } else if (/arm6|armv6/i.test(ua)) {
     return 'armv6l';
@@ -55,5 +82,7 @@ function getArch(ua) {
   }
 }
 
-module.exports.os = getOs;
-module.exports.arch = getArch;
+var uaDetect = module.exports;
+uaDetect.os = getOs;
+uaDetect.arch = getArch;
+uaDetect.request = getRequest;