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

Commit 8004b2b

Browse files
authored
More DAG API goodness (#123)
* feat: update dag.put spec * feat: update dag.get spec * feat: tests for all permutations of dag.get * feat: add dag.ls and dag.tree spec * feat: update tree spec * docs: add examples to put and get * fix: examples links * test: .tree tests everywhere
1 parent 13f2432 commit 8004b2b

File tree

3 files changed

+279
-35
lines changed

3 files changed

+279
-35
lines changed

API/dag/README.md

+102-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,28 @@ dag API
1313

1414
- `dagNode` - a DAG node that follows one of the supported IPLD formats.
1515
- `options` - a object that might contain the follwing values:
16-
- `format` - The IPLD format multicodec.
17-
- `hashAlg` - The hash algorithm to be used over the serialized dagNode.
18-
- `cid` - the CID of the node passed.
16+
- `format` - The IPLD format multicodec.
17+
- `hashAlg` - The hash algorithm to be used over the serialized dagNode.
18+
- or
19+
- `cid` - the CID of the node passed.
1920
- **Note** - You should only pass the CID or the format + hashAlg pair and not both
20-
- `callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
21+
- `callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and CID is the CID generated through the process or the one that was passed
2122

2223
If no `callback` is passed, a [promise][] is returned.
2324

25+
**Example:**
26+
27+
```JavaScript
28+
const obj = { simple: 'object' }
29+
30+
ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha3-512' }, (err, cid) => {
31+
console.log(cid.toBaseEncodedString())
32+
// zdpuAzE1oAAMpsfdoexcJv6PmL9UhE8nddUYGU32R98tzV5fv
33+
})
34+
```
35+
36+
A great source of [examples][] can be found in the tests for this API.
37+
2438
#### `dag.get`
2539

2640
> Retrieve an IPLD format node
@@ -29,7 +43,10 @@ If no `callback` is passed, a [promise][] is returned.
2943

3044
##### `JavaScript` - ipfs.dag.get(cid [, path, options], callback)
3145

32-
- `cid` - a [CID][https://github.com/ipfs/js-cid] instance.
46+
- `cid` - can be one of the following:
47+
- a [CID](https://github.com/ipfs/js-cid) instance.
48+
- a CID in its String format (i.e: zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y)
49+
- a CID in its String format concatenated with the path to be resolved
3350
- `path` - the path to be resolved. Optional.
3451
- `options` - a object that might contain the following values:
3552
- `localResolve` - bool - if set to true, it will avoid resolving through different objects.
@@ -38,3 +55,83 @@ If no `callback` is passed, a [promise][] is returned.
3855

3956
- `value` - the value or node that was fetched during the get operation.
4057
- `remainderPath` - The remainder of the Path that the node was unable to resolve or what was left in a localResolve scenario.
58+
59+
If no `callback` is passed, a [promise][] is returned.
60+
61+
**Example:**
62+
63+
```JavaScript
64+
// example obj
65+
const obj = {
66+
a: 1,
67+
b: [1, 2, 3],
68+
c: {
69+
ca: [5, 6, 7],
70+
cb: 'foo"'
71+
}
72+
}
73+
74+
ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
75+
console.log(cid.toBaseEncodedString())
76+
// zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5
77+
})
78+
79+
function errOrLog(err, result) {
80+
if (err) {
81+
console.error('error: ' + err)
82+
} else {
83+
console.log(result.value)
84+
}
85+
}
86+
87+
ipfs.dag.get('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/a', errOrLog)
88+
// Returns:
89+
// 1
90+
91+
ipfs.dag.get('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/b', errOrLog)
92+
// Returns:
93+
// [1, 2, 3]
94+
95+
ipfs.dag.get('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/c', errOrLog)
96+
// Returns:
97+
// {
98+
// ca: [5, 6, 7],
99+
// cb: 'foo'
100+
// }
101+
102+
ipfs.dag.get('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5/c/ca/1', errOrLog)
103+
// Returns:
104+
// 6
105+
```
106+
107+
A great source of [examples][] can be found in the tests for this API.
108+
109+
#### `dag.tree`
110+
111+
> Enumerate all the entries in a graph
112+
113+
##### `Go` **WIP**
114+
115+
##### `JavaScript` - ipfs.dag.tree(cid [, path, options], callback)
116+
117+
- `cid` - can be one of the following:
118+
- a [CID](https://github.com/ipfs/js-cid) instance.
119+
- a CID in its String format (i.e: zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y)
120+
- a CID in its String format concatenated with the path to be resolved
121+
- `path` - the path to be resolved. Optional.
122+
- `options` - a object that might contain the following values:
123+
- `recursive` - bool - if set to true, it will follow the links and continuously run tree on them, returning all the paths in the graph.
124+
125+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful and `result` is an Array with the paths passed.
126+
127+
If no `callback` is passed, a [promise][] is returned.
128+
129+
**Example:**
130+
131+
```JavaScript
132+
```
133+
134+
A great source of [examples][] can be found in the tests for this API.
135+
136+
137+
[examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/dag.js

package.json

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@
2626
},
2727
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
2828
"dependencies": {
29-
"async": "^2.1.4",
29+
"async": "^2.1.5",
3030
"bl": "^1.2.0",
3131
"bs58": "^4.0.0",
3232
"chai": "^3.5.0",
33+
"cids": "^0.4.1",
3334
"concat-stream": "^1.6.0",
3435
"detect-node": "^2.0.3",
35-
"ipfs-block": "^0.5.4",
36-
"ipld-dag-cbor": "^0.9.0",
37-
"ipld-dag-pb": "^0.9.4",
38-
"multiaddr": "^2.2.0",
39-
"multihashes": "^0.3.2",
36+
"ipfs-block": "^0.5.5",
37+
"ipld-dag-cbor": "^0.9.1",
38+
"ipld-dag-pb": "^0.9.5",
39+
"multiaddr": "^2.2.1",
40+
"multihashes": "^0.4.3",
4041
"pull-stream": "^3.5.0",
41-
"readable-stream": "2.2.2"
42+
"readable-stream": "1.1.14"
4243
},
4344
"devDependencies": {
4445
"aegir": "^10.0.0"
@@ -54,4 +55,4 @@
5455
5556
"nginnever <[email protected]>"
5657
]
57-
}
58+
}

0 commit comments

Comments
 (0)