From 2baf9dfed025273d75dbeb246cded0e38f9e3d60 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Mon, 12 Aug 2024 18:36:19 -0700 Subject: [PATCH 1/5] build: Cargo.lock updates to mitigate num_enum_derive problem --- Cargo.lock | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 52942f5..7787248 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2732,7 +2732,7 @@ version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate", "proc-macro2", "quote", "syn 1.0.109", @@ -2744,7 +2744,7 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate", "proc-macro2", "quote", "syn 2.0.77", @@ -2756,7 +2756,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ - "proc-macro-crate 3.2.0", + "proc-macro-crate", "proc-macro2", "quote", "syn 2.0.77", @@ -3157,15 +3157,6 @@ dependencies = [ "toml_edit 0.19.15", ] -[[package]] -name = "proc-macro-crate" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" -dependencies = [ - "toml_edit 0.22.20", -] - [[package]] name = "proc-macro2" version = "1.0.86" @@ -4428,7 +4419,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.20", + "toml_edit 0.22.21", ] [[package]] @@ -4453,9 +4444,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap", "serde", From 76df2868379ce7fc5e6c4b4ccb8a88cb7c7a855a Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Tue, 8 Oct 2024 10:10:45 -0700 Subject: [PATCH 2/5] add .rustfmt.toml to specify edition --- .rustfmt.toml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..3a26366 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1 @@ +edition = "2021" From 26cb1a2c074e516c9df047be0ed808860e24454e Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Wed, 16 Oct 2024 17:30:59 -0700 Subject: [PATCH 3/5] Fix parsing of subscription id for RelayMessage::Event --- enostr/src/relay/message.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/enostr/src/relay/message.rs b/enostr/src/relay/message.rs index b48e5ae..dea89e8 100644 --- a/enostr/src/relay/message.rs +++ b/enostr/src/relay/message.rs @@ -90,7 +90,17 @@ impl<'a> RelayMessage<'a> { // Event // Relay response format: ["EVENT", , ] if &msg[0..=7] == "[\"EVENT\"" { - return Ok(Self::event(msg, "fixme")); + let mut start = 9; + while let Some(&b' ') = msg.as_bytes().get(start) { + start += 1; // Move past optional spaces + } + if let Some(comma_index) = msg[start..].find(',') { + let subid_end = start + comma_index; + let subid = &msg[start..subid_end].trim().trim_matches('"'); + return Ok(Self::event(msg, subid)); + } else { + return Ok(Self::event(msg, "fixme")); + } } // EOSE (NIP-15) From e166dc17ef4c23d52f471cc8883414804a9f1f70 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Thu, 17 Oct 2024 20:26:12 -0700 Subject: [PATCH 4/5] Skip adding relays that are already in the pool --- enostr/src/relay/pool.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/enostr/src/relay/pool.rs b/enostr/src/relay/pool.rs index f4aab0c..30963c9 100644 --- a/enostr/src/relay/pool.rs +++ b/enostr/src/relay/pool.rs @@ -152,6 +152,10 @@ impl RelayPool { url: String, wakeup: impl Fn() + Send + Sync + Clone + 'static, ) -> Result<()> { + // Check if the URL already exists in the pool. + if self.has(&url) { + return Ok(()); + } let relay = Relay::new(url, wakeup)?; let pool_relay = PoolRelay::new(relay); From 570d64c3cdcaa6191195e9fe436b419783e0aa53 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Sat, 19 Oct 2024 11:18:41 -0700 Subject: [PATCH 5/5] canonicalize relay urls to avoid false duplicates --- Cargo.lock | 1 + enostr/Cargo.toml | 1 + enostr/src/relay/pool.rs | 11 +++++++++++ 3 files changed, 13 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7787248..1ffd999 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1271,6 +1271,7 @@ dependencies = [ "serde_derive", "serde_json", "tracing", + "url", ] [[package]] diff --git a/enostr/Cargo.toml b/enostr/Cargo.toml index 4b74228..2af528a 100644 --- a/enostr/Cargo.toml +++ b/enostr/Cargo.toml @@ -15,3 +15,4 @@ nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", rev = "9bbafd8a2e904 hex = "0.4.3" tracing = "0.1.40" env_logger = "0.11.1" +url = "2.5.2" diff --git a/enostr/src/relay/pool.rs b/enostr/src/relay/pool.rs index 30963c9..de4e1ef 100644 --- a/enostr/src/relay/pool.rs +++ b/enostr/src/relay/pool.rs @@ -4,6 +4,8 @@ use nostrdb::Filter; use std::time::{Duration, Instant}; +use url::Url; + #[cfg(not(target_arch = "wasm32"))] use ewebsock::{WsEvent, WsMessage}; @@ -152,6 +154,7 @@ impl RelayPool { url: String, wakeup: impl Fn() + Send + Sync + Clone + 'static, ) -> Result<()> { + let url = Self::canonicalize_url(url); // Check if the URL already exists in the pool. if self.has(&url) { return Ok(()); @@ -164,6 +167,14 @@ impl RelayPool { Ok(()) } + // standardize the format (ie, trailing slashes) + fn canonicalize_url(url: String) -> String { + match Url::parse(&url) { + Ok(parsed_url) => parsed_url.to_string(), + Err(_) => url, // If parsing fails, return the original URL. + } + } + /// Attempts to receive a pool event from a list of relays. The /// function searches each relay in the list in order, attempting to /// receive a message from each. If a message is received, return it.