This repository was archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
56 lines (48 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const traverse = require('pull-traverse')
const pull = require('pull-stream')
const CID = require('cids')
const isIPFS = require('is-ipfs')
const util = require('./../util')
const switchType = util.switchType
const cleanMultihash = util.cleanMultihash
const dirExporter = require('./dir')
const fileExporter = require('./file')
module.exports = (hash, ipldResolver, options) => {
if (!isIPFS.multihash(hash)) {
return pull.error(new Error('not valid multihash'))
}
hash = cleanMultihash(hash)
options = options || {}
function visitor (item) {
if (!item.hash) {
// having no hash means that this visitor got a file object
// which needs no further resolving.
// No further resolving means that the visitor does not
// need to do anyting else, so he's returning
// an empty stream
// TODO: perhaps change the pull streams construct.
// Instead of traversing with a visitor, consider recursing.
return pull.empty()
}
return pull(
ipldResolver.getStream(new CID(item.hash)),
pull.map((node) => switchType(
node,
() => dirExporter(node, item.path, ipldResolver),
() => fileExporter(node, item.path, ipldResolver)
)),
pull.flatten()
)
}
// Traverse the DAG
return pull(
ipldResolver.getStream(new CID(hash)),
pull.map((node) => switchType(
node,
() => traverse.widthFirst({path: hash, hash}, visitor),
() => fileExporter(node, hash, ipldResolver)
)),
pull.flatten()
)
}