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

Commit e238443

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

File tree

2 files changed

+138
-9
lines changed

2 files changed

+138
-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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
before((done) => {})
86+
it.skip('dag-pb node', (done) => {})
87+
it.skip('dag-cbor node', (done) => {})
88+
})
89+
90+
describe('.resolve', () => {})
91+
describe('.rm', () => {})
92+
})
93+
94+
describe('promise API', () => {
95+
describe('.put', () => {})
96+
describe('.get', () => {})
97+
describe('.resolve', () => {})
98+
describe('.rm', () => {})
99+
})
100+
})
101+
}

0 commit comments

Comments
 (0)