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

Commit b77240b

Browse files
committed
exporter: maxDepth instead of recursive flag
1 parent 7345240 commit b77240b

File tree

7 files changed

+29
-32
lines changed

7 files changed

+29
-32
lines changed

src/exporter/dir-flat.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
'use strict'
22

33
const pull = require('pull-stream')
4-
const CID = require('cids')
54
const cat = require('pull-cat')
65

76
// Logic to export a unixfs directory.
87
module.exports = dirExporter
98

10-
function dirExporter (node, name, pathRest, resolve) {
9+
function dirExporter (node, path, pathRest, resolve) {
1110
const accepts = pathRest[0]
1211

1312
const dir = {
14-
path: name,
13+
path: path,
1514
hash: node.multihash
1615
}
1716

1817
const streams = [
1918
pull(
2019
pull.values(node.links),
2120
pull.map((link) => ({
22-
path: name + '/' + link.name,
21+
size: link.size,
22+
name: link.name,
23+
path: path + '/' + link.name,
2324
multihash: link.multihash,
2425
linkName: link.name,
2526
pathRest: pathRest.slice(1)

src/exporter/dir-hamt-sharded.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
'use strict'
22

33
const pull = require('pull-stream')
4-
const CID = require('cids')
54
const cat = require('pull-cat')
65
const cleanHash = require('./clean-multihash')
76

87
// Logic to export a unixfs directory.
98
module.exports = shardedDirExporter
109

11-
function shardedDirExporter (node, name, pathRest, resolve, dag, parent) {
10+
function shardedDirExporter (node, path, pathRest, resolve, dag, parent) {
1211
let dir
13-
if (!parent || parent.path !== name) {
12+
if (!parent || parent.path !== path) {
1413
dir = [{
15-
path: name,
14+
path: path,
1615
hash: cleanHash(node.multihash)
1716
}]
1817
}
@@ -23,7 +22,7 @@ function shardedDirExporter (node, name, pathRest, resolve, dag, parent) {
2322
pull.map((link) => {
2423
// remove the link prefix (2 chars for the bucket index)
2524
const p = link.name.substring(2)
26-
const pp = p ? name + '/' + p : name
25+
const pp = p ? path + '/' + p : path
2726
let accept = true
2827
let fromPathRest = false
2928

src/exporter/file.js

Lines changed: 3 additions & 3 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, name, pathRest, resolve, dag) => {
10+
module.exports = (node, path, pathRest, resolve, dag) => {
1111
function getData (node) {
1212
try {
1313
const file = UnixFS.unmarshal(node.data)
@@ -27,7 +27,7 @@ module.exports = (node, name, pathRest, resolve, dag) => {
2727

2828
const accepts = pathRest[0]
2929

30-
if (accepts !== undefined && accepts !== name) {
30+
if (accepts !== undefined && accepts !== path) {
3131
return pull.empty()
3232
}
3333

@@ -39,7 +39,7 @@ module.exports = (node, name, pathRest, resolve, dag) => {
3939
const file = UnixFS.unmarshal(node.data)
4040
return pull.values([{
4141
content: content,
42-
path: name,
42+
path: path,
4343
hash: node.multihash,
4444
size: file.fileSize()
4545
}])

src/exporter/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const pull = require('pull-stream')
44
const CID = require('cids')
5-
const b58Encode = require('bs58').encode
65

76
const createResolver = require('./resolve').createResolver
87

@@ -37,11 +36,10 @@ function pathBaseAndRest (path) {
3736
}
3837

3938
const defaultOptions = {
40-
recurse: true
39+
maxDepth: Infinity
4140
}
4241

4342
module.exports = (path, dag, _options) => {
44-
4543
const options = Object.assign({}, defaultOptions, _options)
4644

4745
let dPath
@@ -51,6 +49,9 @@ module.exports = (path, dag, _options) => {
5149
return pull.error(err)
5250
}
5351

52+
const pathLengthToCut = join(
53+
[dPath.base].concat(dPath.rest.slice(0, dPath.rest.length - 1))).length
54+
5455
return pull(
5556
pull.values([{
5657
multihash: new CID(dPath.base),
@@ -60,21 +61,21 @@ module.exports = (path, dag, _options) => {
6061
}]),
6162
createResolver(dag, options),
6263
pull.map((node) => ({
64+
name: node.name,
6365
path: finalPathFor(node),
6466
size: node.size,
65-
hash: node.hash,
66-
content: node.content
67+
hash: node.hash || node.multihash,
68+
content: node.content,
69+
type: node.type
6770
}))
6871
)
6972

70-
function finalPathFor(node) {
73+
function finalPathFor (node) {
7174
if (!dPath.rest.length) {
7275
return node.path
7376
}
7477

75-
const cutElements = [dPath.base].concat(dPath.rest.slice(0, dPath.rest.length - 1))
76-
const lengthToCut = join(cutElements).length
77-
let retPath = node.path.substring(lengthToCut)
78+
let retPath = node.path.substring(pathLengthToCut)
7879
if (retPath.charAt(0) === '/') {
7980
retPath = retPath.substring(1)
8081
}
@@ -85,11 +86,11 @@ module.exports = (path, dag, _options) => {
8586
}
8687
}
8788

88-
function join(paths) {
89+
function join (paths) {
8990
return paths.reduce((acc, path) => {
9091
if (acc.length) {
9192
acc += '/'
9293
}
9394
return acc + path
9495
}, '')
95-
}
96+
}

src/exporter/object.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,3 @@ module.exports = (node, name, pathRest, resolve, dag, parent) => {
2626
return pull.error(new Error('invalid node type'))
2727
}
2828
}
29-
30-
function sanitizeCID (cid) {
31-
return new CID(cid.version, cid.codec, cid.multihash)
32-
}

src/exporter/resolve.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ module.exports = Object.assign({
1818
}, resolvers)
1919

2020
function createResolver (dag, options, depth, parent) {
21-
if (! depth) {
21+
if (!depth) {
2222
depth = 0
2323
}
2424

25-
if (!options.recurse && depth > 0) {
25+
if (depth > options.maxDepth) {
2626
return pull.map(identity)
2727
}
2828

@@ -42,14 +42,14 @@ function createResolver (dag, options, depth, parent) {
4242
pull.flatten()
4343
)
4444

45-
function resolve (node, hash, pathRest, parentNode) {
45+
function resolve (node, path, pathRest, parentNode) {
4646
const type = typeOf(node)
4747
const nodeResolver = resolvers[type]
4848
if (!nodeResolver) {
4949
return pull.error(new Error('Unkown node type ' + type))
5050
}
5151
const resolveDeep = createResolver(dag, options, depth + 1, node)
52-
return nodeResolver(node, hash, pathRest, resolveDeep, dag, parentNode)
52+
return nodeResolver(node, path, pathRest, resolveDeep, dag, parentNode)
5353
}
5454
}
5555

test/exporter-subtree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const smallFile = loadFixture(__dirname, 'fixtures/200Bytes.txt')
1717

1818
module.exports = (repo) => {
1919
describe('exporter subtree', () => {
20-
this.timeout(10 * 1000)
20+
// this.timeout(10 * 1000)
2121

2222
let ipldResolver
2323

0 commit comments

Comments
 (0)