Skip to content

Commit

Permalink
fix(webtorrent): Allow duplicate torrents to be on the page.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Oct 2, 2017
1 parent 5c651cc commit 8187c4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
const ZComponent = require('zcomponent')
const WebTorrent = require('webtorrent')
const magnetURL = require('magnet-uri')

const client = new WebTorrent()

const getTorrent = (magnet, fn) => {
let decoded = magnetURL(magnet)
for (let torrent of client.torrents) {
if (torrent.infoHash === decoded.infoHash) {
// TODO: make sure all trackers and web seeds are added
return fn(torrent)
}
}
client.add(magnet, fn)
}

class WebTorrentElement extends ZComponent {
set file (file) {
this._file = file
}
set src (magnet) {
client.add(magnet, torrent => {
getTorrent(magnet, torrent => {
torrent.files.forEach(file => {
if (this._file) {
if (file.name === this._file || file.path === this._file) {
file.appendTo(this)
}
} else {
file.appendTo(this)
}
})
})
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "budo dist.js:bundle.js",
"precommit": "npm test",
"prepush": "npm test",
"prepublish": "mkdir -p dist && browserify dist.js > dist/webtorrent-element.js && cat dist/webtorrent-element.js | minify > dist/webtorrent-element.min.js",
"prepublishOnly": "distjs",
"commitmsg": "validate-commit-msg",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
Expand All @@ -18,17 +18,25 @@
"dist"
]
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"keywords": [],
"author": "Mikeal Rogers <[email protected]> (http://www.mikealrogers.com)",
"license": "Apache-2.0",
"dependencies": {
"distjs": "^1.0.0",
"magnet-uri": "^5.1.7",
"webtorrent": "^0.98.19",
"zcomponent": "^1.0.4"
},
"devDependencies": {
"babel-minify": "^0.2.0",
"browserify": "^14.4.0",
"budo": "^10.0.4",
"cz-conventional-changelog": "^2.0.0",
"husky": "^0.14.3",
"load-js": "^2.0.0",
"standard": "^10.0.3",
Expand All @@ -38,4 +46,4 @@
"type": "git",
"url": "https://github.com/mikeal/webtorrent-component.git"
}
}
}

3 comments on commit 8187c4a

@feross
Copy link

@feross feross commented on 8187c4a Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a client.get() method that essentially does what your method does. Returns the torrent if it already exists in the client, or null if it does not.

@mikeal
Copy link
Owner Author

@mikeal mikeal commented on 8187c4a Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feross how does it define a match?

If the magnet URL has additional trackers on it, does it add them?

@feross
Copy link

@feross feross commented on 8187c4a Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeal It defines a match as having the same infoHash and ignores other differences. It's a getter method, so it doesn't make any changes. There currently isn't a way to add a new tracker once a torrent has already been added to the client.

Please sign in to comment.