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

Commit fee55d1

Browse files
pgtedaviddias
authored andcommitted
fix: case for empty file (#132)
* fix: case for empty file * always yield empty file * updated dep version
1 parent f958df5 commit fee55d1

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"lodash": "^4.17.4",
6464
"multihashes": "^0.3.2",
6565
"pull-batch": "^1.0.0",
66-
"pull-block": "^1.0.2",
66+
"pull-block": "^1.1.0",
6767
"pull-pair": "^1.1.0",
6868
"pull-paramap": "^1.2.1",
6969
"pull-pause": "0.0.0",

src/builder/balanced/balanced-reducer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ module.exports = function balancedReduceToRoot (reduce, options) {
2020
result.push(roots[0])
2121
result.end()
2222
} else if (roots.length > 1) {
23-
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
23+
result.end(new Error('expected a maximum of 1 roots and got ' + roots.length))
24+
} else {
25+
result.end()
2426
}
2527
})
2628

src/builder/builder.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const extend = require('deep-extend')
4-
const assert = require('assert')
54
const UnixFS = require('ipfs-unixfs')
65
const pull = require('pull-stream')
76
const through = require('pull-through')
@@ -32,8 +31,10 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
3231
if (err) {
3332
return cb(err)
3433
}
35-
source.push(node)
36-
files.push(node)
34+
if (node) {
35+
source.push(node)
36+
files.push(node)
37+
}
3738
cb()
3839
})
3940
}
@@ -43,8 +44,10 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
4344
if (err) {
4445
return cb(err)
4546
}
46-
source.push(node)
47-
files.push(node)
47+
if (node) {
48+
source.push(node)
49+
files.push(node)
50+
}
4851
cb()
4952
})
5053
}), cb)
@@ -141,7 +144,6 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
141144
if (err) {
142145
callback(err)
143146
} else {
144-
assert.equal(roots.length, 1, 'should result in exactly one root')
145147
callback(null, roots[0])
146148
}
147149
})

src/builder/flat/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const assert = require('assert')
43
const pull = require('pull-stream')
54
const pushable = require('pull-pushable')
65
const pullPair = require('pull-pair')
@@ -20,9 +19,14 @@ module.exports = function (reduce, options) {
2019
result.end(err)
2120
return // early
2221
}
23-
assert.equal(roots.length, 1, 'need one root')
24-
result.push(roots[0])
25-
result.end()
22+
if (roots.length === 1) {
23+
result.push(roots[0])
24+
result.end()
25+
} else if (roots.length > 1) {
26+
result.end(new Error('expected a maximum of 1 roots and got ' + roots.length))
27+
} else {
28+
result.end()
29+
}
2630
})
2731
)
2832

src/builder/trickle/trickle-reducer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module.exports = function trickleReduceToRoot (reduce, options) {
2828
result.push(roots[0])
2929
result.end()
3030
} else if (roots.length > 1) {
31-
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
31+
result.end(new Error('expected a maximum of 1 roots and got ' + roots.length))
32+
} else {
33+
result.end()
3234
}
3335
}
3436
})

src/chunker/fixed-size.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ const pullBlock = require('pull-block')
44

55
module.exports = (options) => {
66
let maxSize = (typeof options === 'number') ? options : options.maxChunkSize
7-
return pullBlock(maxSize, { zeroPadding: false })
7+
return pullBlock(maxSize, { zeroPadding: false, emitEmpty: true })
88
}

test/test-importer.js

+16
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ module.exports = (repo) => {
195195
}))
196196
})
197197

198+
it('doesn\'t yield anything on empty file', (done) => {
199+
pull(
200+
pull.values([{
201+
path: 'emptyfile',
202+
content: pull.empty()
203+
}]),
204+
importer(ipldResolver, options),
205+
pull.collect((err, nodes) => {
206+
expect(err).to.not.exist
207+
expect(nodes.length).to.be.eql(1)
208+
// always yield empty node
209+
expect(mh.toB58String(nodes[0].multihash)).to.be.eql('QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP')
210+
done()
211+
}))
212+
})
213+
198214
it('fails on more than one root', (done) => {
199215
pull(
200216
pull.values([

0 commit comments

Comments
 (0)