All files / lib/utils user-agent.ts

61.81% Statements 68/110
62.5% Branches 5/8
41.66% Functions 5/12
61.81% Lines 68/110

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 1111x 1x 1x 1x 1x 1x 1x   1x 1x       1x 1x           1x 1x           1x 1x     1x 1x 1x 1x 1x 1x 707x 707x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 932x 932x 932x 932x 932x 932x             932x 932x 932x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x       1x 1x 1x 1x 707x 707x 707x 1x 932x 932x 932x 932x 932x                     932x 932x 569x  
interface UserAgentVersionNumber {
  readonly major: number;
  readonly minor?: number;
}
 
class UserAgentVersion implements UserAgentVersionNumber {
  readonly major: number;
  readonly minor?: number;
 
  constructor(major: number, minor?: number) {
    this.major = major;
    this.minor = minor;
  }
 
  public static parse(version: string): UserAgentVersion {
    const parts = version.split('.');
    const major = parseInt(parts[0]);
    const minor = parts.length >= 2 ? parseInt(parts[1]) : undefined;
    return new UserAgentVersion(major, minor);
  }
 
  public largerOrEqualThan(b: UserAgentVersionNumber): boolean {
    return (
      this.major > b.major ||
      (this.major === b.major && (this.minor ?? 0) >= (b.minor ?? 0))
    );
  }
 
  public lessThan(b: UserAgentVersionNumber): boolean {
    return !this.largerOrEqualThan(b);
  }
}
 
/**
 * Test that the browser is based on WebKit (eg Safari)
 */
export function isWebkit() {
  return !isBlink() && window.navigator.userAgent.includes('AppleWebKit/');
}
 
/**
 * Test if the user agent is reporting usage of the 'Gecko' engine (i.e. Firefox's rendering engine).
 * Optionally may specify additional limits on the version number, if a limit is not set no lower or upper
 * limit is enforced respectively.
 * @param minVersion minimum version (inclusive)
 * @param maxVersion maximum version (exclusive)
 * @returns `true` if usage of the Gecko engine within the version bounds is detected, `false` otherwise
 */
export function isGecko(
  minVersion?: UserAgentVersionNumber,
  maxVersion?: UserAgentVersionNumber,
): boolean {
  const version = getGeckoVersion();
  if (version === undefined) {
    return false;
  } else {
    return (
      (minVersion !== undefined
        ? version.largerOrEqualThan(minVersion)
        : true) &&
      (maxVersion !== undefined ? version.lessThan(maxVersion) : true)
    );
  }
}
 
/**
 * Test that the user's platform is based on macOS or iOS
 */
export function isOSX(): boolean {
  const userAgent = window.navigator.userAgent.toLowerCase();
  return (
    userAgent.includes('mac') ||
    userAgent.includes('ipad') ||
    userAgent.includes('ios')
  );
}
 
/**
 * Test that the user's platform is based on Linux or Windows
 */
export function isLinuxOrWindows(): boolean {
  const userAgent = window.navigator.userAgent.toLowerCase();
  return userAgent.includes('linux') || userAgent.includes('win');
}
 
/**
 * Test that the browser is based on Blink (eg Chrome, Edge)
 */
function isBlink() {
  return window.navigator.userAgent.includes('Chrome/');
}
 
function getGeckoVersion(): UserAgentVersion | undefined {
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#rendering_engine
  const userAgent = window.navigator.userAgent;
  if (userAgent.includes('Gecko/')) {
    // quick first check without regex to improve performance when it's not Gecko
    const regex = /^.+ \((.+; )?rv:([0-9.]+)\)(.+)? Gecko\/[0-9.]+( .+)?$/;
    const matches = regex.exec(userAgent);
    if (matches) {
      const versionString = matches[2];
      return UserAgentVersion.parse(versionString);
    } else {
      return undefined;
    }
  } else {
    return undefined;
  }
}