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
|
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
const WebTorrent = require('../../')
test('extension support', function (t) {
t.plan(6)
let extendedHandshakes = 0
function Extension (wire) {
wire.extendedHandshake.test = 'Hello, World!'
}
Extension.prototype.name = 'wt_test'
Extension.prototype.onExtendedHandshake = function (extendedHandshake) {
extendedHandshakes += 1
t.equal(
extendedHandshake.test.toString(), 'Hello, World!',
'handshake.test === Hello, World!'
)
if (extendedHandshakes === 2) {
client1.destroy(function (err) {
t.error(err, 'client1 destroyed')
})
client2.destroy(function (err) {
t.error(err, 'client2 destroyed')
})
}
}
const client1 = new WebTorrent({ dht: false, tracker: false })
client1.on('error', function (err) { t.fail(err) })
client1.on('warning', function (err) { t.fail(err) })
const client2 = new WebTorrent({ dht: false, tracker: false })
client2.on('error', function (err) { t.fail(err) })
client2.on('warning', function (err) { t.fail(err) })
client1.add(fixtures.leaves.parsedTorrent, function (torrent1) {
torrent1.on('wire', function (wire) {
t.pass('client1 onWire')
wire.use(Extension)
})
const torrent2 = client2.add(fixtures.leaves.parsedTorrent.infoHash)
torrent2.on('wire', function (wire) {
t.pass('client2 onWire')
wire.use(Extension)
})
torrent2.on('infoHash', function () {
torrent2.addPeer('127.0.0.1:' + client1.address().port)
})
})
})
|