Sha256: 29630691ab3016b74f805e0509b7bbcb529b40a4894478f6ad1d6e3dca30133f

Contents?: true

Size: 1.73 KB

Versions: 21

Compression:

Stored size: 1.73 KB

Contents

require 'json'
require 'tempfile'
require 'securerandom'
require 'open3'

#Interactive pipe for testing.
#Creates a server that routes $stdin into if_dispatch and $stdout to int_dispatch.

class InteractiveServer
  def initialize app_js_path
    @app_js = File.read app_js_path
    inject_dispatch_shims
  end

  #Will take over any remaining IO
  def begin_pipe
    tmp = Tempfile.new(SecureRandom.hex)
    path = tmp.path
    tmp.close!
    File.write path, @app_js

    p = Open3.popen3 "boojs #{path}" do |inp, out, err, t|
      pid = t[:pid]
      begin
        loop do
          results = select([out, err, STDIN], [])
          if results[0].include? err
            err_msg = err.readline
            $stderr.puts err_msg
            #exit 1 unless err_msg =~ /[debug]/
          end

          if results[0].include? STDIN
            begin
              q = gets.strip
              inp.puts "if_dispatch(JSON.parse('#{q}'))"
            rescue Errno::EIO
              #Can't say anything here, we don't have a pipe
              exit 1
            rescue NoMethodError
              exit 1
            end
          end

          if results[0].include? out
            res = out.readline
            $stdout.puts res
            $stdout.flush
          end
        end
      ensure
        begin
          Process.kill :INT, pid
        rescue Errno::ESRCH
        end
      end
    end
  end

  #JS Functions#####################################################
  #Make calls to if_dispatch go to $stdout, make $stdin call int_dispatch
  def inject_dispatch_shims
    @app_js << %{
      function int_dispatch(q) {
        system.stdout.writeLine(JSON.stringify(q));
      }
    }
  end
  ##################################################################
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
flok-0.0.36 app/drivers/chrome/pipe.rb
flok-0.0.35 app/drivers/chrome/pipe.rb
flok-0.0.34 app/drivers/chrome/pipe.rb
flok-0.0.33 app/drivers/chrome/pipe.rb
flok-0.0.32 app/drivers/chrome/pipe.rb
flok-0.0.31 app/drivers/chrome/pipe.rb
flok-0.0.30 app/drivers/chrome/pipe.rb
flok-0.0.29 app/drivers/chrome/pipe.rb
flok-0.0.28 app/drivers/chrome/pipe.rb
flok-0.0.27 app/drivers/chrome/pipe.rb
flok-0.0.26 app/drivers/chrome/pipe.rb
flok-0.0.25 app/drivers/chrome/pipe.rb
flok-0.0.24 app/drivers/chrome/pipe.rb
flok-0.0.23 app/drivers/chrome/pipe.rb
flok-0.0.21 app/drivers/chrome/pipe.rb
flok-0.0.20 app/drivers/chrome/pipe.rb
flok-0.0.19 app/drivers/chrome/pipe.rb
flok-0.0.18 app/drivers/chrome/pipe.rb
flok-0.0.17 app/drivers/chrome/pipe.rb
flok-0.0.16 app/drivers/chrome/pipe.rb