new Node(contact, optionsopt)
Kademlia protocol implementation. Creates an interface for orchestrating a Kademlia network and extending it.
Name | Type | Attributes | Description |
---|---|---|---|
contact | module:kdns/contacts~Contact | Address and fingerprint of this node | |
options | module:kdns/node~NodeOptions | <optional> | Internal overrides |
Extends
- EventEmitter
Methods
expire() → {undefined}
Items expire T_EXPIRE seconds after the original publication. All items are assigned an expiration time which is "exponentially inversely proportional to the number of nodes between the current node and the node whose ID is closest to the key", where this number is "inferred from the bucket structure of the current node".
- Type:
- undefined
iterativeFindNode(key) → {Promise.<Array.<module:kdns/contacts~Contact>>}
Basic kademlia lookup operation that builds a set of K contacts closest to the given key
Name | Type | Description |
---|---|---|
key | buffer | | Reference key for node lookup |
- Type:
- Promise.<Array.<module:kdns/contacts~Contact>>
iterativeFindValue(key) → {Promise.<(Array.<module:kdns/contacts~Contact>|module:kdns/node~StoredItem)>}
Kademlia search operation that is conducted as a node lookup and builds a list of K closest contacts. If at any time during the lookup the value is returned, the search is abandoned. If no value is found, the K closest contacts are returned. Upon success, we must store the value at the nearest node seen during the search that did not return the value.
Name | Type | Description |
---|---|---|
key | buffer | | Key for value lookup |
- Type:
- Promise.<(Array.<module:kdns/contacts~Contact>|module:kdns/node~StoredItem)>
iterativeStore(key, value) → {Promise.<number>}
Performs a lookup to collect K contacts nearest to the given key, sending a STORE message to each of them. Note that if there is a protocol/validation error, you will not receive it as a rejection. Be sure to also check that stored > 0 as part of error handling here.
Name | Type | Description |
---|---|---|
key | buffer | | Key to store data under |
value | module:kdns/node~StoredItem | | Value to store by key |
- Type:
- Promise.<number>
join(peer) → {Promise.<undefined>}
Inserts the given contact into the routing table and uses it to perform a Node#iterativeFindNode for this node's identity, then refreshes all buckets further than it's closest neighbor, which will be in the occupied bucket with the lowest index
Name | Type | Description |
---|---|---|
peer | module:kdns/contacts~Contact | Peer to bootstrap from |
- Type:
- Promise.<undefined>
ping(peer) → {Promise.<number>}
Sends a PING message to the supplied contact, resolves with latency.
Name | Type | Description |
---|---|---|
peer | module:kdns/contacts~Contact | Peer to PING |
- Type:
- Promise.<number>
refresh(startIndex) → {Promise.<undefined>}
If no node lookups have been performed in any given bucket's range for T_REFRESH, the node selects a random number in that range and does a refresh, an iterativeFindNode using that number as key.
Name | Type | Default | Description |
---|---|---|---|
startIndex | number | 0 | bucket index to start refresh from |
- Type:
- Promise.<undefined>
replicate() → {undefined}
Performs a scan of the storage adapter and performs republishing/replication of items stored. Items that we did not publish ourselves get republished every T_REPLICATE. Items we did publish get republished every T_REPUBLISH.
- Type:
- undefined
Events
message_queued
RPC message is queued for you to send. This is implementation specific based on how you want to handle networking. This event contains all the data necessary for you to handle transport.
Name | Type | Description |
---|---|---|
method | string | RPC method name to call |
params | array.<(string|object|number|boolean|Stream)> | Arguments to pass the method call |
target | module:kdns/contacts~Contact | Contact address information |
respond | module:kdns/protocol~HandlerResponse | Resolves the message response |
storage_delete
Indicates that you may delete the item stored by the key. This is triggered most likely by the expiration routine.
Name | Type | Description |
---|---|---|
key | string | Key of storage item to delete |
respond | module:kdns/protocol~HandlerResponse | Resolves the delete request |
storage_expire
Expiration routine has started. A writable stream is provided. Only items requiring expiration will be requested for deletion.
Name | Type | Description |
---|---|---|
expirator | module:kdns/node~ExpirationStream | Reads from a module:kdns/node~ReadableStore |
storage_replicate
Replication routine has started. A writable stream is provided. Only items requiring replication will be replicated.
Name | Type | Description |
---|---|---|
replicator | module:kdns/node~ReplicatorStream | Reads from a module:kdns/node~ReadableStore |