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 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-bittorrent

v0.0.2

Published

BitTorrent DHT implementation in node

Readme

node-bittorrent

node-bittorrent is a BitTorrent DHT implementation in node.

If you like it, please give me a star. Thanks a lot!

Install

npm install node-bittorrent

Example

const DHT = require('node-bittorrent')

let dht = new DHT({
  port: 20000
})

dht.on('listening', address => {
  console.log(`server listening ${address.address}:${address.port}`)
})

dht.on('error', err => {
  console.log(`server error:\n${err}`)
})

dht.on('announce', (infoHash, address) => {
  console.log(`receive magnet:?xt=urn:btih:${infoHash.toUpperCase()} from ${address.address}:${address.port}`)

  // find peers for the given info hash
  dht.lookup(infoHash, (err, peers) => {
    if(err) {
      console.log(err)
    }else {
      console.log(`find peers of ${infoHash}`)
      console.log(peers)
    }
  })
})

API

new DHT([opt])

Create a new DHT instance.

if opt is specified, then the default options (show below) will be overridden.

{
  id: '',        // 160-bit DHT node ID(Buffer or hex String, default: randomly generated)
  port: 6881,    // make the DHT listen on the given port
  bootstraps: [] // bootstrap servers (default: router.bittorrent.com:6881, router.utorrent.com:6881, dht.transmissionbt.com:6881)
}

ping(address, port, id, callback)

Corresponse to the ping in DHT Queries.

example
dht.ping('router.bittorrent.com', 6881, 'abcdefghij0123456789', (err, id, address) => {
  if(err) console.log(err)
  else console.log(`${id} from ${address.address}:${address.port}`)
})

findNode(address, port, id, target, callback)

Corresponse to the find_node in DHT Queries.

example
dht.findNode('router.bittorrent.com', 6881, 'abcdefghij0123456789', 'mnopqrstuvwxyz123456', (err, id, nodes, address) => {
  if(err) console.log(err)
  else console.log(nodes)
})

getPeers(address, port, id, infohash, callback)

Corresponse to the get_peers in DHT Queries.

example
dht.getPeers('router.bittorrent.com', 6881, 'abcdefghij0123456789', 'e3811b9539cacff680e418124272177c47477157', (err, id, values, nodes, address) => {
  if(err) {
    console.log(err)
    return
  }
  
  if(values) console.log(values)
  if(nodes) console.log(nodes)
})

lookup(infoHash, callback)

Find peers for the given info hash.

example
dht.lookup('DDCB691E7CC497BF17FA13F06EC3BB9583AA6E60', (err, peers) => {
  if(err) {
    console.log(err)
    return
  } 

  console.log(`find peers of ${infoHash}`)
  console.log(peers)
})

getInfo()

Returns an object containing the information of the DHT. This object contains id, address and port properties.

getAllNodes()

Returns an array containing all nodes in the routing table.

destroy()

Destroy the DHT.

example
dht.lookup('DDCB691E7CC497BF17FA13F06EC3BB9583AA6E60', (err, peers) => {
  if(err) {
    console.log(err)
    return
  } 

  console.log(`find peers of ${infoHash}`)
  console.log(peers)
})

Event

dht.on('listening', callback)

Emitted when the DHT is listening.

example
dht.on('listening', address => {
  console.log(`server listening ${address.address}:${address.port}`)
})

dht.on('error', callback)

Emitted when the DHT has a fatal error.

example
dht.on('error', err => {
  console.log(`server error:\n${err}`)
})

dht.on('announce', callback)

Emitted when a node announces that it is downloading a torrent on a port.

example
dht.on('announce', (infoHash, address) => {
  console.log(`receive magnet:?xt=urn:btih:${infoHash.toUpperCase()} from ${address.address}:${address.port}`)
})

License

node-bittorrent is distributed under the MIT License.