<commit_before>require! <[net mkdirp async fs path byline]>
{DBG, ERR, WARN, INFO} = global.get-logger __filename
{elem-index} = require \prelude-ls

class DomainSocket
  (@name, @full-path, @app, @helpers) ->
    self = @
    @connections = []
    server = @server = net.create-server (c) -> return self.incoming-connection c
    server.on \listen, -> DBG "#{name.cyan} (#{full-path.cyan}) is listening"
    server.on \end, -> INFO "#{name.cyan} closed"
    server.on \error, (err) -> ERR err, "#{name.cyan} unexpected error"

  start: (done) ->
    self = @
    {name, full-path, server} = self
    DBG "[#{name.cyan}] enter"
    dir = path.dirname full-path
    err0 <- mkdirp dir
    return done "[#{name.cyan}] failed to create dir #{dir}, err: #{err0}" if err0?
    DBG "[#{name.cyan}] successfully create #{dir}"
    err1, stats <- fs.stat full-path
    return done "[#{name.cyan}] #{full-path} exists but not a domain socket file" if (not err1?) and (not stats.is-socket!)
    fs.unlink-sync full-path unless err1?
    DBG "[#{name.cyan}] successfully cleanup previous domain socket" if err?
    err2 <- server.listen full-path
    return done "[#{name.cyan}] failed to create domain socket, err: #{err2}" if err2?
    INFO "listening #{full-path.cyan}"
    return done!

  incoming-connection: (c) ->
    self = @
    {name, helpers, connections} = self
    {line-emitter-currying} = helpers
    {remote-address, remote-family, remote-port} = c
    DBG "#{name.cyan} incoming-connection: #{remote-address}, #{remote-family}, #{remote-port}"
    connections.push c
    f = line-emitter-currying name, \from-peer, {}
    r = byline.create-stream c
    r.on \data, f
    c.on \end, ->
      idx = connections |> elem-index c
      connections.splice idx, 1 if idx?
      DBG "#{name.cyan} #{remote-address} disconnected"
    return

  write-line: (line) ->
    {connections} = @
    INFO "line: #{line}"
    for c in connections
      c.write "#{line}\n"


class Manager
  (@sockets, @socket-map) -> return

  send-line: (name, line) ->
    s = @socket-map[name]
    return unless s?
    return s.write-line line


<commit_msg>lower down verbose level of unix-sock server<commit_after>require! <[net mkdirp async fs path byline]>
{DBG, ERR, WARN, INFO} = global.get-logger __filename
{elem-index} = require \prelude-ls

class DomainSocket
  (@name, @full-path, @app, @helpers) ->
    self = @
    @connections = []
    server = @server = net.create-server (c) -> return self.incoming-connection c
    server.on \listen, -> DBG "#{name.cyan} (#{full-path.cyan}) is listening"
    server.on \end, -> INFO "#{name.cyan} closed"
    server.on \error, (err) -> ERR err, "#{name.cyan} unexpected error"

  start: (done) ->
    self = @
    {name, full-path, server} = self
    DBG "[#{name.cyan}] enter"
    dir = path.dirname full-path
    err0 <- mkdirp dir
    return done "[#{name.cyan}] failed to create dir #{dir}, err: #{err0}" if err0?
    DBG "[#{name.cyan}] successfully create #{dir}"
    err1, stats <- fs.stat full-path
    return done "[#{name.cyan}] #{full-path} exists but not a domain socket file" if (not err1?) and (not stats.is-socket!)
    fs.unlink-sync full-path unless err1?
    DBG "[#{name.cyan}] successfully cleanup previous domain socket" if err?
    err2 <- server.listen full-path
    return done "[#{name.cyan}] failed to create domain socket, err: #{err2}" if err2?
    INFO "listening #{full-path.cyan}"
    return done!

  incoming-connection: (c) ->
    self = @
    {name, helpers, connections} = self
    {line-emitter-currying} = helpers
    {remote-address, remote-family, remote-port} = c
    DBG "#{name.cyan} incoming-connection: #{remote-address}, #{remote-family}, #{remote-port}"
    connections.push c
    f = line-emitter-currying name, \from-peer, {}
    r = byline.create-stream c
    r.on \data, f
    c.on \end, ->
      idx = connections |> elem-index c
      connections.splice idx, 1 if idx?
      DBG "#{name.cyan} #{remote-address} disconnected"
    return

  write-line: (line) ->
    {connections} = @
    DBG "line: #{line}"
    for c in connections
      c.write "#{line}\n"


class Manager
  (@sockets, @socket-map) -> return

  send-line: (name, line) ->
    s = @socket-map[name]
    return unless s?
    return s.write-line line



{'fraction_non_alphanumeric': 0.16038175046554934, 'fraction_numerical': 0.004655493482309125, 'mean_word_length': 2.9567219152854514, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '790857', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1444, 'n_words': 432}