Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
feat: implement /resolve
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Sep 1, 2020
1 parent d3c51df commit 3cb2db4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
11 changes: 10 additions & 1 deletion conformance/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ const factory = createFactory(options)
//
tests.miscellaneous(factory, { skip: [
'dns',
'resolve',
// the cidBase param is not implemented yet
'should resolve an IPFS hash and return a base64url encoded CID in path',
// different Cid, the /path/to/testfile.txt suffix shouldn't be there
'should resolve an IPFS path link',
// different Cid, missing "/path/to" in the middle
'should resolve up to the last node across multiple nodes',
// expected "true", got "false"
'should resolve an IPNS DNS link',
// HTTP: not implemented
'should resolve IPNS link recursively',
// these cause a hang 20% of time:
'should respect timeout option when getting the node id',
'should respect timeout option when getting the node version',
Expand Down
2 changes: 2 additions & 0 deletions http/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod block;
pub mod dag;
pub mod dht;
pub mod id;
pub mod ipns;
pub mod pin;
pub mod pubsub;
pub mod refs;
Expand Down Expand Up @@ -90,6 +91,7 @@ pub fn routes<T: IpfsTypes>(
and_boxed!(warp::path!("get"), root_files::get(ipfs)),
and_boxed!(warp::path!("refs" / "local"), refs::local(ipfs)),
and_boxed!(warp::path!("refs"), refs::refs(ipfs)),
and_boxed!(warp::path!("resolve"), ipns::resolve(ipfs)),
warp::path!("version")
.and(query::<version::Query>())
.and_then(version::version),
Expand Down
45 changes: 45 additions & 0 deletions http/src/v0/ipns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::v0::support::{with_ipfs, StringError, StringSerialized};
use ipfs::{Ipfs, IpfsPath, IpfsTypes};
use serde::{Deserialize, Serialize};
use warp::{query, Filter, Rejection, Reply};

#[derive(Debug, Deserialize)]
pub struct ResolveQuery {
// the name to resolve
arg: StringSerialized<IpfsPath>,
#[serde(rename = "dht-record-count")]
dht_record_count: Option<usize>,
#[serde(rename = "dht-timeout")]
dht_timeout: Option<String>,
}

pub fn resolve<T: IpfsTypes>(
ipfs: &Ipfs<T>,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
with_ipfs(ipfs)
.and(query::<ResolveQuery>())
.and_then(resolve_query)
}

async fn resolve_query<T: IpfsTypes>(
ipfs: Ipfs<T>,
query: ResolveQuery,
) -> Result<impl Reply, Rejection> {
let ResolveQuery { arg, .. } = query;
let name = arg.into_inner();
let path = ipfs
.resolve(&name)
.await
.map_err(StringError::from)?
.to_string();

let response = ResolveResponse { path };

Ok(warp::reply::json(&response))
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct ResolveResponse {
path: String,
}
2 changes: 1 addition & 1 deletion src/ipns/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use thiserror::Error;
#[error("no dnslink entry")]
pub struct DnsLinkError;

type FutureAnswer = Pin<Box<dyn Future<Output = Result<Answer, io::Error>>>>;
type FutureAnswer = Pin<Box<dyn Future<Output = Result<Answer, io::Error>> + Send>>;

pub struct DnsLinkFuture {
query: SelectOk<FutureAnswer>,
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,10 @@ impl<Types: IpfsTypes> Ipfs<Types> {
refs::iplds_refs(self, iplds, max_depth, unique)
}

pub async fn resolve(&self, path: &IpfsPath) -> Result<IpfsPath, Error> {
self.ipns().resolve(path).await
}

/// Exit daemon.
pub async fn exit_daemon(self) {
// FIXME: this is a stopgap measure needed while repo is part of the struct Ipfs instead of
Expand Down

0 comments on commit 3cb2db4

Please sign in to comment.