Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit d4bb501

Browse files
committed
feat: dag-api (WIP)
1 parent fc3f216 commit d4bb501

File tree

2 files changed

+207
-9
lines changed

2 files changed

+207
-9
lines changed

API/dag/README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
dag API
22
=======
33

4+
> The dag API comes to replace the `object API`, it support the creation and manipulation of dag-pb object, as well as other IPLD formats (i.e dag-cbor, ethereum-block, git, etc)
5+
46
#### `dag.put`
57

68
> Store an IPLD format node
@@ -9,13 +11,10 @@ dag API
911

1012
##### `JavaScript` - ipfs.dag.put(dagNode, formatMulticodec, hashAlg, callback)
1113

12-
`dagNode` - a DAG node that follows one of the supported IPLD formats.
13-
14-
`formatMulticodec` - The IPLD format multicodec.
15-
16-
`hashAlg` - The hash algorithm to be used over the serialized dagNode.
17-
18-
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
14+
- `dagNode` - a DAG node that follows one of the supported IPLD formats.
15+
- `formatMulticodec` - The IPLD format multicodec.
16+
- `hashAlg` - The hash algorithm to be used over the serialized dagNode.
17+
- `callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
1918

2019
If no `callback` is passed, a [promise][] is returned.
2120

@@ -25,10 +24,39 @@ If no `callback` is passed, a [promise][] is returned.
2524
2625
##### `Go` **WIP**
2726

28-
##### `JavaScript` - ipfs.object.get(cid, callback)
27+
##### `JavaScript` - ipfs.dag.get(cid, callback)
2928

30-
`cid` is a [CID][https://github.com/ipfs/js-cid] instance.
29+
- `cid` is a [CID][https://github.com/ipfs/js-cid] instance.
3130

3231
`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.
3332

3433
If no `callback` is passed, a [promise][] is returned.
34+
35+
#### `dag.resolve`
36+
37+
> Resolves an IPLD path
38+
39+
##### `Go` **WIP**
40+
41+
##### `JavaScript` - ipfs.dag.resolve(cid, path, callback)
42+
43+
- `cid` is a [CID][https://github.com/ipfs/js-cid] instance.
44+
- `path` is a String that represents a valid path to be resolved
45+
46+
`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.
47+
48+
If no `callback` is passed, a [promise][] is returned.
49+
50+
#### `dag.remove`
51+
52+
> Deletes an IPLD node
53+
54+
##### `Go` **WIP**
55+
56+
##### `JavaScript` - ipfs.dag.rm(cid, callback)
57+
58+
- `cid` is a [CID][https://github.com/ipfs/js-cid] instance.
59+
60+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
61+
62+
If no `callback` is passed, a [promise][] is returned.

src/dag.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 DAGNode = dagPB.DAGNode
9+
const dagCBOR = require('ipld-dag-pb')
10+
// const series = require('async/series')
11+
12+
module.exports = (common) => {
13+
describe('.dag', () => {
14+
let ipfs
15+
16+
before(function (done) {
17+
// CI is slow
18+
this.timeout(20 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist
22+
factory.spawnNode((err, node) => {
23+
expect(err).to.not.exist
24+
ipfs = node
25+
done()
26+
})
27+
})
28+
})
29+
30+
after((done) => {
31+
common.teardown(done)
32+
})
33+
34+
describe('callback API', () => {
35+
let pbNode
36+
let cborNode
37+
38+
before((done) => {
39+
const someData = new Buffer('some data')
40+
41+
pbNode = DAGNode.create(someData, (err, node) => {
42+
expect(err).to.not.exist
43+
pbNode = node
44+
done()
45+
})
46+
47+
cborNode = {
48+
data: someData
49+
}
50+
})
51+
52+
describe('.put', () => {
53+
it('dag-pb with default hash func (sha2-256)', (done) => {
54+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', done)
55+
})
56+
57+
it('dag-pb with custom hash func (sha3-512)', (done) => {
58+
ipfs.dag.put(pbNode, 'dag-pb', 'sha3-512', done)
59+
})
60+
61+
it('dag-pb node with wrong multicodec', (done) => {
62+
ipfs.dag.put(pbNode, 'dag-cbor', 'sha3-512', (err) => {
63+
expect(err).to.exist
64+
done()
65+
})
66+
})
67+
68+
it('dag-cbor with default hash func (sha2-256)', (done) => {
69+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', done)
70+
})
71+
72+
it('dag-cbor with custom hash func (sha3-512)', (done) => {
73+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha3-512', done)
74+
})
75+
76+
it('dag-cbor node with wrong multicodec', (done) => {
77+
ipfs.dag.put(cborNode, 'dag-pb', 'sha3-512', (err) => {
78+
expect(err).to.exist
79+
done()
80+
})
81+
})
82+
})
83+
84+
describe('.get', () => {
85+
let pbNode
86+
let cborNode
87+
88+
before((done) => {
89+
const someData = new Buffer('some other data')
90+
91+
pbNode = DAGNode.create(someData, (err, node) => {
92+
expect(err).to.not.exist
93+
pbNode = node
94+
done()
95+
})
96+
97+
cborNode = {
98+
data: someData
99+
}
100+
})
101+
102+
it('dag-pb node', (done) => {
103+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', (err) => {
104+
expect(err).to.not.exist
105+
dagPB.util.cid(pbNode, (err, cid) => {
106+
expect(err).to.not.exist
107+
ipfs.dag.get(cid, (err, node) => {
108+
expect(err).to.not.exist
109+
expect(pbNode.toJSON()).to.eql(node.toJSON())
110+
done()
111+
})
112+
})
113+
})
114+
})
115+
116+
it('dag-cbor node', (done) => {
117+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', (err) => {
118+
expect(err).to.not.exist
119+
dagCBOR.util.cid(cborNode, (err, cid) => {
120+
expect(err).to.not.exist
121+
ipfs.dag.get(cid, (err, node) => {
122+
expect(err).to.not.exist
123+
expect(cborNode).to.eql(node)
124+
done()
125+
})
126+
})
127+
})
128+
})
129+
})
130+
131+
describe('.resolve', () => {})
132+
133+
describe('.rm', () => {
134+
let pbNode
135+
136+
before((done) => {
137+
const someData = new Buffer('some other data')
138+
139+
pbNode = DAGNode.create(someData, (err, node) => {
140+
expect(err).to.not.exist
141+
pbNode = node
142+
done()
143+
})
144+
})
145+
146+
it('dag-pb node', (done) => {
147+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', (err) => {
148+
expect(err).to.not.exist
149+
dagPB.util.cid(pbNode, (err, cid) => {
150+
expect(err).to.not.exist
151+
ipfs.dag.get(cid, (err, node) => {
152+
expect(err).to.not.exist
153+
ipfs.dag.rm(cid, done)
154+
// TODO When we get timeouts in js-ipfs, try to fetch again
155+
// and observe it timesout without the node
156+
})
157+
})
158+
})
159+
})
160+
})
161+
})
162+
163+
describe('promise API', () => {
164+
describe('.put', () => {})
165+
describe('.get', () => {})
166+
describe('.resolve', () => {})
167+
describe('.rm', () => {})
168+
})
169+
})
170+
}

0 commit comments

Comments
 (0)