libp2p-kad-dht
Advanced tools
Comparing version 0.17.1 to 0.18.0
@@ -0,1 +1,16 @@ | ||
<a name="0.18.0"></a> | ||
# [0.18.0](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.17.1...v0.18.0) (2019-11-30) | ||
### Features | ||
* find providers and closest peers return async iterable ([#157](https://github.com/libp2p/js-libp2p-kad-dht/issues/157)) ([f0e6800](https://github.com/libp2p/js-libp2p-kad-dht/commit/f0e6800)) | ||
### BREAKING CHANGES | ||
* API for find providers and closest peers return async iterable instead of an array of PeerInfo | ||
<a name="0.17.1"></a> | ||
@@ -2,0 +17,0 @@ ## [0.17.1](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.17.0...v0.17.1) (2019-11-28) |
{ | ||
"name": "libp2p-kad-dht", | ||
"version": "0.17.1", | ||
"version": "0.18.0", | ||
"description": "JavaScript implementation of the Kad-DHT for libp2p", | ||
@@ -5,0 +5,0 @@ "leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>", |
'use strict' | ||
const errcode = require('err-code') | ||
const pFilter = require('p-filter') | ||
const pTimeout = require('p-timeout') | ||
@@ -111,18 +109,20 @@ | ||
// put record to the closest peers | ||
const peers = await dht.getClosestPeers(key, { shallow: true }) | ||
const results = await pFilter(peers, async (peer) => { | ||
let counterAll = 0 | ||
let counterSuccess = 0 | ||
for await (const peer of dht.getClosestPeers(key, { shallow: true })) { | ||
try { | ||
counterAll += 1 | ||
await dht._putValueToPeer(key, record, peer) | ||
return true | ||
counterSuccess += 1 | ||
} catch (err) { | ||
dht._log.error('Failed to put to peer (%b): %s', peer.id, err) | ||
return false | ||
} | ||
}) | ||
} | ||
// verify if we were able to put to enough peers | ||
const minPeers = options.minPeers || peers.length // Ensure we have a default `minPeers` | ||
const minPeers = options.minPeers || counterAll // Ensure we have a default `minPeers` | ||
if (minPeers > results.length) { | ||
const error = errcode(new Error(`Failed to put value to enough peers: ${results.length}/${minPeers}`), 'ERR_NOT_ENOUGH_PUT_PEERS') | ||
if (minPeers > counterSuccess) { | ||
const error = errcode(new Error(`Failed to put value to enough peers: ${counterSuccess}/${minPeers}`), 'ERR_NOT_ENOUGH_PUT_PEERS') | ||
dht._log.error(error) | ||
@@ -129,0 +129,0 @@ throw error |
@@ -43,8 +43,7 @@ 'use strict' | ||
// Notify closest peers | ||
const peers = await dht.getClosestPeers(key.buffer) | ||
const msg = new Message(Message.TYPES.ADD_PROVIDER, key.buffer, 0) | ||
msg.providerPeers = [dht.peerInfo] | ||
await Promise.all(peers.map(async (peer) => { | ||
// Notify closest peers | ||
for await (const peer of dht.getClosestPeers(key.buffer)) { | ||
dht._log('putProvider %s to %s', key.toBaseEncodedString(), peer.toB58String()) | ||
@@ -56,3 +55,3 @@ try { | ||
} | ||
})) | ||
} | ||
@@ -74,5 +73,5 @@ if (errors.length) { | ||
* @param {number} options.maxNumProviders - maximum number of providers to find | ||
* @returns {Promise<PeerInfo>} | ||
* @returns {AsyncIterable<PeerInfo>} | ||
*/ | ||
async findProviders (key, options = {}) { | ||
async * findProviders (key, options = {}) { | ||
const providerTimeout = options.timeout || c.minute | ||
@@ -155,5 +154,7 @@ const n = options.maxNumProviders || c.K | ||
return out.toArray() | ||
for (const pInfo of out.toArray()) { | ||
yield pInfo | ||
} | ||
} | ||
} | ||
} |
@@ -260,6 +260,8 @@ 'use strict' | ||
* @param {number} options.maxNumProviders - maximum number of providers to find | ||
* @returns {Promise<PeerInfo>} | ||
* @returns {AsyncIterable<PeerInfo>} | ||
*/ | ||
async findProviders (key, options = {}) { // eslint-disable-line require-await | ||
return this.contentRouting.findProviders(key, options) | ||
async * findProviders (key, options = {}) { | ||
for await (const pInfo of this.contentRouting.findProviders(key, options)) { | ||
yield pInfo | ||
} | ||
} | ||
@@ -286,6 +288,8 @@ | ||
* @param {boolean} [options.shallow] shallow query (default: false) | ||
* @returns {Promise<Array<PeerId>>} | ||
* @returns {AsyncIterable<PeerId>} | ||
*/ | ||
async getClosestPeers (key, options = { shallow: false }) { // eslint-disable-line require-await | ||
return this.peerRouting.getClosestPeers(key, options) | ||
async * getClosestPeers (key, options = { shallow: false }) { | ||
for await (const pId of this.peerRouting.getClosestPeers(key, options)) { | ||
yield pId | ||
} | ||
} | ||
@@ -292,0 +296,0 @@ |
@@ -188,5 +188,5 @@ 'use strict' | ||
* @param {boolean} [options.shallow] shallow query (default: false) | ||
* @returns {Promise<Array<PeerId>>} | ||
* @returns {AsyncIterable<PeerId>} | ||
*/ | ||
async getClosestPeers (key, options = { shallow: false }) { | ||
async * getClosestPeers (key, options = { shallow: false }) { | ||
dht._log('getClosestPeers to %b', key) | ||
@@ -217,3 +217,6 @@ | ||
const sorted = await utils.sortClosestPeers(Array.from(res.finalSet), id) | ||
return sorted.slice(0, dht.kBucketSize) | ||
for (const pId of sorted.slice(0, dht.kBucketSize)) { | ||
yield pId | ||
} | ||
}, | ||
@@ -220,0 +223,0 @@ |
@@ -12,2 +12,3 @@ /* eslint-env mocha */ | ||
const all = require('async-iterator-all') | ||
const pMapSeries = require('p-map-series') | ||
@@ -450,3 +451,3 @@ const pEachSeries = require('p-each-series') | ||
const provs = await dhts[n].findProviders(v.cid, { timeout: 5000 }) | ||
const provs = await all(dhts[n].findProviders(v.cid, { timeout: 5000 })) | ||
@@ -475,4 +476,4 @@ expect(provs).to.have.length(1) | ||
const res0 = await dhts[0].findProviders(val.cid) | ||
const res1 = await dhts[0].findProviders(val.cid, { maxNumProviders: 2 }) | ||
const res0 = await all(dhts[0].findProviders(val.cid)) | ||
const res1 = await all(dhts[0].findProviders(val.cid, { maxNumProviders: 2 })) | ||
@@ -570,3 +571,3 @@ // find providers find all the 3 providers | ||
// Make the query | ||
const out = await guy.getClosestPeers(val) | ||
const out = await all(guy.getClosestPeers(val)) | ||
const actualClosest = await kadUtils.sortClosestPeers(otherIds, rtval) | ||
@@ -604,3 +605,3 @@ | ||
const res = await dhts[1].getClosestPeers(Buffer.from('foo')) | ||
const res = await all(dhts[1].getClosestPeers(Buffer.from('foo'))) | ||
expect(res).to.have.length(c.K) | ||
@@ -607,0 +608,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8406
2025198