feat: update container
This commit is contained in:
@@ -49,10 +49,11 @@ pub fn do_fetch(params: &str) -> FnResult {
|
|||||||
let mut has_user_agent = false;
|
let mut has_user_agent = false;
|
||||||
if let Some(headers) = &options.headers {
|
if let Some(headers) = &options.headers {
|
||||||
for (k, v) in 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;
|
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 {
|
if !has_user_agent {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ extern crate core;
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
||||||
|
use crate::fn_common::FnResult;
|
||||||
|
|
||||||
mod fn_common;
|
mod fn_common;
|
||||||
mod fn_fetch;
|
mod fn_fetch;
|
||||||
@@ -10,10 +11,16 @@ mod fn_fetch;
|
|||||||
wit_bindgen_wasmtime::export!("../container.wit");
|
wit_bindgen_wasmtime::export!("../container.wit");
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MyContainer;
|
pub struct MyContainer {
|
||||||
|
fetch_invoked_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
impl container::Container for MyContainer {
|
impl container::Container for MyContainer {
|
||||||
fn fetch(&mut self, params: &str) -> String {
|
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()
|
fn_fetch::do_fetch(params).to_json()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,27 +37,36 @@ fn main() -> Result<()> {
|
|||||||
)?;
|
)?;
|
||||||
let a = exports.eval_javascript(&mut store, r##"
|
let a = exports.eval_javascript(&mut store, r##"
|
||||||
function hi(name) { return "hi: " + name; }
|
function hi(name) { return "hi: " + name; }
|
||||||
// let a = [];
|
let a = [];
|
||||||
// a.push(fetch('https://hatter.ink/util/print_request.action'));
|
// 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); }
|
// for (let i = 0; i < 3; i++) { a.push(i); }
|
||||||
// a.push({
|
// a.push({
|
||||||
// id: 1, name: 'hatter'
|
// id: 1, name: 'hatter'
|
||||||
// });
|
// });
|
||||||
// a.push(hi('001'));
|
// a.push(hi('001'));
|
||||||
// a.push(hi('002'));
|
// a.push(hi('002'));
|
||||||
let a = fetch('https://hatter.ink/util/print_request.action');
|
|
||||||
JSON.stringify(a)
|
JSON.stringify(a)
|
||||||
// a
|
// a
|
||||||
"##);
|
"##);
|
||||||
let a = a.expect("error");
|
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 {
|
let a = match a {
|
||||||
Value::String(a) => a,
|
Value::String(a) => a,
|
||||||
_ => panic!("error"),
|
_ => panic!("error: {}", a),
|
||||||
};
|
};
|
||||||
|
|
||||||
// println!(">>>>>>> {}", a);
|
let v: Value = match serde_json::from_str(&a) {
|
||||||
let v: Value = serde_json::from_str(&a).expect("errorr");
|
Ok(v) => v,
|
||||||
|
Err(e) => panic!("Parse json1 failed: {}, raw json:\n - {}", e, a),
|
||||||
|
};
|
||||||
let p = serde_json::to_string_pretty(&v).expect("error");
|
let p = serde_json::to_string_pretty(&v).expect("error");
|
||||||
// println!("------- {}", v);
|
// println!("------- {}", v);
|
||||||
println!("{}", p);
|
println!("{}", p);
|
||||||
|
|||||||
Reference in New Issue
Block a user