-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrentParser.js
55 lines (44 loc) · 1.81 KB
/
torrentParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
import fs from 'fs';
import bencode from 'bencode';
import * as big from 'bigint-buffer';
// open torrent file and decode contents
export function open(filePath) {
console.log(`Opening the torrent file...`);
return bencode.decode(fs.readFileSync(filePath));
}
// compute total size
export function size(torrent) {
const size = torrent.info.files
? torrent.info.files
.map((file) => file.length)
.reduce((acc, curr) => acc + curr, 0)
: torrent.info.length;
// file size may be greater than 32-bit integer
return big.toBufferBE(BigInt(size), 8);
}
export function infoHash(torrent) {
const info = bencode.encode(torrent.info);
return crypto.createHash('sha1').update(info).digest();
}
// 16 KB block size
export const BLOCK_LENGTH = Math.pow(2, 14);
export function pieceLen(torrent, pieceIndex) {
const totalLength = Number(big.toBigIntBE(size(torrent)));
const pieceLength = torrent.info['piece length'];
console.log(`length info - ${totalLength, pieceLength}`);
const lastPieceLength = totalLength % pieceLength;
const lastPieceIndex = Math.floor(totalLength/pieceLength);
console.log(`last piece info - ${lastPieceIndex, lastPieceLength}`);
return lastPieceIndex === pieceIndex ? lastPieceLength : pieceLength;
}
export function blocksPerPiece(torrent, pieceIndex) {
const pieceLength = pieceLen(torrent, pieceIndex);
return Math.ceil(pieceLength / BLOCK_LENGTH); // partial block should also count as full block.
}
export function blockLen(torrent, pieceIndex, blockIndex) {
const pieceLength = pieceLen(torrent, pieceIndex);
const lastBlockLength = pieceLength % BLOCK_LENGTH;
const lastBlockIndex = Math.floor(pieceLength / BLOCK_LENGTH);
return blockIndex === lastBlockIndex ? lastBlockLength : BLOCK_LENGTH;
}