dot-torrent
Advanced tools
Comparing version 1.0.3 to 1.1.0
/// <reference types="node" /> | ||
export default class PieceCollector { | ||
private _buffer; | ||
private _files; | ||
private _pieces; | ||
private _pieceLength; | ||
private _totalLength; | ||
/** | ||
* Constructor | ||
*/ | ||
constructor(); | ||
constructor(files: Array<string>, totalLength: number); | ||
/** | ||
* Calculate piece length | ||
*/ | ||
getPieceLength(): number; | ||
/** | ||
* Read files and collect pieces | ||
*/ | ||
collectFromFiles(files: Array<string>): Promise<void>; | ||
collectFromFiles(): Promise<void>; | ||
/** | ||
@@ -15,0 +20,0 @@ * Get collected pieces |
@@ -28,15 +28,25 @@ "use strict"; | ||
*/ | ||
constructor() { | ||
constructor(files, totalLength) { | ||
this._buffer = Buffer.alloc(0, 'utf-8'); | ||
this._files = files; | ||
this._pieces = []; | ||
this._pieceLength = 32768; // 262144; | ||
this._totalLength = totalLength; | ||
} | ||
/** | ||
* Calculate piece length | ||
*/ | ||
getPieceLength() { | ||
return Math.max(16384, 1 << Math.log2(this._totalLength < 1024 | ||
? 1 | ||
: this._totalLength / 1024) + 0.5 | 0); | ||
} | ||
/** | ||
* Read files and collect pieces | ||
*/ | ||
collectFromFiles(files) { | ||
collectFromFiles() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (let i = 0; i < files.length; i++) { | ||
yield this._readFile(files[i]); | ||
for (let i = 0; i < this._files.length; i++) { | ||
yield this._readFile(this._files[i]); | ||
} | ||
this._flushBuffer(); | ||
}); | ||
@@ -48,3 +58,2 @@ } | ||
getPieces() { | ||
this._flushBuffer(); | ||
return Buffer.concat(this._pieces); | ||
@@ -68,3 +77,3 @@ } | ||
stream.on('error', reject); | ||
stream.on('end', resolve); | ||
stream.on('close', resolve); | ||
}); | ||
@@ -77,3 +86,3 @@ } | ||
this._buffer = Buffer.concat([this._buffer, data]); | ||
while (this._buffer.length > this._pieceLength) { | ||
while (this._buffer.length > this.getPieceLength()) { | ||
this._collectPiece(); | ||
@@ -86,4 +95,4 @@ } | ||
_collectPiece() { | ||
const piece = this._buffer.slice(0, this._pieceLength); | ||
this._buffer = this._buffer.slice(this._pieceLength); | ||
const piece = this._buffer.slice(0, this.getPieceLength()); | ||
this._buffer = this._buffer.slice(this.getPieceLength()); | ||
this._pieces.push(crypto_1.default.createHash('sha1').update(piece).digest()); | ||
@@ -90,0 +99,0 @@ } |
@@ -18,6 +18,2 @@ import { ISourceFileInfo } from '../types'; | ||
/** | ||
* Parse source and collect files info | ||
*/ | ||
parseSource(): void; | ||
/** | ||
* Get files info | ||
@@ -27,2 +23,10 @@ */ | ||
/** | ||
* Get all file locations | ||
*/ | ||
getFilePathList(): string[]; | ||
/** | ||
* Get total of files size | ||
*/ | ||
getTotalFilesSize(): number; | ||
/** | ||
* Get file info | ||
@@ -36,2 +40,6 @@ */ | ||
/** | ||
* Parse source and collect files info | ||
*/ | ||
private _parseSource; | ||
/** | ||
* Find all files in directory and collent info | ||
@@ -38,0 +46,0 @@ */ |
@@ -19,2 +19,3 @@ "use strict"; | ||
this._source = source; | ||
this._parseSource(); | ||
} | ||
@@ -34,15 +35,26 @@ /** | ||
/** | ||
* Parse source and collect files info | ||
* Get files info | ||
*/ | ||
parseSource() { | ||
const stats = fs.lstatSync(this._source); | ||
if (stats.isDirectory()) { | ||
this._collectDirectoryFilesRecursively(this._source); | ||
getFiles() { | ||
return this._files; | ||
} | ||
/** | ||
* Get all file locations | ||
*/ | ||
getFilePathList() { | ||
const filePathList = []; | ||
for (let i = 0; i < this._files.length; i++) { | ||
filePathList.push(this._files[i].fullPath); | ||
} | ||
return filePathList; | ||
} | ||
/** | ||
* Get files info | ||
* Get total of files size | ||
*/ | ||
getFiles() { | ||
return this._files; | ||
getTotalFilesSize() { | ||
let total = 0; | ||
for (let i = 0; i < this._files.length; i++) { | ||
total += this._files[i].length; | ||
} | ||
return total; | ||
} | ||
@@ -60,2 +72,11 @@ /** | ||
/** | ||
* Parse source and collect files info | ||
*/ | ||
_parseSource() { | ||
const stats = fs.lstatSync(this._source); | ||
if (stats.isDirectory()) { | ||
this._collectDirectoryFilesRecursively(this._source); | ||
} | ||
} | ||
/** | ||
* Find all files in directory and collent info | ||
@@ -62,0 +83,0 @@ */ |
@@ -57,2 +57,5 @@ import { ICreateTorrentParams } from '../types'; | ||
private _pieceLength; | ||
/** | ||
* Set 'pieces' field to torrent info | ||
*/ | ||
private _pieces; | ||
@@ -59,0 +62,0 @@ /** |
@@ -33,6 +33,6 @@ "use strict"; | ||
this._parameters = parameters; | ||
this._pieceCollector = new PieceCollector_1.default(); | ||
this._sourceParser = new SourceParser_1.default(this._parameters.source); | ||
this._torrent = {}; | ||
this._torrentInfo = {}; | ||
this._sourceParser = new SourceParser_1.default(this._parameters.source); | ||
this._pieceCollector = new PieceCollector_1.default(this._sourceParser.getFilePathList(), this._sourceParser.getTotalFilesSize()); | ||
} | ||
@@ -45,3 +45,2 @@ /** | ||
// TODO: validate mandatory fields | ||
this._sourceParser.parseSource(); | ||
this._announce(); | ||
@@ -145,12 +144,10 @@ this._announceList(); | ||
_pieceLength() { | ||
return this._torrentInfo['piece length'] = 32768; // 262144; | ||
return this._torrentInfo['piece length'] = this._pieceCollector.getPieceLength(); | ||
} | ||
/** | ||
* Set 'pieces' field to torrent info | ||
*/ | ||
_pieces() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const files = this._sourceParser.getFiles(); | ||
const filePathList = []; | ||
for (let i = 0; i < files.length; i++) { | ||
filePathList.push(files[i].fullPath); | ||
} | ||
yield this._pieceCollector.collectFromFiles(filePathList); | ||
yield this._pieceCollector.collectFromFiles(); | ||
this._torrentInfo.pieces = this._pieceCollector.getPieces(); | ||
@@ -157,0 +154,0 @@ }); |
{ | ||
"name": "dot-torrent", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Parse and create torrent files.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
30999
949