all files / oak/lib/ core.js

30.36% Statements 17/56
11.11% Branches 2/18
20% Functions 1/5
30.91% Lines 17/55
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                                                                                                                                                                                                              
const { app, dialog, crashReporter } = require('electron')
 
const { join } = require('path')
const { EventEmitter2 } = require('eventemitter2')
const _ = require('lodash')
const minimatch = require('minimatch')
const { parse } = require('url')
const pino = require('pino')
 
class Core extends EventEmitter2 {
  constructor () {
    super({
      wildcard: true,
      newListener: false,
      maxListeners: 200
    })
    let _this = this
    this.ready = false
    this.version = require(join(__dirname, '..', 'package.json')).version
 
    this.debug = process.env.OAK_DEBUG === 'true'
    this.log = pino({
      level: this.debug ? 'debug' : 'error',
      prettyPrint: this.debug ? { colorize: true } : false
    })
    this.crashReporter = crashReporter
 
    this.app = app
    this.quit = app.quit
 
    this.sslExceptions = []
 
    app.on('certificate-error', (event, webContents, url, error, certificate, cb) => {
      let { host } = parse(url)
      let isTrusted = false
      if (_.filter(_this.sslExceptions, pat => minimatch(host, pat))) {
        _this.log.debug({
          name: 'ignoring SSL certificate error',
          url,
          host
        })
        event.preventDefault()
        isTrusted = true
      }
      cb(isTrusted)
    })
 
    app.on('ready', function () {
      _this.ready = true
      _this.emit('ready')
    })
 
    return this
  }
 
  quit (code) {
    if (code) {
      this.app.exit(code)
    } else {
      this.app.quit()
    }
    return this
  }
 
  load (opts = false, callback = function () {}) {
    if (!opts || !_.isObject(opts)) {
      throw new Error('You need to pass options into Oak as the first parameter.')
    }
    if (!this.ready) {
      throw new Error("Oak isn't ready yet, wait your turn!")
    }
    if (!_.isString(opts.url)) {
      throw new Error('Oak requires the option: url')
    }
 
    let Window = require(join(__dirname, 'window'))
 
    opts = _.defaultsDeep(opts, {
      scripts: [],
      flags: []
    })
 
    this.sslExceptions = _.uniq(
      _.union(
        this.sslExceptions,
        opts.sslExceptions
      )
    )
 
    // Chrome switch flags
    if (!opts.cache) {
      opts.flags.push('--disable-http-cache')
    }
 
    opts.flags.forEach(app.commandLine.appendSwitch)
 
    global.scripts = opts.scripts
 
    return new Window(this, opts, callback)
  }
 
  catchErrors () {
    let _this = this
 
    // we are (ideally) running without errors on the screen, so we will ignore the dialog box for JS errors
    dialog.showErrorBox = (title, err) => {
      return undefined
    }
    // take on uncaughtExceptions, send them to the logger
    process.on('uncaughtException', err => {
      _this.log.error(err)
      return false
    })
 
    return this
  }
}
 
module.exports = Core