nt 0.7.0 → 0.7.2
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 +24 -24
- package/lib/hasher.js +71 -58
- package/lib/index.d.ts +211 -0
- package/lib/make.js +42 -42
- package/lib/queue.js +20 -21
- package/lib/read.js +6 -6
- package/lib/schema.js +27 -31
- package/lib/torrent.js +14 -14
- package/lib/util.js +8 -8
- package/package.json +10 -12
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Read, make, and hash check torrents with node.js!
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/nt)
|
|
6
|
+
[](https://github.com/fent/node-torrent/actions/workflows/nodejs.yml)
|
|
7
7
|
[](https://codecov.io/gh/fent/node-torrent)
|
|
8
8
|
|
|
9
9
|
# Usage
|
|
@@ -22,7 +22,7 @@ nt.read('path/to/file.torrent', (err, torrent) => {
|
|
|
22
22
|
## Make a torrent
|
|
23
23
|
|
|
24
24
|
```javascript
|
|
25
|
-
|
|
25
|
+
let rs = nt.make('http://myannounce.net/url', __dirname + '/files');
|
|
26
26
|
rs.pipe(fs.createWriteStream('mytorrent.torrent'));
|
|
27
27
|
|
|
28
28
|
// callback style
|
|
@@ -37,9 +37,9 @@ nt.makeWrite('outputfile', 'http://announce.me', __dirname + '/files',
|
|
|
37
37
|
## Hash check a torrent
|
|
38
38
|
|
|
39
39
|
```js
|
|
40
|
-
|
|
40
|
+
let hasher = torrent.hashCheck(file);
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
let p;
|
|
43
43
|
hasher.on('match', (i, hash, percent) => {
|
|
44
44
|
p = percent;
|
|
45
45
|
});
|
|
@@ -153,36 +153,36 @@ Finished examining files to be hashed and ready to start hashing their contents.
|
|
|
153
153
|
Emits raw bencoded torrent data only when hasher is returned from the `make` function.
|
|
154
154
|
|
|
155
155
|
### 'progress'
|
|
156
|
-
* `
|
|
157
|
-
* `
|
|
158
|
-
* `
|
|
156
|
+
* `number` - percent
|
|
157
|
+
* `number` - speed
|
|
158
|
+
* `number` - avgSpeed
|
|
159
159
|
|
|
160
160
|
Emits the progress calculated by amount of bytes read from files. `speed` and `avgSpeed` are in bytes.
|
|
161
161
|
|
|
162
162
|
### 'hash'
|
|
163
|
-
* `
|
|
164
|
-
* `
|
|
165
|
-
* `
|
|
166
|
-
* `
|
|
167
|
-
* `
|
|
163
|
+
* `number` - index
|
|
164
|
+
* `string` - hash
|
|
165
|
+
* `string` - file
|
|
166
|
+
* `number` - position
|
|
167
|
+
* `number` - length
|
|
168
168
|
|
|
169
169
|
Emitted when a piece is hashed along with hash position and source.
|
|
170
170
|
|
|
171
171
|
### 'match'
|
|
172
|
-
* `
|
|
173
|
-
* `
|
|
174
|
-
* `
|
|
175
|
-
* `
|
|
176
|
-
* `
|
|
177
|
-
* `
|
|
172
|
+
* `number` - index
|
|
173
|
+
* `string` - hash
|
|
174
|
+
* `number` - percentMatched
|
|
175
|
+
* `string` - file
|
|
176
|
+
* `number` - position
|
|
177
|
+
* `number` - length
|
|
178
178
|
|
|
179
179
|
Emitted when a piece matches with its `index`, the piece, and the percentage of pieces matched so far.
|
|
180
180
|
|
|
181
181
|
### 'matcherror'
|
|
182
|
-
* `
|
|
183
|
-
* `
|
|
184
|
-
* `
|
|
185
|
-
* `
|
|
182
|
+
* `number` - index
|
|
183
|
+
* `string` - file
|
|
184
|
+
* `number` - position
|
|
185
|
+
* `number` - length
|
|
186
186
|
|
|
187
187
|
Emitted when a piece does not match.
|
|
188
188
|
|
|
@@ -202,7 +202,7 @@ Hash checking is finished.
|
|
|
202
202
|
|
|
203
203
|
|
|
204
204
|
# Tests
|
|
205
|
-
Tests are written with [
|
|
205
|
+
Tests are written with [mocha](https://mochajs.org/) and [chai](https://www.chaijs.com/)
|
|
206
206
|
|
|
207
207
|
```bash
|
|
208
208
|
npm test
|
package/lib/hasher.js
CHANGED
|
@@ -3,7 +3,6 @@ const path = require('path');
|
|
|
3
3
|
const crypto = require('crypto');
|
|
4
4
|
const Stream = require('stream').Stream;
|
|
5
5
|
const parallel = require('async').parallel;
|
|
6
|
-
const _ = require('underscore');
|
|
7
6
|
const StreamSpeed = require('streamspeed');
|
|
8
7
|
|
|
9
8
|
const queue = require('./queue');
|
|
@@ -23,34 +22,40 @@ const MAX_FILES_OPENED = 250;
|
|
|
23
22
|
* @param {Buffer} buffer
|
|
24
23
|
* @return {Buffer}
|
|
25
24
|
*/
|
|
26
|
-
|
|
25
|
+
const sha1 = (buffer) => {
|
|
27
26
|
return crypto.createHash('sha1').update(buffer).digest();
|
|
28
|
-
}
|
|
27
|
+
};
|
|
29
28
|
|
|
30
29
|
|
|
31
30
|
/**
|
|
32
31
|
* Rounds a number to given decimal place.
|
|
33
32
|
*
|
|
34
|
-
* @param {
|
|
35
|
-
* @param {
|
|
36
|
-
* @return {
|
|
33
|
+
* @param {number} num
|
|
34
|
+
* @param {number} dec
|
|
35
|
+
* @return {number}
|
|
37
36
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const round = (num, dec) => {
|
|
38
|
+
const pow = Math.pow(10, dec);
|
|
40
39
|
return Math.round(num * pow) / pow;
|
|
41
|
-
}
|
|
40
|
+
};
|
|
42
41
|
|
|
43
42
|
|
|
44
43
|
/**
|
|
45
44
|
* Allows arrays of objects with a `path` property to be
|
|
46
45
|
* sorted alphabetically
|
|
47
46
|
*
|
|
48
|
-
* @param {Object}
|
|
49
|
-
* @
|
|
47
|
+
* @param {Array.<Object>} a
|
|
48
|
+
* @param {Array.<Object>} b
|
|
49
|
+
* @return {string}
|
|
50
50
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
};
|
|
54
59
|
|
|
55
60
|
|
|
56
61
|
module.exports = class Hasher extends Stream {
|
|
@@ -58,13 +63,13 @@ module.exports = class Hasher extends Stream {
|
|
|
58
63
|
* Calculates piece hashes of file list.
|
|
59
64
|
*
|
|
60
65
|
* @constructor
|
|
61
|
-
* @param {String} dir Directory where files are.
|
|
62
|
-
* @param {Array.<String>} list Array of files to hash.
|
|
63
|
-
* @param {Number} pieceLength How many bytes long each piece will be.
|
|
64
|
-
* @param {Object} options
|
|
65
|
-
* @param {Number} maxFiles Max number of files to open during hashing.
|
|
66
|
-
* @param {Boolean} stopOnErr Hashing stops if encounters an error.
|
|
67
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.
|
|
68
73
|
*/
|
|
69
74
|
constructor(dir, list, pieceLength, options) {
|
|
70
75
|
super();
|
|
@@ -95,9 +100,18 @@ module.exports = class Hasher extends Stream {
|
|
|
95
100
|
return this.emit('error', err);
|
|
96
101
|
}
|
|
97
102
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
+
}
|
|
101
115
|
|
|
102
116
|
if (err) return this.emit('error', err);
|
|
103
117
|
|
|
@@ -113,10 +127,10 @@ module.exports = class Hasher extends Stream {
|
|
|
113
127
|
};
|
|
114
128
|
|
|
115
129
|
// Check if list containts objects or strings.
|
|
116
|
-
if (
|
|
130
|
+
if (fromDisk) {
|
|
117
131
|
|
|
118
132
|
// Create queue to handle having many files opened.
|
|
119
|
-
|
|
133
|
+
const statqueue = queue(fs.stat, this.maxFiles);
|
|
120
134
|
this._getFilesStats(statqueue, list, readTasks);
|
|
121
135
|
|
|
122
136
|
// In the case of objects, a file list from a torrent file was given.
|
|
@@ -133,7 +147,7 @@ module.exports = class Hasher extends Stream {
|
|
|
133
147
|
*/
|
|
134
148
|
_CreateFileTasks() {
|
|
135
149
|
// Variables to help calculate piece position.
|
|
136
|
-
|
|
150
|
+
let totalSize, piece, offset;
|
|
137
151
|
totalSize = piece = offset = 0;
|
|
138
152
|
|
|
139
153
|
// Create tasks to figure out what pieces belong to exactly
|
|
@@ -144,9 +158,9 @@ module.exports = class Hasher extends Stream {
|
|
|
144
158
|
this.filetasks.push({
|
|
145
159
|
length : task.length,
|
|
146
160
|
path : task.path,
|
|
147
|
-
// Piece this task will write to.
|
|
161
|
+
// Piece this task will read from or write to.
|
|
148
162
|
piece,
|
|
149
|
-
// Piece buffer offset to start writing at.
|
|
163
|
+
// Piece buffer offset to start reading or writing at.
|
|
150
164
|
offset,
|
|
151
165
|
});
|
|
152
166
|
|
|
@@ -177,41 +191,40 @@ module.exports = class Hasher extends Stream {
|
|
|
177
191
|
this.stopped = false;
|
|
178
192
|
|
|
179
193
|
// Create piece objects.
|
|
180
|
-
|
|
181
|
-
for (
|
|
194
|
+
const pieces = [];
|
|
195
|
+
for (let i = 0; i < this.pieces; i++) {
|
|
182
196
|
pieces[i] = {
|
|
183
|
-
buffer
|
|
184
|
-
length
|
|
185
|
-
bytesWritten
|
|
197
|
+
buffer: null,
|
|
198
|
+
length: this.pieceLength,
|
|
199
|
+
bytesWritten: 0,
|
|
186
200
|
};
|
|
187
201
|
}
|
|
188
202
|
pieces[pieces.length - 1].length = this.lastPieceLength;
|
|
189
|
-
|
|
190
203
|
|
|
191
204
|
// Keep track of progress.
|
|
192
|
-
|
|
205
|
+
let totalRead = 0;
|
|
193
206
|
|
|
194
207
|
// Keep track of reading speed.
|
|
195
208
|
this.streamGroup = new StreamSpeed();
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
209
|
+
let lastPercent = 0;
|
|
210
|
+
let speed = 0;
|
|
211
|
+
let avg = 0;
|
|
199
212
|
|
|
200
213
|
this.streamGroup.on('speed', (s, a) => {
|
|
201
214
|
speed = s;
|
|
202
215
|
avg = a;
|
|
203
216
|
});
|
|
204
217
|
|
|
205
|
-
|
|
218
|
+
const filesqueue = queue((task, callback) => {
|
|
206
219
|
if (this.err || this.stopped) return callback();
|
|
207
220
|
|
|
208
221
|
// Keep track how much has been read.
|
|
209
|
-
|
|
210
|
-
|
|
222
|
+
let readSoFar = 0;
|
|
223
|
+
let pieceIndex = task.piece;
|
|
211
224
|
|
|
212
225
|
// Open file for reading.
|
|
213
|
-
|
|
214
|
-
|
|
226
|
+
const file = path.join(this.dir, ...task.path);
|
|
227
|
+
const rs = fs.createReadStream(file);
|
|
215
228
|
this.streamGroup.add(rs);
|
|
216
229
|
|
|
217
230
|
rs.on('open', () => {
|
|
@@ -224,8 +237,8 @@ module.exports = class Hasher extends Stream {
|
|
|
224
237
|
Math.floor((task.offset + readSoFar) / this.pieceLength);
|
|
225
238
|
|
|
226
239
|
// Write data to piece buffer.
|
|
227
|
-
|
|
228
|
-
|
|
240
|
+
const targetStart = (task.offset + readSoFar) % this.pieceLength;
|
|
241
|
+
let sourceWritten = this._writePiece(
|
|
229
242
|
pieces, pieceIndex, targetStart,
|
|
230
243
|
data, 0, data.length, file, readSoFar);
|
|
231
244
|
|
|
@@ -241,7 +254,7 @@ module.exports = class Hasher extends Stream {
|
|
|
241
254
|
totalRead += data.length;
|
|
242
255
|
|
|
243
256
|
// Emit hash event with piece info and progress.
|
|
244
|
-
|
|
257
|
+
let percent =
|
|
245
258
|
round(totalRead / this.totalSize * 100, 2);
|
|
246
259
|
if (lastPercent !== percent) {
|
|
247
260
|
this.emit('progress', percent, speed, avg);
|
|
@@ -277,21 +290,21 @@ module.exports = class Hasher extends Stream {
|
|
|
277
290
|
_writePiece(pieces, index, offset,
|
|
278
291
|
data, start, end,
|
|
279
292
|
file, readSoFar) {
|
|
280
|
-
|
|
293
|
+
const piece = pieces[index];
|
|
281
294
|
if (!piece) return;
|
|
282
295
|
if (piece.buffer === null) {
|
|
283
296
|
piece.buffer = Buffer.alloc(piece.length);
|
|
284
297
|
}
|
|
285
298
|
|
|
286
299
|
// Write data to piece buffer and update bytes written.
|
|
287
|
-
|
|
300
|
+
const size = Math.min(end - start, piece.length - offset);
|
|
288
301
|
piece.bytesWritten += size;
|
|
289
302
|
data.copy(piece.buffer, offset, start, start + size);
|
|
290
303
|
|
|
291
304
|
// Check if piece buffer is full.
|
|
292
305
|
if (piece.bytesWritten === piece.length) {
|
|
293
306
|
pieces[index] = null;
|
|
294
|
-
|
|
307
|
+
const buf = Buffer.from(sha1(piece.buffer), 'binary');
|
|
295
308
|
this.emit('hash', index, buf, file, readSoFar, size);
|
|
296
309
|
}
|
|
297
310
|
|
|
@@ -303,9 +316,9 @@ module.exports = class Hasher extends Stream {
|
|
|
303
316
|
* Recursively traverses files to call fs.stat on them.
|
|
304
317
|
* Will add all files in folders if any are encountered.
|
|
305
318
|
*
|
|
306
|
-
* @param {
|
|
307
|
-
* @param {Array.<
|
|
308
|
-
* @param {
|
|
319
|
+
* @param {boolean} queue
|
|
320
|
+
* @param {Array.<string>} list List of files and/or directories.
|
|
321
|
+
* @param {Function(!Error)} callback
|
|
309
322
|
*/
|
|
310
323
|
_getFilesStats(queue, list, callback) {
|
|
311
324
|
// Read all file stats in parallel.
|
|
@@ -318,12 +331,12 @@ module.exports = class Hasher extends Stream {
|
|
|
318
331
|
/**
|
|
319
332
|
* Stat is needed to know file size.
|
|
320
333
|
*
|
|
321
|
-
* @param {
|
|
322
|
-
* @param {Array.<
|
|
323
|
-
* @param {
|
|
334
|
+
* @param {boolean} queue
|
|
335
|
+
* @param {Array.<string>} list List of files and/or directories.
|
|
336
|
+
* @param {Function(!Error)} callback
|
|
324
337
|
*/
|
|
325
338
|
_getFileStat(queue, file, callback) {
|
|
326
|
-
|
|
339
|
+
const filepath = path.join(this.dir, file);
|
|
327
340
|
|
|
328
341
|
queue.push(filepath, (err, stat) => {
|
|
329
342
|
if (err) return callback(err);
|
|
@@ -369,7 +382,7 @@ module.exports = class Hasher extends Stream {
|
|
|
369
382
|
|
|
370
383
|
|
|
371
384
|
/**
|
|
372
|
-
* Resumes hashing
|
|
385
|
+
* Resumes hashing.
|
|
373
386
|
*/
|
|
374
387
|
resume() {
|
|
375
388
|
if (!this.paused) return false;
|
|
@@ -384,7 +397,7 @@ module.exports = class Hasher extends Stream {
|
|
|
384
397
|
|
|
385
398
|
|
|
386
399
|
/**
|
|
387
|
-
* Continues hashing if paused or pauses if not
|
|
400
|
+
* Continues hashing if paused or pauses if not.
|
|
388
401
|
*/
|
|
389
402
|
toggle() {
|
|
390
403
|
return this.paused ? this.resume() : this.pause();
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
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/make.js
CHANGED
|
@@ -20,28 +20,28 @@ const APP_VERSION = pkg.version;
|
|
|
20
20
|
/**
|
|
21
21
|
* Makes torrent file object
|
|
22
22
|
*
|
|
23
|
-
* @param {
|
|
24
|
-
* @param {
|
|
25
|
-
* @param {Array.<
|
|
23
|
+
* @param {string} announce The announce URL
|
|
24
|
+
* @param {string} dir Directory where the files in the files array are
|
|
25
|
+
* @param {Array.<string>} files Optional
|
|
26
26
|
* @param {Object} options Any options for the torrent go here
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
27
|
+
* @param {Array.<string>} options.announceList
|
|
28
|
+
* @param {string} options.comment
|
|
29
|
+
* @param {string} options.name Used in multifile mode. If not present, folder
|
|
30
|
+
* name will be used.
|
|
31
|
+
* @param {number} options.pieceLength A power of 2 representing the byte length
|
|
32
|
+
* of each piece.
|
|
33
|
+
* @param {boolean} options.private
|
|
34
|
+
* @param {Object} options.moreInfo Typically used to generate a different info hash.
|
|
35
|
+
* @param {number} options.maxFiles Max files to open at the same time when hashing.
|
|
36
|
+
* @param {number} options.maxMemory Max amount of memory to allocate when hashing.
|
|
37
|
+
* Can be bytes or in human readable form such as 300.50MB
|
|
38
38
|
* @param {Function(!Error, Torrent)} callback
|
|
39
39
|
* @return {Hasher}
|
|
40
40
|
*/
|
|
41
|
-
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
const make = exports.make = (announce, dir, files, options, callback) => {
|
|
42
|
+
// Handle all of the optional arguments.
|
|
43
|
+
const typeofFiles = typeof files;
|
|
44
|
+
const typeofOptions = typeof options;
|
|
45
45
|
|
|
46
46
|
if (typeofFiles === 'function') {
|
|
47
47
|
callback = files;
|
|
@@ -68,58 +68,58 @@ var make = exports.make = (announce, dir, files, options, callback) => {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
const hashOptions = { maxFiles: options.maxFiles, stopOnErr: true };
|
|
72
72
|
|
|
73
73
|
// Default piece length is 256kb.
|
|
74
|
-
|
|
74
|
+
const pieceLength = options.pieceLength ?
|
|
75
75
|
(1 << options.pieceLength) : 262144;
|
|
76
76
|
|
|
77
77
|
// Start hashing files.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
const hasher = new Hasher(dir, files, pieceLength, hashOptions);
|
|
79
|
+
const order = new OrderedEmitter();
|
|
80
|
+
const buf = Buffers();
|
|
81
|
+
const info = {};
|
|
82
|
+
let data, piecesLength, piecesPosition, err;
|
|
83
83
|
|
|
84
84
|
hasher.readable = true;
|
|
85
85
|
|
|
86
86
|
// Check announce URL.
|
|
87
87
|
if (!util.isURL(announce)) {
|
|
88
|
-
err =
|
|
88
|
+
err = Error('Not a URL: ' + announce);
|
|
89
89
|
process.nextTick(() => {
|
|
90
90
|
hasher.destroy();
|
|
91
91
|
hasher.emit('error', err);
|
|
92
92
|
callback(err);
|
|
93
93
|
});
|
|
94
|
-
return;
|
|
94
|
+
return hasher;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
// Check list of files is not empty.
|
|
98
98
|
if (files.length === 0) {
|
|
99
|
-
err =
|
|
99
|
+
err = Error('no files given');
|
|
100
100
|
process.nextTick(() => {
|
|
101
101
|
hasher.destroy();
|
|
102
102
|
hasher.emit('error', err);
|
|
103
103
|
callback(err);
|
|
104
104
|
});
|
|
105
|
-
return;
|
|
105
|
+
return hasher;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
// Make metadata object.
|
|
109
|
-
|
|
109
|
+
const metadata = {};
|
|
110
110
|
metadata.announce = announce;
|
|
111
111
|
|
|
112
112
|
// Check and validate announce list.
|
|
113
113
|
if (options.announceList != null) {
|
|
114
|
-
|
|
114
|
+
const msg = schema.announceList(options.announceList);
|
|
115
115
|
if (msg !== null) {
|
|
116
|
-
err =
|
|
116
|
+
err = Error(msg);
|
|
117
117
|
process.nextTick(() => {
|
|
118
118
|
hasher.destroy();
|
|
119
119
|
hasher.emit('error', err);
|
|
120
120
|
callback(err);
|
|
121
121
|
});
|
|
122
|
-
return;
|
|
122
|
+
return hasher;
|
|
123
123
|
}
|
|
124
124
|
metadata['announce-list'] = options.announceList;
|
|
125
125
|
}
|
|
@@ -143,7 +143,7 @@ var make = exports.make = (announce, dir, files, options, callback) => {
|
|
|
143
143
|
// Single file mode.
|
|
144
144
|
} else {
|
|
145
145
|
info.length = hasher.files[0].length;
|
|
146
|
-
info.name = path.join
|
|
146
|
+
info.name = path.join(...hasher.files[0].path);
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
info['piece length'] = pieceLength;
|
|
@@ -155,7 +155,7 @@ var make = exports.make = (announce, dir, files, options, callback) => {
|
|
|
155
155
|
info.pieces = Buffer.alloc(piecesLength);
|
|
156
156
|
|
|
157
157
|
// Add additional moreInfo to info.
|
|
158
|
-
for (
|
|
158
|
+
for (let moreKey in options.moreInfo) {
|
|
159
159
|
// Only add moreInfo if it doesn't overwrite info.
|
|
160
160
|
if (!Object.prototype.hasOwnProperty.call(info, moreKey)) {
|
|
161
161
|
info[moreKey] = options.moreInfo[moreKey];
|
|
@@ -164,8 +164,8 @@ var make = exports.make = (announce, dir, files, options, callback) => {
|
|
|
164
164
|
|
|
165
165
|
// Bencode data.
|
|
166
166
|
data = Buffer.from(b.encode(metadata), 'binary');
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
const strData = data.toString();
|
|
168
|
+
const dict = '6:pieces' + piecesLength + ':';
|
|
169
169
|
piecesPosition = strData.indexOf(dict) + dict.length;
|
|
170
170
|
piecesPosition = Buffer.byteLength(strData.substr(0, piecesPosition));
|
|
171
171
|
|
|
@@ -211,16 +211,16 @@ var make = exports.make = (announce, dir, files, options, callback) => {
|
|
|
211
211
|
/**
|
|
212
212
|
* Calls make and creates a write stream with the returned read stream.
|
|
213
213
|
*
|
|
214
|
-
* @param {
|
|
215
|
-
* @param {
|
|
216
|
-
* @param {
|
|
217
|
-
* @param {Array.<
|
|
214
|
+
* @param {string} output
|
|
215
|
+
* @param {string} announce
|
|
216
|
+
* @param {string} dir
|
|
217
|
+
* @param {Array.<string>} files
|
|
218
218
|
* @param {Object} options
|
|
219
219
|
* @param {Function(!Error, Torrent)} callback
|
|
220
220
|
* @return {Hasher}
|
|
221
221
|
*/
|
|
222
222
|
exports.makeWrite = (output, announce, dir, files, options, callback) => {
|
|
223
|
-
|
|
223
|
+
const rs = make(announce, dir, files, options, callback);
|
|
224
224
|
rs.pipe(fs.createWriteStream(output));
|
|
225
225
|
return rs;
|
|
226
226
|
};
|
package/lib/queue.js
CHANGED
|
@@ -2,32 +2,32 @@
|
|
|
2
2
|
* Creates a queue
|
|
3
3
|
*
|
|
4
4
|
* @param {Function(data, callback)} worker
|
|
5
|
-
* @param {
|
|
5
|
+
* @param {number|Function(workers, task)} concurrency
|
|
6
6
|
* @return {Object}
|
|
7
7
|
*/
|
|
8
8
|
module.exports = (worker, concurrency) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
let workers = 0;
|
|
10
|
+
const tasks = [];
|
|
11
|
+
const q = {
|
|
12
12
|
saturated: null,
|
|
13
13
|
empty: null,
|
|
14
14
|
drain: null,
|
|
15
|
-
push
|
|
16
|
-
|
|
15
|
+
push(data, callback) {
|
|
16
|
+
const length = tasks.push({ data, callback });
|
|
17
17
|
if (q.saturated && concurrency(workers, null, length)) {
|
|
18
18
|
q.saturated();
|
|
19
19
|
}
|
|
20
20
|
process.nextTick(q.process);
|
|
21
21
|
},
|
|
22
|
-
process
|
|
22
|
+
process() {
|
|
23
23
|
if (tasks.length > 0) {
|
|
24
|
-
|
|
24
|
+
const task = tasks.shift();
|
|
25
25
|
if (q.concurrency(workers, task.data)) {
|
|
26
26
|
if (q.empty && tasks.length === 0) q.empty();
|
|
27
27
|
workers++;
|
|
28
|
-
worker(task.data,
|
|
28
|
+
worker(task.data, (...args) => {
|
|
29
29
|
workers--;
|
|
30
|
-
if (task.callback) task.callback
|
|
30
|
+
if (task.callback) task.callback(...args);
|
|
31
31
|
if (q.drain && tasks.length + workers === 0) {
|
|
32
32
|
q.drain();
|
|
33
33
|
} else {
|
|
@@ -41,18 +41,17 @@ module.exports = (worker, concurrency) => {
|
|
|
41
41
|
},
|
|
42
42
|
length: () => tasks.length,
|
|
43
43
|
running: () => workers,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
concurrency = c;
|
|
44
|
+
get concurrency() { return concurrency; },
|
|
45
|
+
set concurrency(c) {
|
|
46
|
+
if (typeof c !== 'function') {
|
|
47
|
+
concurrency = (workers, task, length) => {
|
|
48
|
+
return length ? length === c : workers < c;
|
|
49
|
+
};
|
|
50
|
+
} else {
|
|
51
|
+
concurrency = c;
|
|
52
|
+
}
|
|
54
53
|
}
|
|
55
|
-
}
|
|
54
|
+
};
|
|
56
55
|
|
|
57
56
|
q.concurrency = concurrency;
|
|
58
57
|
return q;
|
package/lib/read.js
CHANGED
|
@@ -8,27 +8,27 @@ const Torrent = require('./torrent');
|
|
|
8
8
|
/**
|
|
9
9
|
* Read torrent data.
|
|
10
10
|
*
|
|
11
|
-
* @param {
|
|
11
|
+
* @param {string|ReadableStream} file File where the torrent
|
|
12
12
|
* resides, can be local file, remote, or a readable stream.
|
|
13
13
|
* @param {Function(!Error, Torrent)} callback
|
|
14
|
-
* @param {
|
|
14
|
+
* @param {boolean} validate validate or not schema
|
|
15
15
|
* @param {ReadableStream}
|
|
16
16
|
*/
|
|
17
17
|
module.exports = (file, validate = true, callback) => {
|
|
18
18
|
if (typeof validate === 'function') {
|
|
19
19
|
callback = validate;
|
|
20
|
-
validate = true
|
|
20
|
+
validate = true;
|
|
21
21
|
}
|
|
22
22
|
if (!callback) {
|
|
23
23
|
callback = () => {};
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const rs = typeof file === 'string' ? fs.createReadStream(file) : file;
|
|
27
|
+
const bstream = new BStream();
|
|
28
28
|
rs.pipe(bstream);
|
|
29
29
|
|
|
30
30
|
bstream.on('error', (err) => {
|
|
31
|
-
if (
|
|
31
|
+
if ('fd' in rs) {
|
|
32
32
|
fs.close(rs.fd);
|
|
33
33
|
}
|
|
34
34
|
callback(err);
|
package/lib/schema.js
CHANGED
|
@@ -6,10 +6,10 @@ const Torrent = require('./torrent');
|
|
|
6
6
|
* Check if field that should be a buffer and exists is a buffer.
|
|
7
7
|
*
|
|
8
8
|
* @param {Object} torrent
|
|
9
|
-
* @param {
|
|
10
|
-
* @return {!
|
|
9
|
+
* @param {string} field Key from torrent object to check
|
|
10
|
+
* @return {!string} Possible error
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
const checkBuffer = (torrent, field) => {
|
|
13
13
|
return torrent[field] && !Buffer.isBuffer(torrent[field]) ?
|
|
14
14
|
'`' + field + '` is not a buffer' : null;
|
|
15
15
|
};
|
|
@@ -19,15 +19,14 @@ var checkBuffer = function(torrent, field) {
|
|
|
19
19
|
* Checks announce fields are correct.
|
|
20
20
|
*
|
|
21
21
|
* @param {Object} torrent
|
|
22
|
-
* @return {!
|
|
22
|
+
* @return {!string} Possible error
|
|
23
23
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
|
|
24
|
+
const checkAnnounce = (torrent) => {
|
|
25
|
+
let err;
|
|
26
|
+
if (torrent.announce) {
|
|
27
|
+
if ((err = checkBuffer(torrent, 'announce'))) return err;
|
|
28
|
+
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
|
|
28
29
|
}
|
|
29
|
-
if ((err = checkBuffer(torrent, 'announce'))) return err;
|
|
30
|
-
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
|
|
31
30
|
return null;
|
|
32
31
|
};
|
|
33
32
|
|
|
@@ -36,19 +35,17 @@ var checkAnnounce = (torrent) => {
|
|
|
36
35
|
* Checks announce list field.
|
|
37
36
|
*
|
|
38
37
|
* @param {Object} torrent
|
|
39
|
-
* @return {!
|
|
38
|
+
* @return {!string} Possible error
|
|
40
39
|
*/
|
|
41
|
-
|
|
40
|
+
const checkAnnounceList = exports.announceList = (list) => {
|
|
42
41
|
if (list) {
|
|
43
42
|
if (!Array.isArray(list)) return '`announce-list` is not a list';
|
|
44
43
|
|
|
45
|
-
for (
|
|
46
|
-
var item = list[i];
|
|
44
|
+
for (let item of list) {
|
|
47
45
|
if (!Array.isArray(item)) {
|
|
48
46
|
return '`announce-list` is not a list of lists';
|
|
49
47
|
}
|
|
50
|
-
for (
|
|
51
|
-
var ann = item[k];
|
|
48
|
+
for (let ann of item) {
|
|
52
49
|
if (!Buffer.isBuffer(ann)) {
|
|
53
50
|
return 'Item in `announce-list` is not a buffer';
|
|
54
51
|
}
|
|
@@ -65,10 +62,10 @@ var checkAnnounceList = exports.announceList = (list) => {
|
|
|
65
62
|
/**
|
|
66
63
|
* Check if the creation date exists and is an integer.
|
|
67
64
|
*
|
|
68
|
-
* @param {
|
|
69
|
-
* @return {!
|
|
65
|
+
* @param {number} date
|
|
66
|
+
* @return {!string} Possible error
|
|
70
67
|
*/
|
|
71
|
-
|
|
68
|
+
const checkCreationDate = (date) => {
|
|
72
69
|
return date != null && !util.isInteger(date) ?
|
|
73
70
|
'`creation date` is not an integer' : null;
|
|
74
71
|
};
|
|
@@ -79,10 +76,10 @@ var checkCreationDate = (date) => {
|
|
|
79
76
|
*
|
|
80
77
|
* @param {Object} parent Info metadata of torrent or file object if
|
|
81
78
|
* in multi file mode.
|
|
82
|
-
* @return {!
|
|
79
|
+
* @return {!string} Possible error
|
|
83
80
|
*/
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
const md5sumRegexp = /[a-f0-9]{32}/i;
|
|
82
|
+
const checkmd5sum = (parent) => {
|
|
86
83
|
if (parent.md5sum != null) {
|
|
87
84
|
if (!Buffer.isBuffer(parent.md5sum)) return '`md5sum` is not a buffer';
|
|
88
85
|
if (!md5sumRegexp.test(parent.md5sum)) {
|
|
@@ -99,9 +96,9 @@ var checkmd5sum = (parent) => {
|
|
|
99
96
|
*
|
|
100
97
|
* @param {Object} torrent
|
|
101
98
|
*/
|
|
102
|
-
|
|
99
|
+
const buf2str = (torrent, path) => {
|
|
103
100
|
Object.keys(torrent).forEach((key) => {
|
|
104
|
-
|
|
101
|
+
const val = torrent[key];
|
|
105
102
|
|
|
106
103
|
if (Buffer.isBuffer(val)) {
|
|
107
104
|
if (path !== '.info' || key !== 'pieces') {
|
|
@@ -118,10 +115,10 @@ var buf2str = (torrent, path) => {
|
|
|
118
115
|
* Checks torrent metadata is correct.
|
|
119
116
|
*
|
|
120
117
|
* @param {Object} torrent
|
|
121
|
-
* @return {!
|
|
118
|
+
* @return {!string} Possible error with the torrent object
|
|
122
119
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
const checkTorrent = exports.checkTorrent = (torrent) => {
|
|
121
|
+
let err;
|
|
125
122
|
|
|
126
123
|
if (typeof torrent !== 'object') return 'Torrent is not hash';
|
|
127
124
|
if ((err = checkAnnounce(torrent, 'announce'))) return err;
|
|
@@ -136,8 +133,7 @@ var checkTorrent = exports.checkTorrent = (torrent) => {
|
|
|
136
133
|
// Multi file mode.
|
|
137
134
|
if (torrent.info.files != null) {
|
|
138
135
|
if (!Array.isArray(torrent.info.files)) return '`info.files` is not a list';
|
|
139
|
-
for (
|
|
140
|
-
var file = torrent.info.files[i];
|
|
136
|
+
for (let file of torrent.info.files) {
|
|
141
137
|
if (file.length == null) {
|
|
142
138
|
return '`length` field not found in file';
|
|
143
139
|
}
|
|
@@ -201,9 +197,9 @@ var checkTorrent = exports.checkTorrent = (torrent) => {
|
|
|
201
197
|
* @param {Function(!Error, Torrent)} callback
|
|
202
198
|
*/
|
|
203
199
|
exports.validate = (torrent, callback) => {
|
|
204
|
-
|
|
200
|
+
let errmsg = checkTorrent(torrent);
|
|
205
201
|
if (errmsg !== null) {
|
|
206
|
-
|
|
202
|
+
let err = Error(errmsg);
|
|
207
203
|
err.name = 'SchemaError';
|
|
208
204
|
return callback(err);
|
|
209
205
|
}
|
package/lib/torrent.js
CHANGED
|
@@ -18,7 +18,7 @@ module.exports = class Torrent {
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* @return {
|
|
21
|
+
* @return {string} Returns info hash of torrent
|
|
22
22
|
*/
|
|
23
23
|
infoHash() {
|
|
24
24
|
return crypto
|
|
@@ -34,11 +34,11 @@ module.exports = class Torrent {
|
|
|
34
34
|
* @return {ReadableStream}
|
|
35
35
|
*/
|
|
36
36
|
createReadStream() {
|
|
37
|
-
|
|
37
|
+
const memStream = new MemoryStream();
|
|
38
38
|
memStream.readable = true;
|
|
39
39
|
|
|
40
40
|
// Bencode data and emit it from readstream.
|
|
41
|
-
|
|
41
|
+
const data = b.encode(this.metadata);
|
|
42
42
|
|
|
43
43
|
process.nextTick(() => {
|
|
44
44
|
memStream.write(data, () => {
|
|
@@ -54,13 +54,13 @@ module.exports = class Torrent {
|
|
|
54
54
|
* Shortcut to pipe the readable stream created by Torrent#createReadStream
|
|
55
55
|
* to a writable stream of a file, then return it.
|
|
56
56
|
*
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {string} path
|
|
58
58
|
* @param {Object} options
|
|
59
59
|
* @return {WritableStream}
|
|
60
60
|
*/
|
|
61
61
|
createWriteStream(path, options) {
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
const rs = this.createReadStream();
|
|
63
|
+
const ws = fs.createWriteStream(path, options);
|
|
64
64
|
rs.pipe(ws);
|
|
65
65
|
return ws;
|
|
66
66
|
}
|
|
@@ -69,24 +69,24 @@ module.exports = class Torrent {
|
|
|
69
69
|
/**
|
|
70
70
|
* Hash checks torrent.
|
|
71
71
|
*
|
|
72
|
-
* @param {
|
|
72
|
+
* @param {string} dir Directory where files are.
|
|
73
73
|
* @param {Object} options
|
|
74
74
|
* @return {Hasher}
|
|
75
75
|
*/
|
|
76
76
|
hashCheck(dir, options) {
|
|
77
77
|
options = options || {};
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
const info = this.metadata.info;
|
|
80
80
|
|
|
81
81
|
// Check if this is a single or multi file mode torrent.
|
|
82
|
-
|
|
82
|
+
let files = info.files || [{ path: [info.name], length: info.length }];
|
|
83
83
|
|
|
84
84
|
// Call the hasher.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
let hashOptions = { maxFiles: options.maxFiles };
|
|
86
|
+
let hasher = new Hasher(dir, files, info['piece length'], hashOptions);
|
|
87
|
+
let percentMatched = 0;
|
|
88
|
+
let piecesMatched = 0;
|
|
89
|
+
let pieces = info.pieces.length / 20;
|
|
90
90
|
|
|
91
91
|
hasher.on('hash', (index, hash, file, position, length) => {
|
|
92
92
|
// Check that hash matches.
|
package/lib/util.js
CHANGED
|
@@ -3,8 +3,8 @@ const urlRegexp = /^(http|udp|ftp|dht)s?:\/\//;
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns true if str is a URL.
|
|
5
5
|
*
|
|
6
|
-
* @param {
|
|
7
|
-
* @return {
|
|
6
|
+
* @param {string} str
|
|
7
|
+
* @return {boolean}
|
|
8
8
|
*/
|
|
9
9
|
exports.isURL = str => urlRegexp.test(str);
|
|
10
10
|
|
|
@@ -12,8 +12,8 @@ exports.isURL = str => urlRegexp.test(str);
|
|
|
12
12
|
/**
|
|
13
13
|
* Returns true if n is an integer.
|
|
14
14
|
*
|
|
15
|
-
* @param {
|
|
16
|
-
* @return {
|
|
15
|
+
* @param {number} n
|
|
16
|
+
* @return {boolean}
|
|
17
17
|
*/
|
|
18
18
|
exports.isInteger = n => !isNaN(parseInt(n, 10));
|
|
19
19
|
|
|
@@ -21,8 +21,8 @@ exports.isInteger = n => !isNaN(parseInt(n, 10));
|
|
|
21
21
|
/**
|
|
22
22
|
* Splits a path into an array, platform safe.
|
|
23
23
|
*
|
|
24
|
-
* @param {
|
|
25
|
-
* @return {Array.<
|
|
24
|
+
* @param {string} path
|
|
25
|
+
* @return {Array.<string>}
|
|
26
26
|
*/
|
|
27
27
|
exports.splitPath = path => path.split(path.sep);
|
|
28
28
|
|
|
@@ -32,10 +32,10 @@ exports.splitPath = path => path.split(path.sep);
|
|
|
32
32
|
*
|
|
33
33
|
* @param {Buffer} a
|
|
34
34
|
* @param {Buffer} b
|
|
35
|
-
* @param {
|
|
35
|
+
* @param {number} start
|
|
36
36
|
*/
|
|
37
37
|
exports.buffersMatch = (a, b, start) => {
|
|
38
|
-
for (
|
|
38
|
+
for (let i = 0, l = b.length; i < l; i++) {
|
|
39
39
|
if (a[start + i] !== b[i]) {
|
|
40
40
|
return false;
|
|
41
41
|
}
|
package/package.json
CHANGED
|
@@ -6,13 +6,14 @@
|
|
|
6
6
|
"bittorremt",
|
|
7
7
|
"file"
|
|
8
8
|
],
|
|
9
|
-
"version": "0.7.
|
|
9
|
+
"version": "0.7.2",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "git://github.com/fent/node-torrent.git"
|
|
13
13
|
},
|
|
14
|
-
"author": "fent (https://github.com/fent)",
|
|
14
|
+
"author": "fent <fentbox@gmail.com> (https://github.com/fent)",
|
|
15
15
|
"main": "./lib/index.js",
|
|
16
|
+
"types": "./lib/index.d.ts",
|
|
16
17
|
"files": [
|
|
17
18
|
"lib"
|
|
18
19
|
],
|
|
@@ -20,26 +21,23 @@
|
|
|
20
21
|
"lib": "./lib"
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
23
|
-
"test": "
|
|
24
|
-
"snyk-protect": "snyk protect",
|
|
25
|
-
"prepare": "npm run snyk-protect"
|
|
24
|
+
"test": "nyc mocha test/*-test.js"
|
|
26
25
|
},
|
|
27
26
|
"dependencies": {
|
|
28
|
-
"async": "^3.
|
|
27
|
+
"async": "^3.2.0",
|
|
29
28
|
"bncode": "~0.5.2",
|
|
30
29
|
"buffers": "~0.1.1",
|
|
31
30
|
"memorystream": "~0.3.1",
|
|
32
31
|
"ordered-emitter": "^1.0.0",
|
|
33
|
-
"
|
|
34
|
-
"streamspeed": "~1.1.0",
|
|
35
|
-
"underscore": "^1.6.0"
|
|
32
|
+
"streamspeed": "~2.0.0"
|
|
36
33
|
},
|
|
37
34
|
"devDependencies": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
35
|
+
"chai": "6.2.1",
|
|
36
|
+
"mocha": "12.0.3",
|
|
37
|
+
"nyc": "18.0.0"
|
|
40
38
|
},
|
|
41
39
|
"engines": {
|
|
42
|
-
"node": ">=
|
|
40
|
+
"node": ">=6"
|
|
43
41
|
},
|
|
44
42
|
"license": "MIT",
|
|
45
43
|
"snyk": true
|