Skip to content

Commit

Permalink
upgrade js code
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 4, 2024
1 parent ade7e0f commit 20766a7
Show file tree
Hide file tree
Showing 21 changed files with 791 additions and 571 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions iroh-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ async-trait = "0.1.80"
blake3 = "1.3.3"
bytes = "1"
data-encoding = { version = "2.3.3" }
iroh = "0.29.0"
iroh-gossip = "0.29.0"
iroh-gossip = { version = "0.29", features = ["rpc"] }
iroh-docs = { version = "0.29", features = ["rpc"] }
iroh-blobs = { version = "0.29", features = ["rpc"] }
iroh-metrics = { version = "0.29" }
iroh-node-util = { version = "0.29", features = [] }
iroh = "0.29"
iroh-io = { version = "0.6" }
libc = "0.2.141"
num_cpus = { version = "1.15.0" }
Expand All @@ -33,6 +37,7 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17" }
chrono = "0.4.38"
derive_more = { version = "1.0.0", features = ["debug"] }
quic-rpc = "0.17"


[build-dependencies]
Expand Down
36 changes: 26 additions & 10 deletions iroh-js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,9 @@ export declare class Iroh {
* All data will be only persistet in memory.
*/
static memory(opts?: NodeOptions | undefined | null): Promise<Iroh>
/** Create a new iroh client, connecting to an existing node. */
static client(addr?: string | undefined | null): Promise<Iroh>
/** Access to node specific funtionaliy. */
get node(): Node

}

/** Iroh net client. */
Expand All @@ -434,9 +433,7 @@ export declare class Node {
status(): Promise<NodeStatus>
/** Shutdown this iroh node. */
shutdown(): Promise<void>
/** Returns `Some(addr)` if an RPC endpoint is running, `None` otherwise. */
myRpcAddr(): string | null
endpoint(): Endpoint | null
endpoint(): Endpoint
}

/**
Expand Down Expand Up @@ -601,6 +598,29 @@ export declare class SetTagOption {
static named(tag: Array<number>): SetTagOption
}

/** A response to a list collections request */
export declare class TagInfo {
/** The tag */
name: Array<number>
/** The format of the associated blob */
format: BlobFormat
/** The hash of the associated blob */
hash: string
}

/** Iroh tags client. */
export declare class Tags {
/**
* List all tags
*
* Note: this allocates for each `ListTagsResponse`, if you have many `Tags`s this may be a prohibitively large list.
* Please file an [issue](https://github.com/n0-computer/iroh-ffi/issues/new) if you run into this issue
*/
list(): Promise<Array<TagInfo>>
/** Delete a tag */
delete(name: Array<number>): Promise<void>
}

/** Progress updates for the add operation. */
export interface AddProgress {
/** An item was found with name `name`, from now on referred to via `id` */
Expand Down Expand Up @@ -1277,15 +1297,11 @@ export interface NodeOptions {
ipv4Addr?: string
/** Overwrites the default IPv6 address to bind to */
ipv6Addr?: string
/** Enable RPC. Defaults to `false`. */
enableRpc?: boolean
/** Overwrite the default RPC address. */
rpcAddr?: string
/** Configure the node discovery. */
nodeDiscovery?: NodeDiscoveryConfig
/** Provide a specific secret key, identifying this node. Must be 32 bytes long. */
secretKey?: Array<number>
protocols?: Record<Array<number>, ((err: Error | null, arg0: Endpoint, arg1: Iroh) => ProtocolHandler)>
protocols?: Record<Array<number>, ((err: Error | null, arg: Endpoint) => ProtocolHandler)>
}

/** The response to a status request */
Expand Down
2 changes: 2 additions & 0 deletions iroh-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ module.exports.RecvStream = nativeBinding.RecvStream
module.exports.Sender = nativeBinding.Sender
module.exports.SendStream = nativeBinding.SendStream
module.exports.SetTagOption = nativeBinding.SetTagOption
module.exports.TagInfo = nativeBinding.TagInfo
module.exports.Tags = nativeBinding.Tags
module.exports.AddrInfoOptions = nativeBinding.AddrInfoOptions
module.exports.BlobExportFormat = nativeBinding.BlobExportFormat
module.exports.BlobExportMode = nativeBinding.BlobExportMode
Expand Down
35 changes: 15 additions & 20 deletions iroh-js/src/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ use futures::TryStreamExt;
use napi::bindgen_prelude::*;
use napi_derive::napi;

use crate::Iroh;
use crate::{AuthorsClient, Iroh};

/// Identifier for an [`Author`]
#[derive(Debug, Clone, PartialEq, Eq)]
#[napi]
pub struct AuthorId(pub(crate) iroh::docs::AuthorId);
pub struct AuthorId(pub(crate) iroh_docs::AuthorId);

#[napi]
impl AuthorId {
/// Get an [`AuthorId`] from a String.
#[napi(factory)]
pub fn from_string(str: String) -> Result<Self> {
let author: iroh::docs::AuthorId = str.parse()?;
let author: iroh_docs::AuthorId = str.parse()?;
Ok(AuthorId(author))
}

Expand All @@ -35,14 +35,14 @@ impl AuthorId {
/// Internally, an author is a `SigningKey` which is used to sign entries.
#[derive(Debug, Clone)]
#[napi]
pub struct Author(pub(crate) iroh::docs::Author);
pub struct Author(pub(crate) iroh_docs::Author);

#[napi]
impl Author {
/// Get an [`Author`] from a String
#[napi(factory)]
pub fn from_string(str: String) -> Result<Self> {
let author: iroh::docs::Author = str.parse()?;
let author: iroh_docs::Author = str.parse()?;
Ok(Author(author))
}

Expand All @@ -61,21 +61,17 @@ impl Author {
/// Iroh authors client.
#[napi]
pub struct Authors {
node: Iroh,
client: AuthorsClient,
}

#[napi]
impl Iroh {
/// Access to authors specific funtionaliy.
#[napi(getter)]
pub fn authors(&self) -> Authors {
Authors { node: self.clone() }
}
}

impl Authors {
fn client(&self) -> &iroh::client::Iroh {
self.node.inner_client()
Authors {
client: self.authors_client.clone().expect("missing docs"),
}
}
}

Expand All @@ -89,16 +85,15 @@ impl Authors {
/// The default author can be set with [`Self::set_default`].
#[napi]
pub async fn default(&self) -> Result<AuthorId> {
let author = self.client().authors().default().await?;
let author = self.client.default().await?;
Ok(AuthorId(author))
}

/// List all the AuthorIds that exist on this node.
#[napi]
pub async fn list(&self) -> Result<Vec<AuthorId>> {
let authors = self
.client()
.authors()
.client
.list()
.await?
.map_ok(|id| AuthorId(id))
Expand All @@ -115,7 +110,7 @@ impl Authors {
/// If you need only a single author, use [`Self::default`].
#[napi]
pub async fn create(&self) -> Result<AuthorId> {
let author = self.client().authors().create().await?;
let author = self.client.create().await?;

Ok(AuthorId(author))
}
Expand All @@ -125,7 +120,7 @@ impl Authors {
/// Warning: This contains sensitive data.
#[napi]
pub async fn export(&self, author: &AuthorId) -> Result<Author> {
let author = self.client().authors().export(author.0).await?;
let author = self.client.export(author.0).await?;
match author {
Some(author) => Ok(Author(author)),
None => Err(anyhow::anyhow!("Author Not Found").into()),
Expand All @@ -137,7 +132,7 @@ impl Authors {
/// Warning: This contains sensitive data.
#[napi]
pub async fn import(&self, author: &Author) -> Result<AuthorId> {
self.client().authors().import(author.0.clone()).await?;
self.client.import(author.0.clone()).await?;
Ok(AuthorId(author.0.id()))
}

Expand All @@ -146,7 +141,7 @@ impl Authors {
/// Warning: This permanently removes this author.
#[napi]
pub async fn delete(&self, author: &AuthorId) -> Result<()> {
self.client().authors().delete(author.0).await?;
self.client.delete(author.0).await?;
Ok(())
}
}
Loading

0 comments on commit 20766a7

Please sign in to comment.