种子文件 
分析、检查和编码 BitTorrent metainfo 文件.
支持 BitTorrent v1 (帮助最佳环境做法3页:1BEP 52 最佳环境做法),和包含两种格式的混合种子文件.
该项目的基础是: 解析格式 财务报告和审定财务报表 节点编码它包括严格的 编码 在周围建造了编码器和解码器 Uint8Array 而不是节点.js Buffer。 。 。 .
发音: https://torrent-file.pages.dev 互联网档案馆的存檔,存档日期2013-12-21
安装
npm install @ctrl/torrent-file
API 密码
info(file)
返回显示元数据、跟踪器、网络种子和检测到的 version数字 : 'v1', (中文(简体) ), 'v2',或 'hybrid'。 。 。 .
import fs from 'fs';
import { info } from '@ctrl/torrent-file';
const torrentInfo = info(fs.readFileSync('myfile'));
console.log({ torrentInfo });
files(file)
返回文件和块信息 :
length是列出文件长度的总和.offset是协议块空间中文件的字节偏移。 BEP 52 将每个非空的 v2 文件对齐到一块边界,因此 v2 偏移可以包含不包含在其中的缺口length。 。 。 .pieces包含 v1/ hybrid torrents 的十六进制 SHA-1 散列, 而 v2- 只包含 torrents 则不包含 .piecesRoot是一个十六进制的 SHA-256 Merkle root 在 v2/hybrid 文件中.pieceLayers将十六进制片根映射到十六进制的数组 SHA-256 散列.
路径使用主机平台的路径分隔符. 对于 v2 种子, 咨询种子 name 准备到BEP 52文件树返回的路径.
import fs from 'fs';
import { files } from '@ctrl/torrent-file';
const torrentFiles = files(fs.readFileSync('myfile'));
console.log({ torrentFiles });
hash(file)
返回 v1 SHA-1 信息散列 。 它只为 v2 种子扔出, 因为种子没有 v1 身份 .
import fs from 'fs';
import { hash } from '@ctrl/torrent-file';
const torrentHash = hash(fs.readFileSync('myfile'));
console.log({ torrentHash });
hashV2(file)
返回完整的32字节 v2 SHA-256信息散列为十六进制. 它只为 v1- torrent.
import fs from 'fs';
import { hashV2 } from '@ctrl/torrent-file';
const torrentHashV2 = hashV2(fs.readFileSync('myfile'));
console.log({ torrentHashV2 });
hashes(file)
返回可用的 v1( SHA-1) 和 v2( SHA-256) 信息散列以及检测到的 torrent 版本 . infoHash 用于 v1 和混合种子; infoHashV2 用于 v2 和混合种子.
import fs from 'fs';
import { hashes } from '@ctrl/torrent-file';
const h = hashes(fs.readFileSync('myfile'));
console.log(h.version); // 'v1', 'v2', or 'hybrid'
console.log(h.infoHash); // SHA-1 (v1/hybrid only)
console.log(h.infoHashV2); // SHA-256 (v2/hybrid only)
信息散列由精确的 Ben 编码计算 info 文件所找到的字典字节, 按要求 最佳环境做法3 财务报告和审定财务报表 BEP 52 最佳环境做法。它们不是从解码/编码往返计算.
低等贝恩码
decode() 返回被编码的字节字符串为 Uint8Array,包括用作字典值的字节字符串。 已解码的词典有一个无效的原型,所以键如 __proto__, (中文(简体) ), constructor,以及 toString 是普通的自有属性,而不是被继承的JavaScript行为. encode() 接受字符串, Uint8Array、安全整数、数组和词典.
解码器拒绝非卡通或不完整的本码,包括无序/重复的字典密钥,领先-零整数,负零,不安全整数,截取值,以及后缀数据. 更高级的解析和散列函数使用相同的严格解码器,并因此投放不正确的输入.
词典键可以是任意字节. 解码词典保留足够的内部信息 encode(decode(data)) 保存二进制键, 如 BEP 52 块根 。 新构造的文字字典密钥为UTF-8编码.
编码
将已解析的种子对象返回到一个 .torrent 文件缓冲器.
import fs from 'fs';
import { toTorrentFile } from '@ctrl/torrent-file';
// Minimal example: create a .torrent buffer from fields
const buf = toTorrentFile({
info: {
// Required info fields
'piece length': 16384,
pieces: new Uint8Array(/* 20-byte SHA1 hashes concatenated */),
name: 'example.txt',
length: 12345,
},
// Optional fields
announce: ['udp://tracker.publicbt.com:80/announce'],
urlList: ['https://example.com/example.txt'],
private: false,
created: new Date(),
createdBy: 'my-app/1.0.0',
comment: 'Generated by @ctrl/torrent-file',
});
fs.writeFileSync('example.torrent', buf);
toTorrentFile() 序列化所提供的元数据;它不读取文件内容,不计算出片段散列,不构建 v2 Merkle 树,也不插入混合编码文件.
用于 v2 pieceLayers,字典密钥是原始的32字节片根而不是十六进制文本. 在手动构建时, 以 JavaScript 字符代表每个字节 :
const rawDictionaryKey = (bytes: Uint8Array) => String.fromCharCode(...bytes);
const buf = toTorrentFile({
info: v2Info,
pieceLayers: {
[rawDictionaryKey(piecesRoot)]: concatenatedLayerHashes,
},
});
这个 pieceLayers 返回时间 files() 是一个方便显示的十六进制表示式,不是被 toTorrentFile()。 。 。 .
审定范围
剖析器验证了canonical bencoding及其所消耗的v1/v2文件-信息结构,包括v2元数据版本,块大小,文件树形状,文件长度,片根,和散列字节长度.
它不重算文件散列,不用密码验证块层与默克勒根相比,也不证明混合种子的v1和v2侧描述相同内容. 应用程序下载或信任内容必须分别进行这些完整性检查.
演示
运行本地演示 UI 以丢出一个 .torrent 文件与视图解析输出 :
pnpm install
pnpm demo:watch
要构建静态托管的演示:
pnpm demo:build