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

seed-stream.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a18cc4ad289b7f31fbe048e250096816c871191d (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
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)

  const tracker = new Tracker({ udp: false, ws: false })

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

  let seeder, client, announceUrl, magnetURI

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

    cb => {
      const port = tracker.http.address().port
      announceUrl = `http://localhost:${port}/announce`

      seeder = new WebTorrent({ dht: false, lsd: false })

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

      const stream = new Readable()
      stream._read = () => {}
      stream.push('HELLO WORLD\n')
      stream.push(null)

      const seederOpts = {
        name: 'hello.txt',
        pieceLength: 5,
        announce: [announceUrl]
      }
      seeder.seed([stream], seederOpts, torrent => {
        magnetURI = torrent.magnetURI
        cb(null)
      })
    },

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

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

      client.add(magnetURI, dl => {
        t.equal(dl.files.length, 1)
        t.equal(dl.files[0].name, 'hello.txt')
        t.equal(dl.files[0].length, 12)
        dl.files[0].getBuffer((err, buf) => {
          t.error(err)
          t.equal(buf.toString('utf8'), 'HELLO WORLD\n', 'content')

          cb(null)
        })
      })
    }
  ], err => {
    t.error(err)
    seeder.destroy(err => { t.error(err, 'seeder destroyed') })
    client.destroy(err => { t.error(err, 'client destroyed') })
    tracker.close(() => { t.pass('tracker closed') })
  })
})