Class: Rack::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/process/version.rb,
lib/rack/process/net_string.rb,
lib/rack/process/worker/rack_input.rb,
lib/rack/process/worker.rb,
lib/rack/process/error.rb,
lib/rack/process/json.rb,
lib/rack/process.rb

Defined Under Namespace

Modules: JSON, NetString Classes: Error, Worker

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Process

Returns a new instance of Process.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
# File 'lib/rack/process.rb', line 6

def initialize config_path
  @config_path = config_path
  @config_path = ::File.join @config_path, "config.ru" if ::File.directory? @config_path
  @config_path = ::File.absolute_path @config_path

  raise ArgumentError, "Rackup file #{config_path} does not exist." unless ::File.exists? config_path

  at_exit { close }
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



4
5
6
# File 'lib/rack/process.rb', line 4

def config_path
  @config_path
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rack/process.rb', line 16

def call env
  write "request"
  write JSON.encode env.reject { |key| key[/\A(?:rack|async)/] }

  status = headers = body = nil

  while status.nil?
    case command = read
    when "status"
      status = read.to_i
    when "input"
      write "input"
      write env["rack.input"].read
    when "error"
      raise Error, "Rack process error: #{JSON.decode(read).inspect}"
    else
      raise Error, "Expecting status, got #{command}"
    end
  end

  while headers.nil?
    case command = read
    when "headers"
      headers = JSON.decode read
    when "input"
      write "input"
      write env["rack.input"].read
    when "error"
      raise Error, "Rack process error: #{JSON.decode(read).inspect}"
    else
      raise Error, "Expecting headers, got #{command}"
    end
  end

  body = Enumerator.new do |body|
    while command = read
      case command
      when "output"
        body << read
      when "input"
        write "input"
        write env['rack.input'].read
      when "done"
        break
      when "error"
        raise Error, "Rack process error: #{JSON.decode(read).inspect}"
      else
        raise Error, "Expecting output, got #{command}"
      end
    end
  end

  [status, headers, body]
end