magnet-uri
Advanced tools
Comparing version 3.1.0 to 4.0.0
21
index.js
@@ -29,4 +29,5 @@ var base32 = require('thirty-two') | ||
// Address tracker (tr) is an encoded URI, so decode it | ||
if (key === 'tr') val = decodeURIComponent(val) | ||
// Address tracker (tr), exact source (xs), and acceptable source (as) are encoded | ||
// URIs, so decode them | ||
if (key === 'tr' || key === 'xs' || key === 'as') val = decodeURIComponent(val) | ||
@@ -51,8 +52,14 @@ // Return keywords as an array | ||
var m | ||
if (result.xt && (m = result.xt.match(/^urn:btih:(.{40})/))) { | ||
result.infoHash = new Buffer(m[1], 'hex').toString('hex') | ||
} else if (result.xt && (m = result.xt.match(/^urn:btih:(.{32})/))) { | ||
var decodedStr = base32.decode(m[1]) | ||
result.infoHash = new Buffer(decodedStr, 'binary').toString('hex') | ||
if (result.xt) { | ||
var xts = Array.isArray(result.xt) ? result.xt : [ result.xt ] | ||
xts.forEach(function (xt) { | ||
if ((m = xt.match(/^urn:btih:(.{40})/))) { | ||
result.infoHash = new Buffer(m[1], 'hex').toString('hex') | ||
} else if ((m = xt.match(/^urn:btih:(.{32})/))) { | ||
var decodedStr = base32.decode(m[1]) | ||
result.infoHash = new Buffer(decodedStr, 'binary').toString('hex') | ||
} | ||
}) | ||
} | ||
if (result.dn) result.name = result.dn | ||
@@ -59,0 +66,0 @@ if (result.kt) result.keywords = result.kt |
{ | ||
"name": "magnet-uri", | ||
"description": "Parse a magnet URI and return an object of keys/values", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -7,5 +7,2 @@ var magnet = require('../') | ||
test('parse valid magnet uris', function (t) { | ||
t.doesNotThrow(function () { | ||
magnet(leavesOfGrass) | ||
}) | ||
var result = magnet(leavesOfGrass) | ||
@@ -41,7 +38,4 @@ t.equal(result.xt, 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36') | ||
t.doesNotThrow(function () { magnet(empty1) }) | ||
t.deepEquals(magnet(empty1), {}) | ||
t.doesNotThrow(function () { magnet(empty2) }) | ||
t.deepEquals(magnet(empty2), {}) | ||
t.doesNotThrow(function () { magnet(empty3) }) | ||
t.deepEquals(magnet(empty3), {}) | ||
@@ -54,3 +48,2 @@ t.end() | ||
t.doesNotThrow(function () { magnet(uri) }) | ||
t.deepEquals(magnet(uri), { a: '', b: '', c: '' }) | ||
@@ -65,7 +58,4 @@ t.end() | ||
t.doesNotThrow(function () { magnet(invalid1) }) | ||
t.deepEquals(magnet(invalid1), {}) | ||
t.doesNotThrow(function () { magnet(invalid2) }) | ||
t.deepEquals(magnet(invalid2), {}) | ||
t.doesNotThrow(function () { magnet(invalid3) }) | ||
t.deepEquals(magnet(invalid3), {}) | ||
@@ -80,7 +70,4 @@ t.end() | ||
t.doesNotThrow(function () { magnet(invalid1) }) | ||
t.deepEquals(magnet(invalid1), { a: 'a' }) | ||
t.doesNotThrow(function () { magnet(invalid2) }) | ||
t.deepEquals(magnet(invalid2), { b: 'b' }) | ||
t.doesNotThrow(function () { magnet(invalid3) }) | ||
t.deepEquals(magnet(invalid3), { c: 'c' }) | ||
@@ -107,1 +94,26 @@ t.end() | ||
}) | ||
test('complicated magnet uri (multiple xt params, and as, xs)', function (t) { | ||
var result = magnet('magnet:?xt=urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1&xt=urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY&xt=urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q&xl=10826029&dn=mediawiki-1.15.1.tar.gz&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&as=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&xs=http%3A%2F%2Fcache.example.org%2FXRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5&xs=dchub://example.org') | ||
t.equal(result.infoHash, '81e177e2cc00943b29fcfc635457f575237293b0') | ||
t.deepEquals(result.xt, [ | ||
'urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1', | ||
'urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY', | ||
'urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q' | ||
]) | ||
t.equal(result.xl, '10826029') | ||
t.equal(result.dn, 'mediawiki-1.15.1.tar.gz') | ||
var announce = 'udp://tracker.openbittorrent.com:80/announce' | ||
var announceList = [ | ||
[ 'udp://tracker.openbittorrent.com:80/announce' ] | ||
] | ||
t.equal(result.tr, announce) | ||
t.deepEquals(result.announce, [ announce ]) | ||
t.deepEquals(result.announceList, announceList) | ||
t.equal(result.as, 'http://download.wikimedia.org/mediawiki/1.15/mediawiki-1.15.1.tar.gz') | ||
t.deepEquals(result.xs, [ | ||
'http://cache.example.org/XRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5', | ||
'dchub://example.org' | ||
]) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10664
159