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

Commit

Permalink
fix: stop export visitor from trying to resolve leaf object (#130)
Browse files Browse the repository at this point in the history
* fixing recursive get, stopping visitor from trying to resolve leaf objects

* fixed typo on comment

* test for exporting deeply nested dirs
  • Loading branch information
pgte authored and daviddias committed Jan 30, 2017
1 parent e1c6c9e commit 651f113
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/exporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ module.exports = (hash, ipldResolver, options) => {
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(
Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ describe('IPFS data importing tests on the Browser', function () {
require('./test-importer')(repo)
require('./test-import-export')(repo)
require('./test-hash-parity-with-go-ipfs')(repo)
require('./test-nested-dir-import-export')(repo)
})
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ describe('IPFS UnixFS Engine', () => {
require('./test-importer')(repo)
require('./test-import-export')(repo)
require('./test-hash-parity-with-go-ipfs')(repo)
require('./test-nested-dir-import-export')(repo)
})
118 changes: 118 additions & 0 deletions test/test-nested-dir-import-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const BlockService = require('ipfs-block-service')
const IPLDResolver = require('ipld-resolver')
const pull = require('pull-stream')
const mh = require('multihashes')
const map = require('async/map')

const unixFSEngine = require('./../')

module.exports = (repo) => {
describe('import adn export big nested dir', () => {
const rootHash = 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK'
let ipldResolver

before(() => {
const bs = new BlockService(repo)
ipldResolver = new IPLDResolver(bs)
})

it('imports', (done) => {
pull(
pull.values([
{ path: 'a/b/c/d/e', content: pull.values([new Buffer('banana')]) },
{ path: 'a/b/c/d/f', content: pull.values([new Buffer('strawberry')]) },
{ path: 'a/b/g', content: pull.values([new Buffer('ice')]) },
{ path: 'a/b/h', content: pull.values([new Buffer('cream')]) }
]),
unixFSEngine.importer(ipldResolver),
pull.collect((err, files) => {
expect(err).to.not.exist
expect(files.map(normalizeNode).sort(byPath)).to.be.eql([
{ path: 'a/b/h',
multihash: 'QmWHMpCtdNjemT2F3SjyrmnBXQXwEohaZd4apcbFBhbFRC' },
{ path: 'a/b/g',
multihash: 'QmQGwYzzTPcbqTiy2Nbp88gqqBqCWY4QZGfen45LFZkD5n' },
{ path: 'a/b/c/d/f',
multihash: 'QmNVHs2dy7AjGUotsubWVncRsD3SpRXm8MgmCCQTVdVACz' },
{ path: 'a/b/c/d/e',
multihash: 'QmYPbDKwc7oneCcEc6BcRSN5GXthTGWUCd19bTCyP9u3vH' },
{ path: 'a/b/c/d',
multihash: 'QmQGDXr3ysARM38n7h79Tx7yD3YxuzcnZ1naG71WMojPoj' },
{ path: 'a/b/c',
multihash: 'QmYTVcjYpN3hQLtJstCPE8hhEacAYjWAuTmmAAXoonamuE' },
{ path: 'a/b',
multihash: 'QmWyWYxq1GD9fEyckf5LrJv8hMW35CwfWwzDBp8bTw3NQj' },
{ path: 'a',
multihash: rootHash }
])
done()
})
)
})

it('exports', done => {
pull(
unixFSEngine.exporter(rootHash, ipldResolver),
pull.collect((err, files) => {
expect(err).to.not.exist
map(
files,
(file, callback) => {
if (file.content) {
pull(
file.content,
pull.collect(mapFile(file, callback))
)
} else {
callback(null, { path: file.path })
}
},
(err, files) => {
expect(err).to.not.exist
expect(files.filter(fileHasContent).sort(byPath)).to.eql([
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/h',
content: 'cream' },
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/g',
content: 'ice' },
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/c/d/f',
content: 'strawberry' },
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/c/d/e',
content: 'banana' }
])
done()
})
})
)

function mapFile (file, callback) {
return (err, fileContent) => {
callback(err, fileContent && {
path: file.path,
content: fileContent.toString()
})
}
}
})
})
}

function normalizeNode (node) {
return {
path: node.path,
multihash: mh.toB58String(node.multihash)
}
}

function fileHasContent (file) {
return Boolean(file.content)
}

function byPath (a, b) {
if (a.path > b.path) return -1
if (a.path < b.path) return 1
return 0
}

0 comments on commit 651f113

Please sign in to comment.