使用node.js阅读,制作和哈希检查Torrents!
const nt = require ( 'nt' ) ;
nt . read ( 'path/to/file.torrent' , ( err , torrent ) => {
if ( err ) throw err ;
console . log ( 'Info hash:' , torrent . infoHash ( ) ) ;
} ) ;
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!' ) ;
} ) ;
let hasher = torrent . hashCheck ( file ) ;
let p ;
hasher . on ( 'match' , ( i , hash , percent ) => {
p = percent ;
} ) ;
hasher . on ( 'end' , ( ) => {
console . log ( 'Hash Check:' , p + '%' , 'matched' ) ;
} ) ;
读取本地文件或可读流。返回可读流。
如果洪流格式不正确,可以返回错误。不检查dictonaries是否按字母顺序列出。有关Torrent Metainfo的更多信息,请参阅Bittorrent规范。
做一个新的洪流。 dir
是洪流的根目录。 files
数组将相对读取文件。如果省略了文件,则隐式将所有dir
中的所有文件添加到洪流中,包括子目录中的所有文件。 options
可以具有以下内容:
announceList
- 一系列额外的宣布URL阵列。comment
name
- 只能在多文件模式下使用。如果未给出,则默认为目录名称。pieceLength
- 如何分解碎片。必须是一个整数n
,说件长度为2^n
。默认值为256KB,或2^18。private
- 如果这是私人洪流,则设置为true。moreInfo
这些进入洪流的info
字典。如果您想使Torrent具有来自某个跟踪器的唯一信息,则有用。maxFiles
在片段哈希期间打开的最大文件。默认为250。 callback
带有可能的Error
,并且在哈希完成时是Torrent
对象。
make
返回散发出原始的data
事件的哈希对象。
一个快捷方式, make
返回的可读流泵送到可写入的流流,该流指向文件output
。返回哈希对象。
read
make
功能都用洪流对象调用他们的回调。
包含洪流的元数据。例子:
{
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 7 a . . . >
}
}
获取洪流的信息哈希。
创建一个可读取的式驱动器,为此洪流发出原始的Baccododed数据。返回可读的流。
将流从Torrent#createReadStream()
到可写的文件流的快捷方式。返回可写的流。
哈希检查dir
中的文件。返回哈希对象。 options
哈希可以在哈希期间打开maxFiles
。默认为250
。返回哈希对象。
当使用make
和Torrent#hashCheck
创建洪流时,将返回Hasher对象。它从readableStream继承。
暂停哈希检查。
简历检查。
如果暂停或暂停,继续进行散列。
完全停止哈希。关闭文件描述符,并且不会发出任何事件。
完成检查的文件,并准备好开始哈希内容。
Buffer
- 数据仅当从make
函数返回Hasher时,发射原始的torrent数据。
number
- 百分比number
- 速度number
- AVGSPEED发出根据从文件中读取的字节数量计算得出的进度。 speed
和avgSpeed
在字节中。
number
- 索引string
- 哈希string
- 文件number
- 位置number
- 长度将作品与哈希位置和来源一起发出时发出。
number
- 索引string
- 哈希number
- 百分比string
- 文件number
- 位置number
- 长度当作品与其index
,作品以及到目前为止的零件百分比匹配时,发出。
number
- 索引string
- 文件number
- 位置number
- 长度当一件不匹配时发出。
Error
- 错误错误哈希检查。
哈希检查完成。
npm install nt
测试用誓言编写
npm test