Skip to content

Commit

Permalink
OS detection improved
Browse files Browse the repository at this point in the history
  • Loading branch information
RNEvok committed Mar 8, 2024
1 parent f1186fa commit 3ee961e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SPECIAL_INSTRUCTIONS_TABLE, SPECIAL_INSTRUCTIONS } from './constants/ev
import { io, Socket } from "socket.io-client";
import { codebudConsoleLog, codebudConsoleWarn, jsonStringifyKeepMeta, stringifyIfNotString } from './helpers/helperFunctions';
import { getProcessEnv } from './helpers/environment';
import { getBrowserInfo } from './helpers/browserInfo';
import { getOS } from './helpers/os';
import { remoteSettingsService } from './services/remoteSettingsService';
import { asyncStoragePlugin } from './asyncStorage/asyncStorage';
Expand Down Expand Up @@ -80,11 +81,13 @@ export class Connector {
try {
const envInfo = getProcessEnv();
const osInfo = config?.ReactNativePlugin ? config.ReactNativePlugin.getOS() : getOS();
const browserInfo = getBrowserInfo();
const additionalInfo = config?.ReactNativePlugin ? config.ReactNativePlugin.getPlatformInfo() : {};

return {
...envInfo,
...osInfo,
...browserInfo,
...additionalInfo
};
} catch (e) {
Expand Down
39 changes: 39 additions & 0 deletions helpers/browserInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ObjectT } from "../types";

export type BrowserName = null | 'Opera' | 'MS Edge' | 'Chrome' | 'Mozilla Firefox' | 'Safari' | 'MS Explorer' | 'UC Browser' | 'Samsung Browser';

export const getBrowserInfo = (): ObjectT<any> => {
try {
const test = (regexp: RegExp) => {
return regexp.test(navigator.userAgent);
};

let browserName: BrowserName = null;

// @ts-ignore
if (test(/opr\//i) || !!window.opr) {
browserName = 'Opera';
} else if (test(/edg/i)) {
browserName = 'MS Edge';
} else if (test(/chrome|chromium|crios/i)) {
browserName = 'Chrome';
} else if (test(/firefox|fxios/i)) {
browserName = 'Mozilla Firefox';
} else if (test(/safari/i)) {
browserName = 'Safari';
} else if (test(/trident/i)) {
browserName = 'MS Explorer';
} else if (test(/ucbrowser/i)) {
browserName = 'UC Browser';
} else if (test(/samsungbrowser/i)) {
browserName = 'Samsung Browser';
}

if (browserName !== null)
return { browserName };

return {};
} catch (e) {
return {};
}
}
68 changes: 63 additions & 5 deletions helpers/os.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { ObjectT } from "../types";

export type UserOS = null | 'Mac OS' | 'iOS' | 'Windows' | 'Android' | 'Linux';
export type UserOS = null | 'MacOS' | 'iOS' | 'Windows' | 'Android' | 'Linux';

export const getOS = (): ObjectT<any> => {
export const getOSBrowser = (): ObjectT<any> => {
try {
// @ts-ignore
var userAgent = window.navigator.userAgent, platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K', 'macOS'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os: UserOS = null;

if (macosPlatforms.indexOf(platform) !== -1) {
os = 'Mac OS';
os = 'MacOS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
Expand All @@ -27,4 +27,62 @@ export const getOS = (): ObjectT<any> => {
} catch (e) {
return {};
}
}
}

export const getOSNodeProcess = (): ObjectT<any> => {
try {
const platform = process.platform;

let os: UserOS = null;

switch (platform) {
case "aix":
break;
case "darwin":
os = "MacOS";
break;
case "freebsd":
break;
case "linux":
os = "Linux";
break;
case "openbsd":
break;
case "sunos":
break;
case "win32":
os = "Windows";
break;
case "android":
os = "Android";
break;
case "cygwin":
os = "Windows";
break;
case "haiku":
break;
case "netbsd":
break;

default:
break;
}

return { OS: os };
} catch (e) {
return {};
}
};

export const getOS = (): ObjectT<any> => {
try {
const browserOS = getOSBrowser();

if (browserOS.OS)
return browserOS;

return getOSNodeProcess();
} catch (e) {
return {};
}
};

0 comments on commit 3ee961e

Please sign in to comment.