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

feat: Add requesting storage persistence for more quota #318

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ web-sys = { version = "0.3.69", features = [
"Navigator",
"SharedWorker",
"SharedWorkerGlobalScope",
"StorageManager",
"Worker",
"WorkerGlobalScope",
"WorkerOptions",
Expand Down
7 changes: 6 additions & 1 deletion node-wasm/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use libp2p::identity::Keypair;
use libp2p::multiaddr::Protocol;
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::to_value;
use tracing::error;
use wasm_bindgen::prelude::*;
use web_sys::BroadcastChannel;

Expand All @@ -14,7 +15,7 @@ use lumina_node::node::NodeConfig;
use lumina_node::store::IndexedDbStore;

use crate::error::{Context, Result};
use crate::utils::{is_chrome, js_value_from_display, Network};
use crate::utils::{is_chrome, js_value_from_display, request_storage_persistence, Network};
use crate::worker::commands::{CheckableResponseExt, NodeCommand, SingleHeaderQuery};
use crate::worker::{AnyWorker, WorkerClient};
use crate::wrapper::libp2p::NetworkInfoSnapshot;
Expand Down Expand Up @@ -92,6 +93,10 @@ impl NodeDriver {
worker_script_url: &str,
worker_type: Option<NodeWorkerKind>,
) -> Result<NodeDriver> {
if let Err(e) = request_storage_persistence().await {
zvolin marked this conversation as resolved.
Show resolved Hide resolved
error!("Error requesting storage persistence: {e}");
}

// For chrome we default to running in a dedicated Worker because:
// 1. Chrome Android does not support SharedWorkers at all
// 2. On desktop Chrome, restarting Lumina's worker causes all network connections to fail.
Expand Down
29 changes: 29 additions & 0 deletions node-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use lumina_node::network;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use tracing::{info, warn};
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::format::Pretty;
use tracing_subscriber::fmt::time::UtcTime;
Expand Down Expand Up @@ -150,6 +151,34 @@ where
}
}

/// Request persistent storage from user for us, which has side effect of increasing the quota we
/// have. This function doesn't `await` on JavaScript promise, as that would block until user
/// either allows or blocks our request in a prompt (and we cannot do much with the result anyway).
pub(crate) async fn request_storage_persistence() -> Result<(), Error> {
let fullfiled = Closure::once(move |granted: JsValue| {
if granted.is_truthy() {
info!("Storage persistence acquired: {:?}", granted);
} else {
warn!("User rejected storage persistance request")
}
});
let rejected = Closure::once(move |_ev: JsValue| {
warn!("Error during persistant storage request");
});

// don't drop the promise, we'll log the result and hope the user clicked the right button
let _promise = get_navigator()?
.storage()
.persist()?
.then2(&fullfiled, &rejected);

// stop rust from dropping them
fullfiled.forget();
rejected.forget();

Ok(())
}

const CHROME_USER_AGENT_DETECTION_STR: &str = "Chrome/";

// Currently, there's an issue with SharedWorkers on Chrome where restarting Lumina's worker
Expand Down
2 changes: 1 addition & 1 deletion node-wasm/src/worker/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl AnyWorker {

fn setup_on_error_callback(&self) -> Closure<dyn Fn(MessageEvent)> {
let onerror = Closure::new(|ev: MessageEvent| {
error!("received error from Worker: {:?}", ev.to_string());
error!("received error from Worker: {:?}", ev);
});
match self {
AnyWorker::SharedWorker(worker) => {
Expand Down
Loading