1. lt_donthave
The BitTorrent lt_donthave extension (BEP 54)
lt_donthave
Package: lt_donthave
Created by: webtorrent
Last modified: Wed, 31 May 2023 11:24:27 GMT
Version: 2.0.0
License: MIT
Downloads: 11,749
Repository: https://github.com/webtorrent/lt_donthave

Install

npm install lt_donthave
yarn add lt_donthave

lt_donthave npm downloads javascript style guide

The BitTorrent lt_donthave extension (BEP 54)

JavaScript implementation of the The BitTorrent lt_donthave extension (BEP 54). Use with bittorrent-protocol.

The purpose of this extension is to allow peers to indicate that they no longer have a piece. It provides a single donthave message that means the opposite of the standard have message. In addition, when a client receives donthave, it knows that all requests for the matching piece have failed.

Works in the browser with browserify! This module is used by WebTorrent.

install

npm install lt_donthave

usage

This package should be used with bittorrent-protocol, which supports a plugin-like system for extending the protocol with additional functionality.

Say you're already using bittorrent-protocol. Your code might look something like this:

 import BitField from 'bitfield'
import Protocol from 'bittorrent-protocol'
import net from 'net'

net.createServer(socket => {
  var wire = new Protocol()
  socket.pipe(wire).pipe(socket)

  // handle handshake
  wire.on('handshake', (infoHash, peerId) => {
    wire.handshake(Buffer.from('my info hash'), Buffer.from('my peer id'))

    // advertise that we have all 10 pieces of the torrent
    const bitfield = new BitField(10)
    for (let i = 0; i <= 10; i++) {
      bitfield.set(i, true)
    }
    wire.bitfield(bitfield)
  })

}).listen(6881)

To add support for BEP 54, simply modify your code like this:

 import BitField from 'bitfield'
import Protocol from 'bittorrent-protocol'
import net from 'net'
import lt_donthave from 'lt_donthave'

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

  // initialize the extension
  wire.use(lt_donthave())

  // all `lt_donthave` functionality can now be accessed at wire.lt_donthave

  wire.on('request', (pieceIndex, offset, length, cb) => {
    // whoops, turns out we don't have any pieces after all
    wire.lt_donthave.donthave(pieceIndex)
    cb(new Error('not found'))
  })

  // 'donthave' event will fire when the remote peer indicates it no longer has a piece
  wire.lt_donthave.on('donthave', index => {
    // remote peer no longer has piece `index`
  })

  // handle handshake
  wire.on('handshake', (infoHash, peerId) => {
    wire.handshake(Buffer.from('my info hash'), Buffer.from('my peer id'))

    // advertise that we have all 10 pieces of the torrent
    const bitfield = new BitField(10)
    for (let i = 0; i <= 10; i++) {
      bitfield.set(i, true)
    }
    wire.bitfield(bitfield)
  })

}).listen(6881)

api

lt_donthave()

Initialize the extension.

 wire.use(lt_donthave())

lt_donthave.donthave(index)

Tell the remote peer that this peer no longer has the piece with the specified index.

Opposite of wire.have.

lt_donthave.on('donthave', index => {})

Fired when the remote peer no longer has the piece with the specified index.

Opposite of wire.on('have', index => ())

After this is fired, all outstanding requests to the remote peer for that piece will automatically fail.

license

MIT. Copyright (c) John Hiesey and WebTorrent, LLC.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code