kdns/node~ Node

new Node(contact, optionsopt)

Kademlia protocol implementation. Creates an interface for orchestrating a Kademlia network and extending it.

Parameters:
NameTypeAttributesDescription
contactmodule:kdns/contacts~Contact

Address and fingerprint of this node

optionsmodule: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".

Returns:
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

Parameters:
NameTypeDescription
keybuffer | string

Reference key for node lookup

Returns:
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.

Parameters:
NameTypeDescription
keybuffer | string

Key for value lookup

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.

Parameters:
NameTypeDescription
keybuffer | string

Key to store data under

valuemodule:kdns/node~StoredItem | Buffer

Value to store by key

Returns:
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

Parameters:
NameTypeDescription
peermodule:kdns/contacts~Contact

Peer to bootstrap from

Returns:
Type: 
Promise.<undefined>

ping(peer) → {Promise.<number>}

Sends a PING message to the supplied contact, resolves with latency.

Parameters:
NameTypeDescription
peermodule:kdns/contacts~Contact

Peer to PING

Returns:
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.

Parameters:
NameTypeDefaultDescription
startIndexnumber0

bucket index to start refresh from

Returns:
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.

Returns:
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.

Parameters:
NameTypeDescription
methodstring

RPC method name to call

paramsarray.<(string|object|number|boolean|Stream)>

Arguments to pass the method call

targetmodule:kdns/contacts~Contact

Contact address information

respondmodule: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.

Parameters:
NameTypeDescription
keystring

Key of storage item to delete

respondmodule: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.

Parameters:
NameTypeDescription
expiratormodule: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.

Parameters:
NameTypeDescription
replicatormodule:kdns/node~ReplicatorStream

Reads from a module:kdns/node~ReadableStore