diff --git a/Cargo.lock b/Cargo.lock index 2a93c751..3018a01c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -761,9 +761,9 @@ dependencies = [ [[package]] name = "curl" -version = "0.4.43" +version = "0.4.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d855aeef205b43f65a5001e0997d81f8efca7badad4fad7d897aa7f0d0651f" +checksum = "509bd11746c7ac09ebd19f0b17782eae80aadee26237658a6b4808afb5c11a22" dependencies = [ "curl-sys", "libc", @@ -776,9 +776,9 @@ dependencies = [ [[package]] name = "curl-sys" -version = "0.4.55+curl-7.83.1" +version = "0.4.56+curl-7.83.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23734ec77368ec583c2e61dd3f0b0e5c98b93abe6d2a004ca06b91dd7e3e2762" +checksum = "6093e169dd4de29e468fa649fbae11cdcd5551c81fe5bf1b0677adad7ef3d26f" dependencies = [ "cc", "libc", @@ -1470,9 +1470,14 @@ dependencies = [ "async-std", "async-trait", "cfg-if", + "futures 0.3.21", "http-types", "isahc", + "js-sys", "log", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", ] [[package]] diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 5660bab6..70edf82d 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -14,13 +14,13 @@ license = "Apache-2.0" [features] default = ["parallel", "kv-tikv", "kv-echodb", "kv-yokudb", "scripting", "http"] -parallel = ["executor"] -kv-tikv = ["tikv"] -kv-echodb = ["echodb"] -kv-indxdb = ["indxdb"] +parallel = ["dep:executor"] +kv-tikv = ["dep:tikv"] +kv-echodb = ["dep:echodb"] +kv-indxdb = ["dep:indxdb"] kv-yokudb = [] -scripting = ["js", "executor"] -http = ["surf"] +scripting = ["dep:js", "dep:executor"] +http = ["dep:surf"] [dependencies] argon2 = "0.4.1" @@ -53,7 +53,6 @@ serde = { version = "1.0.139", features = ["derive"] } sha-1 = "0.10.0" sha2 = "0.10.2" storekey = "0.3.0" -surf = { version = "2.3.2", optional = true } thiserror = "1.0.31" tikv = { version = "0.1.0", package = "tikv-client", optional = true } trice = "0.1.0" @@ -62,3 +61,9 @@ uuid = { version = "1.1.2", features = ["serde", "v4"] } [dev-dependencies] tokio = { version = "1.20.0", features = ["macros"] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +surf = { version = "2.3.2", optional = true, default-features = false, features = ["encoding", "wasm-client"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +surf = { version = "2.3.2", optional = true, default-features = false, features = ["encoding", "curl-client"] } diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index be4a8af5..79cc9c81 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -59,6 +59,10 @@ pub enum Error { message: String, }, + /// Remote HTTP request functions are not enabled + #[error("Remote HTTP request functions are not enabled")] + HttpDisabled, + /// There was an error with the provided JavaScript code #[error("Problem with embedded script function. {message}")] InvalidScript { @@ -285,6 +289,7 @@ impl From> for Error { } } +#[cfg(feature = "http")] impl From for Error { fn from(e: surf::Error) -> Error { Error::Http(e.to_string()) diff --git a/lib/src/fnc/http.rs b/lib/src/fnc/http.rs index ecf98510..2a8e1f3b 100644 --- a/lib/src/fnc/http.rs +++ b/lib/src/fnc/http.rs @@ -1,14 +1,44 @@ use crate::ctx::Context; use crate::err::Error; -use crate::fnc::util::http; use crate::sql::object::Object; use crate::sql::value::Value; +#[cfg(not(feature = "http"))] +pub async fn head(_: &Context<'_>, _: Vec) -> Result { + Err(Error::HttpDisabled) +} + +#[cfg(not(feature = "http"))] +pub async fn get(_: &Context<'_>, _: Vec) -> Result { + Err(Error::HttpDisabled) +} + +#[cfg(not(feature = "http"))] +pub async fn put(_: &Context<'_>, _: Vec) -> Result { + Err(Error::HttpDisabled) +} + +#[cfg(not(feature = "http"))] +pub async fn post(_: &Context<'_>, _: Vec) -> Result { + Err(Error::HttpDisabled) +} + +#[cfg(not(feature = "http"))] +pub async fn patch(_: &Context<'_>, _: Vec) -> Result { + Err(Error::HttpDisabled) +} + +#[cfg(not(feature = "http"))] +pub async fn delete(_: &Context<'_>, _: Vec) -> Result { + Err(Error::HttpDisabled) +} + +#[cfg(feature = "http")] pub async fn head(_: &Context<'_>, mut args: Vec) -> Result { match args.len() { 2 => match args.remove(0) { Value::Strand(uri) => match args.remove(0) { - Value::Object(opt) => http::head(uri, opt).await, + Value::Object(opt) => crate::fnc::util::http::head(uri, opt).await, _ => Err(Error::InvalidArguments { name: String::from("http::head"), message: String::from("The second argument should be an object."), @@ -20,7 +50,7 @@ pub async fn head(_: &Context<'_>, mut args: Vec) -> Result }), }, 1 => match args.remove(0) { - Value::Strand(uri) => http::head(uri, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::head(uri, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::head"), message: String::from("The first argument should be a string."), @@ -33,11 +63,12 @@ pub async fn head(_: &Context<'_>, mut args: Vec) -> Result } } +#[cfg(feature = "http")] pub async fn get(_: &Context<'_>, mut args: Vec) -> Result { match args.len() { 2 => match args.remove(0) { Value::Strand(uri) => match args.remove(0) { - Value::Object(opt) => http::get(uri, opt).await, + Value::Object(opt) => crate::fnc::util::http::get(uri, opt).await, _ => Err(Error::InvalidArguments { name: String::from("http::get"), message: String::from("The second argument should be an object."), @@ -49,7 +80,7 @@ pub async fn get(_: &Context<'_>, mut args: Vec) -> Result }), }, 1 => match args.remove(0) { - Value::Strand(uri) => http::get(uri, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::get(uri, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::get"), message: String::from("The first argument should be a string."), @@ -62,11 +93,12 @@ pub async fn get(_: &Context<'_>, mut args: Vec) -> Result } } +#[cfg(feature = "http")] pub async fn put(_: &Context<'_>, mut args: Vec) -> Result { match args.len() { 3 => match (args.remove(0), args.remove(0)) { (Value::Strand(uri), val) => match args.remove(0) { - Value::Object(opts) => http::put(uri, val, opts).await, + Value::Object(opts) => crate::fnc::util::http::put(uri, val, opts).await, _ => Err(Error::InvalidArguments { name: String::from("http::put"), message: String::from("The third argument should be an object."), @@ -78,14 +110,14 @@ pub async fn put(_: &Context<'_>, mut args: Vec) -> Result }), }, 2 => match (args.remove(0), args.remove(0)) { - (Value::Strand(uri), val) => http::put(uri, val, Object::default()).await, + (Value::Strand(uri), val) => crate::fnc::util::http::put(uri, val, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::put"), message: String::from("The first argument should be a string."), }), }, 1 => match args.remove(0) { - Value::Strand(uri) => http::put(uri, Value::Null, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::put(uri, Value::Null, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::put"), message: String::from("The first argument should be a string."), @@ -98,11 +130,12 @@ pub async fn put(_: &Context<'_>, mut args: Vec) -> Result } } +#[cfg(feature = "http")] pub async fn post(_: &Context<'_>, mut args: Vec) -> Result { match args.len() { 3 => match (args.remove(0), args.remove(0)) { (Value::Strand(uri), val) => match args.remove(0) { - Value::Object(opts) => http::post(uri, val, opts).await, + Value::Object(opts) => crate::fnc::util::http::post(uri, val, opts).await, _ => Err(Error::InvalidArguments { name: String::from("http::post"), message: String::from("The third argument should be an object."), @@ -114,14 +147,14 @@ pub async fn post(_: &Context<'_>, mut args: Vec) -> Result }), }, 2 => match (args.remove(0), args.remove(0)) { - (Value::Strand(uri), val) => http::post(uri, val, Object::default()).await, + (Value::Strand(uri), val) => crate::fnc::util::http::post(uri, val, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::post"), message: String::from("The first argument should be a string."), }), }, 1 => match args.remove(0) { - Value::Strand(uri) => http::post(uri, Value::Null, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::post(uri, Value::Null, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::post"), message: String::from("The first argument should be a string."), @@ -134,11 +167,12 @@ pub async fn post(_: &Context<'_>, mut args: Vec) -> Result } } +#[cfg(feature = "http")] pub async fn patch(_: &Context<'_>, mut args: Vec) -> Result { match args.len() { 3 => match (args.remove(0), args.remove(0)) { (Value::Strand(uri), val) => match args.remove(0) { - Value::Object(opts) => http::patch(uri, val, opts).await, + Value::Object(opts) => crate::fnc::util::http::patch(uri, val, opts).await, _ => Err(Error::InvalidArguments { name: String::from("http::patch"), message: String::from("The third argument should be an object."), @@ -150,14 +184,14 @@ pub async fn patch(_: &Context<'_>, mut args: Vec) -> Result match (args.remove(0), args.remove(0)) { - (Value::Strand(uri), val) => http::patch(uri, val, Object::default()).await, + (Value::Strand(uri), val) => crate::fnc::util::http::patch(uri, val, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::patch"), message: String::from("The first argument should be a string."), }), }, 1 => match args.remove(0) { - Value::Strand(uri) => http::patch(uri, Value::Null, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::patch(uri, Value::Null, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::patch"), message: String::from("The first argument should be a string."), @@ -170,11 +204,12 @@ pub async fn patch(_: &Context<'_>, mut args: Vec) -> Result, mut args: Vec) -> Result { match args.len() { 2 => match args.remove(0) { Value::Strand(uri) => match args.remove(0) { - Value::Object(opt) => http::delete(uri, opt).await, + Value::Object(opt) => crate::fnc::util::http::delete(uri, opt).await, _ => Err(Error::InvalidArguments { name: String::from("http::delete"), message: String::from("The second argument should be an object."), @@ -186,7 +221,7 @@ pub async fn delete(_: &Context<'_>, mut args: Vec) -> Result match args.remove(0) { - Value::Strand(uri) => http::delete(uri, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::delete(uri, Object::default()).await, _ => Err(Error::InvalidArguments { name: String::from("http::delete"), message: String::from("The first argument should be a string."), diff --git a/lib/src/fnc/util/mod.rs b/lib/src/fnc/util/mod.rs index 2cd099a5..d3ca8f56 100644 --- a/lib/src/fnc/util/mod.rs +++ b/lib/src/fnc/util/mod.rs @@ -1,4 +1,6 @@ pub mod geo; -pub mod http; pub mod math; pub mod string; + +#[cfg(feature = "http")] +pub mod http;