<commit_before>require 'faye/websocket'
require 'pty'
require 'pry'

PTY.spawn("/bin/bash -li") do |stdout, stdin, pid|
  WebsocketServer = lambda do |env|
    @ws = Faye::WebSocket.new(env)

    def read_without_blocking(file)
      begin
        @ws.send(file.read_nonblock(1024 * 1024))
      rescue IO::WaitReadable
      end
    end

    @ws.on :open do |event|
      read_without_blocking(stdout)
    end

    @ws.on :message do |event|
      stdin.write(event.data)
      # hack for making sure tty buffer is filled
      sleep 0.01
      read_without_blocking(stdout)
    end

    @ws.on :close do |event|
      @ws = nil
    end

    @ws.rack_response
  end
end

Faye::WebSocket.load_adapter('thin')

run WebsocketServer<commit_msg>Optimize pty io<commit_after>require 'faye/websocket'
require 'pty'
require 'pry'

stdout, stdin, pid = PTY.spawn("/bin/bash -il")

WebsocketServer = lambda do |env|
  @ws = Faye::WebSocket.new(env)

  @ws.on :open do
    Thread.new do
      loop do
        begin
          output = stdout.readpartial(4096)
          @ws.send(output)
        rescue
          puts $!.inspect
        end
      end
    end
  end

  @ws.on :message do |event|
    stdin << event.data
  end

  @ws.on :close do |event|
    @ws = nil
  end

  @ws.rack_response
end

Faye::WebSocket.load_adapter('thin')

run WebsocketServer
{'fraction_non_alphanumeric': 0.11160714285714286, 'fraction_numerical': 0.011160714285714286, 'mean_word_length': 2.5301837270341205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2308352', 'n_tokens_mistral': 510, 'n_tokens_neox': 482, 'n_words': 118}