Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(node-wasm): Switch to bundler target for wasm-pack for manifest v3 #398

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 2 additions & 0 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 Down
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>;
4 changes: 1 addition & 3 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 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 } 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 NodeWorker");

await worker.run();
});
worker.run();
2 changes: 1 addition & 1 deletion node-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl NodeClient {
// response, leaving an extra pong on the wire. This will eventually fail on
// decoding worker response in a future. 100ms should be enough to avoid that.
loop {
if timeout(100, worker.exec(NodeCommand::InternalPing))
if timeout(200, worker.exec(NodeCommand::InternalPing))
.await
.is_ok()
{
Expand Down
52 changes: 24 additions & 28 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,35 +81,28 @@ pub(crate) fn js_value_from_display<D: fmt::Display>(value: D) -> JsValue {
JsValue::from(value.to_string())
}

pub(crate) trait WorkerSelf {
type GlobalScope;

fn worker_self() -> Self::GlobalScope;
fn is_worker_type() -> bool;
}

impl WorkerSelf for SharedWorker {
type GlobalScope = SharedWorkerGlobalScope;
trait WorkerSelf {
type GlobalScope: JsCast;

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

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

impl WorkerSelf for SharedWorker {
type GlobalScope = SharedWorkerGlobalScope;
}

impl WorkerSelf for Worker {
type GlobalScope = DedicatedWorkerGlobalScope;
}

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

fn is_worker_type() -> bool {
js_sys::global().has_type::<Self::GlobalScope>()
}
impl WorkerSelf for ServiceWorker {
type GlobalScope = ServiceWorkerGlobalScope;
}

/// This type is useful in cases where we want to deal with de/serialising `Result<T, E>`, with
Expand Down Expand Up @@ -182,6 +175,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 +214,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 +254,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 +271,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
Loading