Skip to content

Commit

Permalink
Merge pull request #14 from isolomak/handle_nested_files
Browse files Browse the repository at this point in the history
Handle nested files
  • Loading branch information
isolomak authored Jul 6, 2024
2 parents 5f1a59f + 5fde47d commit b0848e2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dot-torrent",
"version": "2.1.0",
"version": "2.1.1",
"description": "Parse and create torrent files",
"main": "./lib/index.js",
"scripts": {
Expand Down Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://github.com/isolomak/dot-torrent#readme",
"dependencies": {
"bencodec": "^2.4.3"
"bencodec": "^3.0.1"
},
"devDependencies": {
"@types/jest": "^29.2.3",
Expand Down
17 changes: 12 additions & 5 deletions src/parse/TorrentParser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import bencodec, { BencodeDictionary, BencodeTypes } from 'bencodec';
import crypto from 'crypto';
import { IDotTorrent, IDotTorrentFileInfo } from '../types';
import { join } from 'path';

export default class TorrentParser {

/**
* Parse torrent file
*/
public parse(data: Buffer | string): IDotTorrent {
const decodedData = bencodec.decode(data) as BencodeDictionary;;
const decodedData = bencodec.decode<BencodeDictionary>(data);

if (Array.isArray(decodedData) || Buffer.isBuffer(decodedData) || typeof decodedData !== 'object') {
throw new Error('TorrentParser failed to parse torrent. Invalid data');
Expand Down Expand Up @@ -137,11 +138,17 @@ export default class TorrentParser {
continue ;
}

const filePath = TorrentParser._getStringFromBencode(fileInfo.path[0]);

if (!filePath) {
if (!fileInfo.path.length) {
continue ;
}

const pathItems = fileInfo.path.map((item: BencodeTypes) => TorrentParser._getStringFromBencode(item));

if (pathItems.some((item: string) => !item)) {
continue ;
}

const filePath = join(...pathItems as Array<string>);

fileMap.set(filePath, fileInfo.length);
}
Expand Down Expand Up @@ -263,7 +270,7 @@ export default class TorrentParser {
return null;
}

return decodedData.info;
return decodedData.info as BencodeDictionary | null;
}

private static _getStringFromBencode(data: BencodeTypes): string | null {
Expand Down
37 changes: 37 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert';
import bencodec from 'bencodec';
import crypto from 'crypto';
import { parse } from '../src';
import { join } from 'path';

describe('Parse tests', () => {

Expand Down Expand Up @@ -159,6 +160,10 @@ describe('Parse tests', () => {
{
path: [ 100500 ],
length: 100500,
},
{
path: [ ],
length: 100500,
}
]
}
Expand Down Expand Up @@ -193,6 +198,38 @@ describe('Parse tests', () => {
assert.deepStrictEqual(result.files.length, 0);
});

test('should parse nested files', () => {
const result = parse(bencodec.encode({
info: {
files: [
{
length: 100500,
path: [
'folder',
'nested_folder',
'test_file_1.txt'
]
},
{
length: 500100,
path: [
'folder',
'other_test_file.txt'
]
},
]
}
}));

assert.deepStrictEqual(result.files.length, 2);

assert.deepStrictEqual(result.files[0].length, 100500);
assert.deepStrictEqual(result.files[1].length, 500100);

assert.deepStrictEqual(result.files[0].path, join('folder', 'nested_folder', 'test_file_1.txt'));
assert.deepStrictEqual(result.files[1].path, join('folder', 'other_test_file.txt'));
});

});

describe('InfoHash field tests', () => {
Expand Down

0 comments on commit b0848e2

Please sign in to comment.