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

fix: case for empty file #132

Merged
merged 3 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"lodash": "^4.17.4",
"multihashes": "^0.3.2",
"pull-batch": "^1.0.0",
"pull-block": "^1.0.2",
"pull-block": "^1.1.0",
"pull-pair": "^1.1.0",
"pull-paramap": "^1.2.1",
"pull-pause": "0.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/builder/balanced/balanced-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module.exports = function balancedReduceToRoot (reduce, options) {
result.push(roots[0])
result.end()
} else if (roots.length > 1) {
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
result.end(new Error('expected a maximum of 1 roots and got ' + roots.length))
} else {
result.end()
}
})

Expand Down
14 changes: 8 additions & 6 deletions src/builder/builder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const extend = require('deep-extend')
const assert = require('assert')
const UnixFS = require('ipfs-unixfs')
const pull = require('pull-stream')
const through = require('pull-through')
Expand Down Expand Up @@ -32,8 +31,10 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
if (err) {
return cb(err)
}
source.push(node)
files.push(node)
if (node) {
source.push(node)
files.push(node)
}
cb()
})
}
Expand All @@ -43,8 +44,10 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
if (err) {
return cb(err)
}
source.push(node)
files.push(node)
if (node) {
source.push(node)
files.push(node)
}
cb()
})
}), cb)
Expand Down Expand Up @@ -141,7 +144,6 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
if (err) {
callback(err)
} else {
assert.equal(roots.length, 1, 'should result in exactly one root')
callback(null, roots[0])
}
})
Expand Down
12 changes: 8 additions & 4 deletions src/builder/flat/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('assert')
const pull = require('pull-stream')
const pushable = require('pull-pushable')
const pullPair = require('pull-pair')
Expand All @@ -20,9 +19,14 @@ module.exports = function (reduce, options) {
result.end(err)
return // early
}
assert.equal(roots.length, 1, 'need one root')
result.push(roots[0])
result.end()
if (roots.length === 1) {
result.push(roots[0])
result.end()
} else if (roots.length > 1) {
result.end(new Error('expected a maximum of 1 roots and got ' + roots.length))
} else {
result.end()
}
})
)

Expand Down
4 changes: 3 additions & 1 deletion src/builder/trickle/trickle-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = function trickleReduceToRoot (reduce, options) {
result.push(roots[0])
result.end()
} else if (roots.length > 1) {
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
result.end(new Error('expected a maximum of 1 roots and got ' + roots.length))
} else {
result.end()
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/chunker/fixed-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const pullBlock = require('pull-block')

module.exports = (options) => {
let maxSize = (typeof options === 'number') ? options : options.maxChunkSize
return pullBlock(maxSize, { zeroPadding: false })
return pullBlock(maxSize, { zeroPadding: false, emitEmpty: true })
}
16 changes: 16 additions & 0 deletions test/test-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ module.exports = (repo) => {
}))
})

it('doesn\'t yield anything on empty file', (done) => {
pull(
pull.values([{
path: 'emptyfile',
content: pull.empty()
}]),
importer(ipldResolver, options),
pull.collect((err, nodes) => {
expect(err).to.not.exist
expect(nodes.length).to.be.eql(1)
// always yield empty node
expect(mh.toB58String(nodes[0].multihash)).to.be.eql('QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP')
done()
}))
})

it('fails on more than one root', (done) => {
pull(
pull.values([
Expand Down