@ctrl/torrent-file

中文文档
4.5.2发布于 2026/10/05MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 8678

分析种子文件( 名称、 散列、 文件、 部件)

中文使用文档
文档目录12 节
  1. 安装
  2. API 密码
  3. info(file)
  4. files(file)
  5. hash(file)
  6. hashV2(file)
  7. hashes(file)
  8. 低等贝恩码
  9. 编码
  10. 审定范围
  11. 演示
  12. 规格和相关项目

种子文件 npm

分析、检查和编码 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

版本与发布记录

@ctrl/torrent-file 共发布 24 个版本,首次发布于 2019/05/19;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.5.2latest2026/10/051 个月前
4.5.12026/07/133 个月前
4.5.02026/02/178 个月前
4.4.02025/10/061.1 年前
4.3.02025/10/061.1 年前
4.2.02025/09/161.1 年前

关于 @ctrl/torrent-file 的常见问题

@ctrl/torrent-file 是什么?

分析种子文件( 名称、 散列、 文件、 部件)

如何安装 @ctrl/torrent-file?

在项目目录运行 npm install @ctrl/torrent-file,或使用 pnpm add / yarn add / bun add。

@ctrl/torrent-file 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ctrl/torrent-file 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ctrl/torrent-file 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ctrl/torrent-file 可以商用吗?

@ctrl/torrent-file 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ctrl/torrent-file 的下载量有多少?

根据 npm 官方下载统计,@ctrl/torrent-file 在 2026/10/11 – 2026/11/09 这 30 天内被下载 8,678 次,日均约 289 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ctrl/torrent-file 还在维护吗?

最新版本 4.5.2 发布于 2026/10/05(1 个月前);至今共发布 24 个版本,其中过去 12 个月发布了 3 个;首次发布于 2019/05/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。