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

Commit 1bccb91

Browse files
committed
feat: implement /dns
Signed-off-by: ljedrz <[email protected]>
1 parent 3cb2db4 commit 1bccb91

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

conformance/test/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const factory = createFactory(options)
3333
// Phase 1.0-ish
3434
//
3535
tests.miscellaneous(factory, { skip: [
36-
'dns',
36+
// recursive resolving is not implemented yet
37+
'should recursively resolve ipfs.io',
3738
// the cidBase param is not implemented yet
3839
'should resolve an IPFS hash and return a base64url encoded CID in path',
3940
// different Cid, the /path/to/testfile.txt suffix shouldn't be there

http/src/v0.rs

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub fn routes<T: IpfsTypes>(
8888
and_boxed!(warp::path!("id"), id::identity(ipfs)),
8989
and_boxed!(warp::path!("add"), root_files::add(ipfs)),
9090
and_boxed!(warp::path!("cat"), root_files::cat(ipfs)),
91+
and_boxed!(warp::path!("dns"), ipns::dns(ipfs)),
9192
and_boxed!(warp::path!("get"), root_files::get(ipfs)),
9293
and_boxed!(warp::path!("refs" / "local"), refs::local(ipfs)),
9394
and_boxed!(warp::path!("refs"), refs::refs(ipfs)),

http/src/v0/ipns.rs

+31
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,34 @@ async fn resolve_query<T: IpfsTypes>(
4343
struct ResolveResponse {
4444
path: String,
4545
}
46+
47+
#[derive(Debug, Deserialize)]
48+
pub struct DnsQuery {
49+
// the name to resolve
50+
arg: StringSerialized<IpfsPath>,
51+
}
52+
53+
pub fn dns<T: IpfsTypes>(
54+
ipfs: &Ipfs<T>,
55+
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
56+
with_ipfs(ipfs).and(query::<DnsQuery>()).and_then(dns_query)
57+
}
58+
59+
async fn dns_query<T: IpfsTypes>(ipfs: Ipfs<T>, query: DnsQuery) -> Result<impl Reply, Rejection> {
60+
let DnsQuery { arg, .. } = query;
61+
let path = ipfs
62+
.resolve(&arg.into_inner())
63+
.await
64+
.map_err(StringError::from)?
65+
.to_string();
66+
67+
let response = DnsResponse { path };
68+
69+
Ok(warp::reply::json(&response))
70+
}
71+
72+
#[derive(Debug, Serialize)]
73+
#[serde(rename_all = "PascalCase")]
74+
struct DnsResponse {
75+
path: String,
76+
}

src/path.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ impl FromStr for IpfsPath {
2424
let empty = subpath.next().expect("there's always the first split");
2525

2626
let root = if !empty.is_empty() {
27-
// by default if there is no prefix it's an ipfs or ipld path
28-
PathRoot::Ipld(Cid::try_from(empty)?)
27+
// by default if there is no prefix it's an ipfs or ipld path...
28+
if let Ok(cid) = Cid::try_from(empty) {
29+
PathRoot::Ipld(cid)
30+
} else {
31+
// ...but if that isn't the case, it might be a domain name too
32+
PathRoot::Dns(empty.to_string())
33+
}
2934
} else {
3035
let root_type = subpath.next();
3136
let key = subpath.next();

0 commit comments

Comments
 (0)