Intro
Installable via npm install --save libp2p-kad-dht
, it can also be used directly in the browser.
Download
The source is available for download from GitHub. Alternatively, you can install using npm:
$ npm install --save libp2p-kad-dht
You can then require()
libp2p-kad-dht as normal:
const libp2PKadDht = require('libp2p-kad-dht')
In the Browser
Libp2p-kad-dht should work in any ES2015 environment out of the box.
Usage:
<script type="text/javascript" src="index.js"></script>
The portable versions of libp2p-kad-dht, including index.js
and index.min.js
, are included in the /dist
folder. Libp2p-kad-dht can also be found on unkpkg.com under
KadDHT
A DHT implementation modeled after Kademlia with Coral and S/Kademlia modifications.
Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
Parameters
swarm: any
:options: any
:
instance
KadDHT.prototype.datastore
datastore: Datastore
Reference to the datastore, uses an in-memory store if none given.
KadDHT.prototype.start
start(callback: function (Error)): void
Start listening to incoming connections.
Parameters
callback: function (Error)
:
Returns
void
KadDHT.prototype.stop
stop(callback: function (Error)): void
Stop accepting incoming connections and sending outgoing messages.
Parameters
callback: function (Error)
:
Returns
void
KadDHT.prototype.getClosestPeers
getClosestPeers(key: Buffer, callback: function (Error, Array<PeerId>)): void
Kademlia 'node lookup' operation.
Returns
void
KadDHT.prototype.put
put(key: Buffer, value: Buffer, callback: function (Error)): void
Store the given key/value pair in the DHT.
Returns
void
KadDHT.prototype.get
get(key: Buffer, maxTimeout: number, callback: function (Error, Buffer)): void
Get the value to the given key. Times out after 1 minute.
Parameters
Returns
void
KadDHT.prototype.getMany
getMany(key: Buffer, nvals: number, maxTimeout: number, callback: function (Error, Array<{from: PeerId, val: Buffer}>)): void
Get the n
values to the given key without sorting.
Parameters
Returns
void
KadDHT.prototype.getPublicKey
getPublicKey(peer: PeerId, callback: function (Error, PubKey)): void
Get the public key for the given peer id.
Parameters
peer: PeerId
:callback: function (Error, PubKey)
:
Returns
void
KadDHT.prototype.provide
provide(key: CID, callback: function (Error)): void
Announce to the network that a node can provide the given key. This is what Coral and MainlineDHT do to store large values in a DHT.
Parameters
key: CID
:callback: function (Error)
:
Returns
void
KadDHT.prototype.findProviders
findProviders(key: CID, timeout: number, callback: function (Error, Array<PeerInfo>)): void
Search the dht for up to K
providers of the given CID.
Parameters
key: CID
:timeout: number
:how long the query should maximally run, in milliseconds.
Returns
void
KadDHT.prototype.findPeer
findPeer(id: PeerId, maxTimeout: number, callback: function (Error, PeerInfo)): void
Search for a peer with the given ID.
Returns
void
KadDHT.prototype.findPeerLocal
findPeerLocal(peer: PeerId, callback: function (Error, PeerInfo)): void
Look if we are connected to a peer with the given id.
Returns the PeerInfo
for it, if found, otherwise undefined
.
Parameters
peer: PeerId
:callback: function (Error, PeerInfo)
:
Returns
void
Network
Handle network operations for the dht
Parameters
self: any
:
instance
Network.prototype.start
start(callback: function (Error)): void
Start the network.
Parameters
callback: function (Error)
:
Returns
void
Network.prototype.stop
stop(callback: function (Error)): void
Stop all network activity.
Parameters
callback: function (Error)
:
Returns
void
PeerList
A list of unique peer infos.
instance
PeerList.prototype.push
push(info: PeerInfo): bool
Add a new info. Returns true
if it was a new one
Parameters
info: PeerInfo
:
Returns
bool
PeerList.prototype.has
has(info: PeerInfo): bool
Check if this PeerInfo is already in here.
Parameters
info: PeerInfo
:
Returns
bool
PeerQueue
PeerQueue is a heap that sorts its entries (PeerIds) by their xor distance to the inital provided key.
Parameters
from: any
:
Providers
This class manages known providers. A provider is a peer that we know to have the content for a given CID.
Every cleanupInterval
providers are checked if they
are still valid, i.e. younger than the provideValidity
.
If they are not, they are deleted.
To ensure the list survives restarts of the daemon, providers are stored in the datastore, but to ensure access is fast there is an LRU cache in front of that.
Parameters
datastore: any
:self: any
:cacheSize: any
:
instance
Providers.prototype.cleanupInterval
cleanupInterval: number
How often invalid records are cleaned. (in seconds)
Providers.prototype.provideValidity
provideValidity: number
How long is a provider valid for. (in seconds)
RoutingTable
A wrapper around k-bucket
, to provide easy store and
retrival for peers.
Parameters
self: any
:kBucketSize: any
:
instance
RoutingTable.prototype.find
find(peer: PeerId, callback: function (Error, PeerId)): void
Find a specific peer by id.
Parameters
peer: PeerId
:callback: function (Error, PeerId)
:
Returns
void
RoutingTable.prototype.closestPeer
closestPeer(key: Buffer, count: number): (PeerId | undefined)
Retrieve the closest peers to the given key.
Returns
(PeerId | undefined)
RoutingTable.prototype.closestPeers
closestPeers(key: Buffer, count: number): Array<PeerId>
Retrieve the count
-closest peers to the given key.
Returns
Array<PeerId>
protocolHandler
Handle incoming streams from the swarm, on the dht protocol.
Parameters
protocol: string
:conn: Connection
: