<commit_before>net = require 'net'
child_process = require 'child_process'

{paths} = require '../../misc'
client = require '../client'

module.exports =

  freePort: ->
    new Promise (resolve) ->
      server = net.createServer()
      server.listen 0, '127.0.0.1', ->
        port = server.address().port
        server.close()
        resolve port

  createProc: (proc, obj = {}) ->
    obj.onExit = (f) ->
      proc.on 'exit', f
      {dispose: -> proc.removeListener('exit', f)}
    obj.stdin ?= proc.stdin
    obj.stdout ?= proc.stdout
    obj.stderr ?= proc.stderr
    obj

  socket: (proc, port) ->
    new Promise (resolve, reject) ->
      proc.stderr.on 'data', f = (data) ->
        if data.toString() == 'juno-msg-ready'
          proc.stderr.removeListener 'data', f
          resolve net.connect port
      proc.on 'exit', (code, status) ->
        reject [code, status]

  getUnix: (path, args) ->
    @freePort().then (port) =>
      proc = child_process.spawn path, [args..., paths.script('boot.jl'), port]

      @createProc proc,
        kill: -> proc.kill()
        interrupt: -> proc.kill 'SIGINT'
        socket: @socket proc, port
<commit_msg>make start-up communication more robust which fixes the start-up process on Windows<commit_after>net = require 'net'
child_process = require 'child_process'

{paths} = require '../../misc'
client = require '../client'

module.exports =

  freePort: ->
    new Promise (resolve) ->
      server = net.createServer()
      server.listen 0, '127.0.0.1', ->
        port = server.address().port
        server.close()
        resolve port

  createProc: (proc, obj = {}) ->
    obj.onExit = (f) ->
      proc.on 'exit', f
      {dispose: -> proc.removeListener('exit', f)}
    obj.stdin ?= proc.stdin
    obj.stdout ?= proc.stdout
    obj.stderr ?= proc.stderr
    obj

  socket: (proc, port) ->
    new Promise (resolve, reject) ->
      proc.stderr.on 'data', f = (data) ->
        if data.toString().indexOf('juno-msg-ready') != -1
          proc.stderr.removeListener 'data', f
          resolve net.connect port
      proc.on 'exit', (code, status) ->
        reject [code, status]

  getUnix: (path, args) ->
    @freePort().then (port) =>
      proc = child_process.spawn path, [args..., paths.script('boot.jl'), port]

      @createProc proc,
        kill: -> proc.kill()
        interrupt: -> proc.kill 'SIGINT'
        socket: @socket proc, port

{'fraction_non_alphanumeric': 0.16267547481420314, 'fraction_numerical': 0.006193228736581338, 'mean_word_length': 2.28319783197832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '652212', 'n_tokens_mistral': 778, 'n_tokens_neox': 760, 'n_words': 199}