-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple peer and content routing modules
- Loading branch information
Showing
7 changed files
with
276 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,71 @@ | ||
'use strict' | ||
|
||
const tryEach = require('async/tryEach') | ||
const parallel = require('async/parallel') | ||
|
||
module.exports = (node) => { | ||
const routers = node._modules.contentRouting || [] | ||
|
||
// If we have the dht, make it first | ||
if (node._dht) { | ||
routers.unshift(node._dht) | ||
} | ||
|
||
return { | ||
/** | ||
* Iterates over all content routers in series to find providers of the given key. | ||
* Once a content router succeeds, iteration will stop. | ||
* | ||
* @param {CID} key The CID key of the content to find | ||
* @param {number} timeout How long the query should run | ||
* @param {function(Error, Result<Array>)} callback | ||
* @returns {void} | ||
*/ | ||
findProviders: (key, timeout, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
if (routers.length === 0) { | ||
return callback(new Error('No content routers available')) | ||
} | ||
|
||
node._dht.findProviders(key, timeout, callback) | ||
const tasks = routers.map((router) => { | ||
return (cb) => router.findProviders(key, timeout, (err, results) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
// If we don't have any results, we need to provide an error to keep trying | ||
if (!results || Object.keys(results).length === 0) { | ||
return cb(true, null) | ||
} | ||
|
||
cb(null, results) | ||
}) | ||
}) | ||
|
||
tryEach(tasks, (err, results) => { | ||
if (err && err !== true) { | ||
return callback(err) | ||
} | ||
results = results || [] | ||
callback(null, results) | ||
}) | ||
}, | ||
|
||
/** | ||
* Iterates over all content routers in parallel to notify it is | ||
* a provider of the given key. | ||
* | ||
* @param {CID} key The CID key of the content to find | ||
* @param {function(Error)} callback | ||
* @returns {void} | ||
*/ | ||
provide: (key, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
if (routers.length === 0) { | ||
return callback(new Error('No content routers available')) | ||
} | ||
|
||
node._dht.provide(key, callback) | ||
parallel(routers.map((router) => { | ||
return (cb) => router.provide(key, cb) | ||
}), callback) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
'use strict' | ||
|
||
const tryEach = require('async/tryEach') | ||
|
||
module.exports = (node) => { | ||
const routers = node._modules.peerRouting || [] | ||
|
||
// If we have the dht, make it first | ||
if (node._dht) { | ||
routers.unshift(node._dht) | ||
} | ||
|
||
return { | ||
/** | ||
* Iterates over all peer routers in series to find the given peer. | ||
* | ||
* @param {String} id The id of the peer to find | ||
* @param {function(Error, Result<Array>)} | ||
* @returns {void} | ||
*/ | ||
findPeer: (id, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
if (routers.length === 0) { | ||
return callback(new Error('No peer routers available')) | ||
} | ||
|
||
node._dht.findPeer(id, callback) | ||
const tasks = routers.map((router) => { | ||
return (cb) => router.findPeer(id, (err, result) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
// If we don't have a result, we need to provide an error to keep trying | ||
if (!result || Object.keys(result).length === 0) { | ||
return cb(true, null) | ||
} | ||
|
||
cb(null, result) | ||
}) | ||
}) | ||
|
||
tryEach(tasks, (err, results) => { | ||
if (err && err !== true) { | ||
return callback(err) | ||
} | ||
results = results || null | ||
callback(null, results) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters