Skip to content

Commit

Permalink
Add WASM support
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Sep 2, 2023
1 parent 62d2c8a commit 00dca91
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
34 changes: 9 additions & 25 deletions interop-tests/src/arch.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::Boxed;
use libp2p::PeerId;

// Native re-exports
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use native::{build_swarm, init_logger, sleep, Instant, RedisClient};

// Wasm re-exports
#[cfg(target_arch = "wasm32")]
pub(crate) use wasm::{build_swarm, init_logger, sleep, swarm_builder, Instant, RedisClient};

type BoxedTransport = Boxed<(PeerId, StreamMuxerBox)>;
pub(crate) use wasm::{build_swarm, init_logger, sleep, Instant, RedisClient};

#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod native {
Expand All @@ -29,8 +23,6 @@ pub(crate) mod native {

use crate::{from_env, Muxer, SecProtocol, Transport};

use super::BoxedTransport;

pub(crate) type Instant = std::time::Instant;

pub(crate) fn init_logger() {
Expand Down Expand Up @@ -154,15 +146,14 @@ pub(crate) mod native {
pub(crate) mod wasm {
use anyhow::{bail, Result};
use futures::future::{BoxFuture, FutureExt};
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::identity::Keypair;
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder};
use libp2p::PeerId;
use libp2p::swarm::{NetworkBehaviour, Swarm};
use libp2p::Transport as _;
use std::time::Duration;

use crate::{BlpopRequest, Transport};

use super::BoxedTransport;

pub(crate) type Instant = instant::Instant;

pub(crate) fn init_logger() {
Expand All @@ -174,18 +165,19 @@ pub(crate) mod wasm {
futures_timer::Delay::new(duration).boxed()
}

pub(crate) fn build_swarm(
pub(crate) async fn build_swarm<B: NetworkBehaviour>(
ip: &str,
transport: Transport,
behaviour_constructor: FnOnce(Keypair) -> B,
) -> Result<(BoxedTransport, String)> {
behaviour_constructor: impl FnOnce(&Keypair) -> B,
) -> Result<(Swarm<B>, String)> {
if let Transport::Webtransport = transport {
let swarm = libp2p::SwarmBuilder::with_new_identity()
.with_wasm_executor()
.with_wasm_bindgen()
.with_other_transport(|key| {
libp2p::webtransport_websys::Transport::new(
libp2p::webtransport_websys::Config::new(key),
)
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
})?
.with_behaviour(behaviour_constructor)?
.build();
Expand All @@ -195,14 +187,6 @@ pub(crate) mod wasm {
}
}

pub(crate) fn swarm_builder<TBehaviour: NetworkBehaviour>(
transport: BoxedTransport,
behaviour: TBehaviour,
peer_id: PeerId,
) -> SwarmBuilder<TBehaviour> {
SwarmBuilder::with_wasm_executor(transport, behaviour, peer_id)
}

pub(crate) struct RedisClient(String);

impl RedisClient {
Expand Down
35 changes: 35 additions & 0 deletions libp2p/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ impl SwarmBuilder<NoProviderSpecified, ProviderPhase> {
phase: TcpPhase {},
}
}

#[cfg(feature = "wasm-bindgen")]
pub fn with_wasm_bindgen(
self,
) -> SwarmBuilder<WasmBindgen, OtherTransportPhase<impl AuthenticatedMultiplexedTransport>>
{
SwarmBuilder {
keypair: self.keypair,
phantom: PhantomData,
phase: OtherTransportPhase {
transport: libp2p_core::transport::dummy::DummyTransport::new(),
},
}
}
}

pub struct TcpPhase {}
Expand Down Expand Up @@ -1328,6 +1342,24 @@ impl<T: AuthenticatedMultiplexedTransport, B: NetworkBehaviour>
}
}

#[cfg(feature = "wasm-bindgen")]
impl<T: AuthenticatedMultiplexedTransport, B: NetworkBehaviour>
SwarmBuilder<WasmBindgen, SwarmPhase<T, B>>
{
pub fn build(self) -> libp2p_swarm::Swarm<B> {
SwarmBuilder {
phase: BuildPhase {
behaviour: self.phase.behaviour,
transport: self.phase.transport,
swarm_config: libp2p_swarm::SwarmConfig::with_wasm_executor(),
},
keypair: self.keypair,
phantom: PhantomData::<WasmBindgen>,
}
.build()
}
}

pub struct BuildPhase<T, B> {
behaviour: B,
transport: T,
Expand Down Expand Up @@ -1361,6 +1393,9 @@ pub enum AsyncStd {}
#[cfg(feature = "tokio")]
pub enum Tokio {}

#[cfg(feature = "wasm-bindgen")]
pub enum WasmBindgen {}

pub trait AuthenticatedMultiplexedTransport:
Transport<
Error = Self::E,
Expand Down

0 comments on commit 00dca91

Please sign in to comment.