Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: webtorrent/parse-torrent
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: leoherzog/parse-torrent
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 6 commits
  • 4 files changed
  • 3 contributors

Commits on May 12, 2021

  1. Initial Stab at v2 Support

    leoherzog committed May 12, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    a0c2286 View commit details

Commits on Aug 11, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8476c08 View commit details
  2. chore: jsdoc

    DiegoRBaquero authored Aug 11, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    267d444 View commit details

Commits on Jan 10, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    fc6e67e View commit details

Commits on Mar 27, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    dc731e7 View commit details

Commits on May 4, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    cb9c439 View commit details
Showing with 16 additions and 9 deletions.
  1. +14 −8 index.js
  2. +2 −1 package.json
  3. BIN test/torrents/bittorrent-v2-hybrid-test.torrent
  4. BIN test/torrents/bittorrent-v2-test.torrent
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ const get = require('simple-get')
const magnet = require('magnet-uri')
const path = require('path')
const sha1 = require('simple-sha1')
const sha256 = require('simple-sha256')
const queueMicrotask = require('queue-microtask')

module.exports = parseTorrent
@@ -19,31 +20,35 @@ module.exports.toTorrentFile = encodeTorrentFile
/**
* Parse a torrent identifier (magnet uri, .torrent file, info hash)
* @param {string|Buffer|Object} torrentId
* @return {Object}
* @return {Object} parsed torrent object
*/
function parseTorrent (torrentId) {
if (typeof torrentId === 'string' && /^(stream-)?magnet:/.test(torrentId)) {
// if magnet uri (string)
const torrentObj = magnet(torrentId)

// infoHash won't be defined if a non-bittorrent magnet is passed
if (!torrentObj.infoHash) {
if (!torrentObj.infoHash && !torrentObj.infoHashV2) {
throw new Error('Invalid torrent identifier')
}

return torrentObj
} else if (typeof torrentId === 'string' && (/^[a-f0-9]{40}$/i.test(torrentId) || /^[a-z2-7]{32}$/i.test(torrentId))) {
// if info hash (hex/base-32 string)
// if v1 info hash (hex/base-32 string)
return magnet(`magnet:?xt=urn:btih:${torrentId}`)
} else if (typeof torrentId === 'string' && /^1220(.{64})/.test(torrentId)) {
// if v2 info hash (base-32 string)
return magnet(`magnet:?xt=urn:btmh:${torrentId}`)
} else if (Buffer.isBuffer(torrentId) && torrentId.length === 20) {
// if info hash (buffer)
// if v1 info hash (buffer)
return magnet(`magnet:?xt=urn:btih:${torrentId.toString('hex')}`)
} else if (Buffer.isBuffer(torrentId)) {
// if .torrent file (buffer)
return decodeTorrentFile(torrentId) // might throw
} else if (torrentId && torrentId.infoHash) {
} else if (torrentId && (torrentId.infoHash || torrentId.infoHashV2)) {
// if parsed torrent (from `parse-torrent` or `magnet-uri`)
torrentId.infoHash = torrentId.infoHash.toLowerCase()
if (torrentId.infoHash) torrentId.infoHash = torrentId.infoHash.toLowerCase()
if (torrentId.infoHashV2) torrentId.infoHashV2 = torrentId.infoHashV2.toLowerCase()

if (!torrentId.announce) torrentId.announce = []

@@ -71,7 +76,7 @@ function parseTorrentRemote (torrentId, opts, cb) {
// filesystem path, so don't consider it an error yet.
}

if (parsedTorrent && parsedTorrent.infoHash) {
if (parsedTorrent && (parsedTorrent.infoHash || parsedTorrent.infoHashV2)) {
queueMicrotask(() => {
cb(null, parsedTorrent)
})
@@ -109,7 +114,7 @@ function parseTorrentRemote (torrentId, opts, cb) {
} catch (err) {
return cb(err)
}
if (parsedTorrent && parsedTorrent.infoHash) cb(null, parsedTorrent)
if (parsedTorrent && (parsedTorrent.infoHash || parsedTorrent.infoHashV2)) cb(null, parsedTorrent)
else cb(new Error('Invalid torrent identifier'))
}
}
@@ -148,6 +153,7 @@ function decodeTorrentFile (torrent) {

result.infoHash = sha1.sync(result.infoBuffer)
result.infoHashBuffer = Buffer.from(result.infoHash, 'hex')
result.infoHashV2 = sha256.sync(result.infoBuffer)

if (torrent.info.private !== undefined) result.private = !!torrent.info.private

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@
"magnet-uri": "^6.2.0",
"queue-microtask": "^1.2.3",
"simple-get": "^4.0.1",
"simple-sha1": "^3.1.0"
"simple-sha1": "^3.1.0",
"simple-sha256": "^1.1.0"
},
"devDependencies": {
"@webtorrent/semantic-release-config": "1.0.7",
Binary file added test/torrents/bittorrent-v2-hybrid-test.torrent
Binary file not shown.
Binary file added test/torrents/bittorrent-v2-test.torrent
Binary file not shown.