{"name":"@hyperswarm/dht","version":"6.4.0","description":"The DHT powering Hyperswarm","main":"index.js","browser":"browser.js","bin":{"hyperswarm-dht":"bin.js"},"dependencies":{"@hyperswarm/secret-stream":"^6.0.0","b4a":"^1.3.1","bogon":"^1.0.0","compact-encoding":"^2.4.1","compact-encoding-net":"^1.0.1","debugging-stream":"^2.0.0","dht-rpc":"^6.5.0","events":"^3.3.0","hypercore-crypto":"^3.3.0","noise-curve-ed":"^2.0.0","noise-handshake":"^3.0.0","record-cache":"^1.1.1","safety-catch":"^1.0.1","sodium-universal":"^3.0.4","udx-native":"^1.5.1","xache":"^1.1.0"},"devDependencies":{"brittle":"^3.0.0","keypear":"^1.1.1","standard":"^16.0.4"},"scripts":{"test":"standard && node test/all.js","test:generate":"brittle -r test/all.js test/*.js","lint":"standard"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","directories":{"lib":"lib","test":"test"},"repository":{"type":"git","url":"git+https://github.com/hyperswarm/dht.git"},"keywords":[],"bugs":{"url":"https://github.com/hyperswarm/dht/issues"},"homepage":"https://github.com/hyperswarm/dht#readme","standard":{"ignore":["**/*.mjs"]},"gitHead":"b9e101ad80119c734f9c6270a7db9e75fe68e93c","_id":"@hyperswarm/dht@6.4.0","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"integrity":"sha512-LLIfirUPgSsELso3fx/32j2pyoc1XGXBwMSXJHogoU/1rpuILXjiP0JHQnXSpkBGVcT/mRew0bBcOyIzG/rzzQ==","shasum":"c5698074c1c036455db8499e68dbc826da47e375","tarball":"http://registry-npm.ywwl.com/@hyperswarm/dht/download/@hyperswarm/dht-6.4.0.tgz","fileCount":24,"unpackedSize":110412,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC/1FYo2Voz6X8l4oF/QsT7uvgAgOPPrAQkiXFtlfFygAIgdToPWDzjE3HXsxtJJDtK+d2TASgxKE+SBCMRAoQYU+0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjo0YrACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrlgA/9ERyN5eD7RXDgQdPTl4rtV3Lkmi5Rj2Ybwsi72OBcPjshXd5v\r\n2d7GO+ashGqLfHoAC/Y7QAM+LXROXUIrH75zVFC7ELHoX6wc0eV79l1BKPou\r\nGacBxwavtEhFaTeNVxY/OolXHi0ykWRMZ6QAuz9U8alGD6jvYnE3zavaiwkt\r\nIw/yo8da1U7JPjcNoSGkzEgjN0L0f7goYLf1pCDuHDdwGg3svTKxG7ofmzsn\r\nWlbIIfVEKPIXJjVTQm+m/7SVkR3YOllWR10HGReF3lPRRQG5LfoGkkbicMql\r\nw4eC4PCiKpYLH488TKmt9E+DMLUVNP07dkUT24Qv3xUO+FMLLl7rUsHAEG6G\r\n0yyzgnRQ4idqDwhkqi4enqexydIwUsUumXnpgtP4LJG3ldpgz7ERVqrHjdkL\r\nK8DB1FzZ72LnCSWlL8v+zMsZHF6bgGRdaYul066k5peomUnGTdC/IcGancYj\r\n77lE7Wwb0VOaJXiww9JuwyAyyASWA6eD6grI7RQ5I6BCTxxxt7M29hvF0E1L\r\nQTK4wh31LJdKABJbYPht7fxJhRUdBJ71qzThis+aYudX4rnC1NadVKlDPspv\r\nJjg66jiiUx5j0k51dXaAaCbCjHiZPqDgcb7HXYKfbujgrRAzwckrFtAe1SMh\r\n5kCbWHoKYZHchqCdYnH6XcWuHuFDXc5d5Mg=\r\n=Gpb+\r\n-----END PGP SIGNATURE-----\r\n","size":28106,"noattachment":false,"key":"/@hyperswarm/dht/-/@hyperswarm/dht-6.4.0.tgz"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"maintainers":[{"name":"kasperisager","email":"kasperisager@hey.com"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"davidmarkclements","email":"david.mark.clements@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dht_6.4.0_1671644715796_0.9322663318316826"},"_hasShrinkwrap":false,"readme":"# @hyperswarm/dht\n\n### [See the full API docs at docs.holepunch.to](https://docs.holepunch.to/building-blocks/hyperswarm#dht)\n\nThe DHT powering Hyperswarm\n\n```\nnpm install @hyperswarm/dht\n```\n\nBuilt on top of [dht-rpc](https://github.com/mafintosh/dht-rpc).\n\nThe Hyperswarm DHT uses a series of holepunching techniques to make sure connectivity works on most networks,\nand is mainly used to facilitate finding and connecting to peers using end to end encrypted Noise streams.\n\n## Usage\n\nTo try it out, first instantiate a DHT instance\n\n``` js\nimport DHT from '@hyperswarm/dht'\n\nconst node = new DHT()\n```\n\nThen on one computer listen for connections\n\n``` js\n// create a server to listen for secure connections\nconst server = node.createServer()\n\nserver.on('connection', function (socket) {\n  // socket is E2E encrypted between you and the other peer\n  console.log('Remote public key', socket.remotePublicKey)\n\n  // pipe it somewhere like any duplex stream\n  process.stdin.pipe(socket).pipe(process.stdout)\n})\n\n// make a ed25519 keypair to listen on\nconst keyPair = DHT.keyPair()\n\n// this makes the server accept connections on this keypair\nawait server.listen(keyPair)\n```\n\nThen on another connect to the computer using the public key of the key-pair it is listening on\n\n``` js\n// publicKey here is keyPair.publicKey from above\nconst socket = anotherNode.connect(publicKey)\n\nsocket.on('open', function () {\n  // socket fully open with the other peer\n})\n\n// pipe it somewhere like any duplex stream\nprocess.stdin.pipe(socket).pipe(process.stdout)\n```\n\n## API\n\n#### `const node = new DHT([options])`\n\nCreate a new HyperSwarm DHT node.\n\nOptions include:\n\n```js\n{\n  // Optionally overwrite the default bootstrap servers, just need to be an array of any known dht node(s)\n  // Defaults to ['node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737']\n  bootstrap: ['host:port'],\n  keyPair // set the default key pair to use for server.listen and connect\n}\n```\n\nSee [dht-rpc](https://github.com/mafintosh/dht-rpc) for more options as HyperDHT inherits from that.\n\n*Note:* The default bootstrap servers are publicly served on behalf of the commons. To run a fully isolated DHT, start one or more dht nodes with an empty bootstrap array (`new DHT({bootstrap:[]})`) and then use the addresses of those nodes as the `bootstrap` option in all other dht nodes. You'll need at least one persistent node for the network to be completely operational.\n\n#### `keyPair = DHT.keyPair([seed])`\n\nUse this method to generate the required keypair for DHT operations.\n\nReturns an object with `{publicKey, secretKey}`. `publicKey` holds a public key buffer, `secretKey` holds a private key buffer.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n#### `await node.destroy([options])`\n\nFully destroy this DHT node.\n\nThis will also unannounce any running servers.\nIf you want to force close the node without waiting for the servers to unannounce pass `{ force: true }`.\n\n#### `node = DHT.bootstrapper(port, host, [options])`\n\nIf you want to run your own Hyperswarm network use this method to easily create a bootstrap node.\n\n## Creating P2P servers\n\n#### `const server = node.createServer([options], [onconnection])`\n\nCreate a new server for accepting incoming encrypted P2P connections.\n\nOptions include:\n\n```js\n{\n  firewall (remotePublicKey, remoteHandshakePayload) {\n    // validate if you want a connection from remotePublicKey\n    // if you do return false, else return true\n    // remoteHandshakePayload contains their ip and some more info\n    return true\n  }\n}\n```\n\nYou can run servers on normal home computers, as the DHT will UDP holepunch connections for you.\n\n#### `await server.listen(keyPair)`\n\nMake the server listen on a keyPair.\nTo connect to this server use keyPair.publicKey as the connect address.\n\n#### `server.refresh()`\n\nRefresh the server, causing it to reannounce its address. This is automatically called on network changes.\n\n#### `server.on('connection', socket)`\n\nEmitted when a new encrypted connection has passed the firewall check.\n\n`socket` is a [NoiseSecretStream](https://github.com/holepunchto/hyperswarm-secret-stream) instance.\n\nYou can check who you are connected to using `socket.remotePublicKey` and `socket.handshakeHash` contains a unique hash representing this crypto session (same on both sides).\n\n#### `server.on('listening')`\n\nEmitted when the server is fully listening on a keyPair.\n\n#### `server.address()`\n\nReturns an object containing the address of the server:\n\n```js\n{\n  host, // external IP of the server,\n  port, // external port of the server if predictable,\n  publicKey // public key of the server\n}\n```\n\nYou can also get this info from `node.remoteAddress()` minus the public key.\n\n#### `await server.close()`\n\nStop listening.\n\n#### `server.on('close')`\n\nEmitted when the server is fully closed.\n\n## Connecting to P2P servers\n\n#### `const socket = node.connect(remotePublicKey, [options])`\n\nConnect to a remote server. Similar to `createServer` this performs UDP holepunching for P2P connectivity.\n\nOptions include:\n\n```js\n{\n  nodes: [...], // optional array of close dht nodes to speed up connecting\n  keyPair // optional key pair to use when connection (defaults to node.defaultKeyPair)\n}\n```\n\n#### `socket.on('open')`\n\nEmitted when the encrypted connection has been fully established with the server.\n\n#### `socket.remotePublicKey`\n\nThe public key of the remote peer.\n\n#### `socket.publicKey`\n\nThe public key of the local socket.\n\n## Additional peer discovery\n\n#### `const stream = node.lookup(topic, [options])`\n\nLook for peers in the DHT on the given topic. Topic should be a 32 byte buffer (normally a hash of something).\n\nThe returned stream looks like this\n\n```js\n{\n  // Who sent the response?\n  from: { id, host, port },\n  // What address they responded to (i.e. your address)\n  to: { host, port },\n  // List of peers announcing under this topic\n  peers: [ { publicKey, nodes: [{ host, port }, ...] } ]\n}\n```\n\nTo connect to the peers you should afterwards call `connect` with those public keys.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n#### `const stream = node.announce(topic, keyPair, [relayAddresses], [options])`\n\nAnnounce that you are listening on a key-pair to the DHT under a specific topic.\n\nWhen announcing you'll send a signed proof to peers that you own the key-pair and wish to announce under the specific topic. Optionally you can provide up to 3 nodes, indicating which DHT nodes can relay messages to you - this speeds up connects later on for other users.\n\nAn announce does a parallel lookup so the stream returned looks like the lookup stream.\n\nCreating a server using `dht.createServer` automatically announces itself periodically on the key-pair it is listening on. When announcing the server under a specific topic, you can access the nodes it is close to using `server.nodes`.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n#### `await node.unannounce(topic, keyPair, [options])`\n\nUnannounce a key-pair.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n## Mutable/immutable records\n\n#### `{ hash, closestNodes } = await node.immutablePut(value, [options])`\n\nStore an immutable value in the DHT. When successful, the hash of the value is returned.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n#### `{ value, from } = await node.immutableGet(hash, [options])`\n\nFetch an immutable value from the DHT. When successful, it returns the value corresponding to the hash.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n#### `await { publicKey, closestNodes, seq, signature } = node.mutablePut(keyPair, value, [options])`\n\nStore a mutable value in the DHT.\n\nIf you pass any options they are forwarded to dht-rpc.\n\n#### `await { value, from, seq, signature } = node.mutableGet(publicKey, [options])`\n\nFetch a mutable value from the DHT.\n\nOptions:\n\n* `seq` - OPTIONAL, default `0`, a number which will only return values with corresponding `seq` values that are greater than or equal to the supplied `seq` option.\n* `latest` - OPTIONAL - default `false`, a boolean indicating whether the query should try to find the highest seq before returning, or just the first verified value larger than `options.seq` it sees.\n\nAny additional options you pass are forwarded to dht-rpc.\n\n## Additional API\n\nSee [dht-rpc](https://github.com/mafintosh/dht-rpc) for the additional APIs the DHT exposes.\n\n## CLI\n\nYou can start a DHT node in the command line, with the bundled cli tool:\n\n```sh\nnpm install -g @hyperswarm/dht\nhyperswarm-dht # runs a DHT node\nhyperswarm-dht --bootstrap # runs a DHT node with bootstrap settings\nhyperswarm-dht --nodes 5 # runs 5 nodes\n```\n\n## License\n\nMIT\n","publish_time":1671644715949,"_cnpm_publish_time":1671644715949,"dist-tags":{"latest":"6.5.1","next":"5.0.25"}}