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

Commit c8774cd

Browse files
authored
Merge pull request #112 from ipfs/feat/dag-api-basics
feat: DAG API basics (get, put, rm)
2 parents 02b16d5 + 0a5d629 commit c8774cd

File tree

4 files changed

+152
-10
lines changed

4 files changed

+152
-10
lines changed

API/dag/README.md

Lines changed: 8 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,9 +24,9 @@ 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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
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",
3637
"ipld-dag-pb": "^0.9.3",
3738
"multiaddr": "^2.1.1",
3839
"multihashes": "^0.3.1",
@@ -52,4 +53,4 @@
5253
5354
"nginnever <[email protected]>"
5455
]
55-
}
56+
}

src/dag.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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-cbor')
10+
11+
module.exports = (common) => {
12+
describe.only('.dag (basics)', () => {
13+
let ipfs
14+
15+
before(function (done) {
16+
// CI is slow
17+
this.timeout(20 * 1000)
18+
19+
common.setup((err, factory) => {
20+
expect(err).to.not.exist
21+
factory.spawnNode((err, node) => {
22+
expect(err).to.not.exist
23+
ipfs = node
24+
done()
25+
})
26+
})
27+
})
28+
29+
after((done) => {
30+
common.teardown(done)
31+
})
32+
33+
describe('callback API', () => {
34+
let pbNode
35+
let cborNode
36+
37+
before((done) => {
38+
const someData = new Buffer('some data')
39+
40+
pbNode = DAGNode.create(someData, (err, node) => {
41+
expect(err).to.not.exist
42+
pbNode = node
43+
done()
44+
})
45+
46+
cborNode = {
47+
data: someData
48+
}
49+
})
50+
51+
describe('.put', () => {
52+
it('dag-pb with default hash func (sha2-256)', (done) => {
53+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', done)
54+
})
55+
56+
it('dag-pb with custom hash func (sha3-512)', (done) => {
57+
ipfs.dag.put(pbNode, 'dag-pb', 'sha3-512', done)
58+
})
59+
60+
/*
61+
* This works because dag-cbor will just treat pbNode as a regular object
62+
it.skip('dag-pb node with wrong multicodec', (done) => {
63+
// This works because dag-cbor will just treat pbNode as a
64+
// regular object
65+
ipfs.dag.put(pbNode, 'dag-cbor', 'sha3-512', (err) => {
66+
expect(err).to.exist
67+
done()
68+
})
69+
})
70+
*/
71+
72+
it('dag-cbor with default hash func (sha2-256)', (done) => {
73+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', done)
74+
})
75+
76+
it('dag-cbor with custom hash func (sha3-512)', (done) => {
77+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha3-512', done)
78+
})
79+
80+
it('dag-cbor node with wrong multicodec', (done) => {
81+
ipfs.dag.put(cborNode, 'dag-pb', 'sha3-512', (err) => {
82+
expect(err).to.exist
83+
done()
84+
})
85+
})
86+
})
87+
88+
describe('.get', () => {
89+
let pbNode
90+
let cborNode
91+
92+
before((done) => {
93+
const someData = new Buffer('some other data')
94+
95+
pbNode = DAGNode.create(someData, (err, node) => {
96+
expect(err).to.not.exist
97+
pbNode = node
98+
done()
99+
})
100+
101+
cborNode = {
102+
data: someData
103+
}
104+
})
105+
106+
it('dag-pb node', (done) => {
107+
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', (err) => {
108+
expect(err).to.not.exist
109+
dagPB.util.cid(pbNode, (err, cid) => {
110+
expect(err).to.not.exist
111+
ipfs.dag.get(cid, (err, node) => {
112+
expect(err).to.not.exist
113+
expect(pbNode.toJSON()).to.eql(node.toJSON())
114+
done()
115+
})
116+
})
117+
})
118+
})
119+
120+
it('dag-cbor node', (done) => {
121+
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', (err) => {
122+
expect(err).to.not.exist
123+
dagCBOR.util.cid(cborNode, (err, cid) => {
124+
expect(err).to.not.exist
125+
ipfs.dag.get(cid, (err, node) => {
126+
expect(err).to.not.exist
127+
expect(cborNode).to.eql(node)
128+
done()
129+
})
130+
})
131+
})
132+
})
133+
})
134+
})
135+
136+
describe('promise API', () => {
137+
describe('.put', () => {})
138+
describe('.get', () => {})
139+
})
140+
})
141+
}

src/index.js

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

0 commit comments

Comments
 (0)