@ctrl/magnet-link
TypeScript icon, indicating that this package has built-in type declarations

4.0.2 • Public • Published

magnet-link npm coverage

Parse a magnet URI into an object

Port of webtorrent/magnet-uri by feross that uses fewer dependencies in typescript

Demo: https://magnet-link.vercel.app

Install

npm install @ctrl/magnet-link

Use

Encode

import { magnetDecode } from '@ctrl/magnet-link';

const uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.example4.com%3A80&tr=udp%3A%2F%2Ftracker.example5.com%3A80&tr=udp%3A%2F%2Ftracker.example3.com%3A6969&tr=udp%3A%2F%2Ftracker.example2.com%3A80&tr=udp%3A%2F%2Ftracker.example1.com%3A1337';
const parsed = magnetDecode(uri);
console.log(parsed.dn) // "Leaves of Grass by Walt Whitman.epub"
console.log(parsed.infoHash) // "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"

The entire parsed object

{
  "xt": "urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
  "dn": "Leaves of Grass by Walt Whitman.epub",
  "tr": [
    "udp://tracker.example1.com:1337",
    "udp://tracker.example2.com:80",
    "udp://tracker.example3.com:6969",
    "udp://tracker.example4.com:80",
    "udp://tracker.example5.com:80"
  ],
  "name": "Leaves of Grass by Walt Whitman.epub",
  "infoHash": "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
  "announce": [
    "udp://tracker.example1.com:1337",
    "udp://tracker.example2.com:80",
    "udp://tracker.example3.com:6969",
    "udp://tracker.example4.com:80",
    "udp://tracker.example5.com:80"
  ]
}

Decode

import { magnetEncode } from '@ctrl/magnet-link';

// convert object to magnet uri
const uri = magnetEncode({
  xt: [
    'urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1',
    'urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY',
    'urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q',
  ],
  xl: '10826029',
  dn: 'mediawiki-1.15.1.tar.gz',
  tr: [
    'udp://tracker.openbittorrent.com:80/announce',
  ],
  as: 'http://download.wikimedia.org/mediawiki/1.15/mediawiki-1.15.1.tar.gz',
  xs: [
    'http://cache.example.org/XRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5',
    'dchub://example.org',
  ],
});


You can also use convenience key names like name (dn), infoHash (xt), infoHashIntArray (xt), announce (tr), and keywords (kt).

See Also

Package Sidebar

Install

npm i @ctrl/magnet-link

Weekly Downloads

1,114

Version

4.0.2

License

MIT

Unpacked Size

15.8 kB

Total Files

7

Last publish

Collaborators

  • scttcper