-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from Sharktheone/wasm-compile
Wasm compile
- Loading branch information
Showing
61 changed files
with
1,802 additions
and
661 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
pub use ureq; | ||
use crate::http::fetcher::RequestAgent; | ||
|
||
pub mod fetcher; | ||
pub mod headers; | ||
pub mod request; | ||
mod request_impl; | ||
pub mod response; | ||
|
||
pub type HttpError = <request_impl::RequestImpl as RequestAgent>::Error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub(crate) mod ureq_impl; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
mod wasm_impl; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
pub type RequestImpl = ureq_impl::UreqAgent; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
pub type RequestImpl = wasm_impl::WasmAgent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use ureq::Agent; | ||
|
||
use crate::http::fetcher::RequestAgent; | ||
use crate::http::headers::Headers; | ||
use crate::http::request::Request; | ||
use crate::http::response::Response; | ||
|
||
#[derive(Debug)] | ||
pub struct UreqAgent { | ||
agent: Agent, | ||
} | ||
|
||
impl From<Agent> for UreqAgent { | ||
fn from(value: Agent) -> Self { | ||
Self { agent: value } | ||
} | ||
} | ||
|
||
impl RequestAgent for UreqAgent { | ||
type Error = ureq::Error; | ||
|
||
fn new() -> Self { | ||
Agent::new().into() | ||
} | ||
|
||
async fn get(&self, url: &str) -> gosub_shared::types::Result<Response> { | ||
let response = self.agent.get(url).call()?; | ||
response.try_into() | ||
} | ||
|
||
async fn get_req(&self, _req: &Request) -> gosub_shared::types::Result<Response> { | ||
todo!() | ||
} | ||
} | ||
|
||
fn get_headers(response: &ureq::Response) -> Headers { | ||
let names = response.headers_names(); | ||
|
||
let mut headers = Headers::with_capacity(names.len()); | ||
|
||
for name in names { | ||
let header = response.header(&name).unwrap_or_default().to_string(); | ||
|
||
headers.set(name, header); | ||
} | ||
|
||
headers | ||
} | ||
|
||
impl TryFrom<ureq::Response> for Response { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: ureq::Response) -> std::result::Result<Self, Self::Error> { | ||
let body = Vec::with_capacity( | ||
value | ||
.header("Content-Length") | ||
.map(|s| s.parse().unwrap_or(0)) | ||
.unwrap_or(0), | ||
); | ||
|
||
let mut this = Self { | ||
status: value.status(), | ||
status_text: value.status_text().to_string(), | ||
version: value.http_version().to_string(), | ||
headers: get_headers(&value), | ||
body, | ||
cookies: Default::default(), | ||
}; | ||
|
||
value.into_reader().read_to_end(&mut this.body)?; | ||
|
||
Ok(this) | ||
} | ||
} |
Oops, something went wrong.