Skip to content

Commit 17b5f73

Browse files
committed
fix: dont call callback before it's properly set
1 parent c18d2a4 commit 17b5f73

4 files changed

+58
-8
lines changed

src/content-routing.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ module.exports = (node) => {
2424
* @returns {void}
2525
*/
2626
findProviders: (key, options, callback) => {
27-
if (!routers.length) {
28-
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
29-
}
30-
3127
if (typeof options === 'function') {
3228
callback = options
3329
options = {}
@@ -37,6 +33,10 @@ module.exports = (node) => {
3733
}
3834
}
3935

36+
if (!routers.length) {
37+
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
38+
}
39+
4040
const tasks = routers.map((router) => {
4141
return (cb) => router.findProviders(key, options, (err, results) => {
4242
if (err) {

src/peer-routing.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ module.exports = (node) => {
2222
* @returns {void}
2323
*/
2424
findPeer: (id, options, callback) => {
25-
if (!routers.length) {
26-
callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE'))
27-
}
28-
2925
if (typeof options === 'function') {
3026
callback = options
3127
options = {}
3228
}
3329

30+
if (!routers.length) {
31+
callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE'))
32+
}
33+
3434
const tasks = routers.map((router) => {
3535
return (cb) => router.findPeer(id, options, (err, result) => {
3636
if (err) {

test/content-routing.node.js

+25
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,29 @@ describe('.contentRouting', () => {
367367
})
368368
})
369369
})
370+
371+
describe('no routers', () => {
372+
let nodeA
373+
before((done) => {
374+
createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
375+
expect(err).to.not.exist()
376+
nodeA = node
377+
done()
378+
})
379+
})
380+
381+
it('.findProviders should return an error with no options', (done) => {
382+
nodeA.contentRouting.findProviders('a cid', (err) => {
383+
expect(err).to.exist()
384+
done()
385+
})
386+
})
387+
388+
it('.findProviders should return an error with options', (done) => {
389+
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err) => {
390+
expect(err).to.exist()
391+
done()
392+
})
393+
})
394+
})
370395
})

test/peer-routing.node.js

+25
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,29 @@ describe('.peerRouting', () => {
266266
})
267267
})
268268
})
269+
270+
describe('no routers', () => {
271+
let nodeA
272+
before((done) => {
273+
createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
274+
expect(err).to.not.exist()
275+
nodeA = node
276+
done()
277+
})
278+
})
279+
280+
it('.findPeer should return an error with no options', (done) => {
281+
nodeA.peerRouting.findPeer('a cid', (err) => {
282+
expect(err).to.exist()
283+
done()
284+
})
285+
})
286+
287+
it('.findPeer should return an error with options', (done) => {
288+
nodeA.peerRouting.findPeer('a cid', { maxTimeout: 5000 }, (err) => {
289+
expect(err).to.exist()
290+
done()
291+
})
292+
})
293+
})
269294
})

0 commit comments

Comments
 (0)