All files pharos.js

79.17% Statements 57/72
40.63% Branches 13/32
100% Functions 17/17
80.3% Lines 53/66

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 1344x 4x   4x 4x   4x 4x   11x 11x 11x     11x   11x       11x       11x 5x                   11x 5x                               11x 11x 5x 5x   5x           2x       2x 2x 1x     2x 2x         2x       1x       2x 2x 1x     2x 2x   2x 2x       15x 15x 4x     15x 17x 4x   13x   15x       7x 8x   7x       3x 3x 5x 5x   3x       17x      
const util = require('./util');
const performance = require('./performance');
 
const INFO = 'info';
const PERF = 'addPerfData';
 
const TIME_POOL = {};
module.exports = class Pharos {
  constructor(site_id, host, opt = {}) {
    this.site_id = site_id;
    this.host = host;
    this[INFO] = {
      screen: global.screen.width + 'x' + global.screen.height
    };
    this[PERF] = this.addPerf();
 
    this.listenError(opt);
  }
 
  listenError({ onError }) {
    Iif (!global.addEventListener) {
      return;
    }
 
    global.addEventListener('error', err => {
      Eif (!err) return;
 
      let message = (err.error && err.error.stack) || err.message;
      if (typeof onError === 'function') {
        message = onError(err);
      }
      if (!message) return;
      this.send(message);
    });
 
    global.addEventListener('unhandledrejection', reject => {
      Eif (!reject) return;
 
      let message = reject.reason;
      if (message instanceof Error) {
        message = (message.error && message.error.stack) || message.message;
      }
 
      if (typeof onError === 'function') {
        message = onError(reject);
      }
      if (!message) return;
      this.send(message);
    });
  }
 
  addPerf() {
    return new Promise(resolve =>
      global.addEventListener('load', () => setTimeout(() => {
        this.add(performance());
        resolve();
 
        this[PERF] = Promise.resolve();
      }))
    );
  }
 
  monitor(info) {
    return this[PERF].then(() => this.send(info));
  }
 
  send(info = this[INFO]) {
    let host = this.host;
    if (!/^(http|\/\/)/i.test(host)) {
      host = location.protocol + '//' + host;
    }
 
    const baseUrl = `${host}/pharos.gif?`;
    util.sendLog(baseUrl + util.build_query({
      site_id: this.site_id,
      ...info
    }));
 
    this.clear();
  }
 
  time(name) {
    TIME_POOL[name] = Date.now();
  }
 
  timeEnd(name) {
    const now = Date.now();
    if (!TIME_POOL[name]) {
      TIME_POOL[name] = now;
    }
 
    const delta = now - TIME_POOL[name];
    this.add(name, delta);
    // eslint-disable-next-line no-console
    console.log(`${name}: ${delta}ms`);
    delete TIME_POOL[name];
  }
 
  add(key, val) {
    let data = key;
    if (util.isNumber(val)) {
      data = { [key]: val };
    }
 
    for (const k in data) {
      if (!util.isNumber(data[k])) {
        continue;
      }
      this[INFO][k] = data[k];
    }
    return true;
  }
 
  delete(...infoKeys) {
    for (let i = 0; i < infoKeys.length; i++) {
      delete this[INFO][infoKeys[i]];
    }
    return true;
  }
 
  clear() {
    const data = {};
    for (const k in this[INFO]) {
      data[k] = this[INFO][k];
      this.delete(k);
    }
    return data;
  }
 
  search(key) {
    return this[INFO][key];
  }
};