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

Commit 7de69fe

Browse files
committed
exporter: exposing name and depth
1 parent b77240b commit 7de69fe

File tree

6 files changed

+58
-33
lines changed

6 files changed

+58
-33
lines changed

src/exporter/dir-flat.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ const cat = require('pull-cat')
66
// Logic to export a unixfs directory.
77
module.exports = dirExporter
88

9-
function dirExporter (node, path, pathRest, resolve) {
9+
function dirExporter (node, name, path, pathRest, resolve, dag, parent, depth) {
1010
const accepts = pathRest[0]
1111

1212
const dir = {
13+
name: name,
14+
depth: depth,
1315
path: path,
14-
hash: node.multihash
16+
hash: node.multihash,
17+
type: 'dir'
1518
}
1619

1720
const streams = [
1821
pull(
1922
pull.values(node.links),
2023
pull.map((link) => ({
24+
depth: depth + 1,
2125
size: link.size,
2226
name: link.name,
2327
path: path + '/' + link.name,
2428
multihash: link.multihash,
2529
linkName: link.name,
26-
pathRest: pathRest.slice(1)
30+
pathRest: pathRest.slice(1),
31+
type: 'dir'
2732
})),
2833
pull.filter((item) => accepts === undefined || item.linkName === accepts),
2934
resolve

src/exporter/dir-hamt-sharded.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ const cleanHash = require('./clean-multihash')
77
// Logic to export a unixfs directory.
88
module.exports = shardedDirExporter
99

10-
function shardedDirExporter (node, path, pathRest, resolve, dag, parent) {
10+
function shardedDirExporter (node, name, path, pathRest, resolve, dag, parent, depth) {
1111
let dir
12-
if (!parent || parent.path !== path) {
13-
dir = [{
12+
if (!parent || (parent.path !== path)) {
13+
dir = {
14+
name: name,
15+
depth: depth,
1416
path: path,
15-
hash: cleanHash(node.multihash)
16-
}]
17+
hash: cleanHash(node.multihash),
18+
type: 'dir'
19+
}
1720
}
1821

1922
const streams = [
@@ -24,20 +27,18 @@ function shardedDirExporter (node, path, pathRest, resolve, dag, parent) {
2427
const p = link.name.substring(2)
2528
const pp = p ? path + '/' + p : path
2629
let accept = true
27-
let fromPathRest = false
2830

2931
if (p && pathRest.length) {
30-
fromPathRest = true
3132
accept = (p === pathRest[0])
3233
}
3334
if (accept) {
3435
return {
35-
fromPathRest: fromPathRest,
36+
depth: depth + 1,
3637
name: p,
3738
path: pp,
3839
multihash: link.multihash,
3940
pathRest: p ? pathRest.slice(1) : pathRest,
40-
parent: (dir && dir[0]) || parent
41+
parent: dir || parent
4142
}
4243
} else {
4344
return ''
@@ -49,7 +50,7 @@ function shardedDirExporter (node, path, pathRest, resolve, dag, parent) {
4950
]
5051

5152
if (!pathRest.length) {
52-
streams.unshift(pull.values(dir))
53+
streams.unshift(pull.values([dir]))
5354
}
5455

5556
return cat(streams)

src/exporter/file.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pull = require('pull-stream')
77
const paramap = require('pull-paramap')
88

99
// Logic to export a single (possibly chunked) unixfs file.
10-
module.exports = (node, path, pathRest, resolve, dag) => {
10+
module.exports = (node, name, path, pathRest, resolve, dag, parent, depth) => {
1111
function getData (node) {
1212
try {
1313
const file = UnixFS.unmarshal(node.data)
@@ -38,9 +38,12 @@ module.exports = (node, path, pathRest, resolve, dag) => {
3838

3939
const file = UnixFS.unmarshal(node.data)
4040
return pull.values([{
41+
depth: depth,
4142
content: content,
43+
name: name,
4244
path: path,
4345
hash: node.multihash,
44-
size: file.fileSize()
46+
size: file.fileSize(),
47+
type: 'file'
4548
}])
4649
}

src/exporter/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@ module.exports = (path, dag, _options) => {
5757
multihash: new CID(dPath.base),
5858
name: dPath.base,
5959
path: dPath.base,
60-
pathRest: dPath.rest
60+
pathRest: dPath.rest,
61+
depth: 0
6162
}]),
6263
createResolver(dag, options),
63-
pull.map((node) => ({
64-
name: node.name,
65-
path: finalPathFor(node),
66-
size: node.size,
67-
hash: node.hash || node.multihash,
68-
content: node.content,
69-
type: node.type
70-
}))
64+
pull.filter(Boolean),
65+
pull.map((node) => {
66+
return {
67+
depth: node.depth,
68+
name: node.name,
69+
path: finalPathFor(node),
70+
size: node.size,
71+
hash: node.hash || node.multihash,
72+
content: node.content,
73+
type: node.type
74+
}
75+
})
7176
)
7277

7378
function finalPathFor (node) {

src/exporter/object.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
const CID = require('cids')
44
const pull = require('pull-stream')
55

6-
module.exports = (node, name, pathRest, resolve, dag, parent) => {
6+
module.exports = (node, name, path, pathRest, resolve, dag, parent, depth) => {
77
let newNode
88
if (pathRest.length) {
99
const pathElem = pathRest[0]
1010
newNode = node[pathElem]
11-
const newName = name + '/' + pathElem
11+
const newName = path + '/' + pathElem
1212
if (!newNode) {
1313
return pull.error('not found')
1414
}
1515
const isCID = CID.isCID(newNode)
1616
return pull(
1717
pull.values([{
18+
depth: depth,
19+
name: pathElem,
1820
path: newName,
1921
pathRest: pathRest.slice(1),
2022
multihash: isCID && newNode,

src/exporter/resolve.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,37 @@ function createResolver (dag, options, depth, parent) {
2828

2929
return pull(
3030
paramap((item, cb) => {
31+
if ((typeof item.depth) !== 'number') {
32+
return pull.error(new Error('no depth'))
33+
}
3134
if (item.object) {
32-
return cb(null, resolve(item.object, item.path, item.pathRest, dag, item.parent || parent))
35+
return cb(null, resolveItem(item.object, item))
3336
}
3437
dag.get(new CID(item.multihash), (err, node) => {
3538
if (err) {
3639
return cb(err)
3740
}
38-
const name = item.fromPathRest ? item.name : item.path
39-
cb(null, resolve(node.value, name, item.pathRest, dag, item.parent || parent))
41+
// const name = item.fromPathRest ? item.name : item.path
42+
cb(null, resolveItem(node.value, item))
4043
})
4144
}),
42-
pull.flatten()
45+
pull.flatten(),
46+
pull.filter(Boolean),
47+
pull.filter((node) => node.depth <= options.maxDepth)
4348
)
4449

45-
function resolve (node, path, pathRest, parentNode) {
50+
function resolveItem (node, item) {
51+
return resolve(node, item.name, item.path, item.pathRest, dag, item.parent || parent, item.depth)
52+
}
53+
54+
function resolve (node, name, path, pathRest, dag, parentNode, depth) {
4655
const type = typeOf(node)
4756
const nodeResolver = resolvers[type]
4857
if (!nodeResolver) {
4958
return pull.error(new Error('Unkown node type ' + type))
5059
}
51-
const resolveDeep = createResolver(dag, options, depth + 1, node)
52-
return nodeResolver(node, path, pathRest, resolveDeep, dag, parentNode)
60+
const resolveDeep = createResolver(dag, options, depth, node)
61+
return nodeResolver(node, name, path, pathRest, resolveDeep, dag, parentNode, depth)
5362
}
5463
}
5564

0 commit comments

Comments
 (0)