nt 0.7.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -4
- package/dist/hasher.d.ts +193 -0
- package/dist/hasher.js +827 -0
- package/dist/hasher.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/make.d.ts +29 -0
- package/dist/make.js +297 -0
- package/dist/make.js.map +1 -0
- package/dist/queue.d.ts +18 -0
- package/dist/queue.js +65 -0
- package/dist/queue.js.map +1 -0
- package/dist/read.d.ts +8 -0
- package/dist/read.js +73 -0
- package/dist/read.js.map +1 -0
- package/dist/schema.d.ts +18 -0
- package/dist/schema.js +280 -0
- package/dist/schema.js.map +1 -0
- package/dist/torrent.d.ts +48 -0
- package/dist/torrent.js +117 -0
- package/dist/torrent.js.map +1 -0
- package/dist/util.d.ts +16 -0
- package/dist/util.js +44 -0
- package/dist/util.js.map +1 -0
- package/package.json +18 -12
- package/lib/hasher.js +0 -428
- package/lib/index.d.ts +0 -211
- package/lib/index.js +0 -3
- package/lib/make.js +0 -226
- package/lib/queue.js +0 -58
- package/lib/read.js +0 -46
- package/lib/schema.js +0 -239
- package/lib/torrent.js +0 -103
- package/lib/util.js +0 -45
package/lib/hasher.js
DELETED
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const crypto = require('crypto');
|
|
4
|
-
const Stream = require('stream').Stream;
|
|
5
|
-
const parallel = require('async').parallel;
|
|
6
|
-
const StreamSpeed = require('streamspeed');
|
|
7
|
-
|
|
8
|
-
const queue = require('./queue');
|
|
9
|
-
const util = require('./util');
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* defaults
|
|
14
|
-
*/
|
|
15
|
-
const PIECE_LENGTH = 262144; // 256kb
|
|
16
|
-
const MAX_FILES_OPENED = 250;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Hashes a buffer.
|
|
21
|
-
*
|
|
22
|
-
* @param {Buffer} buffer
|
|
23
|
-
* @return {Buffer}
|
|
24
|
-
*/
|
|
25
|
-
const sha1 = (buffer) => {
|
|
26
|
-
return crypto.createHash('sha1').update(buffer).digest();
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Rounds a number to given decimal place.
|
|
32
|
-
*
|
|
33
|
-
* @param {number} num
|
|
34
|
-
* @param {number} dec
|
|
35
|
-
* @return {number}
|
|
36
|
-
*/
|
|
37
|
-
const round = (num, dec) => {
|
|
38
|
-
const pow = Math.pow(10, dec);
|
|
39
|
-
return Math.round(num * pow) / pow;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Allows arrays of objects with a `path` property to be
|
|
45
|
-
* sorted alphabetically
|
|
46
|
-
*
|
|
47
|
-
* @param {Array.<Object>} a
|
|
48
|
-
* @param {Array.<Object>} b
|
|
49
|
-
* @return {string}
|
|
50
|
-
*/
|
|
51
|
-
const sortByPath = (a, b) => {
|
|
52
|
-
const len = Math.min(a.path.length, b.path.length);
|
|
53
|
-
for (let i = 0; i < len; i++) {
|
|
54
|
-
if (a.path[i] < b.path[i]) return -1;
|
|
55
|
-
if (a.path[i] > b.path[i]) return 1;
|
|
56
|
-
}
|
|
57
|
-
return a.path.length - b.path.length;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
module.exports = class Hasher extends Stream {
|
|
62
|
-
/**
|
|
63
|
-
* Calculates piece hashes of file list.
|
|
64
|
-
*
|
|
65
|
-
* @constructor
|
|
66
|
-
* @extends Stream
|
|
67
|
-
* @param {string} dir Directory where files are.
|
|
68
|
-
* @param {Array.<string>} list Array of files to hash.
|
|
69
|
-
* @param {number} pieceLength How many bytes long each piece will be.
|
|
70
|
-
* @param {Object} options
|
|
71
|
-
* @param {number} options.maxFiles Max number of files to open during hashing.
|
|
72
|
-
* @param {boolean} optons.stopOnErr Hashing stops if encounters an error.
|
|
73
|
-
*/
|
|
74
|
-
constructor(dir, list, pieceLength, options) {
|
|
75
|
-
super();
|
|
76
|
-
options = options || {};
|
|
77
|
-
|
|
78
|
-
// Set defaults.
|
|
79
|
-
this.pieceLength = pieceLength || PIECE_LENGTH;
|
|
80
|
-
this.maxFiles = options.maxFiles || MAX_FILES_OPENED;
|
|
81
|
-
this.stopOnErr = options.stopOnErr || false;
|
|
82
|
-
|
|
83
|
-
// Init arrays that will be used every time during hashing.
|
|
84
|
-
this.dir = dir;
|
|
85
|
-
this.files = [];
|
|
86
|
-
this.pieces = [];
|
|
87
|
-
this.ready = false;
|
|
88
|
-
this.hashing = false;
|
|
89
|
-
this.paused = false;
|
|
90
|
-
this.stopped = false;
|
|
91
|
-
|
|
92
|
-
if (this.stopOnErr) {
|
|
93
|
-
this.once('error', (err) => {
|
|
94
|
-
this.err = err;
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
fs.access(dir, (err) => {
|
|
99
|
-
if (err) {
|
|
100
|
-
return this.emit('error', err);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const fromDisk = typeof list[0] === 'string';
|
|
104
|
-
|
|
105
|
-
const readTasks = (err) => {
|
|
106
|
-
// Create shallow copy of the files array.
|
|
107
|
-
this.files = Array.from(this.files);
|
|
108
|
-
|
|
109
|
-
// When creating a torrent from files on disk, sort them alphabetically.
|
|
110
|
-
// File lists from an existing torrent must keep their original order,
|
|
111
|
-
// since the piece hashes depend on it, so they are not sorted.
|
|
112
|
-
if (fromDisk) {
|
|
113
|
-
this.files.sort(sortByPath);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (err) return this.emit('error', err);
|
|
117
|
-
|
|
118
|
-
// If files tree traversed with no errors,
|
|
119
|
-
// add read tasks to filetasks.
|
|
120
|
-
this._CreateFileTasks();
|
|
121
|
-
|
|
122
|
-
// Ready to start hashing.
|
|
123
|
-
this.ready = true;
|
|
124
|
-
this.emit('ready');
|
|
125
|
-
this.ready = true;
|
|
126
|
-
this._start();
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
// Check if list containts objects or strings.
|
|
130
|
-
if (fromDisk) {
|
|
131
|
-
|
|
132
|
-
// Create queue to handle having many files opened.
|
|
133
|
-
const statqueue = queue(fs.stat, this.maxFiles);
|
|
134
|
-
this._getFilesStats(statqueue, list, readTasks);
|
|
135
|
-
|
|
136
|
-
// In the case of objects, a file list from a torrent file was given.
|
|
137
|
-
} else {
|
|
138
|
-
this.files = list;
|
|
139
|
-
process.nextTick(readTasks);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Create filetasks array
|
|
147
|
-
*/
|
|
148
|
-
_CreateFileTasks() {
|
|
149
|
-
// Variables to help calculate piece position.
|
|
150
|
-
let totalSize, piece, offset;
|
|
151
|
-
totalSize = piece = offset = 0;
|
|
152
|
-
|
|
153
|
-
// Create tasks to figure out what pieces belong to exactly
|
|
154
|
-
// what files and what part of the files.
|
|
155
|
-
this.filetasks = [];
|
|
156
|
-
this.files.forEach((task) => {
|
|
157
|
-
|
|
158
|
-
this.filetasks.push({
|
|
159
|
-
length : task.length,
|
|
160
|
-
path : task.path,
|
|
161
|
-
// Piece this task will read from or write to.
|
|
162
|
-
piece,
|
|
163
|
-
// Piece buffer offset to start reading or writing at.
|
|
164
|
-
offset,
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// Add file length to total size.
|
|
168
|
-
totalSize += task.length;
|
|
169
|
-
|
|
170
|
-
// Get piece next task will write to and its offset.
|
|
171
|
-
piece = Math.floor(totalSize / this.pieceLength);
|
|
172
|
-
offset = totalSize - (this.pieceLength * piece);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
this.totalSize = totalSize;
|
|
176
|
-
this.pieces = piece + 1;
|
|
177
|
-
// Take note of the length of the last piece.
|
|
178
|
-
// The rest of the pieces will be of `this.pieceLength` length.
|
|
179
|
-
this.lastPieceLength = totalSize - (this.pieceLength * piece);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Starts the actual hashing process
|
|
185
|
-
*/
|
|
186
|
-
_start() {
|
|
187
|
-
if (!this.ready) {
|
|
188
|
-
return this.emit('error', new Error('Cannot start before being ready'));
|
|
189
|
-
}
|
|
190
|
-
this.hashing = true;
|
|
191
|
-
this.stopped = false;
|
|
192
|
-
|
|
193
|
-
// Create piece objects.
|
|
194
|
-
const pieces = [];
|
|
195
|
-
for (let i = 0; i < this.pieces; i++) {
|
|
196
|
-
pieces[i] = {
|
|
197
|
-
buffer: null,
|
|
198
|
-
length: this.pieceLength,
|
|
199
|
-
bytesWritten: 0,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
pieces[pieces.length - 1].length = this.lastPieceLength;
|
|
203
|
-
|
|
204
|
-
// Keep track of progress.
|
|
205
|
-
let totalRead = 0;
|
|
206
|
-
|
|
207
|
-
// Keep track of reading speed.
|
|
208
|
-
this.streamGroup = new StreamSpeed();
|
|
209
|
-
let lastPercent = 0;
|
|
210
|
-
let speed = 0;
|
|
211
|
-
let avg = 0;
|
|
212
|
-
|
|
213
|
-
this.streamGroup.on('speed', (s, a) => {
|
|
214
|
-
speed = s;
|
|
215
|
-
avg = a;
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
const filesqueue = queue((task, callback) => {
|
|
219
|
-
if (this.err || this.stopped) return callback();
|
|
220
|
-
|
|
221
|
-
// Keep track how much has been read.
|
|
222
|
-
let readSoFar = 0;
|
|
223
|
-
let pieceIndex = task.piece;
|
|
224
|
-
|
|
225
|
-
// Open file for reading.
|
|
226
|
-
const baseDir = path.resolve(this.dir);
|
|
227
|
-
const file = path.resolve(baseDir, ...task.path);
|
|
228
|
-
const relative = path.relative(baseDir, file);
|
|
229
|
-
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
230
|
-
this.emit('error', new Error(`Path traversal detected: ${file}`));
|
|
231
|
-
return callback();
|
|
232
|
-
}
|
|
233
|
-
const rs = fs.createReadStream(file);
|
|
234
|
-
this.streamGroup.add(rs);
|
|
235
|
-
|
|
236
|
-
rs.on('open', () => {
|
|
237
|
-
this.emit('open', file);
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
rs.on('data', (data) => {
|
|
241
|
-
// Recalculate the index of current piece based on `readSoFar`.
|
|
242
|
-
pieceIndex = task.piece +
|
|
243
|
-
Math.floor((task.offset + readSoFar) / this.pieceLength);
|
|
244
|
-
|
|
245
|
-
// Write data to piece buffer.
|
|
246
|
-
const targetStart = (task.offset + readSoFar) % this.pieceLength;
|
|
247
|
-
let sourceWritten = this._writePiece(
|
|
248
|
-
pieces, pieceIndex, targetStart,
|
|
249
|
-
data, 0, data.length, file, readSoFar);
|
|
250
|
-
|
|
251
|
-
while (sourceWritten < data.length) {
|
|
252
|
-
pieceIndex += 1;
|
|
253
|
-
sourceWritten += this._writePiece(
|
|
254
|
-
pieces, pieceIndex, 0,
|
|
255
|
-
data, sourceWritten, data.length, file, readSoFar);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// Update amount of bytes written to buffer.
|
|
259
|
-
readSoFar += data.length;
|
|
260
|
-
totalRead += data.length;
|
|
261
|
-
|
|
262
|
-
// Emit hash event with piece info and progress.
|
|
263
|
-
let percent =
|
|
264
|
-
round(totalRead / this.totalSize * 100, 2);
|
|
265
|
-
if (lastPercent !== percent) {
|
|
266
|
-
this.emit('progress', percent, speed, avg);
|
|
267
|
-
lastPercent = percent;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
// Close this file when there is an error or finished reading.
|
|
273
|
-
rs.on('error', (err) => {
|
|
274
|
-
this.emit('error', err);
|
|
275
|
-
callback();
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
rs.on('end', () => {
|
|
279
|
-
callback();
|
|
280
|
-
});
|
|
281
|
-
}, this.maxFiles);
|
|
282
|
-
|
|
283
|
-
// When all file queues are finished, join all the pieces.
|
|
284
|
-
filesqueue.drain = () => {
|
|
285
|
-
this.hashing = false;
|
|
286
|
-
this.emit('end');
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
// Start queueing jobs.
|
|
290
|
-
this.filetasks.forEach((filetask) => {
|
|
291
|
-
filesqueue.push(filetask);
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
_writePiece(pieces, index, offset,
|
|
297
|
-
data, start, end,
|
|
298
|
-
file, readSoFar) {
|
|
299
|
-
const piece = pieces[index];
|
|
300
|
-
if (!piece) return;
|
|
301
|
-
if (piece.buffer === null) {
|
|
302
|
-
piece.buffer = Buffer.alloc(piece.length);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// Write data to piece buffer and update bytes written.
|
|
306
|
-
const size = Math.min(end - start, piece.length - offset);
|
|
307
|
-
piece.bytesWritten += size;
|
|
308
|
-
data.copy(piece.buffer, offset, start, start + size);
|
|
309
|
-
|
|
310
|
-
// Check if piece buffer is full.
|
|
311
|
-
if (piece.bytesWritten === piece.length) {
|
|
312
|
-
pieces[index] = null;
|
|
313
|
-
const buf = Buffer.from(sha1(piece.buffer), 'binary');
|
|
314
|
-
this.emit('hash', index, buf, file, readSoFar, size);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
return size;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Recursively traverses files to call fs.stat on them.
|
|
323
|
-
* Will add all files in folders if any are encountered.
|
|
324
|
-
*
|
|
325
|
-
* @param {boolean} queue
|
|
326
|
-
* @param {Array.<string>} list List of files and/or directories.
|
|
327
|
-
* @param {Function(!Error)} callback
|
|
328
|
-
*/
|
|
329
|
-
_getFilesStats(queue, list, callback) {
|
|
330
|
-
// Read all file stats in parallel.
|
|
331
|
-
parallel(list.map((file) => {
|
|
332
|
-
return this._getFileStat.bind(this, queue, file);
|
|
333
|
-
}), callback);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Stat is needed to know file size.
|
|
339
|
-
*
|
|
340
|
-
* @param {boolean} queue
|
|
341
|
-
* @param {Array.<string>} list List of files and/or directories.
|
|
342
|
-
* @param {Function(!Error)} callback
|
|
343
|
-
*/
|
|
344
|
-
_getFileStat(queue, file, callback) {
|
|
345
|
-
const filepath = path.join(this.dir, file);
|
|
346
|
-
|
|
347
|
-
queue.push(filepath, (err, stat) => {
|
|
348
|
-
if (err) return callback(err);
|
|
349
|
-
|
|
350
|
-
// If this is a directory, add all files in it to torrent files.
|
|
351
|
-
if (stat.isDirectory()) {
|
|
352
|
-
fs.readdir(filepath, (err, dirfiles) => {
|
|
353
|
-
if (err) return callback(err);
|
|
354
|
-
|
|
355
|
-
dirfiles = dirfiles.map(dirfile => path.join(file, dirfile));
|
|
356
|
-
|
|
357
|
-
// Call _getFilesStats again.
|
|
358
|
-
this._getFilesStats(queue, dirfiles, callback);
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
} else if (stat.isFile()) {
|
|
362
|
-
|
|
363
|
-
this.files.push({
|
|
364
|
-
length: stat.size,
|
|
365
|
-
path: util.splitPath(file),
|
|
366
|
-
});
|
|
367
|
-
callback(null);
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Allows pausing of the hashing process by storing functions from the
|
|
375
|
-
* file and read queues that will not get called back until resume()
|
|
376
|
-
* is called.
|
|
377
|
-
*/
|
|
378
|
-
pause() {
|
|
379
|
-
if (this.paused || !this.hashing) return false;
|
|
380
|
-
this.paused = true;
|
|
381
|
-
|
|
382
|
-
this.streamGroup.getStreams().forEach((rs) => {
|
|
383
|
-
rs.pause();
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
return true;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Resumes hashing.
|
|
392
|
-
*/
|
|
393
|
-
resume() {
|
|
394
|
-
if (!this.paused) return false;
|
|
395
|
-
this.paused = false;
|
|
396
|
-
|
|
397
|
-
this.streamGroup.getStreams().forEach((rs) => {
|
|
398
|
-
rs.resume();
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
return true;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Continues hashing if paused or pauses if not.
|
|
407
|
-
*/
|
|
408
|
-
toggle() {
|
|
409
|
-
return this.paused ? this.resume() : this.pause();
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Stops hashing completely.
|
|
415
|
-
* Closes file descriptors and does not emit any more events.
|
|
416
|
-
*/
|
|
417
|
-
destroy() {
|
|
418
|
-
if (!this.hashing) return false;
|
|
419
|
-
this.stopped = true;
|
|
420
|
-
this.paused = false;
|
|
421
|
-
|
|
422
|
-
this.streamGroup.getStreams().forEach((rs) => {
|
|
423
|
-
rs.destroy();
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
return true;
|
|
427
|
-
}
|
|
428
|
-
};
|
package/lib/index.d.ts
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
declare module 'node-torrent' {
|
|
2
|
-
import { Stream } from 'stream';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A Hasher object is returned when a torrent is created with `make` and when `Torrent#hashCheck` is called. It inherits from ReadableStream.
|
|
6
|
-
*/
|
|
7
|
-
class Hasher extends Stream {
|
|
8
|
-
/**
|
|
9
|
-
* Pause hash checking.
|
|
10
|
-
*/
|
|
11
|
-
pause(): boolean;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Resumes hash checking.
|
|
15
|
-
*/
|
|
16
|
-
resume(): boolean;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Continues hashing if paused or pauses if not.
|
|
20
|
-
*/
|
|
21
|
-
toggle(): boolean;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Stops hashing completely. Closes file descriptors and does not emit any more events.
|
|
25
|
-
*/
|
|
26
|
-
destroy(): boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The `read` and `make` functions all call their callback with a Torrent object.
|
|
31
|
-
*/
|
|
32
|
-
class Torrent {
|
|
33
|
-
/**
|
|
34
|
-
* Contains metadata of the torrent. Example:
|
|
35
|
-
*
|
|
36
|
-
* ```js
|
|
37
|
-
* {
|
|
38
|
-
* announce: 'udp://tracker.publicbt.com:80',
|
|
39
|
-
* 'announce-list': [
|
|
40
|
-
* [ 'udp://tracker.publicbt.com:80' ],
|
|
41
|
-
* [ 'udp://tracker.ccc.de:80' ],
|
|
42
|
-
* [ 'udp://tracker.openbittorrent.com:80' ],
|
|
43
|
-
* [ 'http://tracker.thepiratebay.org/announce' ]
|
|
44
|
-
* ],
|
|
45
|
-
* comment: 'Torrent downloaded from http://thepiratebay.org',
|
|
46
|
-
* 'creation date': 1303979726,
|
|
47
|
-
* info: { length: 718583808,
|
|
48
|
-
* name: 'ubuntu-11.04-desktop-i386.iso',
|
|
49
|
-
* 'piece length': 524288,
|
|
50
|
-
* pieces: <Buffer e5 7a ...>
|
|
51
|
-
* }
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
metadata: {
|
|
56
|
-
announce: string;
|
|
57
|
-
'announce-list'?: string[][]
|
|
58
|
-
'creation-date'?: string;
|
|
59
|
-
comment?: string;
|
|
60
|
-
'created-by'?: string;
|
|
61
|
-
encoding?: string;
|
|
62
|
-
info: MultiFileInfo | SingleFileInfo;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Get a torrent's info hash.
|
|
67
|
-
*/
|
|
68
|
-
infoHash(): string;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Creates a ReadableStream that emits raw bencoded data for this torrent. Returns the readable stream.
|
|
72
|
-
*/
|
|
73
|
-
createReadStream(): ReadableStream;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Shortcut that pipes the stream from `Torrent#createReadStream()` to a writable file stream. Returns the writable stream.
|
|
77
|
-
*/
|
|
78
|
-
createWriteStream(
|
|
79
|
-
path: string,
|
|
80
|
-
options?: string | Partial<{
|
|
81
|
-
flags: string;
|
|
82
|
-
encoding: BufferEncoding;
|
|
83
|
-
fd: number;
|
|
84
|
-
mode: number;
|
|
85
|
-
autoClose: boolean;
|
|
86
|
-
emitClose: boolean;
|
|
87
|
-
start: number;
|
|
88
|
-
highWaterMark: number;
|
|
89
|
-
}>
|
|
90
|
-
): WritableStream;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Hash checks torrent against files in `dir`.
|
|
94
|
-
*
|
|
95
|
-
* @param options Hash can have `maxFiles` to open during hashing. Defaults to `250`.
|
|
96
|
-
*/
|
|
97
|
-
hashCheck(
|
|
98
|
-
dir: string,
|
|
99
|
-
options?: Partial<{
|
|
100
|
-
maxFiles: number;
|
|
101
|
-
}>
|
|
102
|
-
): Hasher;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
type MetadataInfo = {
|
|
106
|
-
name?: string;
|
|
107
|
-
'piece-length': number;
|
|
108
|
-
pieces: string;
|
|
109
|
-
private?: 0 | 1;
|
|
110
|
-
source?: string;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
type SingleFileInfo = MetadataInfo & {
|
|
114
|
-
length: number;
|
|
115
|
-
md5sum?: string;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
type MultiFileInfo = MetadataInfo & {
|
|
119
|
-
files: {
|
|
120
|
-
length: number;
|
|
121
|
-
path: string;
|
|
122
|
-
md5sum?: string;
|
|
123
|
-
}[];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
type MakeOptions = {
|
|
127
|
-
/**
|
|
128
|
-
* An array of arrays of additional announce URLs.
|
|
129
|
-
*/
|
|
130
|
-
announceList: string[][];
|
|
131
|
-
|
|
132
|
-
comment: string;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Can be used only in multi file mode. If not given, defaults to name of directory.
|
|
136
|
-
*/
|
|
137
|
-
name: string;
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* How to break up the pieces. Must be an integer `n` that says piece length will be `2^n`. Default is 256KB, or 2^18.
|
|
141
|
-
*/
|
|
142
|
-
pieceLength: number;
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Set true if this is a private torrent.
|
|
146
|
-
*/
|
|
147
|
-
private: boolean;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* These go into the `info` dictionary of the torrent. Useful if you want to make a torrent have a unique info hash from a certain tracker.
|
|
151
|
-
*/
|
|
152
|
-
moreInfo: object;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Max files to open during piece hashing. Defaults to 250.
|
|
156
|
-
*/
|
|
157
|
-
maxFiles: number;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
namespace NodeTorrent {
|
|
161
|
-
/**
|
|
162
|
-
* Reads a local file, or a readable stream. Returns readable stream.
|
|
163
|
-
*
|
|
164
|
-
* An error can be returned if the torrent is formatted incorrectly.
|
|
165
|
-
* Does not check if the dictonaries are listed alphabetically.
|
|
166
|
-
* Refer to the [BitTorrent Specification](https://wiki.theory.org/BitTorrentSpecification) for more info on torrent metainfo.
|
|
167
|
-
*
|
|
168
|
-
* @param file Where the torrent resides, can be local file, or a readable stream.
|
|
169
|
-
* @param validate Validate or not schema.
|
|
170
|
-
* @param callback Called with a possible `Error`, and a `Torrent` object when hashing is finished.
|
|
171
|
-
*/
|
|
172
|
-
export function read(
|
|
173
|
-
file: string | ReadableStream,
|
|
174
|
-
validate?: boolean,
|
|
175
|
-
callback?: (err: Error, torrent: Torrent) => void
|
|
176
|
-
): ReadableStream;
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Makes a new torrent. `dir` is root directory of the torrent. The `files` array will relatively read files from there.
|
|
180
|
-
* If files is omitted, it implicitly adds all of the files in `dir` to the torrent, including those in subdirectories.
|
|
181
|
-
*
|
|
182
|
-
* @param announce The announce URL.
|
|
183
|
-
* @param dir Directory where the files in the files array are.
|
|
184
|
-
* @param files Optional.
|
|
185
|
-
* @param options Any options for the torrent go here.
|
|
186
|
-
* @return A Hasher object that emits raw bencoded `data` events.
|
|
187
|
-
*/
|
|
188
|
-
export function make(
|
|
189
|
-
announce: string,
|
|
190
|
-
dir: string,
|
|
191
|
-
files?: string[],
|
|
192
|
-
options?: Partial<MakeOptions>,
|
|
193
|
-
callback?: (err: Error, torrent: Torrent) => void
|
|
194
|
-
): Hasher;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* A shortcut that pumps the returned readable stream from `make` into a writable stream that points to the file `output`.
|
|
198
|
-
* @return A Hasher object.
|
|
199
|
-
*/
|
|
200
|
-
export function makeWrite(
|
|
201
|
-
output: string,
|
|
202
|
-
announce: string,
|
|
203
|
-
dir: string,
|
|
204
|
-
files?: string[],
|
|
205
|
-
options?: Partial<MakeOptions>,
|
|
206
|
-
callback?: (err: Error, torrent: Torrent) => void
|
|
207
|
-
): Hasher;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export = NodeTorrent;
|
|
211
|
-
}
|
package/lib/index.js
DELETED