hotfix to remove trailing '}' from bin names
[webi-installers/.git] / _webi / ua-detect.js
index b174fd4acf2d5bf59099876fb91e1cb50b56fea0..c0483cb2ee6904c3cb47e65ac68f992d1d77ca0f 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,14 +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 (/^ms$|Microsoft|Windows|win32|win|PowerShell/i.test(ua)) {
+  } else if (/Linux/i.test(ua) && !/cygwin|msysgit/i.test(ua)) {
     // It's the year of the Linux Desktop!
-    // (TODO: what 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';
@@ -53,5 +78,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;