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

Commit 7b054b6

Browse files
pgtedaviddias
authored andcommitted
fix: expect empty stream to not generate any nodes (#131)
1 parent 224ba8d commit 7b054b6

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/builder/balanced/balanced-reducer.js

+6-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')
@@ -17,9 +16,12 @@ module.exports = function balancedReduceToRoot (reduce, options) {
1716
result.end(err)
1817
return // early
1918
}
20-
assert.equal(roots.length, 1, 'need one root')
21-
result.push(roots[0])
22-
result.end()
19+
if (roots.length === 1) {
20+
result.push(roots[0])
21+
result.end()
22+
} else if (roots.length > 1) {
23+
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
24+
}
2325
})
2426

2527
function reduceToParents (_chunks, callback) {

src/builder/trickle/trickle-reducer.js

+7-5
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 batch = require('pull-batch')
@@ -21,13 +20,16 @@ module.exports = function trickleReduceToRoot (reduce, options) {
2120
trickle(0, -1),
2221
batch(Infinity),
2322
pull.asyncMap(reduce),
24-
pull.collect((err, nodes) => {
23+
pull.collect((err, roots) => {
2524
if (err) {
2625
result.end(err)
2726
} else {
28-
assert.equal(nodes.length, 1, 'need one root')
29-
result.push(nodes[0])
30-
result.end()
27+
if (roots.length === 1) {
28+
result.push(roots[0])
29+
result.end()
30+
} else if (roots.length > 1) {
31+
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
32+
}
3133
}
3234
})
3335
)

test/test-importer.js

+11
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ module.exports = (repo) => {
184184
)
185185
})
186186

187+
it('doesn\'t yield anything on empty source', (done) => {
188+
pull(
189+
pull.empty(),
190+
importer(ipldResolver, options),
191+
pull.collect((err, nodes) => {
192+
expect(err).to.not.exist
193+
expect(nodes.length).to.be.eql(0)
194+
done()
195+
}))
196+
})
197+
187198
it('fails on more than one root', (done) => {
188199
pull(
189200
pull.values([

0 commit comments

Comments
 (0)