import * as platform from 'platform' ; import { BROWSER , WEBAPP_SUPPORTED_BROWSERS } from '../config/CommonConfig' ; const UNKNOWN_PROPERTY = 'unknown' ; export enum OperatingSystem { ANDROID = 'OperatingSystem.ANDROID' , IOS = 'OperatingSystem.IOS' , LINUX = 'OperatingSystem.LINUX' , MAC = 'OperatingSystem.MAC' , WINDOWS = 'OperatingSystem.WINDOWS' } export interface OS { architecture : string | number ; family : string ; version : string ; } export interface BrowserVersion { major : number ; minor : number ; } export class Runtime { public static getPlatform ( ) { const unsetPlatform = { } as unknown as typeof platform ; return platform || unsetPlatform ; } public static getOSFamily ( ) { const family = Runtime . getOS ( ) . family ?. toLowerCase ( ) || '' ; if ( family . includes ( 'windows' ) ) { return OperatingSystem . WINDOWS ; } if ( family . includes ( 'android' ) ) { return OperatingSystem . ANDROID ; } if ( family . includes ( 'ios' ) ) { return OperatingSystem . IOS ; } if ( [ 'os x' , 'mac os' ] . includes ( family ) ) { return OperatingSystem . MAC ; } return OperatingSystem . LINUX ; } /**
     * NOTE: This converts the browser name to lowercase.
     */ public static getBrowserName ( ) { return ( Runtime . getPlatform ( ) . name || UNKNOWN_PROPERTY ) . toLowerCase ( ) ; } public static getBrowserVersion ( ) { const [ majorVersion , minorVersion ] = ( Runtime . getPlatform ( ) . version || UNKNOWN_PROPERTY ) . split ( '.' ) ; return { major : parseInt ( majorVersion , 10 ) , minor : parseInt ( minorVersion , 10 ) } ; } /**
     * NOTE: This converts the User-Agent to lowercase.
     */ public static getUserAgent ( ) { return ( Runtime . getPlatform ( ) . ua || UNKNOWN_PROPERTY ) . toLowerCase ( ) ; } public static isWebappSupportedBrowser ( ) { if ( Runtime . isFranz ( ) ) { return false ; } return Object . entries ( WEBAPP_SUPPORTED_BROWSERS ) . some ( ( [ browser , supportedVersion ] ) => { const isBrowserSupported = Runtime . getBrowserName ( ) === browser ; const currentVersion = Runtime . getBrowserVersion ( ) ; const isSupportedMajorVersion = currentVersion . major >= supportedVersion . major ; const isHigherMajorVersion = currentVersion . major > supportedVersion . major ; const isSupportedMinorVersion = isHigherMajorVersion || currentVersion . minor >= supportedVersion . minor ; return isBrowserSupported && isSupportedMajorVersion && isSupportedMinorVersion ; } ) ; } public static getOS ( ) { return { architecture : UNKNOWN_PROPERTY , family : UNKNOWN_PROPERTY , version : UNKNOWN_PROPERTY , ... platform . os , } ; } public static isSupportingWebSockets = ( ) : boolean => { try { return 'WebSocket' in window ; } catch ( error ) { return false ; } } ; public static isSupportingClipboard = ( ) : boolean => { return ! ! navigator . clipboard ; } ; public static isSupportingIndexedDb = ( ) : boolean => { try { return ! ! window . indexedDB ; } catch ( error ) { return false ; } } ; public static isSupportingLegacyCalling = ( ) : boolean => { return ( Runtime . isSupportingRTCPeerConnection ( ) && Runtime . isSupportingRTCDataChannel ( ) && Runtime . isSupportingUserMedia ( ) && Runtime . isSupportingWebSockets ( ) ) ; } ; public static isSupportingConferenceCalling = ( ) : boolean => { const isSupportingEncodedVideoStreams = RTCRtpSender . prototype . hasOwnProperty ( 'createEncodedVideoStreams' ) ; const isSupportingEncodedStreams = RTCRtpSender . prototype . hasOwnProperty ( 'createEncodedStreams' ) ; return isSupportingEncodedStreams || isSupportingEncodedVideoStreams ; } ; public static isSupportingRTCPeerConnection = ( ) : boolean => { return 'RTCPeerConnection' in window ; } ; public static isSupportingRTCDataChannel = ( ) : boolean => { if ( ! Runtime . isSupportingRTCPeerConnection ( ) ) { return false ; } const peerConnection = new RTCPeerConnection ( undefined ) ; return 'createDataChannel' in peerConnection ; } ; public static isSupportingUserMedia = ( ) : boolean => { return 'mediaDevices' in navigator && 'getUserMedia' in navigator . mediaDevices ; } ; public static isSupportingDisplayMedia = ( ) : boolean => { return 'mediaDevices' in navigator && 'getDisplayMedia' in navigator . mediaDevices ; } ; public static isSupportingScreensharing = ( ) : boolean => { const hasScreenCaptureAPI = ! ! ( window as unknown as any ) . desktopCapturer || ( Runtime . isSupportingUserMedia ( ) && Runtime . isSupportingDisplayMedia ( ) ) ; return hasScreenCaptureAPI || Runtime . isFirefox ( ) ; } ; public static isSupportingPermissions = ( ) : boolean => { return ! ! navigator . permissions ; } ; public static isSupportingNotifications = ( ) : boolean => { const notificationNotSupported = window . Notification === undefined ; if ( notificationNotSupported ) { return false ; } const requestPermissionNotSupported = window . Notification . requestPermission === undefined ; return requestPermissionNotSupported ? false : document . visibilityState !== undefined ; } ; public static isChrome ( ) { return Runtime . getBrowserName ( ) === BROWSER . CHROME ; } public static isEdge ( ) { return Runtime . getBrowserName ( ) === BROWSER . MS_EDGE ; } public static isFirefox ( ) { return Runtime . getBrowserName ( ) === BROWSER . FIREFOX ; } public static isInternetExplorer ( ) { return Runtime . getBrowserName ( ) === BROWSER . IE ; } public static isOpera ( ) { return Runtime . getBrowserName ( ) === BROWSER . OPERA ; } public static isSafari ( ) { return Runtime . getBrowserName ( ) === BROWSER . SAFARI ; } public static isDesktopOS ( ) { return Runtime . isMacOS ( ) || Runtime . isWindows ( ) || Runtime . isLinux ( ) ; } public static isElectron ( ) { return Runtime . getBrowserName ( ) === BROWSER . ELECTRON ; } public static isNode ( ) { return typeof process === 'object' ; } public static isDesktopApp ( ) { return Runtime . isElectron ( ) && Runtime . getUserAgent ( ) . includes ( 'wire' ) ; } public static isFranz ( ) { return Runtime . isElectron ( ) && Runtime . getUserAgent ( ) . includes ( 'franz' ) ; } public static isMacOS ( ) { return Runtime . getOSFamily ( ) === OperatingSystem . MAC ; } public static isWindows ( ) { return Runtime . getOSFamily ( ) === OperatingSystem . WINDOWS ; } public static isLinux ( ) { return Runtime . getOSFamily ( ) === OperatingSystem . LINUX ; } public static isMobileOS ( ) { return Runtime . isAndroid ( ) || Runtime . isIOS ( ) ; } public static isAndroid ( ) { return Runtime . getOSFamily ( ) === OperatingSystem . ANDROID ; } public static isIOS ( ) { return Runtime . getOSFamily ( ) === OperatingSystem . IOS ; } }