Skip to content

Commit e202b27

Browse files
feat(browser-chat): udpate to [email protected]
1 parent 44e0f6e commit e202b27

File tree

10 files changed

+1051
-1288
lines changed

10 files changed

+1051
-1288
lines changed

browser-chat/Cargo.lock

Lines changed: 982 additions & 1221 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser-chat/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ members = ["shared", "cli", "browser-wasm"]
44

55
[workspace.dependencies]
66
# we define iroh dependencies here to make upgrading easier.
7-
iroh = { version = "0.93", default-features = false }
8-
iroh-base = { version = "0.93", default-features = false, features = ["ticket"] }
9-
iroh-gossip = { version = "0.93", default-features = false, features = ["net"] }
7+
iroh = { version = "0.94", default-features = false }
8+
iroh-tickets = "0.1"
9+
iroh-gossip = { version = "0.94", default-features = false, features = ["net"] }
10+
11+
[patch.crates-io]
12+
iroh-base = { path = "../../iroh/iroh-base" }

browser-chat/browser-wasm/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ crate-type = ["cdylib", "rlib"]
1010
anyhow = "1.0.86"
1111
chat-shared = { version = "0.1.0", path = "../shared" }
1212
console_error_panic_hook = "0.1.7"
13-
getrandom = { version = "0.2", features = ["js"] }
14-
n0-future = "0.1.2"
13+
getrandom = { version = "0.3", features = [] }
14+
n0-future = "0.3"
1515
serde = "1.0.217"
1616
serde-wasm-bindgen = "0.6.5"
1717
tracing = "0.1.40"

browser-chat/browser-wasm/src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use anyhow::Result;
7-
use chat_shared::{ChatSender, ChatTicket, NodeId, TopicId};
7+
use chat_shared::{ChatSender, ChatTicket, EndpointId, TopicId};
88
use n0_future::{StreamExt, time::Duration};
99
use serde::{Deserialize, Serialize};
1010
use tracing::level_filters::LevelFilter;
@@ -44,9 +44,9 @@ impl ChatNode {
4444
Ok(Self(inner))
4545
}
4646

47-
/// Returns the node id of this node.
48-
pub fn node_id(&self) -> String {
49-
self.0.node_id().to_string()
47+
/// Returns the endpoint id of this node.
48+
pub fn endpoint_id(&self) -> String {
49+
self.0.endpoint_id().to_string()
5050
}
5151

5252
/// Opens a chat.
@@ -73,11 +73,11 @@ impl ChatNode {
7373
chat_shared::Event::Joined { neighbors } => {
7474
neighbors2.lock().unwrap().extend(neighbors.iter().cloned());
7575
}
76-
chat_shared::Event::NeighborUp { node_id } => {
77-
neighbors2.lock().unwrap().insert(*node_id);
76+
chat_shared::Event::NeighborUp { endpoint_id } => {
77+
neighbors2.lock().unwrap().insert(*endpoint_id);
7878
}
79-
chat_shared::Event::NeighborDown { node_id } => {
80-
neighbors2.lock().unwrap().remove(node_id);
79+
chat_shared::Event::NeighborDown { endpoint_id } => {
80+
neighbors2.lock().unwrap().remove(endpoint_id);
8181
}
8282
_ => {}
8383
}
@@ -90,14 +90,14 @@ impl ChatNode {
9090

9191
// Add ourselves to the ticket.
9292
let mut ticket = ticket;
93-
ticket.bootstrap.insert(self.0.node_id());
94-
// ticket.bootstrap = [self.0.node_id()].into_iter().collect();
93+
ticket.bootstrap.insert(self.0.endpoint_id());
94+
// ticket.bootstrap = [self.0.endpoint_id()].into_iter().collect();
9595

9696
let topic = Channel {
9797
topic_id: ticket.topic_id,
9898
bootstrap: ticket.bootstrap,
9999
neighbors,
100-
me: self.0.node_id(),
100+
me: self.0.endpoint_id(),
101101
sender,
102102
receiver,
103103
};
@@ -110,9 +110,9 @@ type ChannelReceiver = wasm_streams::readable::sys::ReadableStream;
110110
#[wasm_bindgen]
111111
pub struct Channel {
112112
topic_id: TopicId,
113-
me: NodeId,
114-
bootstrap: BTreeSet<NodeId>,
115-
neighbors: Arc<Mutex<BTreeSet<NodeId>>>,
113+
me: EndpointId,
114+
bootstrap: BTreeSet<EndpointId>,
115+
neighbors: Arc<Mutex<BTreeSet<EndpointId>>>,
116116
sender: ChannelSender,
117117
receiver: ChannelReceiver,
118118
}
@@ -162,7 +162,7 @@ impl Channel {
162162

163163
#[derive(Debug, Clone, Serialize, Deserialize)]
164164
pub struct PeerInfo {
165-
pub node_id: NodeId,
165+
pub endpoint_id: EndpointId,
166166
pub nickname: String,
167167
pub last_active: Duration,
168168
}

browser-chat/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2024"
77
anyhow = "1.0.95"
88
clap = { version = "4.5.29", features = ["derive"] }
99
iroh = { workspace = true, default-features = false }
10-
n0-future = "0.1.2"
10+
n0-future = "0.3"
1111
rand = "0.9.2"
1212
chat-shared = { version = "0.1.0", path = "../shared" }
1313
tokio = { version = "1.43.0", features = ["rt", "macros"] }

browser-chat/cli/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ async fn main() -> Result<()> {
4848
};
4949

5050
let node = ChatNode::spawn(Some(secret_key)).await?;
51-
println!("node id: {}", node.node_id());
51+
println!("endpoint id: {}", node.endpoint_id());
5252

5353
let ticket = match args.command {
5454
Command::Create => ChatTicket::new_random(),
5555
Command::Join { ticket } => ChatTicket::deserialize(&ticket)?,
5656
};
5757

5858
let mut our_ticket = ticket.clone();
59-
our_ticket.bootstrap = [node.node_id()].into_iter().collect();
59+
our_ticket.bootstrap = [node.endpoint_id()].into_iter().collect();
6060
println!("* ticket to join this chat:");
6161
println!("{}", our_ticket.serialize());
6262

@@ -69,8 +69,8 @@ async fn main() -> Result<()> {
6969
match event {
7070
Event::Joined { neighbors } => {
7171
println!("* swarm joined");
72-
for node_id in neighbors {
73-
println!("* neighbor up: {node_id}")
72+
for endpoint_id in neighbors {
73+
println!("* neighbor up: {endpoint_id}")
7474
}
7575
}
7676
Event::Presence {
@@ -102,11 +102,11 @@ async fn main() -> Result<()> {
102102
}
103103
println!("<{from_short}> {nickname}: {text}");
104104
}
105-
Event::NeighborUp { node_id } => {
106-
println!("* neighbor up: {node_id}")
105+
Event::NeighborUp { endpoint_id } => {
106+
println!("* neighbor up: {endpoint_id}")
107107
}
108-
Event::NeighborDown { node_id } => {
109-
println!("* neighbor down: {node_id}")
108+
Event::NeighborDown { endpoint_id } => {
109+
println!("* neighbor down: {endpoint_id}")
110110
}
111111
Event::Lagged => {
112112
println!("* warn: gossip stream lagged")

browser-chat/frontend/src/components/chatview.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ function MyselfInfo({ peer }: PeerProps) {
253253
<div className="space-y-2">
254254
This is us :)
255255
<div>
256-
<strong>Node ID:</strong>
257-
<NodeId nodeId={peer.id} />
256+
<strong>Endpoint ID:</strong>
257+
<EndpointId endpointId={peer.id} />
258258
</div>
259259
</div>
260260
)
@@ -267,22 +267,22 @@ function RemotePeerInfo({ peer }: PeerProps) {
267267
<strong>Last seen:</strong> <TimeAgo date={peer.lastSeen} />
268268
</p>
269269
<div>
270-
<strong>Node ID:</strong>
271-
<NodeId nodeId={peer.id} />
270+
<strong>Endpoint ID:</strong>
271+
<EndpointId endpointId={peer.id} />
272272
</div>
273273
</div>
274274
)
275275
}
276276

277-
interface NodeIdProps {
278-
nodeId: string
277+
interface EndpointIdProps {
278+
endpointId: string
279279
}
280280

281-
function NodeId({ nodeId }: NodeIdProps) {
281+
function EndpointId({ endpointId }: EndpointIdProps) {
282282
return (
283283
<>
284-
<span className="ml-2 font-mono">{nodeId.substring(0, 8)}</span>
285-
<Button size="sm" onClick={() => copyToClipboard(nodeId)} className="ml-2 inline" variant="outline">
284+
<span className="ml-2 font-mono">{endpointId.substring(0, 8)}</span>
285+
<Button size="sm" onClick={() => copyToClipboard(endpointId)} className="ml-2 inline" variant="outline">
286286
Copy
287287
</Button>
288288
</>

browser-chat/frontend/src/lib/iroh.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class IrohAPI implements API {
2727
static async create(): Promise<IrohAPI> {
2828
log.info("Spawning iroh node")
2929
const chatNode = await ChatNode.spawn()
30-
log.info(`Iroh node spawned. our node id: ${chatNode.node_id()}`)
30+
log.info(`Iroh node spawned. our endpoint id: ${chatNode.endpoint_id()}`)
3131
return new IrohAPI(chatNode)
3232
}
3333

@@ -49,9 +49,9 @@ export class IrohAPI implements API {
4949
let onClosePromise = new Promise<void>(resolve => {
5050
onClose = resolve
5151
})
52-
const nodeId = this.chatNode.node_id()
52+
const endpointId = this.chatNode.endpoint_id()
5353
const myself: PeerInfo = {
54-
id: nodeId,
54+
id: endpointId,
5555
name: nickname,
5656
lastSeen: new Date(),
5757
status: "online",
@@ -70,7 +70,7 @@ export class IrohAPI implements API {
7070
myself,
7171
onClose: onClose!
7272
}
73-
state.peers.set(nodeId, myself)
73+
state.peers.set(endpointId, myself)
7474
this.channels.set(id, state)
7575

7676
const subscribe = async () => {
@@ -130,7 +130,7 @@ export class IrohAPI implements API {
130130
while (true) {
131131
const now = new Date()
132132
for (const peer of state.peers.values()) {
133-
if (peer.id === nodeId) {
133+
if (peer.id === endpointId) {
134134
peer.lastSeen = now
135135
continue
136136
}
@@ -188,7 +188,7 @@ export class IrohAPI implements API {
188188
throw new Error("Channel not found")
189189
}
190190
await state.channel.sender.broadcast(text)
191-
const me = this.chatNode.node_id();
191+
const me = this.chatNode.endpoint_id();
192192
const message = {
193193
sender: me,
194194
id: nextId(state),
@@ -313,12 +313,12 @@ type PresenceEvent = {
313313

314314
type NeighborUpEvent = {
315315
type: "neighborUp"
316-
nodeId: string
316+
endpointId: string
317317
}
318318

319319
type NeighborDownEvent = {
320320
type: "neighborDown"
321-
nodeId: string
321+
endpointId: string
322322
}
323323

324324
type LaggedEvent = {

browser-chat/shared/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ anyhow = "1"
88
blake3 = { version = "1", package = "iroh-blake3" }
99
hex = "0.4"
1010
iroh = { workspace = true }
11-
iroh-base = { workspace = true, default-features = false, features = ["ticket"] }
11+
iroh-tickets = { workspace = true }
1212
iroh-gossip = { workspace = true, default-features = false, features = ["net"] }
13-
n0-future = "0.1.2"
13+
n0-future = "0.3"
1414
postcard = "1.1.1"
1515
rand = "0.9.2"
1616
serde = "1"

browser-chat/shared/src/lib.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use std::{
44
};
55

66
use anyhow::{Context, Result};
7-
pub use iroh::NodeId;
8-
use iroh::{PublicKey, SecretKey, protocol::Router};
9-
use iroh_base::{Signature, ticket::Ticket};
7+
pub use iroh::EndpointId;
8+
use iroh::{PublicKey, SecretKey, Signature, protocol::Router};
109
pub use iroh_gossip::proto::TopicId;
1110
use iroh_gossip::{
1211
api::{Event as GossipEvent, GossipSender},
1312
net::{GOSSIP_ALPN, Gossip},
1413
};
14+
use iroh_tickets::Ticket;
1515
use n0_future::{
1616
StreamExt,
1717
boxed::BoxStream,
@@ -28,7 +28,7 @@ pub const PRESENCE_INTERVAL: Duration = Duration::from_secs(5);
2828
#[derive(Serialize, Deserialize, Clone, Debug)]
2929
pub struct ChatTicket {
3030
pub topic_id: TopicId,
31-
pub bootstrap: BTreeSet<NodeId>,
31+
pub bootstrap: BTreeSet<EndpointId>,
3232
}
3333

3434
impl ChatTicket {
@@ -58,7 +58,7 @@ impl Ticket for ChatTicket {
5858
postcard::to_stdvec(&self).unwrap()
5959
}
6060

61-
fn from_bytes(bytes: &[u8]) -> Result<Self, iroh_base::ticket::ParseError> {
61+
fn from_bytes(bytes: &[u8]) -> Result<Self, iroh_tickets::ParseError> {
6262
let ticket = postcard::from_bytes(bytes)?;
6363
Ok(ticket)
6464
}
@@ -76,14 +76,13 @@ impl ChatNode {
7676
let secret_key = secret_key.unwrap_or_else(|| SecretKey::generate(&mut rand::rng()));
7777
let endpoint = iroh::Endpoint::builder()
7878
.secret_key(secret_key.clone())
79-
.discovery_n0()
8079
.alpns(vec![GOSSIP_ALPN.to_vec()])
8180
.bind()
8281
.await?;
8382

84-
let node_id = endpoint.node_id();
83+
let endpoint_id = endpoint.id();
8584
info!("endpoint bound");
86-
info!("node id: {node_id:#?}");
85+
info!("endpoint id: {endpoint_id:#?}");
8786

8887
let gossip = Gossip::builder().spawn(endpoint.clone());
8988
info!("gossip spawned");
@@ -98,9 +97,9 @@ impl ChatNode {
9897
})
9998
}
10099

101-
/// Returns the node id of this node.
102-
pub fn node_id(&self) -> NodeId {
103-
self.router.endpoint().node_id()
100+
/// Returns the endpoint id of this endpoint.
101+
pub fn endpoint_id(&self) -> EndpointId {
102+
self.router.endpoint().id()
104103
}
105104

106105
/// Joins a chat channel from a ticket.
@@ -239,28 +238,28 @@ impl ChatSender {
239238
pub enum Event {
240239
#[serde(rename_all = "camelCase")]
241240
Joined {
242-
neighbors: Vec<NodeId>,
241+
neighbors: Vec<EndpointId>,
243242
},
244243
#[serde(rename_all = "camelCase")]
245244
MessageReceived {
246-
from: NodeId,
245+
from: EndpointId,
247246
text: String,
248247
nickname: String,
249248
sent_timestamp: u64,
250249
},
251250
#[serde(rename_all = "camelCase")]
252251
Presence {
253-
from: NodeId,
252+
from: EndpointId,
254253
nickname: String,
255254
sent_timestamp: u64,
256255
},
257256
#[serde(rename_all = "camelCase")]
258257
NeighborUp {
259-
node_id: NodeId,
258+
endpoint_id: EndpointId,
260259
},
261260
#[serde(rename_all = "camelCase")]
262261
NeighborDown {
263-
node_id: NodeId,
262+
endpoint_id: EndpointId,
264263
},
265264
Lagged,
266265
}
@@ -269,8 +268,8 @@ impl TryFrom<GossipEvent> for Event {
269268
type Error = anyhow::Error;
270269
fn try_from(event: GossipEvent) -> Result<Self, Self::Error> {
271270
let converted = match event {
272-
GossipEvent::NeighborUp(node_id) => Self::NeighborUp { node_id },
273-
GossipEvent::NeighborDown(node_id) => Self::NeighborDown { node_id },
271+
GossipEvent::NeighborUp(endpoint_id) => Self::NeighborUp { endpoint_id },
272+
GossipEvent::NeighborDown(endpoint_id) => Self::NeighborDown { endpoint_id },
274273
GossipEvent::Received(message) => {
275274
let message = SignedMessage::verify_and_decode(&message.content)
276275
.context("failed to parse and verify signed message")?;
@@ -348,6 +347,6 @@ pub enum Message {
348347
#[derive(Debug, Serialize, Deserialize)]
349348
pub struct ReceivedMessage {
350349
timestamp: u64,
351-
from: NodeId,
350+
from: EndpointId,
352351
message: Message,
353352
}

0 commit comments

Comments
 (0)