-
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.
Changes dht creation to use factory function and updates docs BREAKING CHANGE: libp2p-kad-dht has a new event-based API which is exposed as `_dht`
- Loading branch information
1 parent
443a102
commit 2f598eb
Showing
13 changed files
with
182 additions
and
67 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
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const drain = require('it-drain') | ||
|
||
/** | ||
* @typedef {import('peer-id')} PeerId | ||
* @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule | ||
* @typedef {import('multiformats/cid').CID} CID | ||
*/ | ||
|
||
/** | ||
* Wrapper class to convert events into returned values | ||
* | ||
* @implements {ContentRoutingModule} | ||
*/ | ||
class DHTContentRouting { | ||
/** | ||
* @param {import('libp2p-kad-dht').DHT} dht | ||
*/ | ||
constructor (dht) { | ||
this._dht = dht | ||
} | ||
|
||
/** | ||
* @param {CID} cid | ||
*/ | ||
async provide (cid) { | ||
await drain(this._dht.provide(cid)) | ||
} | ||
|
||
/** | ||
* @param {CID} cid | ||
* @param {*} options | ||
*/ | ||
async * findProviders (cid, options) { | ||
for await (const event of this._dht.findProviders(cid, options)) { | ||
if (event.name === 'PROVIDER') { | ||
yield * event.providers | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { DHTContentRouting } |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
const errCode = require('err-code') | ||
const { messages, codes } = require('../errors') | ||
|
||
/** | ||
* @typedef {import('peer-id')} PeerId | ||
* @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule | ||
*/ | ||
|
||
/** | ||
* Wrapper class to convert events into returned values | ||
* | ||
* @implements {PeerRoutingModule} | ||
*/ | ||
class DHTPeerRouting { | ||
/** | ||
* @param {import('libp2p-kad-dht').DHT} dht | ||
*/ | ||
constructor (dht) { | ||
this._dht = dht | ||
} | ||
|
||
/** | ||
* @param {PeerId} peerId | ||
* @param {any} options | ||
*/ | ||
async findPeer (peerId, options = {}) { | ||
for await (const event of this._dht.findPeer(peerId, options)) { | ||
if (event.name === 'FINAL_PEER') { | ||
return event.peer | ||
} | ||
} | ||
|
||
throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND) | ||
} | ||
|
||
/** | ||
* @param {Uint8Array} key | ||
* @param {any} options | ||
*/ | ||
async * getClosestPeers (key, options = {}) { | ||
for await (const event of this._dht.getClosestPeers(key, options)) { | ||
if (event.name === 'PEER_RESPONSE') { | ||
yield * event.closer | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { DHTPeerRouting } |
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
Oops, something went wrong.