All files / src/lib browserHelper.ts

100% Statements 58/58
100% Branches 42/42
100% Functions 6/6
100% Lines 57/57

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 19313x 13x 13x   13x   13x                                                                                         13x                         13x         13x     359x 359x 110x   249x 178x   71x               39x     39x 39x 39x   39x         6x 6x     39x 6x 6x     39x 7x 7x     39x 4x 4x     39x 9x 9x     39x 31x     39x 39x       33x     33x     39x 39x 1x 1x     39x                             13x 17x 17x 17x 3x   14x 14x 14x   560x   14x   14x                       13x 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 a cookie,
   * when not it's randomly generated and stored in a cookie to be retrieved by later calls.
   *
   * @returns The device id for the current browser.
   */
  export function getDeviceId(): string {
    const cookieKey: string = "scandit-device-id";
    const storedDeviceId: string | undefined = Cookies.get("scandit-device-id");
    if (storedDeviceId != null && storedDeviceId !== "") {
      return storedDeviceId;
    }
    const hexCharacters: string = "0123456789abcdef";
    let randomDeviceId: string = "";
    for (let i: number = 0; i < 40; ++i) {
      // tslint:disable-next-line:insecure-random
      randomDeviceId += hexCharacters.charAt(Math.floor(Math.random() * 16));
    }
    Cookies.set(cookieKey, randomDeviceId, { expires: 3650 });
 
    return randomDeviceId;
  }
 
  /**
   * @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")
    );
  }
}