Skip to content

Commit

Permalink
Merge pull request #624 from Sharktheone/wasm-compile
Browse files Browse the repository at this point in the history
Wasm compile
  • Loading branch information
Sharktheone authored Oct 13, 2024
2 parents 0590669 + df7b9ad commit 388f295
Show file tree
Hide file tree
Showing 61 changed files with 1,802 additions and 661 deletions.
395 changes: 98 additions & 297 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ gosub_render_backend = { path = "./crates/gosub_render_backend", features = [] }
gosub_vello = { path = "./crates/gosub_vello", features = [] }
gosub_useragent = { path = "./crates/gosub_useragent", features = [] }
gosub_taffy = { path = "./crates/gosub_taffy", features = [] }
gosub_net = { path = "./crates/gosub_net", features = [] }
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
derive_more = { version = "1", features = ["from"] }
Expand All @@ -52,6 +53,7 @@ cookie = { version = "0.18.1", features = ["secure", "private"] }
url = "2.5.2"
nom = "7.1.3"
nom-trace = "0.2.1"
log = "0.4.22"

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
Expand All @@ -77,7 +79,6 @@ getrandom = { version = "0.2.15", features = ["js"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
gosub_v8 = { path = "./crates/gosub_v8", features = [] }
gosub_webexecutor = { path = "./crates/gosub_webexecutor", features = [] }
gosub_net = { path = "./crates/gosub_net", features = [] }
ureq = "2.10.1"
futures = "0.3.31"

Expand All @@ -94,3 +95,6 @@ codegen-units = 1

[lib]
crate-type = ["staticlib", "cdylib", "rlib"]

[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
dwarf-debug-info = true
2 changes: 0 additions & 2 deletions crates/gosub_css3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ anyhow = { version = "1.0.89", features = [] }
colors-transform = "0.2.11"
rand = "0.8.5"
itertools = "0.13.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
thiserror = "1.0.63"
Expand Down
7 changes: 3 additions & 4 deletions crates/gosub_html5/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ use std::collections::HashMap;
use std::io::Write;
use std::rc::Rc;

use log::warn;
use url::Url;

use crate::node::{HTML_NAMESPACE, MATHML_NAMESPACE, SVG_NAMESPACE};
use crate::parser::attr_replacements::{
MATHML_ADJUSTMENTS, SVG_ADJUSTMENTS_ATTRIBUTES, SVG_ADJUSTMENTS_TAGS, XML_ADJUSTMENTS,
Expand All @@ -27,6 +24,8 @@ use gosub_shared::traits::node::{ElementDataType, Node, QuirksMode};
use gosub_shared::traits::{Context, ParserConfig};
use gosub_shared::types::{ParseError, Result};
use gosub_shared::{timing_start, timing_stop};
use log::warn;
use url::Url;

mod attr_replacements;
pub mod errors;
Expand Down Expand Up @@ -4130,7 +4129,7 @@ where

/// Load and parse an external stylesheet by URL
#[cfg(target_arch = "wasm32")]
fn load_external_stylesheet(&self, _origin: CssOrigin, _url: Url) -> Option<CssStylesheet> {
fn load_external_stylesheet(&self, _origin: CssOrigin, _url: Url) -> Option<C::Stylesheet> {
None
}

Expand Down
19 changes: 14 additions & 5 deletions crates/gosub_net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ license = "MIT"
gosub_shared = { path = "../gosub_shared", features = [] }
gosub_config = { path = "../gosub_config", features = [] }
derive_more = { version = "1", features = ["from", "display"] }
thiserror = "1.0.64"
ureq = "2.10.1"
anyhow = "1.0.89"
thiserror = "1.0.61"
anyhow = "1.0.87"
log = "0.4.22"
domain-lookup-tree = "0.1"
hickory-resolver = "0.24.1"
simple_logger = "5.0.0"
cookie = { version = "0.18.1", features = ["secure", "private"] }
http = "1.0.0"
url = "2.5.2"



[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
hickory-resolver = "0.24.1"
ureq = "2.10.1"


[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.70", features = ["Headers", "Request", "RequestInit", "RequestMode", "Response", "Window"] }
js-sys = "0.3.70"
wasm-bindgen-futures = "0.4.43"
26 changes: 14 additions & 12 deletions crates/gosub_net/src/dns.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
mod cache;
mod local;
mod remote;

use crate::errors::Error;
use core::str::FromStr;
use derive_more::Display;
use gosub_config::config_store;
use gosub_shared::types::Result;
use log::{debug, info};
use std::net::IpAddr;
use std::time::{SystemTime, UNIX_EPOCH};

use derive_more::Display;
use log::{debug, info};

use gosub_config::{config, config_store};
use gosub_shared::types::Result;

use crate::errors::Error;

mod cache;
mod local;
mod remote;

/// A DNS entry is a mapping of a domain to zero or more IP address mapping
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DnsEntry {
Expand Down Expand Up @@ -181,14 +184,13 @@ impl Dns {

#[cfg(test)]
mod test {
use super::*;
use simple_logger::SimpleLogger;
use crate::dns::{Dns, ResolveType};
use std::time::Instant;

#[test]
fn resolver() {
// Add simple logger, if not possible, that's fine too
let _ = SimpleLogger::new().init();
// let _ = SimpleLogger::new().init();

let mut dns = Dns::new();

Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_net/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("ureq error")]
Request(#[from] Box<ureq::Error>),
Request(#[from] Box<crate::http::HttpError>),

#[error("io error: {0}")]
IO(#[from] std::io::Error),
Expand Down
5 changes: 4 additions & 1 deletion crates/gosub_net/src/http.rs
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;
38 changes: 27 additions & 11 deletions crates/gosub_net/src/http/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
use super::response::Response;
use crate::http::request::Request;
use anyhow::bail;
use gosub_shared::types::Result;
use std::error::Error;
use std::fmt::Debug;
use std::future::Future;
use url::{ParseError, Url};

use gosub_shared::types::Result;

use crate::http::request::Request;
use crate::http::request_impl::RequestImpl;

use super::response::Response;

pub trait RequestAgent: Debug {
type Error: Error;

fn new() -> Self;

fn get(&self, url: &str) -> impl Future<Output = Result<Response>>;

fn get_req(&self, req: &Request) -> impl Future<Output = Result<Response>>;
}

#[derive(Debug)]
pub struct Fetcher {
base_url: Url,
client: ureq::Agent,
client: RequestImpl,
}

impl Fetcher {
pub fn new(base: Url) -> Self {
Self {
base_url: base,
client: ureq::Agent::new(),
client: RequestImpl::new(),
}
}

pub fn get_url(&self, url: &Url) -> Result<Response> {
pub async fn get_url(&self, url: &Url) -> Result<Response> {
let scheme = url.scheme();

let resp = if scheme == "http" || scheme == "https" {
let response = self.client.get(url.as_str()).call()?;

response.try_into()?
self.client.get(url.as_str()).await?
} else if scheme == "file" {
let path = &url.as_str()[7..];

Expand All @@ -37,10 +53,10 @@ impl Fetcher {
Ok(resp)
}

pub fn get(&self, url: &str) -> Result<Response> {
pub async fn get(&self, url: &str) -> Result<Response> {
let url = self.parse_url(url)?;

self.get_url(&url)
self.get_url(&url).await
}

pub fn get_req(&self, _url: &Request) {
Expand Down
1 change: 1 addition & 0 deletions crates/gosub_net/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::http::headers::Headers;
use cookie::CookieJar;
use core::fmt::{Display, Formatter};

#[derive(Debug, Default, Clone)]
pub struct Request {
pub method: String,
pub uri: String,
Expand Down
11 changes: 11 additions & 0 deletions crates/gosub_net/src/http/request_impl.rs
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;
74 changes: 74 additions & 0 deletions crates/gosub_net/src/http/request_impl/ureq_impl.rs
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)
}
}
Loading

0 comments on commit 388f295

Please sign in to comment.