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

torrent-events.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32adb1faa90d9dce1a1b8f18ebc7bef1a3e57558 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
import MemoryChunkStore from 'memory-chunk-store'
import randombytes from 'randombytes'
import WebTorrent from '../../index.js'

test('client.add: emit torrent events in order', t => {
  t.plan(6)

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

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

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

  // Start seeding
  client2.seed(fixtures.leaves.content, {
    name: 'Leaves of Grass by Walt Whitman.epub',
    announce: []
  })

  client2.on('listening', () => {
    // Start downloading
    const torrent = client1.add(fixtures.leaves.parsedTorrent.infoHash, { store: MemoryChunkStore })

    // Manually connect peers
    torrent.addPeer(`127.0.0.1:${client2.address().port}`)

    let order = 0

    torrent.on('infoHash', () => {
      t.equal(++order, 1)
    })

    torrent.on('metadata', () => {
      t.equal(++order, 2)
    })

    torrent.on('ready', () => {
      t.equal(++order, 3)
    })

    torrent.on('done', () => {
      t.equal(++order, 4)

      client1.destroy(err => { t.error(err, 'client 1 destroyed') })
      client2.destroy(err => { t.error(err, 'client 2 destroyed') })
    })
  })
})

test('client.seed: emit torrent events in order', 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.seed(fixtures.leaves.content)

  let order = 0

  torrent.on('infoHash', () => {
    t.equal(++order, 1)
  })

  torrent.on('metadata', () => {
    t.equal(++order, 2)
  })

  torrent.on('ready', () => {
    t.equal(++order, 3)
  })

  torrent.on('done', () => {
    t.equal(++order, 4)
  })
  torrent.on('seed', () => {
    t.equal(++order, 5)
    client.destroy(err => { t.error(err, 'client destroyed') })
  })
})

test('file.select: check multiple done events', t => {
  t.plan(5)

  const client1 = new WebTorrent({ dht: false, tracker: false, lsd: false, utp: false })
  const client2 = new WebTorrent({ dht: false, tracker: false, lsd: false, utp: false })

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

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

  const fileA = Buffer.from(randombytes(16 * 1024).toString('hex'))
  const fileB = Buffer.from(randombytes(16 * 1024).toString('hex'))

  // Start seeding
  client2.seed([fileA, fileB], { announce: [] }, seedTorrent => {
    // Select only fileA (index 0)
    const magnet = seedTorrent.magnetURI + '&so=0'

    // Start downloading
    const torrent = client1.add(magnet, { store: MemoryChunkStore })

    // Manually connect peers
    torrent.addPeer(`127.0.0.1:${client2.address().port}`)

    let order = 0

    torrent.on('infoHash', () => {
      t.equal(++order, 1)
    })

    torrent.on('metadata', () => {
      t.equal(++order, 2)
    })

    torrent.on('ready', () => {
      t.equal(++order, 3)
    })

    torrent.on('done', () => {
      ++order

      if (order === 4) {
        torrent.files[1].select(0)
      } else if (order === 5) {
        client1.destroy(err => { t.error(err, 'client 1 destroyed') })
        client2.destroy(err => { t.error(err, 'client 2 destroyed') })
      }
    })
  })
})