Skip to content

Commit

Permalink
Merge pull request #15 from isolomak/split_pieces
Browse files Browse the repository at this point in the history
split pieces
  • Loading branch information
isolomak authored Jul 6, 2024
2 parents b0848e2 + e328911 commit db21384
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/parse/TorrentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,18 @@ export default class TorrentParser {
*/
private static _pieces(decodedData: BencodeDictionary): Array<Buffer> {
const torrentInfo = TorrentParser._info(decodedData);
const pieceLength = TorrentParser._pieceLength(decodedData);

if (!torrentInfo || !pieceLength || !Buffer.isBuffer(torrentInfo.pieces)) {
if (!torrentInfo || !Buffer.isBuffer(torrentInfo.pieces)) {
return [];
}

// console.log(torrentInfo.pieces.length, torrentInfo.pieces[0], pieceLength);

const pieces: Array<Buffer> = [];

for (let i = 0; i < torrentInfo.pieces.length; i += pieceLength) {
for (let i = 0; i < torrentInfo.pieces.length; i += 20) {
pieces.push(
torrentInfo.pieces.subarray(i, i + pieceLength)
torrentInfo.pieces.subarray(i, i + 20)
);
}

Expand Down
13 changes: 10 additions & 3 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,20 @@ describe('Parse tests', () => {
assert.deepStrictEqual(result.pieces, [ Buffer.from('4d92fb3893cc6ed5fbd0392dda657459b4675605', 'hex') ]);
});

test('should return empty array if no piece length', () => {
test('should split pieces', () => {
const result = parse(bencodec.encode({
info: {
pieces: Buffer.from('4d92fb3893cc6ed5fbd0392dda657459b4675605', 'hex')
'piece length': 32768,
pieces: Buffer.concat([
Buffer.from('4d92fb3893cc6ed5fbd0392dda657459b4675605', 'hex'),
Buffer.from('1234567890abcdef1234567890abcdef12345678', 'hex'),
])
}
}));
assert.deepStrictEqual(result.pieces, []);
assert.deepStrictEqual(result.pieces, [
Buffer.from('4d92fb3893cc6ed5fbd0392dda657459b4675605', 'hex'),
Buffer.from('1234567890abcdef1234567890abcdef12345678', 'hex'),
]);
});

});
Expand Down

0 comments on commit db21384

Please sign in to comment.