All files / src/lib browserHelper.ts

100% Statements 62/62
100% Branches 47/47
100% Functions 7/7
100% Lines 62/62

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 20715x 15x 15x   15x   15x                                                                                         15x                         15x         15x     423x 423x 130x   293x 209x   84x               46x     46x 46x 46x   46x         7x 7x     46x 7x 7x     46x 8x 8x     46x 4x 4x     46x 10x 10x     46x 38x     46x 46x       40x     40x     46x 46x 1x 1x     46x                                 15x 22x 22x 22x 5x     17x 17x 1x   1x     16x 16x 16x   320x   320x       16x   16x                       15x 66x            
import Cookies from "js-cookie";
import { UAParser } from "ua-parser-js";
export { UAParser };
 
import { BrowserCompatibility } from "./browserCompatibility";
 
export namespace BrowserHelper {
  /**
   * @hidden
   */
  interface Browser {
    name?: string;
    version?: string;
  }
 
  /**
   * @hidden
   */
  interface CPU {
    architecture?: string;
  }
 
  /**
   * @hidden
   */
  interface Device {
    model?: string;
    vendor?: string;
    // tslint:disable-next-line:no-reserved-keywords
    type?: string;
  }
 
  /**
   * @hidden
   */
  interface Engine {
    name?: string;
    version?: string;
  }
 
  /**
   * @hidden
   */
  interface OS {
    name?: string;
    version?: string;
  }
 
  /**
   * @hidden
   */
  export const userAgentInfo: {
    getBrowser(): Browser;
    getOS(): OS;
    getEngine(): Engine;
    getDevice(): Device;
    getCPU(): CPU;
    getUA(): string;
    setUA(uastring: string): void;
  } = new UAParser(navigator.userAgent);
 
  /**
   * @hidden
   */
  export const canvas: HTMLCanvasElement = document.createElement("canvas");
 
  /**
   * @returns The built [[BrowserCompatibility]] object representing the current OS/Browser's support for features.
   */
  export function checkBrowserCompatibility(): BrowserCompatibility {
    function objectHasPropertyWithType(object: object, propertyNames: string[], propertyType: string): boolean {
      // tslint:disable-next-line:no-any
      const objectProperty: any = (<any>object)[propertyNames[0]];
      if (objectProperty == null) {
        return false;
      }
      if (propertyNames.length === 1) {
        return typeof objectProperty === propertyType;
      } else {
        return (
          (typeof objectProperty === "function" || typeof objectProperty === "object") &&
          objectHasPropertyWithType(objectProperty, propertyNames.slice(1), propertyType)
        );
      }
    }
 
    function isBrokenWebAssemblyOS(os: OS): boolean {
      return os.name === "iOS" && os.version != null && ["11.2.2", "11.2.5", "11.2.6"].includes(os.version);
    }
 
    let fullSupport: boolean = true;
    let scannerSupport: boolean = true;
    const missingFeatures: BrowserCompatibility.Feature[] = [];
 
    if (
      !objectHasPropertyWithType(navigator, ["mediaDevices", "getUserMedia"], "function") &&
      !objectHasPropertyWithType(navigator, ["enumerateDevices"], "function") &&
      !objectHasPropertyWithType(window, ["MediaStreamTrack", "getSources"], "function")
    ) {
      missingFeatures.push(BrowserCompatibility.Feature.MEDIA_DEVICES);
      fullSupport = false;
    }
 
    if (!objectHasPropertyWithType(window, ["Worker"], "function")) {
      missingFeatures.push(BrowserCompatibility.Feature.WEB_WORKERS);
      fullSupport = scannerSupport = false;
    }
 
    if (!objectHasPropertyWithType(window, ["WebAssembly"], "object")) {
      missingFeatures.push(BrowserCompatibility.Feature.WEB_ASSEMBLY);
      fullSupport = scannerSupport = false;
    }
 
    if (!objectHasPropertyWithType(window, ["Blob"], "function")) {
      missingFeatures.push(BrowserCompatibility.Feature.BLOB);
      fullSupport = scannerSupport = false;
    }
 
    if (!objectHasPropertyWithType(window, ["URL", "createObjectURL"], "function")) {
      missingFeatures.push(BrowserCompatibility.Feature.URL_OBJECT);
      fullSupport = scannerSupport = false;
    }
 
    if (!objectHasPropertyWithType(window, ["OffscreenCanvas"], "function")) {
      missingFeatures.push(BrowserCompatibility.Feature.OFFSCREEN_CANVAS);
    }
 
    try {
      if (
        !objectHasPropertyWithType(window, ["WebGLRenderingContext"], "function") ||
        (canvas.getContext("webgl") == null && canvas.getContext("experimental-webgl") == null)
      ) {
        throw new Error();
      }
    } catch {
      missingFeatures.push(BrowserCompatibility.Feature.WEBGL);
    }
 
    const userAgentOS: OS = userAgentInfo.getOS();
    if (isBrokenWebAssemblyOS(userAgentOS)) {
      missingFeatures.push(BrowserCompatibility.Feature.WEB_ASSEMBLY_ERROR_FREE);
      fullSupport = scannerSupport = false;
    }
 
    return {
      fullSupport,
      scannerSupport,
      missingFeatures,
    };
  }
 
  /**
   * @hidden
   *
   * Get a device id for the current browser.
   *
   * When available it's retrieved from localStorage, as fallback from cookies (used by older library versions),
   * when not available it's randomly generated and stored in localStorage to be retrieved by later calls and returned.
   *
   * @returns The device id for the current browser.
   */
  export function getDeviceId(): string {
    const devideIdKey: string = "scandit-device-id";
    let deviceId: string | undefined | null = localStorage.getItem(devideIdKey);
    if (deviceId != null && deviceId !== "") {
      return deviceId;
    }
 
    deviceId = Cookies.get(devideIdKey);
    if (deviceId != null && deviceId !== "") {
      localStorage.setItem(devideIdKey, deviceId);
 
      return deviceId;
    }
 
    const randomDeviceIdBytes: Uint8Array = new Uint8Array(20);
    crypto.getRandomValues(randomDeviceIdBytes);
    deviceId = Array.from(randomDeviceIdBytes)
      .map((byteNumber) => {
        const byteHex: string = byteNumber.toString(16);
 
        return byteHex.length === 1 ? /* istanbul ignore next */ `0${byteHex}` : byteHex;
      })
      .join("");
 
    localStorage.setItem(devideIdKey, deviceId);
 
    return deviceId;
  }
 
  /**
   * @hidden
   *
   * Check if a given object is a valid HTMLElement
   *
   * @param object The object to check.
   * @returns Whether the given object is a valid HTMLElement.
   */
  // tslint:disable-next-line:no-any
  export function isValidHTMLElement(object: any): boolean {
    return (
      object instanceof HTMLElement ||
      (object != null && typeof object === "object" && typeof object.tagName === "string")
    );
  }
}