Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magnetizer

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magnetizer - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

14

build/decode.d.ts

@@ -1,3 +0,13 @@

/// <reference types="node" />
export declare function decode(data: string | Buffer): void;
interface MagnetURI {
displayNames?: string[];
length?: number;
infoHashes?: string[];
webSeeds?: string[];
acceptableSources?: string[];
sources?: string[];
keywords?: string[];
manifest?: string;
trackers?: string[];
}
export declare function decode(magnetURI: string): MagnetURI;
export default decode;
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const base32 = __importStar(require("hi-base32"));
// TODO: The standard also allows for multiple parameters of the same type to be used by appending ".1", ".2", etc. to the parameter name, e.g.: magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7
/**
* https://en.wikipedia.org/wiki/Magnet_URI_scheme
*/
var HASH;
(function (HASH) {
HASH["TIGER_TREE_HASH"] = "tree";
HASH["SECURE_HASH_ALGORITHM_1"] = "sha1";
HASH["BIT_PRINT"] = "bitprint";
HASH["E_DONKEY_2000"] = "ed2k";
HASH["ADVANCED_INTELLIGENT_CORRUPTION_HANDLER"] = "aich";
HASH["KAZAA_HASH"] = "kzhash";
HASH["BIT_TORRENT_INFO_HASH"] = "btih";
HASH["MESSAGE_DIGEST_5"] = "md5";
})(HASH || (HASH = {}));
var MAGNET_PARAMETER;
(function (MAGNET_PARAMETER) {
MAGNET_PARAMETER["DISPLAY_NAME"] = "dn";
MAGNET_PARAMETER["LENGTH"] = "xl";
MAGNET_PARAMETER["INFO_HASH"] = "xt";
MAGNET_PARAMETER["WEB_SEED"] = "ws";
MAGNET_PARAMETER["ACCEPTABLE_SOURCE"] = "as";
MAGNET_PARAMETER["SOURCE"] = "xs";
MAGNET_PARAMETER["KEYWORD"] = "kt";
MAGNET_PARAMETER["MANIFEST"] = "mt";
MAGNET_PARAMETER["TRACKER"] = "tr";
})(MAGNET_PARAMETER || (MAGNET_PARAMETER = {}));
class MagnetDecoder {
constructor(link) {
this._link = typeof link === 'string'
? Buffer.from(link)
: link;
constructor() {
this._decodedMagnetURI = {};
}
decode() {
static _splitMagnetURI(magnetURI) {
return magnetURI.replace('magnet:?', '').split('&');
}
decode(magnetURI) {
if (!magnetURI.startsWith('magnet:?')) {
return this._decodedMagnetURI;
}
const parametersList = MagnetDecoder._splitMagnetURI(magnetURI);
for (const parameter of parametersList) {
this._decodeParameter(parameter);
}
return this._decodedMagnetURI;
}
_decodeParameter(param) {
const [key, value] = param.split('=');
if (!key || !value) {
return;
}
switch (key) {
case MAGNET_PARAMETER.DISPLAY_NAME:
return this._addDisplayName(value);
case MAGNET_PARAMETER.LENGTH:
return this._addLength(value);
case MAGNET_PARAMETER.INFO_HASH:
return this._addInfoHash(value);
case MAGNET_PARAMETER.WEB_SEED:
return this._addWebSeed(value);
case MAGNET_PARAMETER.ACCEPTABLE_SOURCE:
return this._addAcceptableSource(value);
case MAGNET_PARAMETER.SOURCE:
return this._addSource(value);
case MAGNET_PARAMETER.KEYWORD:
return this._addKeywords(value);
case MAGNET_PARAMETER.MANIFEST:
return this._addManifest(value);
case MAGNET_PARAMETER.TRACKER:
return this._addTracker(value);
default:
return;
}
}
_addDisplayName(file) {
this._decodedMagnetURI.displayNames = this._decodedMagnetURI.displayNames || [];
this._decodedMagnetURI.displayNames.push(decodeURIComponent(file).replace(/\+/g, ' '));
}
_addLength(length) {
this._decodedMagnetURI.length = parseInt(length);
}
_addInfoHash(urnValue) {
this._decodedMagnetURI.infoHashes = this._decodedMagnetURI.infoHashes || [];
const [urn, type, hash] = urnValue.split(':');
if (urn !== 'urn') {
return;
}
if (type === HASH.BIT_TORRENT_INFO_HASH) {
if (hash.length === 40) {
this._decodedMagnetURI.infoHashes.push(hash.toLowerCase());
}
if (hash.length === 32) {
this._decodedMagnetURI.infoHashes.push(Buffer.from(base32.decode.asBytes(hash)).toString('hex'));
}
return;
}
}
_addTracker(tracker) {
this._decodedMagnetURI.trackers = this._decodedMagnetURI.trackers || [];
this._decodedMagnetURI.trackers.push(decodeURIComponent(tracker));
}
_addKeywords(keywords) {
this._decodedMagnetURI.keywords = decodeURIComponent(keywords).split('+');
}
_addWebSeed(webSeed) {
this._decodedMagnetURI.webSeeds = this._decodedMagnetURI.webSeeds || [];
this._decodedMagnetURI.webSeeds.push(decodeURIComponent(webSeed));
}
_addAcceptableSource(source) {
this._decodedMagnetURI.acceptableSources = this._decodedMagnetURI.acceptableSources || [];
this._decodedMagnetURI.acceptableSources.push(decodeURIComponent(source));
}
_addSource(source) {
this._decodedMagnetURI.sources = this._decodedMagnetURI.sources || [];
this._decodedMagnetURI.sources.push(decodeURIComponent(source));
}
_addManifest(manifest) {
this._decodedMagnetURI.manifest = manifest.toLowerCase();
}
}
function decode(data) {
return new MagnetDecoder(data).decode();
function decode(magnetURI) {
return new MagnetDecoder().decode(magnetURI);
}
exports.decode = decode;
exports.default = decode;
// // magnet uri (as a utf8 string)
// parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
// // { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// // infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
//
// // magnet uri with torrent name
// parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves%20of%20Grass%20by%20Walt%20Whitman.epub')
// // { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// // dn: 'Leaves of Grass by Walt Whitman.epub',
// // infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// // name: 'Leaves of Grass by Walt Whitman.epub' }
//
// // magnet uri with trackers
// parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&tr=http%3A%2F%2Ftracker.example.com%2Fannounce')
// // { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// // tr: 'http://tracker.example.com/announce',
// // infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// // announce: [ 'http://tracker.example.com/announce' ] }

5

package.json
{
"name": "magnetizer",
"version": "0.0.1",
"version": "0.1.0",
"description": "Library for decoding and encoding magnet links.",

@@ -23,3 +23,4 @@ "main": "build/index.js",

"dependencies": {
"bencodec": "^2.1.1"
"bencodec": "^2.1.1",
"hi-base32": "^0.5.0"
},

@@ -26,0 +27,0 @@ "devDependencies": {

@@ -7,3 +7,3 @@

## Magnet-parser
## Magnetizer
Library for decoding and encoding [magnet links](https://en.wikipedia.org/wiki/Magnet_URI_scheme).

@@ -14,5 +14,4 @@

Fully tested with 100% code coverage.
Without dependencies.
## Installation

@@ -19,0 +18,0 @@ | npm | yarn |

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc