<commit_before>const { BetterEvents } = require('better-events')
const { StringDecoder } = require('string_decoder')

/**
 * Class representing a connection to the daemon.
 * @class
 */
class Connection extends BetterEvents {
  /**
   * Create a new connection to the daemon.
   * @param {*} socket 
   */
  constructor(socket) {
    super()

    this.socket = socket
    this._message = ''

    const decoder = new StringDecoder()

    socket.on('data', async chunk => {
      this._message += decoder.write(chunk)

      this._parse()
    })

    socket.once('end', async () => {
      this._message += decoder.end()
      this._parse()
    })
  }

  /**
   * Parses this._message and emits the "message" event if one was received.
   */
  _parse() {
    const i = this._message.indexOf('\n')
    if (i === -1) {
      return
    }

    const msg = this._message.substr(0, i)
    this._message = this._message.substr(i + 1)

    try {
      const obj = JSON.parse(msg)
      this.emit('message', obj)
    } catch (error) {
      this.emit('error', error)
    }

    this._parse()
  }

  /**
   * Writes a message to the socket.
   * @param {*} obj - The object to transmit.
   */
  sendMessage(obj) {
    if (!obj.type) {
      throw new Error('Messages must have a type.')
    }

    this.socket.write(JSON.stringify(obj) + '\n')
  }

  get isDestroyed() {
    return this.socket.destroyed
  }
}

module.exports = Connection<commit_msg>fix wite after end errors<commit_after>const { BetterEvents } = require('better-events')
const { StringDecoder } = require('string_decoder')

/**
 * Class representing a connection to the daemon.
 * @class
 */
class Connection extends BetterEvents {
  /**
   * Create a new connection to the daemon.
   * @param {*} socket 
   */
  constructor(socket) {
    super()

    this.socket = socket
    this._message = ''

    const decoder = new StringDecoder()

    socket.on('data', async chunk => {
      this._message += decoder.write(chunk)

      this._parse()
    })

    socket.once('end', async () => {
      this._message += decoder.end()
      this._parse()
    })
  }

  get isDestroyed() {
    return this.socket.destroyed
  }

  /**
   * Parses this._message and emits the "message" event if one was received.
   */
  _parse() {
    const i = this._message.indexOf('\n')
    if (i === -1) {
      return
    }

    const msg = this._message.substr(0, i)
    this._message = this._message.substr(i + 1)

    try {
      const obj = JSON.parse(msg)
      this.emit('message', obj)
    } catch (error) {
      this.emit('error', error)
    }

    this._parse()
  }

  /**
   * Writes a message to the socket. Returns true if the message was written to the socket.
   * @param {*} obj - The object to transmit.
   * @returns {boolean}
   */
  sendMessage(obj) {
    if (!obj.type) {
      throw new Error('Messages must have a type.')
    }

    if (this.isDestroyed) {
      return false
    }

    this.socket.write(JSON.stringify(obj) + '\n')
    return true
  }
}

module.exports = Connection
{'fraction_non_alphanumeric': 0.14281013491280026, 'fraction_numerical': 0.0019743336623889436, 'mean_word_length': 2.3702882483370287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4703548', 'n_tokens_mistral': 984, 'n_tokens_neox': 946, 'n_words': 249}