<commit_before>/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
 * Class PuppetWeb
 *
 * use to control wechat in web browser.
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
 */

/**************************************
 *
 * Class PuppetWeb
 *
 ***************************************/
const util  = require('util')
const fs    = require('fs')
const co    = require('co')

const log = require('../npmlog-env')
const Puppet  = require('../puppet')
const Contact = require('../contact')
const Room    = require('../room')
const Message = require('../message')

const Server  = require('./server')
const Browser = require('./browser')
const Bridge  = require('./bridge')

const Event     = require('./event')
const Watchdog  = require('./watchdog')

const UtilLib = require('../util-lib')
const Config  = require('../config')

class PuppetWeb extends Puppet {
  constructor({
    head = Config.DEFAULT_HEAD
    , profile = null  // if not set profile, then do not store session.
  } = {}) {
    super()
    this.head     = head
    this.profile  = profile

    this.userId = null  // user id
    this.user   = null  // <Contact> of user self
  }

  toString() { return `Class PuppetWeb({browser:${this.browser},port:${this.port}})` }

  init() {
    log.verbose('PuppetWeb', `init() with head:${this.head}, profile:${this.profile}`)

    this.readyState('connecting')

    this.on('watchdog', Watchdog.onFeed.bind(this))

    return co.call(this, function* () {

      this.port = yield UtilLib.getPort(Config.DEFAULT_PUPPET_PORT)
      log.verbose('PuppetWeb', 'init() getPort %d', this.port)

      yield this.initAttach(this)
      log.verbose('PuppetWeb', 'initAttach() done')

      this.server = yield this.initServer()
      log.verbose('PuppetWeb', 'initServer() done')

      this.browser = yield this.initBrowser()
      log.verbose('PuppetWeb', 'initBrowser() done')

      this.bridge = yield this.initBridge()
      log.verbose('PuppetWeb', 'initBridge() done')

      // this.watchDog('inited') // start watchdog
      this.emit('watchdog', { data: 'inited' })
    })
    .catch(e => {   // Reject
      log.error('PuppetWeb', 'init exception: %s', e.message)
      throw e
    })
    .then(() => {   // Finally
      log.verbose('PuppetWeb', 'init() done')
      this.readyState('connected')
      return this   // for Chaining
    })
  }

  quit() {
    log.verbose('PuppetWeb', 'quit()')

    if (this.readyState() === 'disconnecting') {
      log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?')
      throw new Error('do not call quit again when quiting')
    }

    // POISON must feed before readyState set to "disconnecting"
    this.emit('watchdog', {
      data: 'PuppetWeb.quit()',
      type: 'POISON'
    })
<commit_msg>call quit after init fail<commit_after>/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
 * Class PuppetWeb
 *
 * use to control wechat in web browser.
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
 */

/**************************************
 *
 * Class PuppetWeb
 *
 ***************************************/
const util  = require('util')
const fs    = require('fs')
const co    = require('co')

const log = require('../npmlog-env')
const Puppet  = require('../puppet')
const Contact = require('../contact')
const Room    = require('../room')
const Message = require('../message')

const Server  = require('./server')
const Browser = require('./browser')
const Bridge  = require('./bridge')

const Event     = require('./event')
const Watchdog  = require('./watchdog')

const UtilLib = require('../util-lib')
const Config  = require('../config')

class PuppetWeb extends Puppet {
  constructor({
    head = Config.DEFAULT_HEAD
    , profile = null  // if not set profile, then do not store session.
  } = {}) {
    super()
    this.head     = head
    this.profile  = profile

    this.userId = null  // user id
    this.user   = null  // <Contact> of user self
  }

  toString() { return `Class PuppetWeb({browser:${this.browser},port:${this.port}})` }

  init() {
    log.verbose('PuppetWeb', `init() with head:${this.head}, profile:${this.profile}`)

    this.readyState('connecting')

    this.on('watchdog', Watchdog.onFeed.bind(this))

    return co.call(this, function* () {

      this.port = yield UtilLib.getPort(Config.DEFAULT_PUPPET_PORT)
      log.verbose('PuppetWeb', 'init() getPort %d', this.port)

      yield this.initAttach(this)
      log.verbose('PuppetWeb', 'initAttach() done')

      this.server = yield this.initServer()
      log.verbose('PuppetWeb', 'initServer() done')

      this.browser = yield this.initBrowser()
      log.verbose('PuppetWeb', 'initBrowser() done')

      this.bridge = yield this.initBridge()
      log.verbose('PuppetWeb', 'initBridge() done')

      // this.watchDog('inited') // start watchdog
      this.emit('watchdog', { data: 'inited' })
    })
    .catch(e => {   // Reject
      log.error('PuppetWeb', 'init exception: %s', e.message)
      this.quit()
      throw e
    })
    .then(() => {   // Finally
      log.verbose('PuppetWeb', 'init() done')
      this.readyState('connected')
      return this   // for Chaining
    })
  }

  quit() {
    log.verbose('PuppetWeb', 'quit()')

    if (this.readyState() === 'disconnecting') {
      log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?')
      throw new Error('do not call quit again when quiting')
    }

    // POISON must feed before readyState set to "disconnecting"
    this.emit('watchdog', {
      data: 'PuppetWeb.quit()',
      type: 'POISON'
    })

{'fraction_non_alphanumeric': 0.1896062433487052, 'fraction_numerical': 0.0, 'mean_word_length': 3.007818052594172, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4788466', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1784, 'n_words': 429}