magnet-uri
Advanced tools
Comparing version 0.1.5 to 1.0.0
18
index.js
/** | ||
* Parse a magnet URI and return an object of keys/values. If the magnet URI is | ||
* invalid, this module throws an exception. | ||
* Parse a magnet URI and return an object of keys/values | ||
* | ||
@@ -13,5 +12,4 @@ * @param {string} uri | ||
if (!data || data.length === 0) { | ||
if (!data || data.length === 0) | ||
return result | ||
} | ||
@@ -22,13 +20,13 @@ var params = data.split('&') | ||
var keyval = param.split('=') | ||
// This keyval is invalid, skip it | ||
if (keyval.length !== 2) | ||
return | ||
var key = keyval[0] | ||
var val = keyval[1] | ||
if (keyval.length !== 2) { | ||
throw new Error('Invalid magnet URI') | ||
} | ||
// Address tracker (tr) is an encoded URI, so decode it | ||
if (key === 'tr') { | ||
if (key === 'tr') | ||
val = decodeURIComponent(val) | ||
} | ||
@@ -35,0 +33,0 @@ // If there are repeated parameters, return an array of values |
{ | ||
"name": "magnet-uri", | ||
"version": "0.1.5", | ||
"description": "Parse a magnet URI and return an object of keys/values", | ||
"main": "index.js", | ||
"version": "1.0.0", | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
"bugs": { | ||
"url": "https://github.com/feross/magnet-uri/issues" | ||
}, | ||
"devDependencies": { | ||
"tape": "2.x" | ||
}, | ||
"homepage": "https://github.com/feross/magnet-uri", | ||
"keywords": [ | ||
"magnet", | ||
"uri", | ||
"urn", | ||
"p2p", | ||
"peer-to-peer", | ||
"cryptolinks", | ||
"bittorrent", | ||
"webtorrent" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/feross/magnet-uri.git" | ||
}, | ||
"scripts": { | ||
@@ -21,23 +41,3 @@ "test": "tape test/*.js" | ||
] | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/feross/magnet-uri.git" | ||
}, | ||
"keywords": [ | ||
"magnet", | ||
"uri", | ||
"urn", | ||
"p2p", | ||
"peer-to-peer", | ||
"cryptolinks", | ||
"bittorrent", | ||
"webtorrent" | ||
], | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/feross/magnet-uri/issues" | ||
}, | ||
"homepage": "https://github.com/feross/magnet-uri" | ||
} | ||
} |
# magnet-uri | ||
[![Build Status](http://img.shields.io/travis/feross/magnet-uri.svg)](https://travis-ci.org/feross/magnet-uri) | ||
[![NPM Version](http://img.shields.io/npm/v/magnet-uri.svg)](https://npmjs.org/package/magnet-uri) | ||
[![NPM](http://img.shields.io/npm/dm/magnet-uri.svg)](https://npmjs.org/package/magnet-uri) | ||
[![Gittip](http://img.shields.io/gittip/feross.svg)](https://www.gittip.com/feross/) | ||
[![Build Status](http://img.shields.io/travis/feross/magnet-uri.png)](https://travis-ci.org/feross/magnet-uri) | ||
[![NPM Version](http://img.shields.io/npm/v/magnet-uri.png)](https://npmjs.org/package/magnet-uri) | ||
[![NPM](http://img.shields.io/npm/dm/magnet-uri.png)](https://npmjs.org/package/magnet-uri) | ||
[![Gittip](http://img.shields.io/gittip/feross.png)](https://www.gittip.com/feross/) | ||
[![browser support](https://ci.testling.com/feross/magnet-uri.png)](https://ci.testling.com/feross/magnet-uri) | ||
Parse a magnet URI and return an object of keys/values. If the magnet URI is invalid, this module throws an exception. | ||
### Parse a magnet URI and return an object of keys/values. | ||
Also works in the browser with [browserify](http://browserify.org/)! This module is used by [WebTorrent](http://webtorrent.io). | ||
## install | ||
@@ -23,13 +25,10 @@ | ||
// "Leaves of Grass" by Walt Whitman | ||
var leaves = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337' | ||
var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337' | ||
try { | ||
var parse = magnet(leaves) | ||
} catch (e) { | ||
console.error('Invalid magnet URI') | ||
} | ||
var parsed = magnet(uri) | ||
console.log(parsed.dn) // "Leaves+of+Grass+by+Walt+Whitman.epub" | ||
``` | ||
The parsed magnet link object looks like this: | ||
The `parsed` magnet link object looks like this: | ||
@@ -36,0 +35,0 @@ ```js |
@@ -24,7 +24,8 @@ var magnet = require('../') | ||
var empty1 = '' | ||
var empty2 = 'magnet:' | ||
var empty3 = 'magnet:?' | ||
test('empty magnet URIs return empty object', function (t) { | ||
var empty1 = '' | ||
var empty2 = 'magnet:' | ||
var empty3 = 'magnet:?' | ||
t.doesNotThrow(function () { magnet(empty1) }) | ||
@@ -39,12 +40,40 @@ t.deepEquals(magnet(empty1), {}) | ||
test('empty string as keys is okay', function (t) { | ||
var uri = 'magnet:?a=&b=&c=' | ||
var invalid1 = 'magnet:?xt=urn:btih:===' | ||
var invalid2 = 'magnet:?xt' | ||
var invalid3 = 'magnet:?xt=?dn=' | ||
t.doesNotThrow(function () { magnet(uri) }) | ||
t.deepEquals(magnet(uri), { a: '', b: '', c: '' }) | ||
t.end() | ||
}) | ||
test('invalid magnet URIs throw', function (t) { | ||
t.throws(function () { magnet(invalid1) }) | ||
t.throws(function () { magnet(invalid2) }) | ||
t.throws(function () { magnet(invalid3) }) | ||
test('invalid magnet URIs return empty object', function (t) { | ||
var invalid1 = 'magnet:?xt=urn:btih:===' | ||
var invalid2 = 'magnet:?xt' | ||
var invalid3 = 'magnet:?xt=?dn=' | ||
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), {}) | ||
t.end() | ||
}) | ||
}) | ||
test('invalid magnet URIs return only valid keys (ignoring invalid ones)', function (t) { | ||
var invalid1 = 'magnet:?a=a&===' | ||
var invalid2 = 'magnet:?a==&b=b' | ||
var invalid3 = 'magnet:?a=b=&c=c&d===' | ||
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' }) | ||
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7116
98
0
51