Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 1959e34

Browse files
committed
feat: dag.resolve API
1 parent 01b4e32 commit 1959e34

File tree

4 files changed

+172
-5
lines changed

4 files changed

+172
-5
lines changed

API/dag/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ If no `callback` is passed, a [promise][] is returned.
3030

3131
`callback` must follow `function (err, dagNode) {}` signature, where `err` is an error if the operation was not successful and `dagNode` is the IPLD format DAG node retrieved.
3232

33+
#### `dag.resolve`
34+
35+
> Resolves an IPLD path
36+
37+
##### `Go` **WIP**
38+
39+
##### `JavaScript` - ipfs.dag.resolve(cid, path, callback)
40+
41+
- `cid` is a [CID][https://github.com./ipfs/js-cid] instance.
42+
- `path` is a String that represents a valid path to be resolved
43+
44+
`callback` must follow `function (err, value) {}` signature, where `err` is an error if the operation was not successful and `value` is the value it was retrieved.
45+
3346
If no `callback` is passed, a [promise][] is returned.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"concat-stream": "^1.6.0",
3434
"detect-node": "^2.0.3",
3535
"ipfs-block": "^0.5.4",
36-
"ipld-dag-cbor": "^0.8.5",
37-
"ipld-dag-pb": "^0.9.3",
38-
"multiaddr": "^2.1.1",
39-
"multihashes": "^0.3.1",
36+
"ipld-dag-cbor": "^0.9.0",
37+
"ipld-dag-pb": "^0.9.4",
38+
"multiaddr": "^2.2.0",
39+
"multihashes": "^0.3.2",
4040
"readable-stream": "2.2.2"
4141
},
4242
"devDependencies": {
43-
"aegir": "^9.3.0"
43+
"aegir": "^9.4.0"
4444
},
4545
"contributors": [
4646
"David Dias <[email protected]>",

src/dag-resolve.js

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const expect = require('chai').expect
7+
const dagPB = require('ipld-dag-pb')
8+
const dagCBOR = require('ipld-dag-cbor')
9+
const series = require('async/series')
10+
const pull = require('pull-stream')
11+
12+
module.exports = (common) => {
13+
describe.only('.dag.resolve', () => {
14+
let ipfs
15+
let nodePb
16+
let nodeCbor
17+
let cidPb
18+
let cidCbor
19+
20+
before((done) => {
21+
common.setup((err, factory) => {
22+
expect(err).to.not.exist
23+
factory.spawnNode((err, node) => {
24+
expect(err).to.not.exist
25+
ipfs = node
26+
done()
27+
})
28+
})
29+
})
30+
31+
after((done) => {
32+
common.teardown(done)
33+
})
34+
35+
it('populate', (done) => {
36+
series([
37+
(cb) => {
38+
dagPB.DAGNode.create(new Buffer('I am inside a Protobuf'), (err, node) => {
39+
expect(err).to.not.exist
40+
nodePb = node
41+
cb()
42+
})
43+
},
44+
(cb) => {
45+
dagPB.util.cid(nodePb, (err, cid) => {
46+
expect(err).to.not.exist
47+
cidPb = cid
48+
cb()
49+
})
50+
},
51+
(cb) => {
52+
nodeCbor = {
53+
someData: 'I am inside a Cbor object',
54+
pb: { '/': cidPb.toBaseEncodedString() }
55+
}
56+
57+
dagCBOR.util.cid(nodeCbor, (err, cid) => {
58+
expect(err).to.not.exist
59+
cidCbor = cid
60+
console.log(nodeCbor)
61+
cb()
62+
})
63+
}
64+
], store)
65+
66+
function store () {
67+
pull(
68+
pull.values([
69+
{ node: nodePb, multicodec: 'dag-pb', hashAlg: 'sha2-256' },
70+
{ node: nodeCbor, multicodec: 'dag-cbor', hashAlg: 'sha2-256' }
71+
]),
72+
pull.asyncMap((el, cb) => {
73+
ipfs.dag.put(el.node, el.multicodec, el.hashAlg, (err) => {
74+
if (err) {
75+
console.log(err)
76+
}
77+
console.log('put', el.multicodec)
78+
79+
cb()
80+
})
81+
}),
82+
pull.onEnd(done)
83+
)
84+
}
85+
})
86+
87+
describe('callback API', () => {
88+
describe('.resolve', () => {
89+
it('dag-pb get the node', (done) => {
90+
ipfs.dag.resolve(cidPb, '/', (err, result) => {
91+
expect(err).to.not.exist
92+
93+
dagPB.util.cid(result, (err, cid) => {
94+
expect(err).to.not.exist
95+
expect(cid).to.eql(cidPb)
96+
done()
97+
})
98+
})
99+
})
100+
101+
it('dag-pb local scope', (done) => {
102+
ipfs.dag.resolve(cidPb, 'data', (err, result) => {
103+
expect(err).to.not.exist
104+
expect(result).to.eql(new Buffer('I am inside a Protobuf'))
105+
done()
106+
})
107+
})
108+
109+
it.skip('dag-pb one level', (done) => {})
110+
it.skip('dag-pb two levels', (done) => {})
111+
112+
it('dag-cbor get the node', (done) => {
113+
ipfs.dag.get(cidCbor, (err, result) => {
114+
// ipfs.dag.resolve(cidCbor, '/', (err, result) => {
115+
expect(err).to.not.exist
116+
117+
console.log('get the node')
118+
119+
dagCBOR.util.cid(result, (err, cid) => {
120+
expect(err).to.not.exist
121+
expect(cid).to.eql(cidCbor)
122+
done()
123+
})
124+
})
125+
})
126+
127+
it('dag-cbor local scope', (done) => {
128+
ipfs.dag.resolve(cidCbor, 'someData', (err, result) => {
129+
expect(err).to.not.exist
130+
expect(result).to.eql('I am inside a Cbor object')
131+
done()
132+
})
133+
})
134+
135+
it.skip('dag-cbor one level', (done) => {})
136+
it.skip('dag-cbor two levels', (done) => {})
137+
it.skip('from dag-pb to dag-cbor', (done) => {})
138+
139+
it('from dag-cbor to dag-pb', (done) => {
140+
ipfs.dag.resolve(cidCbor, 'pb/data', (err, result) => {
141+
expect(err).to.not.exist
142+
expect(result).to.eql(new Buffer('I am inside a Protobuf'))
143+
done()
144+
})
145+
})
146+
})
147+
})
148+
149+
describe('promise API', () => {
150+
describe('.resolve', () => {})
151+
})
152+
})
153+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ exports.swarm = require('./swarm')
99
exports.block = require('./block')
1010
exports.dht = require('./dht')
1111
exports.dag = require('./dag')
12+
exports.dagResolve = require('./dag-resolve')
1213
exports.pubsub = require('./pubsub')

0 commit comments

Comments
 (0)