Skip to content

Commit

Permalink
feat: optimizing wasm experience
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Oct 17, 2024
1 parent b0eff72 commit f2f669a
Show file tree
Hide file tree
Showing 30 changed files with 202 additions and 202 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.75.0
1.81.0
4 changes: 2 additions & 2 deletions secio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ rand = "0.8"
openssl = "0.10.25"
openssl-sys = "0.9"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
[target.'cfg(not(target_family = "wasm"))'.dependencies]
ring = "0.17"

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(target_family = "wasm")'.dependencies]
rand_core = { version = "0.6" }
getrandom = { version = "0.2", features = ["js"] }
sha2 = "0.10.0"
Expand Down
8 changes: 4 additions & 4 deletions secio/src/codec/hmac_compat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(test, not(unix)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

#[cfg(unix)]
pub use openssl_impl::*;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub use ring_impl::*;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use wasm_compat::*;

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions secio/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use bytes::BytesMut;
pub mod cipher;
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(not(ossl110), test, not(unix)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

/// Variant cipher which contains all possible stream ciphers
Expand Down Expand Up @@ -66,15 +66,15 @@ pub fn new_stream(t: cipher::CipherType, key: &[u8], mode: CryptoMode) -> BoxStr

/// Generate a specific Cipher with key and initialize vector
#[doc(hidden)]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub fn new_stream(t: cipher::CipherType, key: &[u8], mode: CryptoMode) -> BoxStreamCipher {
Box::new(ring_impl::RingAeadCipher::new(t, key, mode))
}

/// Generate a specific Cipher with key and initialize vector
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub fn new_stream(t: cipher::CipherType, key: &[u8], _mode: CryptoMode) -> BoxStreamCipher {
Box::new(wasm_compat::WasmCrypt::new(t, key))
}
Expand Down
8 changes: 4 additions & 4 deletions secio/src/dh_compat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(test, not(unix), not(ossl110)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use wasm_compat::*;

/// Possible key agreement algorithms.
Expand All @@ -19,7 +19,7 @@ pub enum KeyAgreement {

#[cfg(all(ossl110, unix))]
pub use openssl_impl::*;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub use ring_impl::*;
#[cfg(all(not(ossl110), unix))]
Expand Down
2 changes: 1 addition & 1 deletion secio/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl From<openssl::error::ErrorStack> for SecioError {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
impl From<ring::error::Unspecified> for SecioError {
fn from(_err: ring::error::Unspecified) -> SecioError {
SecioError::CryptoError
Expand Down
6 changes: 1 addition & 5 deletions secio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ impl Digest {
}

/// KeyProvider on ecdh procedure
#[cfg_attr(all(target_arch = "wasm32", feature = "async-trait"), async_trait::async_trait(?Send))]
#[cfg_attr(
all(not(target_arch = "wasm32"), feature = "async-trait"),
async_trait::async_trait
)]
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
pub trait KeyProvider: std::clone::Clone + Send + Sync + 'static {
/// Error
type Error: Into<crate::error::SecioError>;
Expand Down
8 changes: 4 additions & 4 deletions secio/src/sha256_compat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(test, not(unix)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

#[cfg(unix)]
pub use openssl_impl::*;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub use ring_impl::*;

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use wasm_compat::*;

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions secio/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const CHACHA20_POLY1305: &str = "CHACHA20_POLY1305";
const SHA_256: &str = "SHA256";
const SHA_512: &str = "SHA512";

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub(crate) const DEFAULT_AGREEMENTS_PROPOSITION: &str = "P-256,P-384,X25519";
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub(crate) const DEFAULT_AGREEMENTS_PROPOSITION: &str = "X25519";
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub(crate) const DEFAULT_CIPHERS_PROPOSITION: &str = "AES-128-GCM,AES-256-GCM,CHACHA20_POLY1305";
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub(crate) const DEFAULT_CIPHERS_PROPOSITION: &str = "CHACHA20_POLY1305";
pub(crate) const DEFAULT_DIGESTS_PROPOSITION: &str = "SHA256,SHA512";

Expand Down
14 changes: 6 additions & 8 deletions tentacle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ async-trait = "0.1"
log = "0.4"
bytes = "1.0.0"
thiserror = "1.0"
once_cell = "1.0"
nohash-hasher = "0.2"

parking_lot = { version = "0.12", optional = true }
tokio-tungstenite = { version = "0.21", optional = true }
tokio-tungstenite = { version = "0.24", optional = true }
futures-timer = { version = "3.0.2", optional = true }
async-std = { version = "1", features = ["unstable"], optional = true }
async-io = { version = "1", optional = true }
Expand All @@ -43,14 +42,14 @@ molecule = "0.8.0"
igd = { version = "0.12", optional = true }

#tls
tokio-rustls = { version = "0.24.0", optional = true }
tokio-rustls = { version = "0.26.0", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
[target.'cfg(not(target_family = "wasm"))'.dependencies]
# rand 0.8 not support wasm32
rand = "0.8"
socket2 = { version = "0.5.0", features = ["all"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(target_family = "wasm")'.dependencies]
js-sys = "0.3"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
Expand All @@ -69,12 +68,11 @@ winapi = { version = "0.3.7", features = [
[dev-dependencies]
env_logger = "0.6.0"
crossbeam-channel = "0.5"
systemstat = "0.1.3"
systemstat = "0.2"
futures-test = "0.3.5"
rustls-pemfile = "1"

[target.'cfg(unix)'.dev-dependencies]
nix = { version = "0.24.1", default-features = false, features = ["signal"] }
nix = { version = "0.29", default-features = false, features = ["signal"] }

[features]
default = ["tokio-runtime", "tokio-timer"]
Expand Down
4 changes: 2 additions & 2 deletions tentacle/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ where
/// then an attempt is made to register the local listener port into the mapping so that it can
/// receive the access request of the external network, and if the external ip of the route is not the public network,
/// Then do nothing
#[cfg(all(not(target_arch = "wasm32"), feature = "upnp"))]
#[cfg(all(not(target_family = "wasm"), feature = "upnp"))]
#[cfg_attr(docsrs, doc(cfg(feature = "upnp")))]
pub fn upnp(mut self, enable: bool) -> Self {
self.config.upnp = enable;
Expand Down Expand Up @@ -214,7 +214,7 @@ where
/// ## Note
///
/// User use `listen(2)` or `connect(2)` on this closure will cause abnormal behavior
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub fn tcp_config<F>(mut self, f: F) -> Self
where
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static,
Expand Down
4 changes: 2 additions & 2 deletions tentacle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
#![deny(missing_docs)]
#![cfg_attr(
target_arch = "wasm32",
target_family = "wasm",
allow(dead_code, unused_variables, unused_imports)
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
Expand Down Expand Up @@ -151,7 +151,7 @@ mod channel;
#[allow(missing_docs)]
pub mod runtime;

#[cfg(all(not(target_arch = "wasm32"), feature = "upnp"))]
#[cfg(all(not(target_family = "wasm"), feature = "upnp"))]
pub(crate) mod upnp;

use std::{fmt, ops::AddAssign};
Expand Down
6 changes: 3 additions & 3 deletions tentacle/src/runtime/async_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub use async_std::task::{spawn, spawn_blocking, yield_now, JoinHandle};

pub fn block_in_place<F, R>(f: F) -> R
Expand All @@ -8,10 +8,10 @@ where
f()
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub use os::*;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
mod os {
use crate::{
runtime::CompatStream2,
Expand Down
10 changes: 10 additions & 0 deletions tentacle/src/runtime/generic_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ impl Interval {
period,
}
}

pub fn set_missed_tick_behavior(&mut self, _behavior: MissedTickBehavior) {}
}

impl Stream for Interval {
Expand All @@ -37,6 +39,14 @@ impl Stream for Interval {
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MissedTickBehavior {
#[default]
Burst,
Delay,
Skip,
}

pub fn interval(period: Duration) -> Interval {
assert!(period > Duration::new(0, 0), "`period` must be non-zero.");

Expand Down
18 changes: 9 additions & 9 deletions tentacle/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
//! this module contains async timer, compat async read/write between futures and tokio, spawn on any runtime
//!
#[cfg(all(not(target_arch = "wasm32"), feature = "async-runtime"))]
#[cfg(all(not(target_family = "wasm"), feature = "async-runtime"))]
mod async_runtime;
#[cfg(any(
feature = "generic-timer",
all(target_arch = "wasm32", feature = "wasm-timer")
all(target_family = "wasm", feature = "wasm-timer")
))]
mod generic_timer;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-runtime"))]
#[cfg(all(not(target_family = "wasm"), feature = "tokio-runtime"))]
mod tokio_runtime;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
mod wasm_runtime;

#[cfg(all(not(target_arch = "wasm32"), feature = "async-runtime"))]
#[cfg(all(not(target_family = "wasm"), feature = "async-runtime"))]
pub use async_runtime::*;
#[cfg(any(
feature = "generic-timer",
all(target_arch = "wasm32", feature = "wasm-timer")
all(target_family = "wasm", feature = "wasm-timer")
))]
pub use generic_timer::*;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-runtime"))]
#[cfg(all(not(target_family = "wasm"), feature = "tokio-runtime"))]
pub use tokio_runtime::*;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use wasm_runtime::*;

#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-runtime"))]
#[cfg(all(not(target_family = "wasm"), feature = "tokio-runtime"))]
pub use tokio::io::{split, ReadHalf, WriteHalf};

#[cfg(not(feature = "tokio-runtime"))]
Expand Down
8 changes: 6 additions & 2 deletions tentacle/src/runtime/tokio_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio::net::TcpSocket as TokioTcp;
#[cfg(feature = "tokio-timer")]
pub use time::{interval, Interval};
#[cfg(feature = "tokio-timer")]
pub use tokio::time::{sleep as delay_for, timeout, Sleep as Delay, Timeout};
pub use tokio::time::{sleep as delay_for, timeout, MissedTickBehavior, Sleep as Delay, Timeout};

#[cfg(feature = "tokio-timer")]
mod time {
Expand All @@ -26,14 +26,18 @@ mod time {
task::{Context, Poll},
time::Duration,
};
use tokio::time::{interval as inner_interval, Interval as Inner};
use tokio::time::{interval as inner_interval, Interval as Inner, MissedTickBehavior};

pub struct Interval(Inner);

impl Interval {
pub fn new(period: Duration) -> Self {
Self(inner_interval(period))
}

pub fn set_missed_tick_behavior(&mut self, behavior: MissedTickBehavior) {
self.0.set_missed_tick_behavior(behavior);
}
}

impl Stream for Interval {
Expand Down
Loading

0 comments on commit f2f669a

Please sign in to comment.