<commit_before>/*global Primus XMLHttpRequest*/

// ----------------------------------------------------------------------------- [Util Functions]

const warn = msg => console.warn(`[StellarClient warn]: ${msg}`)

const error = msg => console.error(`[StellarClient error]: ${msg}`)

const isFunction = val => typeof val === 'function'

const isObject = obj => obj !== null && typeof obj === 'object'

// ----------------------------------------------------------------------------- [Stellar Client]

/**
 * Interface for WebSocket client interact with the server.
 *
 * @param opts    Connections parameters.
 * @param client  Client object (if exists).
 * @constructor
 */
const StellarClient = function (opts, client) {
  this.callbacks = {}
  this.id = null
  this.events = {}
  this.rooms = []
  this.state = 'disconnected'
  this.messageCount = 0

  this.options = this.defaults() || {}

  // override default options
  for (const i in opts) { this.options[ i ] = opts[ i ] }

  if (client) {
    this.externalClient = true
    this.client = client
  }

  // this must print out an error when the Promise object can't be found
  if (Promise === undefined || typeof Promise !== 'function') {
    error(`The browser does not support Promises, you must load a polyfill before load Stellar client lib`)
  }
}

if (typeof Primus === 'undefined') {
  var util = require('util')
  var EventEmitter = require('events').EventEmitter
  util.inherits(StellarClient, EventEmitter)
} else {
  StellarClient.prototype = new Primus.EventEmitter()
}

/**
 * Array of Interceptors
 *
 * This is used to process before and after HTTP processing and WebSokcet.
 *
 * @type {Array}
 */
StellarClient.prototype.interceptors = []

StellarClient.prototype.defaults = function () { return '%%DEFAULTS%%' }

StellarClient.prototype.connect = function () {
  this.messageCount = 0

  return new Promise((resolve, reject) => {
    if (this.client && this.externalClient !== true) {
      this.client.end()
      this.client.removeAllListeners()
      this.client = Primus.connect(this.options.url, this.options)
    } else if (this.client !== null && this.externalClient === true) {
      this.client.end()
      this.client.open()
    } else {
      this.client = Primus.connect(this.options.url, this.options)
    }

    // --- define client event handlers

    // open
    this.client.on('open', () => {
      this.configure().then(details => {
        if (this.state !== 'connected') {
          this.state = 'connected'
          resolve(details)
        }

        this.emit('connected')
      })
    })

    // error
    this.client.on('error', err => {
      this.emit('error', err)
    })

    // reconnect
    this.client.on('reconnect', () => {
      this.messageCount = 0
      this.emit('reconnect')
    })

    // reconnecting
    this.client.on('reconnecting', () => {
      this.emit('reconnecting')
      this.state = 'reconnecting'
      this.emit('disconnected')
    })

    // timeout
    this.client.on('timeout', () => {
      this.state = 'timeout'
      this.emit('timeout')
    })

    // end
    this.client.on('end', () => {
      this.messageCount = 0

      if (this.state !== 'disconnected') {
        this.state = 'disconnected'
        this.emit('disconnected')
      }
    })

    // data
    this.client.on('data', data => this.handleMessage(data))
  })
}

StellarClient.prototype.configure = function () {
  return new Promise((resolve, reject) => {
    // join to all default rooms
    this.options.rooms.forEach(room => this.send({ event: 'roomAdd', room }))

    // request the connection details
    this.detailsView().then(details => {
      // save connection information
      this.id = details.data.id
      this.fingerprint = details.data.fingerprint
      this.rooms = details.data.rooms
<commit_msg>fix regression on the last commit 😒<commit_after>/*global Primus XMLHttpRequest*/

// ----------------------------------------------------------------------------- [Util Functions]

const warn = msg => console.warn(`[StellarClient warn]: ${msg}`)

const error = msg => console.error(`[StellarClient error]: ${msg}`)

const isFunction = val => typeof val === 'function'

const isObject = obj => obj !== null && typeof obj === 'object'

// ----------------------------------------------------------------------------- [Stellar Client]

/**
 * Interface for WebSocket client interact with the server.
 *
 * @param opts    Connections parameters.
 * @param client  Client object (if exists).
 * @constructor
 */
const StellarClient = function (opts, client) {
  this.callbacks = {}
  this.id = null
  this.events = {}
  this.rooms = []
  this.state = 'disconnected'
  this.messageCount = 0

  this.options = this.defaults() || {}

  // override default options
  for (const i in opts) { this.options[ i ] = opts[ i ] }

  if (client) {
    this.externalClient = true
    this.client = client
  }

  // this must print out an error when the Promise object can't be found
  if (Promise === undefined || typeof Promise !== 'function') {
    error(`The browser does not support Promises, you must load a polyfill before load Stellar client lib`)
  }
}

if (typeof Primus === 'undefined') {
  var util = require('util')
  var EventEmitter = require('events').EventEmitter
  util.inherits(StellarClient, EventEmitter)
} else {
  StellarClient.prototype = new Primus.EventEmitter()
}

/**
 * Array of Interceptors
 *
 * This is used to process before and after HTTP processing and WebSokcet.
 *
 * @type {Array}
 */
StellarClient.prototype.interceptors = []

StellarClient.prototype.defaults = function () { return '%%DEFAULTS%%' }

StellarClient.prototype.connect = function () {
  this.messageCount = 0

  return new Promise((resolve, reject) => {
    if (this.client && this.externalClient !== true) {
      this.client.end()
      this.client.removeAllListeners()
      this.client = Primus.connect(this.options.url, this.options)
    } else if (this.client !== null && this.externalClient === true) {
      this.client.end()
      this.client.open()
    } else {
      this.client = Primus.connect(this.options.url, this.options)
    }

    // --- define client event handlers

    // open
    this.client.on('open', () => {
      this.configure().then(details => {
        if (this.state !== 'connected') {
          this.state = 'connected'
          resolve(details)
        }

        this.emit('connected')
      })
    })

    // error
    this.client.on('error', err => {
      this.emit('error', err)
    })

    // reconnect
    this.client.on('reconnect', () => {
      this.messageCount = 0
      this.emit('reconnect')
    })

    // reconnecting
    this.client.on('reconnecting', () => {
      this.emit('reconnecting')
      this.state = 'reconnecting'
      this.emit('disconnected')
    })

    // timeout
    this.client.on('timeout', () => {
      this.state = 'timeout'
      this.emit('timeout')
    })

    // end
    this.client.on('end', () => {
      this.messageCount = 0

      if (this.state !== 'disconnected') {
        this.state = 'disconnected'
        this.emit('disconnected')
      }
    })

    // data
    this.client.on('data', data => this.handleMessage(data))
  })
}

StellarClient.prototype.configure = function () {
  return new Promise((resolve, reject) => {
    // join to all default rooms
    if (this.options.rooms) {
      this.options.rooms.forEach(room => this.send({ event: 'roomAdd', room }))
    }

    // request the connection details
    this.detailsView().then(details => {
      // save connection information
      this.id = details.data.id
      this.fingerprint = details.data.fingerprint
      this.rooms = details.data.rooms

{'fraction_non_alphanumeric': 0.18388268881391123, 'fraction_numerical': 0.0010381520892810796, 'mean_word_length': 2.369916921731526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, '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': '1491729', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2199, 'n_words': 542}