https://github.com/scttcper/torrent-file
TypeScript torrent file parser
https://github.com/scttcper/torrent-file
bencode torrent torrent-files
Last synced: 6 months ago
JSON representation
TypeScript torrent file parser
- Host: GitHub
- URL: https://github.com/scttcper/torrent-file
- Owner: scttcper
- License: mit
- Created: 2019-05-18T22:41:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-20T22:42:39.000Z (about 1 year ago)
- Last Synced: 2025-04-14T16:13:42.100Z (9 months ago)
- Topics: bencode, torrent, torrent-files
- Language: TypeScript
- Homepage:
- Size: 785 KB
- Stars: 12
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# torrent-file [](https://www.npmjs.com/package/@ctrl/torrent-file) [](https://codecov.io/gh/scttcper/torrent-file) [](https://bundlephobia.com/result?p=@ctrl/torrent-file)
> Parse a torrent file and read encoded data.
This project is based on [parse-torrent](https://www.npmjs.com/package/parse-torrent) and [node-bencode](https://github.com/themasch/node-bencode) to parse the data of a torrent file. This library implements its own [bencode](http://www.bittorrent.org/beps/bep_0003.html) encoder and decoder that does not use `Buffer`.
### Install
```console
npm install @ctrl/torrent-file
```
### API
##### info
The content of the metainfo file.
```ts
import fs from 'fs';
import { info } from '@ctrl/torrent-file';
const torrentInfo = info(fs.readFileSync('myfile'));
console.log({ torrentInfo });
```
##### files
data about the files described in the torrent file, includes hashes of the pieces
```ts
import fs from 'fs';
import { files } from '@ctrl/torrent-file';
const torrentFiles = files(fs.readFileSync('myfile'));
console.log({ torrentFiles });
```
##### hash
sha1 of torrent file info. This hash is commenly used by torrent clients as the ID of the torrent. It is async and sha1 encoding is handled by [crypto-hash](https://github.com/sindresorhus/crypto-hash)
```ts
import fs from 'fs';
import { hash } from '@ctrl/torrent-file';
(async () => {
const torrentHash = await hash(fs.readFileSync('myfile'));
console.log({ torrentHash });
})()
```
### See Also
[parse-torrent](https://www.npmjs.com/package/parse-torrent) - "@ctrl/torrent-file" torrent parsing based very heavily off this project
[node-bencode](https://github.com/themasch/node-bencode) - bencoder built into this project heavily based off this project