Skip to content

Commit

Permalink
Add multipart for WASM (seanmonstar#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox authored Jul 8, 2020
1 parent af9fc5c commit a800202
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ url = "2.1"
bytes = "0.5"
serde = "1.0"
serde_urlencoded = "0.6.1"
mime_guess = "2.0"
## json
serde_json = { version = "1.0", optional = true }

Expand All @@ -79,7 +80,6 @@ hyper = { version = "0.13.4", default-features = false, features = ["tcp"] }
lazy_static = "1.4"
log = "0.4"
mime = "0.3.7"
mime_guess = "2.0"
percent-encoding = "2.1"
tokio = { version = "0.2.5", default-features = false, features = ["tcp", "time"] }
pin-project-lite = "0.1.1"
Expand Down Expand Up @@ -141,6 +141,9 @@ features = [
"RequestMode",
"Response",
"Window",
"FormData",
"Blob",
"BlobPropertyBag",
]

[[example]]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,5 @@ if_hyper! {
if_wasm! {
mod wasm;

pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, Response};
pub use self::wasm::{multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response};
}
51 changes: 44 additions & 7 deletions src/wasm/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::multipart::Form;
/// dox
use bytes::Bytes;
use std::fmt;
use js_sys::Uint8Array;
use wasm_bindgen::JsValue;

/// The body of a `Request`.
///
Expand All @@ -9,39 +12,73 @@ use std::fmt;
/// passing many things (like a string or vector of bytes).
///
/// [builder]: ./struct.RequestBuilder.html#method.body
pub struct Body(Bytes);
pub struct Body {
inner: Inner,
}

enum Inner {
Bytes(Bytes),
Multipart(Form),
}

impl Body {
pub(crate) fn bytes(&self) -> &Bytes {
&self.0
pub(crate) fn to_js_value(&self) -> crate::Result<JsValue> {
match &self.inner {
Inner::Bytes(body_bytes) => {
let body_bytes: &[u8] = body_bytes.as_ref();
let body_array: Uint8Array = body_bytes.into();
let js_value: &JsValue = body_array.as_ref();
Ok(js_value.to_owned())
}
Inner::Multipart(form) => {
let form_data = form.to_form_data()?;
let js_value: &JsValue = form_data.as_ref();
Ok(js_value.to_owned())
}
}
}

#[inline]
pub(crate) fn from_form(f: Form) -> Body {
Self {
inner: Inner::Multipart(f),
}
}
}

impl From<Bytes> for Body {
#[inline]
fn from(bytes: Bytes) -> Body {
Body(bytes)
Body {
inner: Inner::Bytes(bytes),
}
}
}

impl From<Vec<u8>> for Body {
#[inline]
fn from(vec: Vec<u8>) -> Body {
Body(vec.into())
Body {
inner: Inner::Bytes(vec.into()),
}
}
}

impl From<&'static [u8]> for Body {
#[inline]
fn from(s: &'static [u8]) -> Body {
Body(Bytes::from_static(s))
Body {
inner: Inner::Bytes(Bytes::from_static(s)),
}
}
}

impl From<String> for Body {
#[inline]
fn from(s: String) -> Body {
Body(s.into())
Body {
inner: Inner::Bytes(s.into()),
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/wasm/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use http::Method;
use js_sys::Uint8Array;
use std::future::Future;
use wasm_bindgen::UnwrapThrowExt as _;
use url::Url;
Expand Down Expand Up @@ -133,9 +132,7 @@ async fn fetch(req: Request) -> crate::Result<Response> {
}

if let Some(body) = req.body() {
let body_bytes: &[u8] = body.bytes();
let body_array: Uint8Array = body_bytes.into();
init.body(Some(&body_array.into()));
init.body(Some(&body.to_js_value()?.as_ref().as_ref()));
}

let js_req = web_sys::Request::new_with_str_and_init(req.url().as_str(), &init)
Expand Down
2 changes: 2 additions & 0 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod body;
mod client;
mod request;
mod response;
/// TODO
pub mod multipart;

pub use self::body::Body;
pub use self::client::{Client, ClientBuilder};
Expand Down
Loading

0 comments on commit a800202

Please sign in to comment.