npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kademlia-table

v1.0.7

Published

XOR based routing table used for P2P networks such as a Kademlia DHT.

Downloads

474

Readme

kademlia-table

XOR distance based routing table used for P2P networks such as a Kademlia DHT.

npm install kademlia-table

An extendable implementation of Kademlia and K-Buckets closely following details set out in the Kademlia DHT paper.

Usage

import { KademliaTable } from "kademlia-table";
import { randomBytes } from "node:crypto";

interface Node {
	id: string;
	// ...properties of Node
}

// Create a new table that stores nodes "close" to the passed in id.
// The id should be uniformily distributed, ie a hash, random bytes etc.
const table = new KademliaTable<Node>(id());

// Add a node to the routing table
table.add({ id: id() });

// Get the 20 nodes "closest" to a passed in id
const closest = table.closest(id(), 20);

Extending The Table

The base class is extendable to add custom logic to operations. For example you may want to act on a bucket if it is full when attempting to add a node, such as comparing response times of nodes.

import { KademliaTable } from "kademlia-table";

interface Node {
	id: string;
	// ...properties of Node
}

class CustomTable extends KademliaTable<Node> {
	add(node: Node): boolean {
		const result = super.add(node);

		if (!result) {
			// Do something if bucket is full and retry
			return this.add(node);
		}

		return true;
	}
}

API

table = new KademliaTable(id, [configuration])

Create a new routing table.

id should be a string that is uniformily distributed. configuration includes:

{
  bucketSize?: 20 // Max number of nodes in a bucket
  encoding?: "utf8" // Encoding of id strings
}

bool = table.add(node)

Insert a new node. node.id must be a string of same or shorter length as table.id. When inserting a node the XOR distance between the node and the table.id is calculated and used to figure which bucket this node should be inserted into.

Returns true if the node could be added or already exists. Returns false if the bucket is full.

bool = table.has(id)

Returns true if a node exists for the passed in id and false otherwise.

node = table.get(id)

Returns a node or undefined if not found.

i = table.getBucketIndex(id)

Returns a node's corresponding bucket index.

nodes = table.closest(id, [maxNodes])

Returns an array of the closest (in XOR distance) nodes to the passed in id.

This method is normally used in a routing context, i.e. figuring out which nodes in a DHT should store a value based on its id.

true = table.remove(id)

Remove a node using its id.

table.nodes

Returns all nodes from table as an array. Ordered from closest to furthest buckets.

table.buckets

A fixed size array of all buckets in the table.

number = KademliaTable.getDistance(idA, idB, encoding)

Gets the XOR distance between two id strings.

1 | -1 | 0 = compare(idA, idB) = KademliaTable.createCompare(targetId, encoding)

Creates a function for sorting ids based on distance from a target id going from closest to furthest

License

MIT