{"_id":"kademlia-routing-table","_rev":"239769","name":"kademlia-routing-table","description":"XOR based routing table used for P2P networks such as a Kademlia DHT.","dist-tags":{"latest":"1.0.1"},"maintainers":[{"name":"mafintosh","email":""}],"time":{"modified":"2023-03-24T16:57:37.000Z","created":"2019-02-23T17:07:42.771Z","1.0.1":"2022-04-22T10:23:52.669Z","1.0.0":"2021-04-22T08:21:37.852Z","0.0.0":"2019-02-23T17:07:42.771Z"},"users":{},"author":{"name":"Mathias Buus","url":"@mafintosh"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/xor-routing-table.git"},"versions":{"1.0.1":{"name":"kademlia-routing-table","version":"1.0.1","description":"XOR based routing table used for P2P networks such as a Kademlia DHT.","main":"index.js","dependencies":{},"devDependencies":{"standard":"^16.0.3","tape":"^5.2.2"},"scripts":{"test":"standard && tape test.js"},"keywords":["kademlia","p2p","k-bucket","k-buckets","xor","routing","distributed","systems"],"repository":{"type":"git","url":"git+https://github.com/mafintosh/xor-routing-table.git"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","bugs":{"url":"https://github.com/mafintosh/xor-routing-table/issues"},"homepage":"https://github.com/mafintosh/xor-routing-table","gitHead":"28ed207b11900b96c535303a6459bbd84f3c5475","_id":"kademlia-routing-table@1.0.1","_nodeVersion":"16.13.2","_npmVersion":"6.14.16","dist":{"shasum":"6f18416f612e885a8d4df128f04c490a90d772f6","size":4618,"noattachment":false,"key":"/kademlia-routing-table/-/kademlia-routing-table-1.0.1.tgz","tarball":"http://name.csiicloud.com:7001/kademlia-routing-table/download/kademlia-routing-table-1.0.1.tgz"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"maintainers":[{"name":"mafintosh","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/kademlia-routing-table_1.0.1_1650623032511_0.32209972928614694"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-04-22T10:50:44.773Z","publish_time":1650623032669,"_cnpm_publish_time":1650623032669},"1.0.0":{"name":"kademlia-routing-table","version":"1.0.0","description":"XOR based routing table used for P2P networks such as a Kademlia DHT.","main":"index.js","dependencies":{},"devDependencies":{"standard":"^16.0.3","tape":"^5.2.2"},"scripts":{"test":"standard && tape test.js"},"keywords":["kademlia","p2p","k-bucket","k-buckets","xor","routing","distributed","systems"],"repository":{"type":"git","url":"git+https://github.com/mafintosh/xor-routing-table.git"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","bugs":{"url":"https://github.com/mafintosh/xor-routing-table/issues"},"homepage":"https://github.com/mafintosh/xor-routing-table","gitHead":"1c1b290921bcfce510c2521f14aaddc4c0366e48","_id":"kademlia-routing-table@1.0.0","_nodeVersion":"15.7.0","_npmVersion":"6.14.11","dist":{"shasum":"fdea00d18bfcd3d90c815d2643806b252c80b210","size":4623,"noattachment":false,"key":"/kademlia-routing-table/-/kademlia-routing-table-1.0.0.tgz","tarball":"http://name.csiicloud.com:7001/kademlia-routing-table/download/kademlia-routing-table-1.0.0.tgz"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"maintainers":[{"name":"mafintosh","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/kademlia-routing-table_1.0.0_1619079697729_0.5002674817857877"},"_hasShrinkwrap":false,"publish_time":1619079697852,"_cnpm_publish_time":1619079697852,"_cnpmcore_publish_time":"2021-12-16T23:52:56.810Z"},"0.0.0":{"name":"kademlia-routing-table","version":"0.0.0","description":"XOR based routing table used for P2P networks such as a Kademlia DHT.","main":"index.js","dependencies":{},"devDependencies":{"standard":"^12.0.1","tape":"^4.10.1"},"scripts":{"test":"standard && tape test.js"},"keywords":["kademlia","p2p","k-bucket","k-buckets","xor","routing","distributed","systems"],"repository":{"type":"git","url":"git+https://github.com/mafintosh/xor-routing-table.git"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","bugs":{"url":"https://github.com/mafintosh/xor-routing-table/issues"},"homepage":"https://github.com/mafintosh/xor-routing-table","gitHead":"ad38c32b2b3dac3b8f53d14041720e82ee7e78c3","_id":"kademlia-routing-table@0.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.1","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"9d5b8c6e841ad8dc053b19e3a459ab5aedb858c6","size":4508,"noattachment":false,"key":"/kademlia-routing-table/-/kademlia-routing-table-0.0.0.tgz","tarball":"http://name.csiicloud.com:7001/kademlia-routing-table/download/kademlia-routing-table-0.0.0.tgz"},"maintainers":[{"name":"mafintosh","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/kademlia-routing-table_0.0.0_1550941662618_0.7953760283437732"},"_hasShrinkwrap":false,"publish_time":1550941662771,"_cnpm_publish_time":1550941662771,"_cnpmcore_publish_time":"2021-12-16T23:52:57.034Z"}},"readme":"# kademlia-routing-table\n\nXOR distance based routing table used for P2P networks such as a Kademlia DHT.\n\n```\nnpm install kademlia-routing-table\n```\n\n[![build status](https://travis-ci.org/mafintosh/kademlia-routing-table.svg?branch=master)](https://travis-ci.org/mafintosh/kademlia-routing-table)\n\nSimilar to k-buckets, but implemented using the simplifications described in https://github.com/ethereum/wiki/wiki/Kademlia-Peer-Selection\n\nTo understand the concept behind peer routing, DHTs, and the terms used here,\nI recommend reading the [Kademlia DHT paper](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf) as well.\n\n## Usage\n\n``` js\nconst RoutingTable = require('kademlia-routing-table')\nconst { randomBytes } = require('crypto')\n\n// Create a new table that stores nodes \"close\" to the passed in id.\n// The id should be uniformily distributed, ie a hash, random bytes etc.\nconst table = new RoutingTable(randomBytes(32))\n\n// Add a node to the routing table\ntable.add({\n  id: randomBytes(32), // this field is required\n  // populate with any other data you want to store\n})\n\ntable.on('row', function (row) {\n  // A new row has been added to the routing table\n  // This row represents row.index similar bits to the table.id\n\n  row.on('full', function (node) {\n    // The row is full and cannot be split, so node cannot be added.\n    // If any of the nodes in the row are \"worse\", based on\n    // some application specific metric then we should remove\n    // the worst node from the row and re-add the node.\n  })\n})\n\n// Get the 20 nodes \"closest\" to a passed in id\nconst closest = table.closest(randomBytes(32), 20)\n```\n\n## API\n\n#### `table = new RoutingTable(id, [options])`\n\nCreate a new routing table.\n\n`id` should be a Buffer that is uniformily distributed. `options` include:\n\n``` js\n{\n  k: 20 // The max row size\n}\n```\n\n#### `bool = table.add(node)`\n\nInsert a new node. `node.id` must be a Buffer of same length as `table.id`.\nWhen inserting a node the XOR distance between the node and the table.id is\ncalculated and used to figure which table row this node should be inserted into.\n\nReturns `true` if the node could be added to the corresponding row or `false` if not.\nIf `false` is returned the onfullrow function is invoked for the corresponding row and node.\n\n#### `node = table.get(id)`\n\nGet a node from the table using its id. Returns `null` if no node has the passed in `id`.\n\n#### `bool = table.has(id)`\n\nReturns `true` if a node exists for the passed in `id` and `false` otherwise.\n\n#### `nodes = table.closest(id, [maxNodes])`\n\nReturns an array of the closest (in XOR distance) nodes to the passed in id.\n\n`id` should be Buffer of same length as `table.id`. Per default at max `k`\nnodes are returned, but this can be configured using the `maxNodes` argument.\n\nThis method is normally used in a routing context, i.e. figuring out which nodes\nin a DHT should store a value based on its id.\n\n#### `bool = table.remove(id)`\n\nRemove a node using its id. Returns `true` if a node existed for the id and\nwas removed and `false` otherwise.\n\n#### `node = table.random()`\n\nGet a random node from the table.\n\n#### `nodes = table.toArray()`\n\nReturns all nodes from table as an array. If you create a new routing table\nfrom these nodes it will be identical to the used here.\n\n#### `table.on('row', row)`\n\nEmitted when a new row is added to the routing table. At max, `bitLength(table.id)`\nwill exist.\n\n#### `table.rows`\n\nA fixed size array of all rows in the table. Normally you would not need to worry\nabout accessing rows directly outside the row event.\n\n## Row API\n\nFor the row passed in the the `onfullrow` function the following API exists.\n\n#### `row.index`\n\nThe row index. Represents how many prefix bits are shared between nodes in the row\nand the table id.\n\n#### `row.nodes`\n\nA list of all the nodes in the row, sorted by their `id`.\n\n#### `row.data`\n\nProperty set to null initially you can use if you want to store optional data on the row.\n\n#### `bool = row.add(node)`\n\nSame as `table.add` but for a specific row. Only use this to add the `newNode`\npassed in `onfullrow` function.\n\n#### `bool = row.remove(node)`\n\nSame as `table.remove` but for a specific row. Only use this to remove the\n\"worst\" node from the row when wanting to add the newNode.\n\n#### `row.on('add', node)`\n\nEmitted when a new node is added to this row.\n\n#### `row.on('remove', node)`\n\nEmitted when a node has been removed from this row.\n\n#### `row.on('full', node)`\n\nEmitted when a node wants to be added to this row, but the row is full (stores `k` nodes).\n\nWhen this happens you should check if any of the nodes already in the row (`row.nodes`) are\n\"worse\" than the passed node. If that is the case, remove the \"worst\" one and re-add the node passed in the arguments.\n\nVarious algorithms can be implemented to handle full rows, which is why the routing table leaves most of this logic\nup to the user. These kind of algorithms include adding the rejected node to a cache and wait for another node in the\nrow to be removed before trying to insert it again, or using an LRU cache to determine which node already in the row\nhas been heard from yet.\n\n## License\n\nMIT\n","_attachments":{},"homepage":"https://github.com/mafintosh/xor-routing-table","bugs":{"url":"https://github.com/mafintosh/xor-routing-table/issues"},"license":"MIT"}