@z-torrent/dht
v0.0.13
Published
BitTorrent DHT implementation (BEP 5, BEP 44)
Downloads
1,063
Maintainers
Readme
@z-torrent/dht
BitTorrent mainline DHT (BEP 5) and BEP 44 mutable/immutable store for JavaScript/TypeScript (Node.js).
Used by Z-Torrent.
Install
npm install @z-torrent/dhtUsage
import { DHT } from '@z-torrent/dht'
import { magnet } from '@z-torrent/magnet'
const uri = 'magnet:?xt=urn:btih:e3811b9539cacff680e418124272177c47477157'
const parsed = magnet.decode(uri)
const dht = new DHT()
dht.listen(20000, () => {
console.log('listening')
})
dht.on('peer', (peer, infoHash, from) => {
console.log('peer', peer.host, peer.port)
})
dht.lookup(parsed.infoHash)API
new DHT([opts])
Create a DHT client instance. Options include bootstrap, nodeId, host, maxTables, maxValues, verify (for BEP44), and are forwarded to the underlying k-rpc stack where applicable.
Methods
listen([port], [address], [cb])— bind UDP socketaddress()—{ port, address, family }destroy([cb])addNode({ host, port, id? })/removeNode(id)lookup(infoHash, [cb])— returns abort functionannounce(infoHash, port, [cb])put(opts, [cb])/get(key, [opts], cb)— BEP 44toJSON()— routing snapshot (nodes + cached values)
Events
ready, listening, peer, node, announce, warning, error, close, and protocol hooks (get_peers, find_node, etc.).
Scripts
bun run build— bundle todist/bun run test— offline tests undertest/*.test.tsbun run test-live— network-dependent tests intest/live/bun run typecheck—tsc --noEmit
License
MIT. See LICENSE.
