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

@z-torrent/protocol

v0.0.13

Published

Simple, robust, BitTorrent peer wire protocol implementation

Downloads

1,103

Readme

@z-torrent/protocol

npm

Simple, robust BitTorrent peer wire protocol implementation (TypeScript, ESM). Used by Z-Torrent.

Works in Node.js and in the browser when bundled (e.g. Vite, esbuild, Webpack).

Install

npm install @z-torrent/protocol

Usage

The protocol is implemented as a duplex stream, so you pipe to and from it.

| duplex streams | a.pipe(b).pipe(a) | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | | duplex streams | a.pipe(b).pipe(a) |

(Images from the "harnessing streams" talk by substack.)

import Wire from '@z-torrent/protocol'
import net from 'node:net'

net
  .createServer((socket) => {
    const wire = new Wire()

    socket.pipe(wire).pipe(socket)

    wire.on('handshake', (infoHash, peerId) => {
      // infoHash and peerId are hex strings from the peer
      wire.handshake(infoHash, peerId)
    })

    wire.on('unchoke', () => {
      console.log('peer is no longer choking us: ' + wire.peerChoking)
    })
  })
  .listen(6881)

Methods

Handshaking

Send and receive a handshake from the peer. This is the first message.

wire.handshake(infoHash, peerId, { dht: true })
wire.on('handshake', (infoHash, peerId, extensions) => {
  console.log(extensions.dht) // supports DHT (BEP-0005)
  console.log(extensions.extended) // supports extension protocol (BEP-0010)
})

For wire.handshake(), infoHash and peerId must be 20 bytes each, as a hex string, Uint8Array, or Node Buffer.

Choking

wire.peerChoking
wire.amChoking

wire.on('choke', () => {})
wire.on('unchoke', () => {})

Interested

wire.peerInterested
wire.amInterested

wire.on('interested', () => {})
wire.on('uninterested', () => {})

Bitfield

wire.bitfield(buffer)
wire.on('bitfield', (bitfield) => {})

wire.have(pieceIndex)
wire.on('have', (pieceIndex) => {})

wire.peerPieces.get(i) // true if peer has piece i

wire.peerPieces is a BitField, see bitfield.

Requests

wire.request(pieceIndex, offset, length, (err, block) => {
  if (err) return
})

wire.cancel(pieceIndex, offset, length)

wire.on('request', (pieceIndex, offset, length, callback) => {
  callback(null, block)
})

wire.requests
wire.peerRequests
wire.setTimeout(5000)

DHT and port

wire.port(dhtPort)
wire.on('port', (dhtPort) => {})

wire.peerExtensions.dht
wire.peerExtensions.extended

Keep-alive

wire.setKeepAlive(true)
wire.on('keep-alive', () => {})

Fast extension (BEP 6)

BitTorrent Fast Extension (BEP 6).

wire.handshake(infoHash, peerId, { fast: true })

wire.hasFast

wire.haveNone()
wire.on('have-none', () => {})

wire.haveAll()
wire.on('have-all', () => {})

wire.suggest(pieceIndex)
wire.on('suggest', (pieceIndex) => {})

wire.on('allowed-fast', (pieceIndex) => {})

wire.peerAllowedFastSet

wire.reject(pieceIndex, offset, length)
wire.on('reject', (pieceIndex, offset, length) => {})

Extension protocol (BEP 10)

BitTorrent Extension Protocol (BEP 10).

wire.extended(code, buffer)

@z-torrent/protocol exposes an extension API (wire.use()) for BEP 10 extensions such as BEP 9 (ut_metadata).

Transfer stats

wire.uploaded
wire.downloaded
wire.uploadSpeed()
wire.downloadSpeed()

wire.on('download', (numberOfBytes) => {})
wire.on('upload', (numberOfBytes) => {})

Extension API

Common extensions in this monorepo:

Register an extension with wire.use(). Extensions can hook handshake, extended, etc.

Example with @z-torrent/ut-metadata:

import Wire from '@z-torrent/protocol'
import { createUtMetadata } from '@z-torrent/ut-metadata'
import net from 'node:net'

net
  .createServer((socket) => {
    const wire = new Wire()
    socket.pipe(wire).pipe(socket)

    wire.use(createUtMetadata())

    wire.ut_metadata.fetch()

    wire.ut_metadata.on('metadata', (metadata) => {
      // Uint8Array .torrent info payload
    })

    wire.ut_metadata.on('warning', (err) => {
      console.log(err.message)
    })

    wire.on('handshake', (infoHash, peerId) => {
      wire.handshake(infoHash, peerId)
    })
  })
  .listen(6881)

See @z-torrent/ut-metadata for implementation details.

License

MIT. Based on bittorrent-protocol by Feross Aboukhadijeh, Mathias Buus, and WebTorrent, LLC. Maintained as @z-torrent/protocol in z-torrent.