feat: update container

This commit is contained in:
2022-07-23 12:48:43 +08:00
parent f0c3aa9df5
commit 1c9669a4a7
2 changed files with 27 additions and 10 deletions

View File

@@ -49,10 +49,11 @@ pub fn do_fetch(params: &str) -> FnResult {
let mut has_user_agent = false;
if let Some(headers) = &options.headers {
for (k, v) in headers {
if k.to_lowercase() == "user-agent" {
let k = k.to_lowercase();
if k == "user-agent" {
has_user_agent = true;
}
request_builder = request_builder.header(k.to_string(), v.to_string());
request_builder = request_builder.header(k, v.to_string());
}
}
if !has_user_agent {

View File

@@ -3,6 +3,7 @@ extern crate core;
use anyhow::Result;
use serde_json::Value;
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
use crate::fn_common::FnResult;
mod fn_common;
mod fn_fetch;
@@ -10,10 +11,16 @@ mod fn_fetch;
wit_bindgen_wasmtime::export!("../container.wit");
#[derive(Default)]
pub struct MyContainer;
pub struct MyContainer {
fetch_invoked_count: u32,
}
impl container::Container for MyContainer {
fn fetch(&mut self, params: &str) -> String {
if self.fetch_invoked_count > 10 {
return FnResult::fail("Fetch invoke count cannot more than 10").to_json();
}
self.fetch_invoked_count += 1;
fn_fetch::do_fetch(params).to_json()
}
}
@@ -30,27 +37,36 @@ fn main() -> Result<()> {
)?;
let a = exports.eval_javascript(&mut store, r##"
function hi(name) { return "hi: " + name; }
// let a = [];
// a.push(fetch('https://hatter.ink/util/print_request.action'));
let a = [];
// for (let i = 0; i < 12; i++) {
a.push(fetch('https://hatter.ink/util/print_request.action'));
// }
a.push(fetch('https://hatter.ink/ip.action'));
a.push(fetch('https://hatter.ink/ip/ip.jsonp'));
a.push(fetch('https://hatter.ink/ip2.action'));
// for (let i = 0; i < 3; i++) { a.push(i); }
// a.push({
// id: 1, name: 'hatter'
// });
// a.push(hi('001'));
// a.push(hi('002'));
let a = fetch('https://hatter.ink/util/print_request.action');
JSON.stringify(a)
// a
"##);
let a = a.expect("error");
let a: Value = serde_json::from_str(&a).expect("error");
let a: Value = match serde_json::from_str(&a) {
Ok(a) => a,
Err(e) => panic!("ERROR: {}", e),
};
let a = match a {
Value::String(a) => a,
_ => panic!("error"),
_ => panic!("error: {}", a),
};
// println!(">>>>>>> {}", a);
let v: Value = serde_json::from_str(&a).expect("errorr");
let v: Value = match serde_json::from_str(&a) {
Ok(v) => v,
Err(e) => panic!("Parse json1 failed: {}, raw json:\n - {}", e, a),
};
let p = serde_json::to_string_pretty(&v).expect("error");
// println!("------- {}", v);
println!("{}", p);