{"size": 994, "ext": "js", "max_stars_repo_path": "lib/stratumClient.js", "max_stars_repo_name": "Block-Lab/uPool", "max_stars_repo_stars_event_min_datetime": "2018-09-03T03:16:08.000Z", "max_stars_repo_stars_event_max_datetime": "2018-09-03T03:16:08.000Z", "max_issues_repo_path": "lib/stratumClient.js", "max_issues_repo_name": "Block-Lab/round", "max_issues_repo_issues_event_min_datetime": "2018-11-17T07:31:59.000Z", "max_issues_repo_issues_event_max_datetime": "2018-11-17T20:07:42.000Z", "max_forks_repo_path": "lib/stratumClient.js", "max_forks_repo_name": "Block-Lab/uPool", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 22.5909090909, "max_line_length": 84, "alphanum_fraction": 0.6076458753}
'use strict'
let net = require('net')
let EventEmitter = require('events')

class StratumClient extends EventEmitter {
  constructor (host, port) {
    super()

    this.host = host
    this.port = port
    this.socket = net.createConnection(port, host)

    this.socket.on('data', (data) => { this.emit('data', data) })
  }

  login (login, pass, rigId, id) {
    var params = { login: login, pass: pass, rigid: rigId, agent: 'kNight' }

    if (pass === undefined) { delete params.pass }
    if (rigId === undefined) { delete params.rigid }

    this.send('login', params, id)
  }

  getjob (id) {
    this.send('getjob', { id: id }, id)
  }

  submit (jobId, nonce, result, id) {
    this.send('submit', { id: id, job_id: jobId, nonce: nonce, result: result }, id)
  }

  send (method, params, id) {
    let data = JSON.stringify({ method: method, params: params, id: id })
    this.socket.write(data + '\n')
  }

  close () {
    this.socket.destroy()
  }
}

module.exports = StratumClient
