Skip to content

Commit

Permalink
Introduce unstable, incomplete WASM support
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Sep 26, 2019
1 parent 6413a43 commit 932defd
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 78 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ matrix:
#- rust: nightly
# env: FEATURES="--features socks"

# trust-dns
#- rust: stable
#- rust: nightly
# env: FEATURES="--features trust-dns"

# wasm
- name: "WASM"
env: TARGET=wasm32-unknown-unknown
rust: nightly
install: rustup target add "$TARGET"
script: cargo check --target "$TARGET"

# android
#- rust: stable
- rust: nightly
Expand Down
63 changes: 40 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,34 @@ publish = false
[package.metadata.docs.rs]
all-features = true

[features]
default = ["default-tls"]

tls = []

default-tls = ["hyper-tls", "native-tls", "tls", "tokio-tls"]
default-tls-vendored = ["default-tls", "native-tls/vendored"]

#rustls-tls = ["hyper-rustls", "tokio-rustls", "webpki-roots", "rustls", "tls"]

blocking = ["futures-channel-preview", "futures-util-preview/io"]

cookies = ["cookie_crate", "cookie_store"]

gzip = ["async-compression"]

#trust-dns = ["trust-dns-resolver"]

[dependencies]
http = "0.1.15"
url = "2.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
base64 = "0.10"
bytes = "0.4"
encoding_rs = "0.8"
futures-core-preview = { version = "=0.3.0-alpha.18" }
futures-util-preview = { version = "=0.3.0-alpha.18" }
http = "0.1.15"
http-body = "=0.2.0-alpha.1"
hyper = "=0.13.0-alpha.2"
log = "0.4"
Expand All @@ -32,13 +53,11 @@ mime_guess = "2.0"
percent-encoding = "2.1"
tokio = { version = "=0.2.0-alpha.5", default-features = false, features = ["rt-full", "tcp"] }
tokio-executor = "=0.2.0-alpha.5"
url = "2.1"
uuid = { version = "0.7", features = ["v4"] }
time = "0.1.42"

# TODO: candidates for optional features


serde = "1.0"
serde_json = "1.0"
serde_urlencoded = "0.6.1"
Expand Down Expand Up @@ -72,36 +91,34 @@ async-compression = { version = "0.1.0-alpha.4", default-features = false, featu
## trust-dns
#trust-dns-resolver = { version = "0.11", optional = true }

[dev-dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
env_logger = "0.6"
hyper = { version = "=0.13.0-alpha.2", features = ["unstable-stream"] }
serde = { version = "1.0", features = ["derive"] }
libflate = "0.1"
doc-comment = "0.3"
tokio-fs = { version = "=0.2.0-alpha.5" }

[features]
default = ["default-tls"]

tls = []

default-tls = ["hyper-tls", "native-tls", "tls", "tokio-tls"]
default-tls-vendored = ["default-tls", "native-tls/vendored"]

# re-enable CI also
#rustls-tls = ["hyper-rustls", "tokio-rustls", "webpki-roots", "rustls", "tls"]

blocking = ["futures-channel-preview", "futures-util-preview/io"]

cookies = ["cookie_crate", "cookie_store"]

gzip = ["async-compression"]

#trust-dns = ["trust-dns-resolver"]

[target.'cfg(windows)'.dependencies]
winreg = "0.6"

# wasm

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.25"
wasm-bindgen = "0.2.48"
wasm-bindgen-futures = { version = "", features = ["futures_0_3"] }

[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3.25"
features = [
"Headers",
"Request",
"RequestInit",
"Response",
"Window",
]

[[example]]
name = "blocking"
path = "examples/blocking.rs"
Expand Down
9 changes: 8 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(target_arch = "wasm32", allow(unused))]
use std::error::Error as StdError;
use std::fmt;
use std::io;
Expand All @@ -12,7 +13,7 @@ pub struct Error {
inner: Box<Inner>,
}

type BoxError = Box<dyn StdError + Send + Sync>;
pub(crate) type BoxError = Box<dyn StdError + Send + Sync>;

struct Inner {
kind: Kind,
Expand Down Expand Up @@ -213,6 +214,12 @@ pub(crate) fn url_bad_scheme(url: Url) -> Error {
Error::new(Kind::Builder, Some("URL scheme is not allowed")).with_url(url)
}

if_wasm! {
pub(crate) fn wasm(js_val: wasm_bindgen::JsValue) -> BoxError {
format!("{:?}", js_val).into()
}
}

// io::Error helpers

#[allow(unused)]
Expand Down
16 changes: 9 additions & 7 deletions src/into_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ impl<'a> PolyfillTryInto for &'a String {
}
}

pub(crate) fn expect_uri(url: &Url) -> hyper::Uri {
url.as_str()
.parse()
.expect("a parsed Url should always be a valid Uri")
}
if_hyper! {
pub(crate) fn expect_uri(url: &Url) -> http::Uri {
url.as_str()
.parse()
.expect("a parsed Url should always be a valid Uri")
}

pub(crate) fn try_uri(url: &Url) -> Option<hyper::Uri> {
url.as_str().parse().ok()
pub(crate) fn try_uri(url: &Url) -> Option<http::Uri> {
url.as_str().parse().ok()
}
}

#[cfg(test)]
Expand Down
114 changes: 67 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,60 +175,32 @@
////! - **trust-dns**: Enables a trust-dns async resolver instead of default
////! threadpool using `getaddrinfo`.

#[cfg(test)]
#[macro_use]
extern crate doc_comment;
macro_rules! if_wasm {
($($item:item)*) => {$(
#[cfg(target_arch = "wasm32")]
$item
)*}
}

#[cfg(test)]
doctest!("../README.md");
macro_rules! if_hyper {
($($item:item)*) => {$(
#[cfg(not(target_arch = "wasm32"))]
$item
)*}
}

pub use hyper::header;
pub use hyper::Method;
pub use hyper::{StatusCode, Version};
pub use url::ParseError as UrlError;
pub use http::header;
pub use http::Method;
pub use http::{StatusCode, Version};
pub use url::Url;

pub use self::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
//pub use self::body::Body;
//pub use self::client::{Client, ClientBuilder};
pub use self::error::{Error, Result};
pub use self::into_url::IntoUrl;
pub use self::proxy::Proxy;
pub use self::redirect::{RedirectAction, RedirectAttempt, RedirectPolicy};
//pub use self::request::{Request, RequestBuilder};
//pub use self::response::Response;
#[cfg(feature = "tls")]
pub use self::tls::{Certificate, Identity};

// this module must be first because of the `try_` macro
// universal mods
#[macro_use]
mod error;

mod async_impl;
#[cfg(feature = "blocking")]
pub mod blocking;
mod connect;
#[cfg(feature = "cookies")]
pub mod cookie;
//#[cfg(feature = "trust-dns")]
//mod dns;
mod into_url;
mod proxy;
mod redirect;
#[cfg(feature = "tls")]
mod tls;

//pub mod multipart;

#[doc(hidden)]
#[deprecated(note = "types moved to top of crate")]
pub mod r#async {
pub use crate::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
}
pub use self::error::{Error, Result};
pub use self::into_url::IntoUrl;

/// Shortcut method to quickly make a `GET` request.
///
Expand Down Expand Up @@ -274,8 +246,56 @@ fn _assert_impls() {
assert_send::<Request>();
assert_send::<RequestBuilder>();

assert_send::<Response>();
#[cfg(not(target_arch = "wasm32"))]
{
assert_send::<Response>();
}

assert_send::<Error>();
assert_sync::<Error>();
}

if_hyper! {
#[cfg(test)]
#[macro_use]
extern crate doc_comment;

#[cfg(test)]
doctest!("../README.md");

pub use self::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
pub use self::proxy::Proxy;
pub use self::redirect::{RedirectAction, RedirectAttempt, RedirectPolicy};
#[cfg(feature = "tls")]
pub use self::tls::{Certificate, Identity};


mod async_impl;
#[cfg(feature = "blocking")]
pub mod blocking;
mod connect;
#[cfg(feature = "cookies")]
pub mod cookie;
//#[cfg(feature = "trust-dns")]
//mod dns;
mod proxy;
mod redirect;
#[cfg(feature = "tls")]
mod tls;

#[doc(hidden)]
#[deprecated(note = "types moved to top of crate")]
pub mod r#async {
pub use crate::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
}
}

if_wasm! {
mod wasm;

pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, Response};
}
3 changes: 3 additions & 0 deletions src/wasm/body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// dox
#[derive(Debug)]
pub struct Body(());
Loading

0 comments on commit 932defd

Please sign in to comment.