Class: Rack::WASI::IncomingRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/wasi/incoming_handler.rb

Overview

resource incoming-request

method: func() -> method;
path-with-query: func() -> option<string>;
scheme: func() -> option<scheme>;
authority: func() -> option<string>;
headers: func() -> headers;
consume: func() -> result<incoming-body>;

github.com/WebAssembly/wasi-http/blob/d163277b8684483a2334363ca1492ca298ea526d/wit/types.wit#L274

Instance Method Summary collapse

Constructor Details

#initialize(js_object_id) ⇒ IncomingRequest

We use a reference to the global JS object to access the incoming request data.



28
29
30
# File 'lib/rack/wasi/incoming_handler.rb', line 28

def initialize(js_object_id)
  @js_object = ::JS.global[js_object_id]
end

Instance Method Details

#authorityObject



48
49
50
51
52
53
# File 'lib/rack/wasi/incoming_handler.rb', line 48

def authority
  auth = @js_object.call(:authority)
  if auth.typeof == "string"
    auth.to_s
  end
end

#consumeObject

NOTE: Currently, we only support text bodies



65
66
67
68
69
70
# File 'lib/rack/wasi/incoming_handler.rb', line 65

def consume
  body = @js_object.call(:consume)
  if body.typeof == "string"
    body.to_s
  end
end

#headersObject



55
56
57
58
59
60
61
62
# File 'lib/rack/wasi/incoming_handler.rb', line 55

def headers
  entries = ::JS.global[:Object].entries(@js_object.call(:headers))
  entries.to_a.each.with_object({}) do |entry_val, acc|
    key, val = entry_val.to_a
    acc[key.to_s] = val.to_s
    acc
  end
end

#methodObject



32
# File 'lib/rack/wasi/incoming_handler.rb', line 32

def method = @js_object.call(:method).to_s

#path_with_queryObject



34
35
36
37
38
39
# File 'lib/rack/wasi/incoming_handler.rb', line 34

def path_with_query
  path = @js_object.call(:pathWithQuery)
  if path.typeof == "string"
    path.to_s
  end
end

#schemeObject



41
42
43
44
45
46
# File 'lib/rack/wasi/incoming_handler.rb', line 41

def scheme
  sch = @js_object.call(:scheme)
  if sch.typeof == "string"
    sch.to_s
  end
end