Skip to content

Commit 247b891

Browse files
authored
feat(iroh): Make iroh compile to wasm32-unknown-unknown (#3189)
## Description (This PR is a rebase & some changes from the #3145 PR, so we don't disturb users who are trying out the iroh browser alpha.) - Adds a bunch of `#[cfg(not(wasm_browser))]` to anything related to socket networking (UDP sockets, DNS, etc.) - Implements a `WebRuntime` for quinn to use instead of `TokioRuntime` in browsers - Implements sleeping & timing utilities for `backoff` that work in browsers - Moves `node_info` out of the `dns` module, so we can disable `dns` as a whole, but keep `node_info` for discovery - Disables `MappedAddr::Ip` in browsers - Adjusts dependencies so we don't depend on unused/unsupported things in browsers ## Breaking Changes - `iroh-relay`: The `iroh_relay::dns::node_info` module was moved to `iroh_relay::node_info` ## Notes & open questions I'm holding off on refactoring bigger parts for now. I have two main ideas: - Group a bunch of structs in `magicsock.rs`, i.e. everything that depends on sockets. However, to do this, I want to refactor the way we *sometimes* use `Arc<netwatch::UdpSocket>` and *sometimes* use `UdpConn`, which is just a newtype over said type. Ideally we store `Arc<netwatch::UdpSocket>` everywhere, since it's more commonly used in APIs, e.g. in iroh-net-report, and only use `UdpConn`, when we need the udp sockets to implement `AsyncUdpSocket` from quinn. - Refactor the `node_info` module so there's fewer places that depend on `DnsResolver` in it. There's a bunch of things in that module that IMO are needless indirections (we go from `hickory::TxtLookup` -> `node_info::TxtAttrs<IrohAttr>` -> `(NodeId, NodeData)` -> `NodeInfo`. And even then, `NodeInfo` is mostly isomorphic to `NodeAttr`). I think both these refactors are helpful, but don't necessarily make the diff easier to read? Not sure. I'd like to merge this & then work on these refactors next week. ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent ac78cf2 commit 247b891

31 files changed

+938
-177
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-unknown-unknown]
2+
runner = "wasm-bindgen-test-runner"

.github/workflows/ci.yml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,30 @@ jobs:
147147
env:
148148
RUST_LOG: ${{ runner.debug && 'TRACE' || 'DEBUG' }}
149149

150-
wasm_build:
151-
name: Build wasm32
150+
wasm_test:
151+
name: Build & test wasm32 for browsers
152152
runs-on: ubuntu-latest
153153
steps:
154154
- name: Checkout sources
155155
uses: actions/checkout@v4
156156

157+
- name: Setup node.js version 22.5 # needed for browser-like websocket API support in node.js
158+
uses: actions/setup-node@v4
159+
with:
160+
node-version: 22.5
161+
157162
- name: Install stable toolchain
158163
uses: dtolnay/rust-toolchain@stable
159164

160165
- name: Add wasm target
161166
run: rustup target add wasm32-unknown-unknown
162167

168+
- name: Install cargo-binstall
169+
uses: cargo-bins/cargo-binstall@main
170+
171+
- name: Install wasm-bindgen-test-runner
172+
run: cargo binstall wasm-bindgen-cli --locked --no-confirm
173+
163174
- name: Install wasm-tools
164175
uses: bytecodealliance/actions/wasm-tools/setup@v1
165176

@@ -172,18 +183,25 @@ jobs:
172183
- name: wasm32 build (iroh-net-report)
173184
run: cargo build --target wasm32-unknown-unknown -p iroh-net-report --no-default-features
174185

186+
- name: wasm32 build (iroh)
187+
run: cargo build --target wasm32-unknown-unknown -p iroh --no-default-features
188+
175189
# If the Wasm file contains any 'import "env"' declarations, then
176190
# some non-Wasm-compatible code made it into the final code.
177191
- name: Ensure no 'import "env"' in iroh-relay Wasm
178192
run: |
179193
! wasm-tools print --skeleton target/wasm32-unknown-unknown/debug/iroh_relay.wasm | grep 'import "env"'
180194
181-
# If the Wasm file contains any 'import "env"' declarations, then
182-
# some non-Wasm-compatible code made it into the final code.
183195
- name: Ensure no 'import "env"' in iroh-net-report Wasm
184196
run: |
185197
! wasm-tools print --skeleton target/wasm32-unknown-unknown/debug/iroh_net_report.wasm | grep 'import "env"'
186-
198+
199+
- name: Ensure no 'import "env"' in iroh Wasm
200+
run: |
201+
! wasm-tools print --skeleton target/wasm32-unknown-unknown/debug/iroh.wasm | grep 'import "env"'
202+
203+
- name: Run integration test in wasm
204+
run: cargo test -p iroh --test integration --target=wasm32-unknown-unknown --no-default-features
187205

188206
check_semver:
189207
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)