Class: Streamdal::HostFunc

Inherits:
Object
  • Object
show all
Defined in:
lib/hostfunc.rb

Instance Method Summary collapse

Constructor Details

#initialize(kv) ⇒ HostFunc

This class holds methods that are called by wasm modules



9
10
11
# File 'lib/hostfunc.rb', line 9

def initialize(kv)
  @kv = kv
end

Instance Method Details

#http_request(caller, ptr, len) ⇒ Object

http_request performs a http request on behalf of a wasm module since WASI cannot talk sockets



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/hostfunc.rb', line 37

def http_request(caller, ptr, len)
  data = caller.export("memory").to_memory.read(ptr, len)

  # Read request from memory and decode into HttpRequest
  req = Streamdal::Protos::HttpRequest.decode(data)

  # Attempt to make HTTP request
  # On error, return a mock 400 response with the error as the body
  begin
    response = _make_http_request(req)
  rescue => e
    wasm_resp = Streamdal::Protos::HttpResponse.new
    wasm_resp.code = 400
    wasm_resp.body = "Unable to execute HTTP request: #{e}"
    return wasm_resp
  end

  # Successful request, build the proto from the httparty response
  wasm_resp = Streamdal::Protos::HttpResponse.new
  wasm_resp.code = response.code
  wasm_resp.body = response.body
  wasm_resp.headers = Google::Protobuf::Map.new(:string, :string, {})

  # Headers can have multiple values, but we just want a map[string]string here for simplicity
  # The client can pase by the delimiter ";" if needed.
  response.headers.each do |k, values|
    wasm_resp.headers[k] = values.kind_of?(Array) ? values.join("; ") : values
  end

  # Write the HttpResponse proto message to WASM memory
  # The .wasm module will read/decode this data internally
  write_to_memory(caller, wasm_resp)
end

#kv_exists(caller, ptr, len) ⇒ Object

kv_exists is a host function that is used to check if a key exists in the KV store



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hostfunc.rb', line 15

def kv_exists(caller, ptr, len)

  data = caller.export("memory").to_memory.read(ptr, len)

  # Read request from memory and decode into HttpRequest
  req = Streamdal::Protos::KVStep.decode(data)

  exists = @kv.exists(req.key)

  msg = exists ? "Key '#{req.key}' exists" : "Key #{req.key} does not exist"

  status = exists ? :KV_STATUS_SUCCESS : :KV_STATUS_FAILURE

  wasm_resp = Streamdal::Protos::KVStepResponse.new
  wasm_resp.status = status
  wasm_resp.message = msg

  write_to_memory(caller, wasm_resp)
end