wasm: Add bytes method to wasm response (#694)
the bytes method was missing from the Response object of the wasm32 compilation target.
This commit is contained in:
committed by
Sean McArthur
parent
43f2ff083c
commit
b24b0be461
@@ -46,10 +46,10 @@ __internal_proxy_sys_no_cache = []
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
http = "0.1.15"
|
http = "0.1.15"
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
bytes = "0.4"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
base64 = "0.11"
|
base64 = "0.11"
|
||||||
bytes = "0.4"
|
|
||||||
encoding_rs = "0.8"
|
encoding_rs = "0.8"
|
||||||
futures-core-preview = { version = "=0.3.0-alpha.19" }
|
futures-core-preview = { version = "=0.3.0-alpha.19" }
|
||||||
futures-util-preview = { version = "=0.3.0-alpha.19" }
|
futures-util-preview = { version = "=0.3.0-alpha.19" }
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
use bytes::Bytes;
|
||||||
|
use js_sys::Uint8Array;
|
||||||
use http::{HeaderMap, StatusCode};
|
use http::{HeaderMap, StatusCode};
|
||||||
|
|
||||||
/// A Response to a submitted `Request`.
|
/// A Response to a submitted `Request`.
|
||||||
@@ -60,6 +62,22 @@ impl Response {
|
|||||||
Err(crate::error::decode("response.text isn't string"))
|
Err(crate::error::decode("response.text isn't string"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the response as bytes
|
||||||
|
pub async fn bytes(self) -> crate::Result<Bytes> {
|
||||||
|
let p = self.http.body().array_buffer()
|
||||||
|
.map_err(crate::error::wasm)
|
||||||
|
.map_err(crate::error::decode)?;
|
||||||
|
|
||||||
|
let buf_js = super::promise::<wasm_bindgen::JsValue>(p)
|
||||||
|
.await
|
||||||
|
.map_err(crate::error::decode)?;
|
||||||
|
|
||||||
|
let buffer = Uint8Array::new(&buf_js);
|
||||||
|
let mut bytes = vec![0; buffer.length() as usize];
|
||||||
|
buffer.copy_to(&mut bytes);
|
||||||
|
Ok(bytes.into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Response {
|
impl fmt::Debug for Response {
|
||||||
|
|||||||
Reference in New Issue
Block a user