mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-26 19:12:15 +00:00
* feat: promisify all api methods that accept callbacks This is a stop-gap until the full async/await migration can be completed. It means we can refactor tests of other modules that depend on this module without having to mix async flow control strategies. N.b. some methods that were previously callable without callbacks (e.g. `node.start()`, `node.stop()`, etc) now require callbacks otherwise a promise is returned which, if rejected, can cause `unhandledPromiseRejection` events and lead to memory leaks. * docs: add a global note to the api about promisify * fix: update the logic for unsubscribe * test(fix): correct pubsub unsubscribe usage for api change * test(fix): update content routing tests for latest delegate version
85 lines
2.5 KiB
JavaScript
85 lines
2.5 KiB
JavaScript
'use strict'
|
|
|
|
const tryEach = require('async/tryEach')
|
|
const parallel = require('async/parallel')
|
|
const errCode = require('err-code')
|
|
const promisify = require('promisify-es6')
|
|
|
|
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 {object} options
|
|
* @param {number} options.maxTimeout How long the query should run
|
|
* @param {number} options.maxNumProviders - maximum number of providers to find
|
|
* @param {function(Error, Result<Array>)} callback
|
|
* @returns {void}
|
|
*/
|
|
findProviders: promisify((key, options, callback) => {
|
|
if (typeof options === 'function') {
|
|
callback = options
|
|
options = {}
|
|
} else if (typeof options === 'number') { // This can be deprecated in a future release
|
|
options = {
|
|
maxTimeout: options
|
|
}
|
|
}
|
|
|
|
if (!routers.length) {
|
|
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
|
}
|
|
|
|
const tasks = routers.map((router) => {
|
|
return (cb) => router.findProviders(key, options, (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(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
|
}
|
|
|
|
cb(null, results)
|
|
})
|
|
})
|
|
|
|
tryEach(tasks, (err, results) => {
|
|
if (err && err.code !== 'NOT_FOUND') {
|
|
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: promisify((key, callback) => {
|
|
if (!routers.length) {
|
|
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
|
}
|
|
|
|
parallel(routers.map((router) => {
|
|
return (cb) => router.provide(key, cb)
|
|
}), callback)
|
|
})
|
|
}
|
|
}
|