webtorrent client add

const fixtures = require("webtorrent-fixtures")
const test = require("tape")
const WebTorrent = require("../index.js")

test("client.add: magnet uri, utf-8 string", t => {
  t.plan(6)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const torrent = client.add(fixtures.leaves.magnetURI)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)
    t.equal(torrent.magnetURI, fixtures.leaves.magnetURI)

    client.remove(fixtures.leaves.magnetURI, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: torrent file, buffer", t => {
  t.plan(6)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const torrent = client.add(fixtures.leaves.torrent)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)
    t.equal(torrent.magnetURI, fixtures.leaves.magnetURI)

    client.remove(fixtures.leaves.torrent, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: info hash, hex string", t => {
  t.plan(6)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const torrent = client.add(fixtures.leaves.parsedTorrent.infoHash)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)
    t.equal(torrent.magnetURI, `magnet:?xt=urn:btih:${fixtures.leaves.parsedTorrent.infoHash}`)

    client.remove(fixtures.leaves.parsedTorrent.infoHash, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: info hash, buffer", t => {
  t.plan(6)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const torrent = client.add(fixtures.leaves.parsedTorrent.infoHashBuffer)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)
    t.ok(torrent.magnetURI.indexOf(`magnet:?xt=urn:btih:${fixtures.leaves.parsedTorrent.infoHash}`) === 0)

    client.remove(Buffer.from(fixtures.leaves.parsedTorrent.infoHash, "hex"), err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: parsed torrent, from `parse-torrent`", t => {
  t.plan(6)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const torrent = client.add(fixtures.leaves.parsedTorrent)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)
    t.equal(torrent.magnetURI, fixtures.leaves.magnetURI)

    client.remove(fixtures.leaves.parsedTorrent, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: parsed torrent, with string type announce property", t => {
  t.plan(7)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const parsedTorrent = Object.assign({}, fixtures.leaves.parsedTorrent)
  parsedTorrent.announce = "http://tracker.local:80"

  const torrent = client.add(parsedTorrent)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)

    const expectedMagnetURI = `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent("http://tracker.local:80")}`
    t.equal(torrent.magnetURI, expectedMagnetURI)

    // `torrent.announce` must always be an array
    t.deepEqual(torrent.announce, ["http://tracker.local:80"])

    client.remove(fixtures.leaves.parsedTorrent, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: parsed torrent, with array type announce property", t => {
  t.plan(7)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => { t.fail(err) })
  client.on("warning", err => { t.fail(err) })

  const parsedTorrent = Object.assign({}, fixtures.leaves.parsedTorrent)
  parsedTorrent.announce = ["http://tracker.local:80", "http://tracker.local:81"]

  const torrent = client.add(parsedTorrent)
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash)

    const expectedMagnetURI = `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent("http://tracker.local:80")}&tr=${encodeURIComponent("http://tracker.local:81")}`
    t.equal(torrent.magnetURI, expectedMagnetURI)

    t.deepEqual(torrent.announce, ["http://tracker.local:80", "http://tracker.local:81"])

    client.remove(fixtures.leaves.parsedTorrent, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

test("client.add: invalid torrent id: empty string", t => {
  t.plan(3)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => {
    t.ok(err instanceof Error)
    t.ok(err.message.includes("Invalid torrent identifier"))

    client.destroy(err => { t.error(err, "client destroyed") })
  })
  client.on("warning", err => { t.fail(err) })

  client.add("")
})

test("client.add: invalid torrent id: short buffer", t => {
  t.plan(3)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", err => {
    t.ok(err instanceof Error)
    t.ok(err.message.includes("Invalid torrent identifier"))

    client.destroy(err => { t.error(err, "client destroyed") })
  })
  client.on("warning", err => { t.fail(err) })

  client.add(Buffer.from("abc"))
})

test("client.add: paused torrent", t => {
  t.plan(5)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on("error", (err) => { t.fail(err) })
  client.on("warning", (err) => { t.fail(err) })

  const torrent = client.add(fixtures.leaves.magnetURI, { paused: true })
  t.equal(client.torrents.length, 1)

  torrent.on("infoHash", () => {
    t.equal(torrent.paused, true)

    client.remove(fixtures.leaves.magnetURI, err => { t.error(err, "torrent destroyed") })
    t.equal(client.torrents.length, 0)

    client.destroy(err => { t.error(err, "client destroyed") })
  })
})

Related articles

webtorrent client add duplicate trackers

const fixtures = require("webtorrent-fixtures")const test = require("tape")const WebTorrent = require("../index.js")test("client.add: duplicate trackers", t => { t.plan(3) const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

webtorrent torrent events

const fixtures = require("webtorrent-fixtures")const test = require("tape")const MemoryChunkStore = require("memory-chunk-store")const randombytes = require("randombytes")const WebTorrent = require("../../index.js")test("client.add: emit torrent ev

webtorrent swarm timeout

// var randombytes = require("randombytes")// var Swarm = require("../../lib/swarm")// var test = require("tape")// var infoHash = "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"// var peerId1 = Buffer.from("-WW0001-" + randombytes(6).toString("hex"), "u

webtorrent swarm reconnect

// var randombytes = require("randombytes")// var Swarm = require("../../lib/swarm")// var test = require("tape")// var infoHash = "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"// var peerId1 = Buffer.from("-WW0001-" + randombytes(6).toString("hex"), "u

webtorrent swarm basic

// var randombytes = require("randombytes")// var Swarm = require("../../lib/swarm")// var test = require("tape")// var infoHash = "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"// var infoHash2 = "d2474e86c95b19b8bcfdb92bc12c9d44667cfa37"// var peerId

webtorrent server

const fs = require("fs")const fixtures = require("webtorrent-fixtures")const get = require("simple-get")const test = require("tape")const WebTorrent = require("../../index.js")test("torrent.createServer: programmatic http server", t => { t.plan(9

webtorrent seed while download

const fs = require("fs")const DHT = require("bittorrent-dht/server")const fixtures = require("webtorrent-fixtures")const MemoryChunkStore = require("memory-chunk-store")const series = require("run-series")const test = require("tape")const WebTorren

webtorrent seed stream

const { Readable } = require("stream")const series = require("run-series")const test = require("tape")const Tracker = require("bittorrent-tracker/server")const WebTorrent = require("../../index.js")test("client.seed: stream", t => { t.plan(9)

webtorrent metadata

const fixtures = require("webtorrent-fixtures")const test = require("tape")const WebTorrent = require("../../index.js")test("ut_metadata transfer", t => { t.plan(6) const client1 = new WebTorrent({ dht: false, tracker: false, lsd: false }) con

webtorrent limit methods

const fixtures = require("webtorrent-fixtures")const test = require("tape")const MemoryChunkStore = require("memory-chunk-store")const WebTorrent = require("../../index.js")const DOWNLOAD_SPEED_LIMIT = 200 * 1000 // 200 KB/sconst UPLOAD_SPEED_LIMIT