diff --git a/src/wasm/body.rs b/src/wasm/js/body.rs similarity index 96% rename from src/wasm/body.rs rename to src/wasm/js/body.rs index 751cf586ed..bc4fd1cd4b 100644 --- a/src/wasm/body.rs +++ b/src/wasm/js/body.rs @@ -221,7 +221,7 @@ mod tests { let js_req = web_sys::Request::new_with_str_and_init("", &init) .expect("could not create JS request"); let text_promise = js_req.text().expect("could not get text promise"); - let text = crate::wasm::promise::(text_promise) + let text = crate::wasm::js::promise::(text_promise) .await .expect("could not get request body as text"); @@ -243,7 +243,7 @@ mod tests { let js_req = web_sys::Request::new_with_str_and_init("", &init) .expect("could not create JS request"); let text_promise = js_req.text().expect("could not get text promise"); - let text = crate::wasm::promise::(text_promise) + let text = crate::wasm::js::promise::(text_promise) .await .expect("could not get request body as text"); @@ -269,7 +269,7 @@ mod tests { let array_buffer_promise = js_req .array_buffer() .expect("could not get array_buffer promise"); - let array_buffer = crate::wasm::promise::(array_buffer_promise) + let array_buffer = crate::wasm::js::promise::(array_buffer_promise) .await .expect("could not get request body as array buffer"); @@ -297,7 +297,7 @@ mod tests { let array_buffer_promise = js_req .array_buffer() .expect("could not get array_buffer promise"); - let array_buffer = crate::wasm::promise::(array_buffer_promise) + let array_buffer = crate::wasm::js::promise::(array_buffer_promise) .await .expect("could not get request body as array buffer"); diff --git a/src/wasm/client.rs b/src/wasm/js/client.rs similarity index 100% rename from src/wasm/client.rs rename to src/wasm/js/client.rs diff --git a/src/wasm/js/mod.rs b/src/wasm/js/mod.rs new file mode 100644 index 0000000000..e99fb11fbe --- /dev/null +++ b/src/wasm/js/mod.rs @@ -0,0 +1,53 @@ +use wasm_bindgen::JsCast; +use web_sys::{AbortController, AbortSignal}; + +mod body; +mod client; +/// TODO +#[cfg(feature = "multipart")] +pub mod multipart; +mod request; +mod response; + +pub use self::body::Body; +pub use self::client::{Client, ClientBuilder}; +pub use self::request::{Request, RequestBuilder}; +pub use self::response::Response; + +async fn promise(promise: js_sys::Promise) -> Result +where + T: JsCast, +{ + use wasm_bindgen_futures::JsFuture; + + let js_val = JsFuture::from(promise).await.map_err(crate::error::wasm)?; + + js_val + .dyn_into::() + .map_err(|_js_val| "promise resolved to unexpected type".into()) +} + +/// A guard that cancels a fetch request when dropped. +struct AbortGuard { + ctrl: AbortController, +} + +impl AbortGuard { + fn new() -> crate::Result { + Ok(AbortGuard { + ctrl: AbortController::new() + .map_err(crate::error::wasm) + .map_err(crate::error::builder)?, + }) + } + + fn signal(&self) -> AbortSignal { + self.ctrl.signal() + } +} + +impl Drop for AbortGuard { + fn drop(&mut self) { + self.ctrl.abort(); + } +} diff --git a/src/wasm/multipart.rs b/src/wasm/js/multipart.rs similarity index 97% rename from src/wasm/multipart.rs rename to src/wasm/js/multipart.rs index 9b5b4c951d..3d29a95b40 100644 --- a/src/wasm/multipart.rs +++ b/src/wasm/js/multipart.rs @@ -377,7 +377,7 @@ mod tests { let form_data_promise = js_req.form_data().expect("could not get form_data promise"); - let form_data = crate::wasm::promise::(form_data_promise) + let form_data = crate::wasm::js::promise::(form_data_promise) .await .expect("could not get body as form data"); @@ -387,7 +387,7 @@ mod tests { assert_eq!(text_file.type_(), text_file_type); let text_promise = text_file.text(); - let text = crate::wasm::promise::(text_promise) + let text = crate::wasm::js::promise::(text_promise) .await .expect("could not get text body as text"); assert_eq!( @@ -408,7 +408,7 @@ mod tests { assert_eq!(string, string_content); let binary_array_buffer_promise = binary_file.array_buffer(); - let array_buffer = crate::wasm::promise::(binary_array_buffer_promise) + let array_buffer = crate::wasm::js::promise::(binary_array_buffer_promise) .await .expect("could not get request body as array buffer"); diff --git a/src/wasm/request.rs b/src/wasm/js/request.rs similarity index 100% rename from src/wasm/request.rs rename to src/wasm/js/request.rs diff --git a/src/wasm/response.rs b/src/wasm/js/response.rs similarity index 99% rename from src/wasm/response.rs rename to src/wasm/js/response.rs index 47a90d04d4..9a00f7356d 100644 --- a/src/wasm/response.rs +++ b/src/wasm/js/response.rs @@ -5,7 +5,7 @@ use http::{HeaderMap, StatusCode}; use js_sys::Uint8Array; use url::Url; -use crate::wasm::AbortGuard; +use crate::wasm::js::AbortGuard; #[cfg(feature = "stream")] use wasm_bindgen::JsCast; diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index e99fb11fbe..0bac9fe1a6 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -1,53 +1,2 @@ -use wasm_bindgen::JsCast; -use web_sys::{AbortController, AbortSignal}; - -mod body; -mod client; -/// TODO -#[cfg(feature = "multipart")] -pub mod multipart; -mod request; -mod response; - -pub use self::body::Body; -pub use self::client::{Client, ClientBuilder}; -pub use self::request::{Request, RequestBuilder}; -pub use self::response::Response; - -async fn promise(promise: js_sys::Promise) -> Result -where - T: JsCast, -{ - use wasm_bindgen_futures::JsFuture; - - let js_val = JsFuture::from(promise).await.map_err(crate::error::wasm)?; - - js_val - .dyn_into::() - .map_err(|_js_val| "promise resolved to unexpected type".into()) -} - -/// A guard that cancels a fetch request when dropped. -struct AbortGuard { - ctrl: AbortController, -} - -impl AbortGuard { - fn new() -> crate::Result { - Ok(AbortGuard { - ctrl: AbortController::new() - .map_err(crate::error::wasm) - .map_err(crate::error::builder)?, - }) - } - - fn signal(&self) -> AbortSignal { - self.ctrl.signal() - } -} - -impl Drop for AbortGuard { - fn drop(&mut self) { - self.ctrl.abort(); - } -} +pub mod js; +pub use js::*;