• Stars
    star
    190
  • Rank 203,739 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Torrent reader, writer, and hash checker for node.

node-torrent

Read, make, and hash check torrents with node.js!

Build Status Depfu codecov

Usage

Read a torrent

const nt = require('nt');

nt.read('path/to/file.torrent', (err, torrent) => {
  if (err) throw err;
  console.log('Info hash:', torrent.infoHash());
});

Make a torrent

let rs = nt.make('http://myannounce.net/url', __dirname + '/files');
rs.pipe(fs.createWriteStream('mytorrent.torrent'));

// callback style
nt.makeWrite('outputfile', 'http://announce.me', __dirname + '/files',
  ['somefile.ext', 'another.one', 'inside/afolder.mkv', 'afolder'],
  (err, torrent) => {
    if (err) throw err;
    console.log('Finished writing torrent!');
  });

Hash check a torrent

let hasher = torrent.hashCheck(file);

let p;
hasher.on('match', (i, hash, percent) => {
  p = percent;
});

hasher.on('end', () => {
  console.log('Hash Check:', p + '%', 'matched');
});

API

read(file, callback(Error, Torrent))

Reads a local file, or a readable stream. Returns readable stream.

An error can be returned if the torrent is formatted incorrectly. Does not check if the dictonaries are listed alphabetically. Refer to the BitTorrent Specification for more info on torrent metainfo.

make(announceURL, dir, [files], [options], [callback(Error, Torrent)])

Makes a new torrent. dir is root directory of the torrent. The files array will relatively read files from there. If files is omitted, it implicitly adds all of the files in dir to the torrent, including those in subdirectories. options can have the following:

  • announceList - An array of arrays of additional announce URLs.
  • comment
  • name - Can be used only in multi file mode. If not given, defaults to name of directory.
  • pieceLength - How to break up the pieces. Must be an integer n that says piece length will be 2^n. Default is 256KB, or 2^18.
  • private - Set true if this is a private torrent.
  • moreInfo - These go into the info dictionary of the torrent. Useful if you want to make a torrent have a unique info hash from a certain tracker.
  • maxFiles - Max files to open during piece hashing. Defaults to 250.

callback is called with a possible Error, and a Torrent object when hashing is finished.

make returns a Hasher object that emits raw bencoded data events.

makeWrite(output, annlounce, dir, [files], [options], [callback(Error, Torrent)])

A shortcut that pumps the returned readable stream from make into a writable stream that points to the file output. Returns a Hasher object.

Torrent

The read and make functions all call their callback with a Torrent object.

Torrent#metadata

Contains metadata of the torrent. Example:

{
  announce: 'udp://tracker.publicbt.com:80',
  'announce-list': [
    [ 'udp://tracker.publicbt.com:80' ],
    [ 'udp://tracker.ccc.de:80' ],
    [ 'udp://tracker.openbittorrent.com:80' ],
    [ 'http://tracker.thepiratebay.org/announce' ]
  ],
  comment: 'Torrent downloaded from http://thepiratebay.org',
  'creation date': 1303979726,
  info: { length: 718583808,
    name: 'ubuntu-11.04-desktop-i386.iso',
    'piece length': 524288,
    pieces: <Buffer e5 7a ...>
  }
}

Torrent#infoHash()

Get a torrent's info hash.

Torrent#createReadStream()

Creates a ReadableStream that emits raw bencoded data for this torrent. Returns the readable stream.

Torrent#createWriteStream(filepath)

Shortcut that pipes the stream from Torrent#createReadStream() to a writable file stream. Returns the writable stream.

Torrent#hashCheck(dir, [options])

Hash checks torrent against files in dir. Returns a Hasher object. options hash can have maxFiles to open during hashing. Defaults to 250. Returns a Hasher object.

Hasher

A Hasher object is returned when a torrent is created with make and when Torrent#hashCheck is called. It inherits from ReadableStream.

Hasher#pause()

Pause hash checking.

Hasher#resume()

Resumes hash checking.

Hasher#toggle()

Continues hashing if paused or pauses if not.

Hasher#destroy()

Stops hashing completely. Closes file descriptors and does not emit any more events.

Event: 'ready'

Finished examining files to be hashed and ready to start hashing their contents.

Event: 'data'

  • Buffer - data

Emits raw bencoded torrent data only when hasher is returned from the make function.

'progress'

  • number - percent
  • number - speed
  • number - avgSpeed

Emits the progress calculated by amount of bytes read from files. speed and avgSpeed are in bytes.

'hash'

  • number - index
  • string - hash
  • string - file
  • number - position
  • number - length

Emitted when a piece is hashed along with hash position and source.

'match'

  • number - index
  • string - hash
  • number - percentMatched
  • string - file
  • number - position
  • number - length

Emitted when a piece matches with its index, the piece, and the percentage of pieces matched so far.

'matcherror'

  • number - index
  • string - file
  • number - position
  • number - length

Emitted when a piece does not match.

'error'

  • Error - err

Error hash checking.

'end'

Hash checking is finished.

Install

npm install nt

Tests

Tests are written with vows

npm test

More Repositories

1

node-ytdl-core

YouTube video downloader in javascript.
JavaScript
4,507
star
2

randexp.js

Create random strings that match a given regular expression.
JavaScript
1,779
star
3

node-ytdl

Command line youtube video downloader.
JavaScript
1,188
star
4

node-m3u8stream

Concatenates segments from a m3u8/dash-mpd playlist into a consumable stream.
TypeScript
213
star
5

node-feedsub

Subscribes to RSS/Atom/JSON feeds and notifies on new items.
TypeScript
196
star
6

timequeue.js

A queue with custom concurrency and time limit.
TypeScript
191
star
7

feedme.js

RSS/Atom/JSON feed parser
TypeScript
155
star
8

clusterhub

Sync data in your cluster applications.
JavaScript
131
star
9

ret.js

Tokenizes a string that represents a regular expression.
JavaScript
90
star
10

socket.io-clusterhub

socket.io storage powered by clusterhub for multi process applications.
JavaScript
70
star
11

chrome-options

Options page for Chrome extensions
JavaScript
64
star
12

node-muk

Mock object methods and dependencies.
JavaScript
57
star
13

node-miniget

A small http(s) GET library.
TypeScript
52
star
14

node-streamify

Streamify helps you easily provide a streaming interface for your code.
JavaScript
51
star
15

pauseable.js

Create event emitters, intervals, and timeouts that can be paused and resumed.
JavaScript
47
star
16

node-streamspeed

A simple way to keep track of the speed of your node streams.
TypeScript
38
star
17

irc-colors.js

Color and formatting for irc bots made easy. Inspired by colors.js and cli-color.
JavaScript
35
star
18

node-kat

File and stream concatenation the right way.
JavaScript
25
star
19

node-stream-equal

Test that two readable streams are equal to each other.
TypeScript
24
star
20

node-streamin

Provide a better streaming api in your app.
JavaScript
20
star
21

npm-updates

Emits update events from the npm repository.
JavaScript
16
star
22

node-jstream

Continuously reads in JSON and outputs Javascript objects.
JavaScript
16
star
23

muk-prop.js

Mock object methods and properties
JavaScript
12
star
24

hook.io-feedsub

hook.io bindings for RSS/Atom/JSON feeds
JavaScript
11
star
25

node-eventyoshi

Allows several event emitters to be listened and emitted through a single one.
JavaScript
11
star
26

node-torrent-cli

CLI for reading, writing, and hash checking torrents
JavaScript
10
star
27

chrome-veefeed

Follow YouTube and Twitch channels, get notifications, categorize videos
JavaScript
9
star
28

pokecry

guess pokemon through their cries
JavaScript
8
star
29

node-newsemitter

An event emitter that emits only new events.
TypeScript
7
star
30

clusterchat

A simple multi process chat demo using socket.io-clusterhub
JavaScript
7
star
31

ordered-queue.js

Queue with concurrency that starts tasks in order and runs them in parallel.
JavaScript
7
star
32

node-module-boilerplate

My personal boilerplate that I use to create node modules.
JavaScript
6
star
33

vim-relative-indent

Hide leading indent
Vim Script
5
star
34

hook.io-npm

Hook that emits on npm module updates.
JavaScript
5
star
35

jps

A scraper for the jpopsuki tracker.
JavaScript
5
star
36

nickserv

Communicates with the NickServ IRC service
JavaScript
5
star
37

node-streamit

JavaScript
4
star
38

ann

IRC bot made to announce and for convenience.
CoffeeScript
4
star
39

vim-frozen

colorscheme for vim
Vim Script
3
star
40

twi

really basic twitter cli client
JavaScript
3
star
41

node-writestreamp

A writable file stream that creates directories as needed.
JavaScript
3
star
42

gifchat

gifphy bot for hipchat
JavaScript
2
star
43

queue2.js

A unordered queue and ordered queue working together.
JavaScript
2
star
44

pokenum

A way to remember numbers using Pokemon.
JavaScript
1
star
45

node-muk-require

Mock dependencies
JavaScript
1
star
46

nickie

Nickserv irc bot.
JavaScript
1
star
47

node-callme

Hey I just met you, and this is crazy, but here's my callback.
JavaScript
1
star
48

jquery.rubber.js

jQuery plugin that stretches text fields to fit their content.
JavaScript
1
star