Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add `torrent.magnetURI` getter function #225

Merged
merged 4 commits into from Dec 30, 2014
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -192,8 +192,6 @@ WebTorrent.prototype.seed = function (input, opts, onseed) {
}
if (!opts) opts = {}

// TODO: support an array of paths

createTorrent.parseInput(input, opts, function (err, files) {
if (err) return self.emit('error', err)
var streams = files.map(function (file) { return file.getStream })
@@ -12,7 +12,6 @@ var inherits = require('inherits')
var MultiStream = require('multistream')
var once = require('once')
var sha1 = require('git-sha1')
var stream = require('stream')

var BLOCK_LENGTH = 16 * 1024

@@ -33,6 +32,7 @@ inherits(Piece, EventEmitter)
function Piece (index, hash, buffer) {
var self = this
EventEmitter.call(self)
if (!debug.enabled) self.setMaxListeners(0)

self.index = index
self.hash = hash
@@ -185,6 +185,7 @@ inherits(File, EventEmitter)
function File (storage, file, pieces, pieceLength) {
var self = this
EventEmitter.call(self)
if (!debug.enabled) self.setMaxListeners(0)

self.storage = storage
self.name = file.name
@@ -278,6 +279,7 @@ inherits(Storage, EventEmitter)
function Storage (parsedTorrent, opts) {
var self = this
EventEmitter.call(self)
if (!debug.enabled) self.setMaxListeners(0)
opts = opts || {}

self.bitfield = new BitField(parsedTorrent.pieces.length)
@@ -43,6 +43,7 @@ inherits(Torrent, EventEmitter)
function Torrent (torrentId, opts) {
var self = this
EventEmitter.call(self)
if (!debug.enabled) self.setMaxListeners(0)
debug('new torrent')

self.client = opts.client
@@ -94,9 +95,9 @@ function Torrent (torrentId, opts) {
} else throw new Error('invalid torrent id')

function onTorrentId (torrentId) {
parsedTorrent = parseTorrent(torrentId)
self.infoHash = parsedTorrent.infoHash
if (parsedTorrent.name) self.name = parsedTorrent.name // preliminary name
self.parsedTorrent = parseTorrent(torrentId)
self.infoHash = self.parsedTorrent.infoHash
if (self.parsedTorrent.name) self.name = self.parsedTorrent.name // preliminary name

// create swarm
self.swarm = new Swarm(self.infoHash, self.client.peerId, {
@@ -111,10 +112,10 @@ function Torrent (torrentId, opts) {

if (process.browser) {
// in browser, swarm does not listen
self._onSwarmListening(parsedTorrent)
self._onSwarmListening()
} else {
// listen for peers
self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self, parsedTorrent))
self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self))
}
process.nextTick(function () {
self.emit('infoHash')
@@ -165,15 +166,21 @@ Object.defineProperty(Torrent.prototype, 'ratio', {
}
})

Torrent.prototype._onSwarmListening = function (parsed, port) {
Object.defineProperty(Torrent.prototype, 'magnetURI', {
get: function () {
return parseTorrent.toMagnetURI(this.parsedTorrent)
}
})

Torrent.prototype._onSwarmListening = function (port) {
var self = this
if (self._destroyed) return

self.client.torrentPort = port

// begin discovering peers via the DHT and tracker servers
self.discovery = new Discovery({
announce: parsed.announce,
announce: self.parsedTorrent.announce,
dht: self.client.dht,
tracker: self.client.tracker,
peerId: self.client.peerId,
@@ -186,7 +193,7 @@ Torrent.prototype._onSwarmListening = function (parsed, port) {
reemit(self.discovery, self, ['dhtAnnounce', 'warning', 'error'])

// if full metadata was included in initial torrent id, use it
if (parsed.info) self._onMetadata(parsed)
if (self.parsedTorrent.info) self._onMetadata(self.parsedTorrent)

self.emit('listening', port)
}
@@ -201,7 +208,7 @@ Torrent.prototype._onMetadata = function (metadata) {

if (metadata && metadata.infoHash) {
// `metadata` is a parsed torrent (from parse-torrent module)
self.metadata = parseTorrent.toBuffer(metadata)
self.metadata = parseTorrent.toTorrentFile(metadata)
self.parsedTorrent = metadata
} else {
self.metadata = metadata
@@ -12,7 +12,7 @@ inherits(VideoStream, stream.Writable)

function VideoStream (video, opts) {
var self = this
if (!(self instanceof VideoStream)) return new VideoStream($video, opts)
if (!(self instanceof VideoStream)) return new VideoStream(video, opts)
stream.Writable.call(self, opts)

self.video = video
@@ -45,10 +45,13 @@ function VideoStream (video, opts) {

VideoStream.prototype._write = function (chunk, encoding, cb) {
var self = this
if (!self._sourceBuffer)
return self._cb = function () {
if (!self._sourceBuffer) {
self._cb = function (err) {
if (err) return cb(err)
self._write(chunk, encoding, cb)
}
return
}

if (self._sourceBuffer.updating)
return cb(new Error('Cannot append buffer while source buffer updating'))
@@ -56,7 +56,7 @@
"multistream": "^1.4.2",
"network-address": "0.0.5",
"once": "^1.3.1",
"parse-torrent": "^2.1.0",
"parse-torrent": "^3.0.0",
"pretty-bytes": "^1.0.1",
"pump": "^1.0.0",
"random-access-file": "^0.3.1",
@@ -10,9 +10,10 @@ var leaves = fs.readFileSync(leavesPath)
var leavesTorrent = parseTorrent(leaves)
var leavesBookPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
var numbersPath = __dirname + '/content/numbers'
var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce'

test('client.add (http url to a torrent file (string))', function (t) {
t.plan(1)
t.plan(2)

var server = http.createServer(function (req, res) {
res.end(leaves)
@@ -25,6 +26,7 @@ test('client.add (http url to a torrent file (string))', function (t) {
var client = new WebTorrent({ dht: false, tracker: false })
client.add(url, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.destroy()
server.close()
})
@@ -33,27 +35,41 @@ test('client.add (http url to a torrent file (string))', function (t) {
})

test('client.add (filesystem path to a torrent file (string))', function (t) {
t.plan(1)
t.plan(2)

var client = new WebTorrent({ dht: false, tracker: false })
client.add(leavesPath, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.destroy()
})
})

test('client.seed (filesystem path to file (string))', function (t) {
t.plan(1)
t.plan(2)

var opts = {
name: 'Leaves of Grass by Walt Whitman.epub',
announce: [
'http://tracker.thepiratebay.org/announce',
'udp://tracker.openbittorrent.com:80',
'udp://tracker.ccc.de:80',
'udp://tracker.publicbt.com:80',
'udp://fr33domtracker.h33t.com:3310/announce',
'http://tracker.bittorrent.am/announce'
]
}

var client = new WebTorrent({ dht: false, tracker: false })
client.seed(leavesBookPath, function (torrent) {
client.seed(leavesBookPath, opts, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.destroy()
})
})

test('client.seed (filesystem path to folder (string))', function (t) {
t.plan(1)
t.plan(2)

var opts = {
pieceLength: 32768, // force piece length to 32KB so info-hash will
@@ -66,6 +82,7 @@ test('client.seed (filesystem path to folder (string))', function (t) {
var client = new WebTorrent({ dht: false, tracker: false })
client.seed(numbersPath, opts, function (torrent) {
t.equal(torrent.infoHash, '80562f38656b385ea78959010e51a2cc9db41ea0')
t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:80562f38656b385ea78959010e51a2cc9db41ea0&dn=numbers&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80&tr=wss%3A%2F%2Ftracker.webtorrent.io')
client.destroy()
})
})
@@ -7,55 +7,79 @@ var leaves = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesTorrent = parseTorrent(leaves)
var leavesBook = fs.readFileSync(__dirname + '/content/Leaves of Grass by Walt Whitman.epub')

function verify (t, client, torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
client.destroy()
}
var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce'

test('client.add (magnet uri, torrent file, info hash, and parsed torrent)', function (t) {
t.plan(5)

// magnet uri (utf8 string)
var client1 = new WebTorrent({ dht: false, tracker: false })
verify(t, client1, client1.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash))
var torrent1 = client1.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
t.equal(torrent1.infoHash, leavesTorrent.infoHash)
t.equal(torrent1.magnetURI, 'magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
client1.destroy()

// torrent file (buffer)
var client2 = new WebTorrent({ dht: false, tracker: false })
verify(t, client2, client2.add(leaves))
var torrent2 = client2.add(leaves)
t.equal(torrent2.infoHash, leavesTorrent.infoHash)
t.equal(torrent2.magnetURI, leavesMagnetURI)
client2.destroy()

// info hash (hex string)
var client3 = new WebTorrent({ dht: false, tracker: false })
verify(t, client3, client3.add(leavesTorrent.infoHash))
var torrent3 = client3.add(leavesTorrent.infoHash)
t.equal(torrent3.infoHash, leavesTorrent.infoHash)
t.equal(torrent3.magnetURI, 'magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
client3.destroy()

// info hash (buffer)
var client4 = new WebTorrent({ dht: false, tracker: false })
verify(t, client4, client4.add(new Buffer(leavesTorrent.infoHash, 'hex')))
var torrent4 = client4.add(new Buffer(leavesTorrent.infoHash, 'hex'))
t.equal(torrent4.infoHash, leavesTorrent.infoHash)
t.equal(torrent4.magnetURI, 'magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
client4.destroy()

// parsed torrent (from parse-torrent)
var client5 = new WebTorrent({ dht: false, tracker: false })
verify(t, client5, client5.add(leavesTorrent))
var torrent5 = client5.add(leavesTorrent)
t.equal(torrent5.infoHash, leavesTorrent.infoHash)
t.equal(torrent5.magnetURI, leavesMagnetURI)
client5.destroy()

t.end()
})

test('client.seed (Buffer, Blob)', function (t) {
t.plan(2)
t.plan(typeof Blob !== 'undefined' ? 4 : 2)

var opts = {
name: 'Leaves of Grass by Walt Whitman.epub'
name: 'Leaves of Grass by Walt Whitman.epub',
announce: [
'http://tracker.thepiratebay.org/announce',
'udp://tracker.openbittorrent.com:80',
'udp://tracker.ccc.de:80',
'udp://tracker.publicbt.com:80',
'udp://fr33domtracker.h33t.com:3310/announce',
'http://tracker.bittorrent.am/announce'
]
}

// torrent file (Buffer)
var client1 = new WebTorrent({ dht: false, tracker: false })
client1.seed(leavesBook, opts, function (torrent) {
verify(t, client1, torrent)
client1.seed(leavesBook, opts, function (torrent1) {
t.equal(torrent1.infoHash, leavesTorrent.infoHash)
t.equal(torrent1.magnetURI, leavesMagnetURI)
client1.destroy()
})

// Blob
if (typeof Blob !== 'undefined') {
var client2 = new WebTorrent({ dht: false, tracker: false })
client2.seed(new Blob([ leavesBook ]), opts, function (torrent) {
verify(t, client2, torrent)
client2.seed(new Blob([ leavesBook ]), opts, function (torrent2) {
t.equal(torrent2.infoHash, leavesTorrent.infoHash)
t.equal(torrent2.magnetURI, leavesMagnetURI)
client2.destroy()
})
} else {
t.pass('Skipping Blob test because missing `Blob` constructor')
console.log('Skipping Blob test because missing `Blob` constructor')
}
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.