UNPKG

3.6 kBMarkdownView Raw
1# magnet-uri [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url]
2
3[travis-image]: https://img.shields.io/travis/feross/magnet-uri.svg?style=flat
4[travis-url]: https://travis-ci.org/feross/magnet-uri
5[npm-image]: https://img.shields.io/npm/v/magnet-uri.svg?style=flat
6[npm-url]: https://npmjs.org/package/magnet-uri
7[downloads-image]: https://img.shields.io/npm/dm/magnet-uri.svg?style=flat
8[downloads-url]: https://npmjs.org/package/magnet-uri
9
10### Parse a magnet URI and return an object of keys/values.
11
12[![Sauce Test Status](https://saucelabs.com/browser-matrix/magnet-uri.svg)](https://saucelabs.com/u/magnet-uri)
13
14Also works in the browser with [browserify](http://browserify.org/)! This module is used by [WebTorrent](http://webtorrent.io).
15
16## install
17
18```
19npm install magnet-uri
20```
21
22## usage
23
24### decode
25
26Parse a magnet URI and return an object of keys/values.
27
28```js
29var magnet = require('magnet-uri')
30
31// "Leaves of Grass" by Walt Whitman
32var 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'
33
34var parsed = magnet.decode(uri)
35console.log(parsed.dn) // "Leaves of Grass by Walt Whitman.epub"
36console.log(parsed.infoHash) // "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"
37
38```
39
40The `parsed` magnet link object looks like this:
41
42```js
43 {
44 "xt": "urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
45 "dn": "Leaves of Grass by Walt Whitman.epub",
46 "tr": [
47 "udp://tracker.openbittorrent.com:80",
48 "udp://tracker.publicbt.com:80",
49 "udp://tracker.istole.it:6969",
50 "udp://tracker.ccc.de:80",
51 "udp://open.demonii.com:1337"
52 ],
53
54 // added for convenience:
55 "infoHash": "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
56 "name": "Leaves of Grass by Walt Whitman.epub",
57 "announce": [
58 "udp://tracker.openbittorrent.com:80",
59 "udp://tracker.publicbt.com:80",
60 "udp://tracker.istole.it:6969",
61 "udp://tracker.ccc.de:80",
62 "udp://open.demonii.com:1337"
63 ]
64 }
65```
66
67### encode
68
69Convert an object of key/values into a magnet URI string.
70
71```js
72var magnet = require('magnet-uri')
73
74var uri = magnet.encode({
75 xt: [
76 'urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1',
77 'urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY',
78 'urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q'
79 ],
80 xl: '10826029',
81 dn: 'mediawiki-1.15.1.tar.gz',
82 tr: [
83 'udp://tracker.openbittorrent.com:80/announce'
84 ],
85 as: 'http://download.wikimedia.org/mediawiki/1.15/mediawiki-1.15.1.tar.gz',
86 xs: [
87 'http://cache.example.org/XRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5',
88 'dchub://example.org'
89 ]
90})
91
92console.log(uri) // the magnet uri
93```
94
95The returned magnet uri will be:
96
97```
98magnet:?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%3A%2F%2Fexample.org
99```
100
101You can also use convenience key names like `name` (`dn`), `infoHash` (`xt`),
102`announce` (`tr`), `announceList` (`tr`), and `keywords` (`kt`).
103
104## license
105
106MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).