{"size": 811, "ext": "js", "max_stars_repo_path": "protocol.js", "max_stars_repo_name": "kasatani/native-messaging", "max_stars_repo_stars_event_min_datetime": "2018-12-09T19:29:57.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-05T18:11:43.000Z", "max_issues_repo_path": "protocol.js", "max_issues_repo_name": "kasatani/native-messaging", "max_issues_repo_issues_event_min_datetime": "2019-03-26T14:39:50.000Z", "max_issues_repo_issues_event_max_datetime": "2020-02-18T02:04:31.000Z", "max_forks_repo_path": "protocol.js", "max_forks_repo_name": "kasatani/native-messaging", "max_forks_repo_forks_event_min_datetime": "2019-01-02T19:23:55.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-15T07:20:36.000Z"}
{"max_stars_count": 32, "max_issues_count": 6, "max_forks_count": 12, "avg_line_length": 20.7948717949, "max_line_length": 49, "alphanum_fraction": 0.6189889026}

module.exports = (handleMessage) => {

  process.stdin.on('readable', () => {
    var input = []
    var chunk
    while (chunk = process.stdin.read()) {
      input.push(chunk)
    }
    input = Buffer.concat(input)

    var msgLen = input.readUInt32LE(0)
    var dataLen = msgLen + 4

    if (input.length >= dataLen) {
      var content = input.slice(4, dataLen)
      var json = JSON.parse(content.toString())
      handleMessage(json)
    }
  })

  function sendMessage (msg) {
    var buffer = Buffer.from(JSON.stringify(msg))

    var header = Buffer.alloc(4)
    header.writeUInt32LE(buffer.length, 0)

    var data = Buffer.concat([header, buffer])
    process.stdout.write(data)
  }

  process.on('uncaughtException', (err) => {
    sendMessage({error: err.toString()})
  })

  return sendMessage

}
