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

download.js « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ec7e03f037cc44a3923b1134d6d4bdbabf8ecce (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var auto = require('run-auto')
var WebTorrent = require('../')
var BlockStream = require('block-stream')
var DHT = require('bittorrent-dht/client')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')
var TrackerServer = require('bittorrent-tracker/server')

var leavesFile = __dirname + '/torrents/Leaves of Grass by Walt Whitman.epub'
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)

function downloadTrackerTest (t, serverType) {
  t.plan(8)

  var trackerStartCount = 0

  auto({
    tracker: function (cb) {
      var tracker = new TrackerServer(
        serverType === 'udp' ? { http: false } : { udp: false }
      )

      tracker.on('error', function (err) {
        t.fail(err)
      })

      tracker.on('start', function () {
        trackerStartCount += 1
      })

      tracker.listen(function (port) {
        var announceUrl = serverType === 'http'
          ? 'http://127.0.0.1:' + port + '/announce'
          : 'udp://127.0.0.1:' + port

        // Overwrite announce with our local tracker
        leavesParsed.announce = [ announceUrl ]
        leavesParsed.announceList = [[ announceUrl ]]

        cb(null, tracker)
      })
    },

    client1: ['tracker', function (cb) {
      var client1 = new WebTorrent({ dht: false })
      client1.on('error', function (err) { t.fail(err) })

      client1.add(leavesParsed)

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

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

        t.deepEqual(torrent.files.map(function (file) { return file.name }), names)

        torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
          cb(err, client1)
        })
      })
    }],

    client2: ['client1', function (cb) {
      var client2 = new WebTorrent({ dht: false })
      client2.on('error', function (err) { t.fail(err) })

      client2.add(leavesParsed)

      client2.on('torrent', function (torrent) {
        torrent.files.forEach(function (file) {
          file.createReadStream()
        })

        torrent.once('done', function () {
          t.pass('client2 downloaded torrent from client1')
          cb(null, client2)
        })
      })
    }]

  }, function (err, r) {
    t.error(err)
    t.equal(trackerStartCount, 2)

    r.tracker.close(function () {
      t.pass('tracker closed')
    })
    r.client1.destroy(function () {
      t.pass('client1 destroyed')
    })
    r.client2.destroy(function () {
      t.pass('client2 destroyed')
    })
  })
}

test('Simple download using UDP tracker', function (t) {
  downloadTrackerTest(t, 'udp')
})

test('Simple download using HTTP tracker', function (t) {
  downloadTrackerTest(t, 'http')
})

test('Simple download using a tracker (only) via a magnet uri', function (t) {
  t.plan(8)

  var trackerStartCount = 0

  var magnetUri
  auto({
    tracker: function (cb) {
      var tracker = new TrackerServer('udp')

      tracker.on('error', function (err) {
        t.fail(err)
      })

      tracker.on('start', function () {
        trackerStartCount += 1
      })

      tracker.listen(function (port) {
        var announceUrl = 'udp://127.0.0.1:' + port
        leavesParsed.announce = [ announceUrl ]
        leavesParsed.announceList = [[ announceUrl ]]
        magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + '&tr=' + encodeURIComponent(announceUrl)
        cb(null, tracker)
      })
    },

    client1: ['tracker', function (cb) {
      var client1 = new WebTorrent({ dht: false })
      client1.on('error', function (err) { t.fail(err) })

      client1.add(leavesParsed)

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

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

        t.deepEqual(torrent.files.map(function (file) { return file.name }), names)

        torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
          cb(err, client1)
        })
      })
    }],

    client2: ['client1', function (cb) {
      var client2 = new WebTorrent({ dht: false })
      client2.on('error', function (err) { t.fail(err) })

      client2.add(magnetUri)

      client2.on('torrent', function (torrent) {
        torrent.files.forEach(function (file) {
          file.createReadStream()
        })

        torrent.once('done', function () {
          t.pass('client2 downloaded torrent from client1')
          cb(null, client2)
        })
      })
    }]

  }, function (err, r) {
    t.error(err)
    t.equal(trackerStartCount, 2)

    r.tracker.close(function () {
      t.pass('tracker closed')
    })
    r.client1.destroy(function () {
      t.pass('client1 destroyed')
    })
    r.client2.destroy(function () {
      t.pass('client2 destroyed')
    })
  })
})

test('Simple download using DHT', function (t) {
  t.plan(7)

  // no trackers
  leavesParsed.announce = []
  leavesParsed.announceList = []

  // TODO: use actual DHT server here, instead of client
  var dhtServer = new DHT({ bootstrap: false })

  dhtServer.on('error', function (err) {
    t.fail(err)
  })

  auto({
    dhtPort: function (cb) {
      dhtServer.listen(function (port) {
        cb(null, port)
      })
    },
    client1: ['dhtPort', function (cb, r) {
      var client1 = new WebTorrent({
        trackers: false,
        dht: { bootstrap: '127.0.0.1:' + r.dhtPort }
      })
      client1.on('error', function (err) { t.fail(err) })

      client1.add(leavesParsed)

      var announced, wroteStorage
      function maybeDone (err) {
        if ((announced && wroteStorage) || err) cb(err, client1)
      }

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

        var names = [ 'Leaves of Grass by Walt Whitman.epub' ]
        t.deepEqual(torrent.files.map(function (file) { return file.name }), names)

        torrent.on('dhtAnnounce', function () {
          announced = true
          maybeDone(null)
        })

        torrent.storage.load(fs.createReadStream(leavesFile), function (err) {
          wroteStorage = true
          maybeDone(err)
        })
      })
    }],

    client2: ['client1', function (cb, r) {
      var client2 = new WebTorrent({
        trackers: false,
        dht: { bootstrap: '127.0.0.1:' + r.dhtPort }
      })
      client2.on('error', function (err) { t.fail(err) })

      client2.add(leavesParsed)

      client2.on('torrent', function (torrent) {
        torrent.files.forEach(function (file) {
          file.createReadStream()
        })

        torrent.once('done', function () {
          t.pass('client2 downloaded torrent from client1')
          cb(null, client2)
        })
      })
    }],

  }, function (err, r) {
    t.error(err)
    r.client1.destroy(function () {
      t.pass('client1 destroyed')
    })
    r.client2.destroy(function () {
      t.pass('client2 destroyed')
    })
    dhtServer.destroy(function () {
      t.pass('dht server destroyed')
    })
  })
})