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

Commit d7b4105

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

File tree

3 files changed

+219
-9
lines changed

3 files changed

+219
-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: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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.only('.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.skip('dag-pb with custom hash func (sha3-512)', (done) => {
58+
ipfs.dag.put(pbNode, 'dag-pb', 'sha3-512', done)
59+
})
60+
61+
it.skip('dag-pb node with wrong multicodec', (done) => {
62+
// This works because dag-cbor will just treat pbNode as a
63+
// regular object
64+
ipfs.dag.put(pbNode, 'dag-cbor', 'sha3-512', (err) => {
65+
expect(err).to.exist
66+
done()
67+
})
68+
})
69+
70+
it('dag-cbor with default hash func (sha2-256)', (done) => {
71+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', done)
72+
})
73+
74+
it.skip('dag-cbor with custom hash func (sha3-512)', (done) => {
75+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha3-512', done)
76+
})
77+
78+
it('dag-cbor node with wrong multicodec', (done) => {
79+
ipfs.dag.put(cborNode, 'dag-pb', 'sha3-512', (err) => {
80+
expect(err).to.exist
81+
done()
82+
})
83+
})
84+
})
85+
86+
describe('.get', () => {
87+
let pbNode
88+
let cborNode
89+
90+
before((done) => {
91+
const someData = new Buffer('some other data')
92+
93+
pbNode = DAGNode.create(someData, (err, node) => {
94+
expect(err).to.not.exist
95+
pbNode = node
96+
done()
97+
})
98+
99+
cborNode = {
100+
data: someData
101+
}
102+
})
103+
104+
it('dag-pb node', (done) => {
105+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', (err) => {
106+
expect(err).to.not.exist
107+
dagPB.util.cid(pbNode, (err, cid) => {
108+
expect(err).to.not.exist
109+
ipfs.dag.get(cid, (err, node) => {
110+
expect(err).to.not.exist
111+
expect(pbNode.toJSON()).to.eql(node.toJSON())
112+
done()
113+
})
114+
})
115+
})
116+
})
117+
118+
it('dag-cbor node', (done) => {
119+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', (err) => {
120+
expect(err).to.not.exist
121+
dagCBOR.util.cid(cborNode, (err, cid) => {
122+
expect(err).to.not.exist
123+
ipfs.dag.get(cid, (err, node) => {
124+
expect(err).to.not.exist
125+
expect(cborNode).to.eql(node)
126+
done()
127+
})
128+
})
129+
})
130+
})
131+
})
132+
133+
describe('.resolve', () => {
134+
it.skip('dag-pb local scope', (done) => {})
135+
it.skip('dag-pb one level', (done) => {})
136+
it.skip('dag-pb two levels', (done) => {})
137+
it.skip('dag-cbor local scope', (done) => {})
138+
it.skip('dag-cbor one level', (done) => {})
139+
it.skip('dag-cbor two levels', (done) => {})
140+
it.skip('from dag-pb to dag-cbor', (done) => {})
141+
it.skip('from dag-cbor to dag-pb', (done) => {})
142+
})
143+
144+
describe('.rm', () => {
145+
let pbNode
146+
147+
before((done) => {
148+
const someData = new Buffer('some other data')
149+
150+
pbNode = DAGNode.create(someData, (err, node) => {
151+
expect(err).to.not.exist
152+
pbNode = node
153+
done()
154+
})
155+
})
156+
157+
it('dag-pb node', (done) => {
158+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', (err) => {
159+
expect(err).to.not.exist
160+
dagPB.util.cid(pbNode, (err, cid) => {
161+
expect(err).to.not.exist
162+
ipfs.dag.get(cid, (err, node) => {
163+
expect(err).to.not.exist
164+
ipfs.dag.rm(cid, done)
165+
// TODO When we get timeouts in js-ipfs, try to fetch again
166+
// and observe it timesout without the node
167+
})
168+
})
169+
})
170+
})
171+
})
172+
})
173+
174+
describe('promise API', () => {
175+
describe('.put', () => {})
176+
describe('.get', () => {})
177+
describe('.resolve', () => {})
178+
describe('.rm', () => {})
179+
})
180+
})
181+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ exports.generic = require('./generic')
88
exports.swarm = require('./swarm')
99
exports.block = require('./block')
1010
exports.dht = require('./dht')
11+
exports.dag = require('./dag')

0 commit comments

Comments
 (0)