Welcome to mirror list, hosted at ThFree Co, Russian Federation.

download-webseed-magnet.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 925c65ce2a134e9ac7763fae5186f58bd0cf2ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import http from 'http'
import path from 'path'
import finalhandler from 'finalhandler'
import fixtures from 'webtorrent-fixtures'
import MemoryChunkStore from 'memory-chunk-store'
import series from 'run-series'
import serveStatic from 'serve-static'
import test from 'tape'
import WebTorrent from '../../index.js'

test('Download using webseed (via magnet uri)', t => {
  t.plan(9)

  const serve = serveStatic(path.dirname(fixtures.leaves.contentPath))
  const httpServer = http.createServer((req, res) => {
    const done = finalhandler(req, res)
    serve(req, res, done)
  })
  let client1, client2

  httpServer.on('error', err => { t.fail(err) })

  series([
    cb => {
      httpServer.listen(cb)
    },

    cb => {
      client1 = new WebTorrent({ dht: false, tracker: false, lsd: false })

      client1.on('error', err => { t.fail(err) })
      client1.on('warning', err => { t.fail(err) })

      let gotTorrent = false
      let gotListening = false
      function maybeDone () {
        if (gotTorrent && gotListening) cb(null)
      }

      client1.on('torrent', torrent => {
        // torrent metadata has been fetched -- sanity check it
        t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub')

        const names = [
          'Leaves of Grass by Walt Whitman.epub'
        ]

        t.deepEqual(torrent.files.map(file => file.name), names)

        // NOTE: client1 is *NOT* a seeder. Just has the metadata.
        gotTorrent = true
        maybeDone()
      })

      const torrent = client1.add(fixtures.leaves.parsedTorrent, { store: MemoryChunkStore })

      torrent.on('infoHash', () => {
        gotListening = true
        maybeDone()
      })
    },

    cb => {
      client2 = new WebTorrent({ dht: false, tracker: false, lsd: false })

      client2.on('error', err => { t.fail(err) })
      client2.on('warning', err => { t.fail(err) })

      const webSeedUrl = `http://localhost:${httpServer.address().port}/${fixtures.leaves.parsedTorrent.name}`
      const magnetURI = `${fixtures.leaves.magnetURI}&ws=${encodeURIComponent(webSeedUrl)}`

      client2.on('torrent', torrent => {
        let gotBuffer = false
        let torrentDone = false

        torrent.files.forEach(file => {
          file.getBuffer((err, buf) => {
            t.error(err)
            t.deepEqual(buf, fixtures.leaves.content, 'downloaded correct content')
            gotBuffer = true
            maybeDone()
          })
        })

        torrent.once('done', () => {
          t.pass('client2 downloaded torrent from client1')
          torrentDone = true
          maybeDone()
        })

        function maybeDone () {
          if (gotBuffer && torrentDone) cb(null)
        }
      })

      const torrent = client2.add(magnetURI, { store: MemoryChunkStore })

      torrent.on('infoHash', () => {
        torrent.addPeer(`127.0.0.1:${client1.address().port}`)
      })
    }
  ], err => {
    t.error(err)
    client1.destroy(err => {
      t.error(err, 'client destroyed')
    })
    client2.destroy(err => {
      t.error(err, 'client destroyed')
    })
    httpServer.close(() => {
      t.pass('http server closed')
    })
  })
})