elvwasm 1.0.0

Contains and collects the bitcode extension API for the Eluvio content fabric
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! elvwasm contains and collects the bitcode extension API for the Eluvio content fabric. <br>
//! The library is intended to be built as wasm and the resultant part uploaded to the content fabric.
//! The main entry point for each client module is implemented by [jpc] which automatically creates and dispatches
//! requests to the [BitcodeContext] <br>
//! Example
/*!
  ```rust
    extern crate elvwasm;
    extern crate serde_json;
    use serde_json::json;

    use elvwasm::{implement_bitcode_module, jpc, register_handler, BitcodeContext, ErrorKinds};

    implement_bitcode_module!("proxy", do_proxy);

    fn do_proxy(bcc: &mut elvwasm::BitcodeContext) -> CallResult {
        let http_p = &bcc.request.params.http;
        let qp = &http_p.query;
        bcc.log_debug(&format!("In DoProxy hash={} headers={:#?} query params={qp:#?}",&bcc.request.q_info.hash, &http_p.headers))?;
        let res = bcc.sqmd_get_json("/request_parameters")?;
        let mut meta_str: String = match String::from_utf8(res){
          Ok(m) => m,
          Err(e) => {return bcc.make_error_with_kind(ErrorKinds::Invalid(format!("failed to parse request params err = {e}")))}
        };
        meta_str = meta_str.replace("${API_KEY}", &qp["API_KEY"][0].to_string()).
          replace("${QUERY}", &qp["QUERY"][0].to_string()).
          replace("${CONTEXT}", &qp["CONTEXT"][0].to_string());
        bcc.log_debug(&format!("MetaData = {}", &meta_str))?;
        let req:serde_json::Map<String,serde_json::Value> = match serde_json::from_str::<serde_json::Map<String,serde_json::Value>>(&meta_str){
          Ok(m) => m,
          Err(e) => return bcc.make_error_with_kind(ErrorKinds::Invalid(format!("serde_json::from_str failed error = {e}"))),
        };
        let proxy_resp =  bcc.proxy_http(Some(json!({"request": req})))?;
        let proxy_resp_json:serde_json::Value = serde_json::from_str(std::str::from_utf8(&proxy_resp).unwrap_or("{}"))?;
        let client_response = serde_json::to_vec(&proxy_resp_json["result"])?;
        let id = &bcc.request.id;
        bcc.callback(200, "application/json", client_response.len())?;
        bcc.write_stream("fos", &client_response)?;
        bcc.make_success_json(&json!(
          {
              "headers" : "application/json",
              "body" : "SUCCESS",
              "result" : 0,
          }))
    }
  ```

  To Build binaries <br>
    *cargo build --all --features "host-wasm"* <br>
  To Build samples <br>
    *cd samples* <br>
    *cargo build --target wasm32-unknown-unknown* <br>
  <br>
  test <br>
    *target/debug/mock ./samples/target/wasm32-unknown-unknown/debug/deps/rproxy.wasm ./samples/fabric.json*
*/

extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate wapc_guest as guest;
#[macro_use(defer)]
extern crate scopeguard;

pub mod bccontext;
pub mod bccontext_core;
pub mod bccontext_error;
pub mod bccontext_ext;
pub mod bccontext_search;
pub mod bccontext_struct;

pub use self::bccontext::*;
pub use self::bccontext_core::*;
pub use self::bccontext_error::*;
pub use self::bccontext_ext::*;
pub use self::bccontext_search::*;
pub use self::bccontext_struct::*;

//use guest::console_log;

use std::str;

use guest::prelude::*;
use std::collections::HashMap;

use lazy_static::lazy_static;
use std::sync::Mutex;

#[derive(Clone)]
struct HandlerData<'a> {
    pub hf: HandlerFunction<'a>,
    pub req: Option<BitcodeContext>,
}

lazy_static! {
    static ref CALLMAP: Mutex<HashMap<String, HandlerData<'static>>> = Mutex::new(HashMap::new());
}

#[macro_export]
macro_rules! register_handlers {
  () => {};
  ($handler_name:literal, $handler_func:ident $(,$more_name:literal, $more_func:ident )*) => {

    register_handler($handler_name, $handler_func);
    register_handlers!($( $more_name, $more_func ),* );
  }
}

/// This macro delivers the required initializtion of the eluvio wasm module
/// In addition the macro also registers a handler of the form
/// ```ignore
/// fn fn_name<'s, 'r>(bcc: &'s mut elvwasm::BitcodeContext<'r>) -> CallResult
///
/// implement_bitcode_module!("proxy", do_proxy, "image", do_image);
/// fn do_proxy<'s, 'r>(bcc: &'s mut elvwasm::BitcodeContext<'r>) -> CallResult {
///   return bcc.make_success("SUCCESS");
/// }
/// fn do_image<'s, 'r>(bcc: &'s mut elvwasm::BitcodeContext<'r>) -> CallResult {
///   return bcc.make_success("SUCCESS");
/// }
/// ```
#[macro_export]
macro_rules! implement_bitcode_module {
  ($handler_name:literal, $handler_func:ident $(, $more_lit:literal, $more:ident)*) => {
    extern crate wapc_guest as guest;

    use guest::{register_function, CallResult, console_log};
    use std::panic;
    use std::io;
    use elvwasm::register_handlers;

    fn hook_impl(info: &std::panic::PanicInfo) {
      let _ = console_log(&format!("Panic is WASM!! {}", info));
    }
    #[no_mangle]
    pub extern "C" fn wapc_init() {
      register_handlers!($handler_name, $handler_func $(, $more_lit, $more)*);
      register_function("_JPC", jpc);
      panic::set_hook(Box::new(hook_impl));
    }
  }
}

// The following are mearly intended to verify internal consistency.  There are no actual calls made
// but the tests verify that the json parsing of the http message is correct
#[cfg(test)]
mod tests {

    macro_rules! output_raw_pointers {
        ($raw_ptr:ident, $raw_len:ident) => {
            unsafe {
                std::str::from_utf8(std::slice::from_raw_parts($raw_ptr, $raw_len))
                    .unwrap_or("unable to convert")
            }
        };
    }

    #[no_mangle]
    pub extern "C" fn __console_log(ptr: *const u8, len: usize) {
        let out_str = output_raw_pointers!(ptr, len);
        println!("console output : {}", out_str);
    }
    #[no_mangle]
    pub extern "C" fn __host_call(
        bd_ptr: *const u8,
        bd_len: usize,
        ns_ptr: *const u8,
        ns_len: usize,
        op_ptr: *const u8,
        op_len: usize,
        ptr: *const u8,
        len: usize,
    ) -> usize {
        let out_bd = output_raw_pointers!(bd_ptr, bd_len);
        let out_ns = output_raw_pointers!(ns_ptr, ns_len);
        let out_op = output_raw_pointers!(op_ptr, op_len);
        let out_ptr = output_raw_pointers!(ptr, len);
        println!(
            "host call bd = {} ns = {} op = {}, ptr={}",
            out_bd, out_ns, out_op, out_ptr
        );
        0
    }
    #[no_mangle]
    pub extern "C" fn __host_response(ptr: *const u8) {
        println!("host __host_response ptr = {:?}", ptr);
    }

    #[no_mangle]
    pub extern "C" fn __host_response_len() -> usize {
        println!("host __host_response_len");
        0
    }

    #[no_mangle]
    pub extern "C" fn __host_error_len() -> usize {
        println!("host __host_error_len");
        0
    }

    #[no_mangle]
    pub extern "C" fn __host_error(ptr: *const u8) {
        println!("host __host_error ptr = {:?}", ptr);
    }

    #[no_mangle]
    pub extern "C" fn __guest_response(ptr: *const u8, len: usize) {
        let out_resp = output_raw_pointers!(ptr, len);
        println!("host  __guest_response ptr = {}", out_resp);
    }

    #[no_mangle]
    pub extern "C" fn __guest_error(ptr: *const u8, len: usize) {
        let out_error = output_raw_pointers!(ptr, len);
        println!("host  __guest_error ptr = {}", out_error);
    }

    #[no_mangle]
    pub extern "C" fn __guest_request(op_ptr: *const u8, ptr: *const u8) {
        println!("host __guest_request op_ptr = {:?} ptr = {:?}", op_ptr, ptr);
    }

    // Note this useful idiom: importing names from outer (for mod tests) scope.
    pub use self::bccontext::*;
    pub use self::bccontext_struct::QList;
    use super::*;
    use serde_json::*;

    fn handler_for_test(bcc: &mut BitcodeContext) -> CallResult {
        bcc.make_success("DONE")
    }

    #[test]
    fn test_basic_http() {
        register_handler("testing", handler_for_test);
        let test_json = json!({
          "id" : "dummydummy",
          "jpc" : "1.0",
          "method" : "content",
          "params" : {
            "http" : {
              "path" : "/testing",
              "verb" : "GET",
            },
          },
          "qinfo" : {
            "qlib_id" : "idlib1234",
            "type" : "some_type",
          },
        });
        match serde_json::to_vec(&test_json) {
            Ok(x) => {
                let res = jpc(&x);
                match res {
                    Ok(_) => {}
                    Err(err) => {
                        panic!("failed test_http err = {:?}", err);
                    }
                }
            }
            Err(err) => {
                panic!("failed test_http err = {:?}", err);
            }
        };
    }

    #[test]
    fn test_basic_http_failure() {
        register_handler("test_handler", handler_for_test);
        // path missing
        let test_json = json!({
          "id" : "dummydummy",
          "jpc" : "1.0",
          "method" : "GET",
          "params" : {
            "http" : {
            },
          },
        });
        match serde_json::to_vec(&test_json) {
            Ok(x) => {
                let res = jpc(&x);
                match res {
                    Ok(k) => {
                        let mut res_json: serde_json::Map<String, serde_json::Value> =
                            serde_json::from_slice(&k).unwrap();
                        let mut err_json: serde_json::Map<String, serde_json::Value> =
                            serde_json::from_value(res_json["error"].take()).unwrap();
                        println!("{:?}", err_json);
                        assert_eq!(err_json["op"], 2);
                        let err_json_data: serde_json::Map<String, serde_json::Value> =
                            serde_json::from_value(err_json["data"].take()).unwrap();
                        assert_eq!(err_json_data["op"], 2);
                    }
                    Err(err) => {
                        panic!("failed test_http err = {:?}", err);
                    }
                }
            }
            Err(err) => {
                panic!("failed test_http err = {:?}", err);
            }
        };
    }
}

type HandlerFunction<'a> = fn(bcc: &'a mut BitcodeContext) -> CallResult;

/// register_handler adjusts the global static call map to associate a bitcode module with a path
/// this map is used by jpc to implement bitcode calls
#[no_mangle]
pub fn register_handler(name: &str, h: HandlerFunction<'static>) {
    let hd = HandlerData { hf: h, req: None };
    match CALLMAP.lock().as_mut() {
        Ok(x) => {
            x.insert(name.to_string(), hd);
        }
        Err(e) => console_log(&format!("MutexGuard unable to aquire lock, error = {e}")),
    };
}

#[cfg(test)]
pub fn console_log(s: &str) {
    println!("{}", s)
}

const ID_NOT_CALCULATED_YET: &str = "id not yet calculated";

fn do_bitcode(json_params: Request) -> CallResult {
    console_log("Parameters parsed");
    let split_path: Vec<&str> = json_params.params.http.path.as_str().split('/').collect();
    console_log(&format!("splitpath={split_path:?}"));

    let mut v_leaks: Vec<Box<BitcodeContext>> = Vec::<Box<BitcodeContext>>::new();

    let cm = match CALLMAP.lock() {
        Ok(c) => c,
        Err(e) => {
            return make_json_error(
                ErrorKinds::BadHttpParams(format!("unable to gain access to callmap: error = {e}")),
                ID_NOT_CALCULATED_YET,
            )
        }
    };
    // let mut element = 1;
    // if json_params.method == "content"{
    //   element = 0;
    // }
    let mut bind = cm.get(split_path[1]).into_iter();
    let cm_handler = match bind.find(|mut _x| true).as_mut() {
        Some(b) => b.to_owned(),
        None => {
            return Err(Box::new(ErrorKinds::Invalid(format!(
                "handler not found {}",
                split_path[1]
            ))))
        }
    };
    match cm_handler.req {
        Some(f) => {
            let id = f.request.id.to_string();
            let bcc = Box::new(f);
            let l = Box::leak(bcc);
            unsafe {
                v_leaks.push(Box::from_raw(l));
            }
            match (cm_handler.hf)(l) {
                Ok(o) => Ok(o),
                Err(e) => make_json_error(ErrorKinds::Other(e.to_string()), &id),
            }
        }
        None => {
            let bcc = BitcodeContext {
                request: json_params.clone(),
            };
            let id = bcc.request.id.clone();
            let l = Box::leak(Box::new(bcc));
            unsafe {
                v_leaks.push(Box::from_raw(l));
            }
            match (cm_handler.hf)(l) {
                Ok(o) => Ok(o),
                Err(e) => make_json_error(ErrorKinds::Other(e.to_string()), &id),
            }
        }
    }
}

/// jpc is the main entry point into a wasm bitcode for the web assembly procedure calls
/// this function will
/// # Steps
///   * parse the input for the appropriately formatted json
///   * construct a BitcodeContext from the json
///   * attempt to call the method using the incomming path
///   * return results to the caller
#[no_mangle]
pub fn jpc(_msg: &[u8]) -> CallResult {
    console_log("In jpc");
    let input_string = str::from_utf8(_msg)?;
    console_log(&format!("parameters = {input_string}"));
    let json_params: Request = match serde_json::from_str(input_string) {
        Ok(m) => m,
        Err(err) => {
            return make_json_error(
                ErrorKinds::Invalid(format!("parse failed for http error = {err}")),
                ID_NOT_CALCULATED_YET,
            );
        }
    };

    console_log("Request parsed");
    do_bitcode(json_params)
}