Skip to content

Commit

Permalink
Switch to bundler target for wasm-pack for manifest v3
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Sep 27, 2024
1 parent b7b35a6 commit ef03ea8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release-plz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ jobs:
fi
# publish lumina-node-wasm
wasm-pack build --target web node-wasm
wasm-pack publish --target web --access public node-wasm
wasm-pack build node-wasm
wasm-pack publish --access public node-wasm
# publish lumina-node
cd node-wasm/js
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ you need to compile wasm node manually. Follow these additional steps:
cargo install wasm-pack

# compile lumina to wasm
wasm-pack build --target web node-wasm
wasm-pack build node-wasm

# install lumina-cli
cargo install --path cli --features browser-node
Expand Down
6 changes: 4 additions & 2 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ web-sys = { version = "0.3.70", features = [
"RequestInit",
"RequestMode",
"Response",
"ServiceWorker",
"ServiceWorkerGlobalScope",
"SharedWorker",
"SharedWorkerGlobalScope",
"StorageManager",
Expand All @@ -79,5 +81,5 @@ targets = ["wasm32-unknown-unknown"]

# Uncomment this if you need debug symbols in release.
# Also check workspace's `Cargo.toml`.
#[package.metadata.wasm-pack.profile.release]
#wasm-opt = ['-O4', '-g']
[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-O4', '-g']
1 change: 0 additions & 1 deletion node-wasm/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
*/
export function spawnNode(): Promise<NodeClient>;
export * from "lumina-node-wasm";
export default function init(): Promise<void>;
6 changes: 2 additions & 4 deletions node-wasm/js/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import init, { NodeClient } from "lumina-node-wasm"
import { NodeClient } from "lumina-node-wasm"

/**
* Spawn a worker running lumina node and get the `NodeClient` connected to it.
*/
export async function spawnNode() {
await init();
let worker = new Worker(new URL("worker.js", import.meta.url), { type: "module" });
let worker = new Worker(new URL("worker.js", import.meta.url));
let client = await new NodeClient(worker);
return client;
}

export * from "lumina-node-wasm";
export default init;
10 changes: 4 additions & 6 deletions node-wasm/js/worker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import init, { NodeWorker } from "lumina-node-wasm"
import { NodeWorker, NodeClient } from "lumina-node-wasm"

Error.stackTraceLimit = 99;

init().then(async () => {
let worker = new NodeWorker(self);
console.log("Starting NodeWorker");
let worker = new NodeWorker(self);
console.log("starting worker: ", worker);

await worker.run();
});
worker.run();
39 changes: 27 additions & 12 deletions node-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::future::Future;
use std::net::{IpAddr, Ipv4Addr};

use gloo_timers::future::TimeoutFuture;
use js_sys::Math;
use js_sys::{Math, Promise};
use libp2p::multiaddr::Protocol;
use libp2p::{Multiaddr, PeerId};
use lumina_node::network;
Expand All @@ -21,7 +21,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
use web_sys::{
DedicatedWorkerGlobalScope, MessageEvent, Request, RequestInit, RequestMode, Response,
SharedWorker, SharedWorkerGlobalScope, Worker,
ServiceWorker, ServiceWorkerGlobalScope, SharedWorker, SharedWorkerGlobalScope, Worker,
};

use crate::error::{Context, Error, Result};
Expand Down Expand Up @@ -81,7 +81,7 @@ pub(crate) fn js_value_from_display<D: fmt::Display>(value: D) -> JsValue {
JsValue::from(value.to_string())
}

pub(crate) trait WorkerSelf {
trait WorkerSelf {
type GlobalScope;

fn worker_self() -> Self::GlobalScope;
Expand Down Expand Up @@ -112,6 +112,18 @@ impl WorkerSelf for Worker {
}
}

impl WorkerSelf for ServiceWorker {
type GlobalScope = ServiceWorkerGlobalScope;

fn worker_self() -> Self::GlobalScope {
JsValue::from(js_sys::global()).into()
}

fn is_worker_type() -> bool {
js_sys::global().has_type::<Self::GlobalScope>()
}
}

/// This type is useful in cases where we want to deal with de/serialising `Result<T, E>`, with
/// [`serde_wasm_bindgen::preserve`] where `T` is a JavaScript object (which are not serializable by
/// Rust standards, but can be passed through unchanged via cast as they implement [`JsCast`]).
Expand Down Expand Up @@ -182,6 +194,9 @@ pub(crate) async fn request_storage_persistence() -> Result<(), Error> {
Worker::worker_self().navigator().storage()
} else if SharedWorker::is_worker_type() {
SharedWorker::worker_self().navigator().storage()
} else if ServiceWorker::is_worker_type() {
warn!("ServiceWorker doesn't have access to StorageManager");
return Ok(());
} else {
return Err(Error::new("`navigator.storage` not found in global scope"));
};
Expand Down Expand Up @@ -218,6 +233,8 @@ pub(crate) fn get_user_agent() -> Result<String, Error> {
Ok(Worker::worker_self().navigator().user_agent()?)
} else if SharedWorker::is_worker_type() {
Ok(SharedWorker::worker_self().navigator().user_agent()?)
} else if ServiceWorker::is_worker_type() {
Ok(ServiceWorker::worker_self().navigator().user_agent()?)
} else {
Err(Error::new(
"`navigator.user_agent` not found in global scope",
Expand Down Expand Up @@ -256,6 +273,12 @@ pub(crate) fn random_id() -> u32 {
(Math::random() * f64::from(u32::MAX)).floor() as u32
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = fetch)]
fn fetch_with_request(input: &Request) -> Promise;
}

async fn fetch(url: &str, opts: &RequestInit, headers: &[(&str, &str)]) -> Result<Response, Error> {
let request = Request::new_with_str_and_init(url, opts)
.with_context(|| format!("failed to create a request to {url}"))?;
Expand All @@ -267,15 +290,7 @@ async fn fetch(url: &str, opts: &RequestInit, headers: &[(&str, &str)]) -> Resul
.with_context(|| format!("failed setting header: '{name}: {value}'"))?;
}

let fetch_promise = if let Some(window) = web_sys::window() {
window.fetch_with_request(&request)
} else if Worker::is_worker_type() {
Worker::worker_self().fetch_with_request(&request)
} else if SharedWorker::is_worker_type() {
SharedWorker::worker_self().fetch_with_request(&request)
} else {
return Err(Error::new("`fetch` not found in global scope"));
};
let fetch_promise = fetch_with_request(&request);

JsFuture::from(fetch_promise)
.await
Expand Down
1 change: 1 addition & 0 deletions node/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ where

#[instrument(level = "trace", skip(self))]
async fn on_kademlia_event(&mut self, ev: kad::Event) -> Result<()> {
info!("KAD: {ev:?}");
match ev {
kad::Event::RoutingUpdated {
peer, addresses, ..
Expand Down

0 comments on commit ef03ea8

Please sign in to comment.