nt 0.7.3 → 0.8.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/dist/hasher.d.ts +81 -0
- package/dist/hasher.js +398 -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 +22 -0
- package/dist/make.js +219 -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 +273 -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 +36 -0
- package/dist/util.js.map +1 -0
- package/package.json +17 -11
- 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/make.js
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const OrderedEmitter = require('ordered-emitter');
|
|
4
|
-
const b = require('bncode');
|
|
5
|
-
const Buffers = require('buffers');
|
|
6
|
-
|
|
7
|
-
const Hasher = require('./hasher');
|
|
8
|
-
const util = require('./util');
|
|
9
|
-
const Torrent = require('./torrent');
|
|
10
|
-
const schema = require('./schema');
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// Used to display program info when creating torrents
|
|
14
|
-
// and with the cli.
|
|
15
|
-
const pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));
|
|
16
|
-
const APP_NAME = pkg.name;
|
|
17
|
-
const APP_VERSION = pkg.version;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Makes torrent file object
|
|
22
|
-
*
|
|
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
|
-
* @param {Object} options Any options for the torrent go here
|
|
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
|
-
* @param {Function(!Error, Torrent)} callback
|
|
39
|
-
* @return {Hasher}
|
|
40
|
-
*/
|
|
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
|
-
|
|
46
|
-
if (typeofFiles === 'function') {
|
|
47
|
-
callback = files;
|
|
48
|
-
options = {};
|
|
49
|
-
files = ['.'];
|
|
50
|
-
|
|
51
|
-
} else if (typeofFiles === 'object' && !Array.isArray(files)) {
|
|
52
|
-
callback = typeofOptions === 'function' ? options : () => {};
|
|
53
|
-
options = files;
|
|
54
|
-
files = ['.'];
|
|
55
|
-
|
|
56
|
-
} else {
|
|
57
|
-
if (typeofOptions === 'function') {
|
|
58
|
-
callback = options;
|
|
59
|
-
options = {};
|
|
60
|
-
|
|
61
|
-
} else {
|
|
62
|
-
callback = callback || (() => {});
|
|
63
|
-
options = options || {};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (typeofFiles === 'string') files = [files];
|
|
67
|
-
else if (typeofFiles === 'undefined') files = ['.'];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const hashOptions = { maxFiles: options.maxFiles, stopOnErr: true };
|
|
72
|
-
|
|
73
|
-
// Default piece length is 256kb.
|
|
74
|
-
const pieceLength = options.pieceLength ?
|
|
75
|
-
(1 << options.pieceLength) : 262144;
|
|
76
|
-
|
|
77
|
-
// Start hashing files.
|
|
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
|
-
|
|
84
|
-
hasher.readable = true;
|
|
85
|
-
|
|
86
|
-
// Check announce URL.
|
|
87
|
-
if (!util.isURL(announce)) {
|
|
88
|
-
err = Error('Not a URL: ' + announce);
|
|
89
|
-
process.nextTick(() => {
|
|
90
|
-
hasher.destroy();
|
|
91
|
-
hasher.emit('error', err);
|
|
92
|
-
callback(err);
|
|
93
|
-
});
|
|
94
|
-
return hasher;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Check list of files is not empty.
|
|
98
|
-
if (files.length === 0) {
|
|
99
|
-
err = Error('no files given');
|
|
100
|
-
process.nextTick(() => {
|
|
101
|
-
hasher.destroy();
|
|
102
|
-
hasher.emit('error', err);
|
|
103
|
-
callback(err);
|
|
104
|
-
});
|
|
105
|
-
return hasher;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Make metadata object.
|
|
109
|
-
const metadata = {};
|
|
110
|
-
metadata.announce = announce;
|
|
111
|
-
|
|
112
|
-
// Check and validate announce list.
|
|
113
|
-
if (options.announceList != null) {
|
|
114
|
-
const msg = schema.announceList(options.announceList);
|
|
115
|
-
if (msg !== null) {
|
|
116
|
-
err = Error(msg);
|
|
117
|
-
process.nextTick(() => {
|
|
118
|
-
hasher.destroy();
|
|
119
|
-
hasher.emit('error', err);
|
|
120
|
-
callback(err);
|
|
121
|
-
});
|
|
122
|
-
return hasher;
|
|
123
|
-
}
|
|
124
|
-
metadata['announce-list'] = options.announceList;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Check comment options.
|
|
128
|
-
if (options.comment != null) metadata.comment = options.comment;
|
|
129
|
-
|
|
130
|
-
metadata['created by'] = APP_NAME + ' ' + APP_VERSION;
|
|
131
|
-
metadata['creation date'] = Math.round(Date.now() / 1000);
|
|
132
|
-
metadata.info = info;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
// Wait until hasher finishes examining file lengths.
|
|
136
|
-
hasher.on('ready', () => {
|
|
137
|
-
|
|
138
|
-
// Multi file mode.
|
|
139
|
-
if (options.multimode || hasher.files.length > 1) {
|
|
140
|
-
info.files = hasher.files;
|
|
141
|
-
info.name = options.name || path.basename(dir);
|
|
142
|
-
|
|
143
|
-
// Single file mode.
|
|
144
|
-
} else {
|
|
145
|
-
info.length = hasher.files[0].length;
|
|
146
|
-
info.name = path.join(...hasher.files[0].path);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
info['piece length'] = pieceLength;
|
|
150
|
-
info.pieces = null;
|
|
151
|
-
if (options.private) info.private = 1;
|
|
152
|
-
|
|
153
|
-
// Generate fake pieces to encode them.
|
|
154
|
-
piecesLength = 20 * hasher.pieces;
|
|
155
|
-
info.pieces = Buffer.alloc(piecesLength);
|
|
156
|
-
|
|
157
|
-
// Add additional moreInfo to info.
|
|
158
|
-
for (let moreKey in options.moreInfo) {
|
|
159
|
-
// Only add moreInfo if it doesn't overwrite info.
|
|
160
|
-
if (!Object.prototype.hasOwnProperty.call(info, moreKey)) {
|
|
161
|
-
info[moreKey] = options.moreInfo[moreKey];
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Bencode data.
|
|
166
|
-
data = Buffer.from(b.encode(metadata), 'binary');
|
|
167
|
-
const strData = data.toString();
|
|
168
|
-
const dict = '6:pieces' + piecesLength + ':';
|
|
169
|
-
piecesPosition = strData.indexOf(dict) + dict.length;
|
|
170
|
-
piecesPosition = Buffer.byteLength(strData.substr(0, piecesPosition));
|
|
171
|
-
|
|
172
|
-
// Write first part of torrent to stream.
|
|
173
|
-
process.nextTick(() => {
|
|
174
|
-
hasher.emit('data', data.slice(0, piecesPosition));
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// Write last part when hashing is finished.
|
|
180
|
-
hasher.on('end', () => {
|
|
181
|
-
info.pieces = buf.toBuffer();
|
|
182
|
-
|
|
183
|
-
// Note that listeners to this `data` event will be called before
|
|
184
|
-
// listeners to the `end` event since its emitted inside this listener.
|
|
185
|
-
hasher.emit('data', data.slice(piecesPosition + piecesLength));
|
|
186
|
-
callback(null, new Torrent(metadata));
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// listen for hash events
|
|
191
|
-
hasher.on('hash', (index, hash) => {
|
|
192
|
-
order.emit('hash', { order: index, hash: hash });
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
// Emit hash events to memStream in correct order.
|
|
196
|
-
order.on('hash', (obj) => {
|
|
197
|
-
buf.push(obj.hash);
|
|
198
|
-
hasher.emit('data', obj.hash);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
// Handle stream errors.
|
|
203
|
-
hasher.on('error', (err) => {
|
|
204
|
-
callback(err);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
return hasher;
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Calls make and creates a write stream with the returned read stream.
|
|
213
|
-
*
|
|
214
|
-
* @param {string} output
|
|
215
|
-
* @param {string} announce
|
|
216
|
-
* @param {string} dir
|
|
217
|
-
* @param {Array.<string>} files
|
|
218
|
-
* @param {Object} options
|
|
219
|
-
* @param {Function(!Error, Torrent)} callback
|
|
220
|
-
* @return {Hasher}
|
|
221
|
-
*/
|
|
222
|
-
exports.makeWrite = (output, announce, dir, files, options, callback) => {
|
|
223
|
-
const rs = make(announce, dir, files, options, callback);
|
|
224
|
-
rs.pipe(fs.createWriteStream(output));
|
|
225
|
-
return rs;
|
|
226
|
-
};
|
package/lib/queue.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a queue
|
|
3
|
-
*
|
|
4
|
-
* @param {Function(data, callback)} worker
|
|
5
|
-
* @param {number|Function(workers, task)} concurrency
|
|
6
|
-
* @return {Object}
|
|
7
|
-
*/
|
|
8
|
-
module.exports = (worker, concurrency) => {
|
|
9
|
-
let workers = 0;
|
|
10
|
-
const tasks = [];
|
|
11
|
-
const q = {
|
|
12
|
-
saturated: null,
|
|
13
|
-
empty: null,
|
|
14
|
-
drain: null,
|
|
15
|
-
push(data, callback) {
|
|
16
|
-
const length = tasks.push({ data, callback });
|
|
17
|
-
if (q.saturated && concurrency(workers, null, length)) {
|
|
18
|
-
q.saturated();
|
|
19
|
-
}
|
|
20
|
-
process.nextTick(q.process);
|
|
21
|
-
},
|
|
22
|
-
process() {
|
|
23
|
-
if (tasks.length > 0) {
|
|
24
|
-
const task = tasks.shift();
|
|
25
|
-
if (q.concurrency(workers, task.data)) {
|
|
26
|
-
if (q.empty && tasks.length === 0) q.empty();
|
|
27
|
-
workers++;
|
|
28
|
-
worker(task.data, (...args) => {
|
|
29
|
-
workers--;
|
|
30
|
-
if (task.callback) task.callback(...args);
|
|
31
|
-
if (q.drain && tasks.length + workers === 0) {
|
|
32
|
-
q.drain();
|
|
33
|
-
} else {
|
|
34
|
-
q.process();
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
} else {
|
|
38
|
-
return tasks.unshift(task);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
length: () => tasks.length,
|
|
43
|
-
running: () => workers,
|
|
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
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
q.concurrency = concurrency;
|
|
57
|
-
return q;
|
|
58
|
-
};
|
package/lib/read.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const BStream = require('bncode').Stream;
|
|
3
|
-
|
|
4
|
-
const schema = require('./schema');
|
|
5
|
-
const Torrent = require('./torrent');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Read torrent data.
|
|
10
|
-
*
|
|
11
|
-
* @param {string|ReadableStream} file File where the torrent
|
|
12
|
-
* resides, can be local file, remote, or a readable stream.
|
|
13
|
-
* @param {Function(!Error, Torrent)} callback
|
|
14
|
-
* @param {boolean} validate validate or not schema
|
|
15
|
-
* @param {ReadableStream}
|
|
16
|
-
*/
|
|
17
|
-
module.exports = (file, validate = true, callback) => {
|
|
18
|
-
if (typeof validate === 'function') {
|
|
19
|
-
callback = validate;
|
|
20
|
-
validate = true;
|
|
21
|
-
}
|
|
22
|
-
if (!callback) {
|
|
23
|
-
callback = () => {};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const rs = typeof file === 'string' ? fs.createReadStream(file) : file;
|
|
27
|
-
const bstream = new BStream();
|
|
28
|
-
rs.pipe(bstream);
|
|
29
|
-
|
|
30
|
-
bstream.on('error', (err) => {
|
|
31
|
-
if ('fd' in rs) {
|
|
32
|
-
fs.close(rs.fd);
|
|
33
|
-
}
|
|
34
|
-
callback(err);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
bstream.on('data', (result) => {
|
|
38
|
-
if (validate) {
|
|
39
|
-
schema.validate(result, callback);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
callback(null, new Torrent(result));
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return rs;
|
|
46
|
-
};
|
package/lib/schema.js
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
const util = require('./util');
|
|
2
|
-
const Torrent = require('./torrent');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Check if field that should be a buffer and exists is a buffer.
|
|
7
|
-
*
|
|
8
|
-
* @param {Object} torrent
|
|
9
|
-
* @param {string} field Key from torrent object to check
|
|
10
|
-
* @return {!string} Possible error
|
|
11
|
-
*/
|
|
12
|
-
const checkBuffer = (torrent, field) => {
|
|
13
|
-
return torrent[field] && !Buffer.isBuffer(torrent[field]) ?
|
|
14
|
-
'`' + field + '` is not a buffer' : null;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Checks that a path segment is safe and does not traverse directories.
|
|
20
|
-
*
|
|
21
|
-
* @param {*} buf
|
|
22
|
-
* @return {boolean}
|
|
23
|
-
*/
|
|
24
|
-
const isValidPathSegment = exports.isValidPathSegment = (buf) => {
|
|
25
|
-
if (!Buffer.isBuffer(buf) || buf.length === 0) return false;
|
|
26
|
-
const str = buf.toString('utf8');
|
|
27
|
-
if (str === '.' || str === '..') return false;
|
|
28
|
-
if (str.includes('/') || str.includes('\\') || str.includes('\0')) return false;
|
|
29
|
-
if (/^[a-zA-Z]:/.test(str)) return false;
|
|
30
|
-
return true;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Checks announce fields are correct.
|
|
36
|
-
*
|
|
37
|
-
* @param {Object} torrent
|
|
38
|
-
* @return {!string} Possible error
|
|
39
|
-
*/
|
|
40
|
-
const checkAnnounce = (torrent) => {
|
|
41
|
-
let err;
|
|
42
|
-
if (torrent.announce) {
|
|
43
|
-
if ((err = checkBuffer(torrent, 'announce'))) return err;
|
|
44
|
-
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Checks announce list field.
|
|
52
|
-
*
|
|
53
|
-
* @param {Object} torrent
|
|
54
|
-
* @return {!string} Possible error
|
|
55
|
-
*/
|
|
56
|
-
const checkAnnounceList = exports.announceList = (list) => {
|
|
57
|
-
if (list) {
|
|
58
|
-
if (!Array.isArray(list)) return '`announce-list` is not a list';
|
|
59
|
-
|
|
60
|
-
for (let item of list) {
|
|
61
|
-
if (!Array.isArray(item)) {
|
|
62
|
-
return '`announce-list` is not a list of lists';
|
|
63
|
-
}
|
|
64
|
-
for (let ann of item) {
|
|
65
|
-
if (!Buffer.isBuffer(ann)) {
|
|
66
|
-
return 'Item in `announce-list` is not a buffer';
|
|
67
|
-
}
|
|
68
|
-
if (!util.isURL(ann)) {
|
|
69
|
-
return 'Item in `announce-list` list is not a URL';
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return null;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Check if the creation date exists and is an integer.
|
|
80
|
-
*
|
|
81
|
-
* @param {number} date
|
|
82
|
-
* @return {!string} Possible error
|
|
83
|
-
*/
|
|
84
|
-
const checkCreationDate = (date) => {
|
|
85
|
-
return date != null && !util.isInteger(date) ?
|
|
86
|
-
'`creation date` is not an integer' : null;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Check md5 sum of torrent is correct.
|
|
92
|
-
*
|
|
93
|
-
* @param {Object} parent Info metadata of torrent or file object if
|
|
94
|
-
* in multi file mode.
|
|
95
|
-
* @return {!string} Possible error
|
|
96
|
-
*/
|
|
97
|
-
const md5sumRegexp = /^[a-f0-9]{32}$/i;
|
|
98
|
-
const checkmd5sum = (parent) => {
|
|
99
|
-
if (parent.md5sum != null) {
|
|
100
|
-
if (!Buffer.isBuffer(parent.md5sum)) return '`md5sum` is not a buffer';
|
|
101
|
-
if (!md5sumRegexp.test(parent.md5sum)) {
|
|
102
|
-
return '`md5sum` is not a 32 length hex in file';
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return null;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Converts all buffers in the torrent object to utf8 strings.
|
|
111
|
-
* Except for the key `info.pieces` which will remain a buffer.
|
|
112
|
-
*
|
|
113
|
-
* @param {Object} torrent
|
|
114
|
-
*/
|
|
115
|
-
const buf2str = (torrent, path) => {
|
|
116
|
-
Object.keys(torrent).forEach((key) => {
|
|
117
|
-
const val = torrent[key];
|
|
118
|
-
|
|
119
|
-
if (Buffer.isBuffer(val)) {
|
|
120
|
-
if (path !== '.info' || key !== 'pieces') {
|
|
121
|
-
torrent[key] = val.toString('utf8');
|
|
122
|
-
}
|
|
123
|
-
} else if (typeof val === 'object') {
|
|
124
|
-
buf2str(val, path + '.' + key);
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Checks torrent metadata is correct.
|
|
132
|
-
*
|
|
133
|
-
* @param {Object} torrent
|
|
134
|
-
* @return {!string} Possible error with the torrent object
|
|
135
|
-
*/
|
|
136
|
-
const checkTorrent = exports.checkTorrent = (torrent) => {
|
|
137
|
-
let err;
|
|
138
|
-
|
|
139
|
-
if (typeof torrent !== 'object' || torrent === null) return 'Torrent is not a hash';
|
|
140
|
-
if ((err = checkAnnounce(torrent, 'announce'))) return err;
|
|
141
|
-
if ((err = checkAnnounceList(torrent['announce-list']))) return err;
|
|
142
|
-
if ((err = checkCreationDate(torrent['creation date']))) return err;
|
|
143
|
-
if ((err = checkBuffer(torrent, 'comment'))) return err;
|
|
144
|
-
if ((err = checkBuffer(torrent, 'created by'))) return err;
|
|
145
|
-
if ((err = checkBuffer(torrent, 'encoding'))) return err;
|
|
146
|
-
|
|
147
|
-
if (torrent.info == null) return '`info` field not found';
|
|
148
|
-
|
|
149
|
-
// Multi file mode.
|
|
150
|
-
if (torrent.info.files != null) {
|
|
151
|
-
if (!Array.isArray(torrent.info.files)) return '`info.files` is not a list';
|
|
152
|
-
for (let file of torrent.info.files) {
|
|
153
|
-
if (file.length == null) {
|
|
154
|
-
return '`length` field not found in file';
|
|
155
|
-
}
|
|
156
|
-
if (!util.isInteger(file.length) || file.length < 0) {
|
|
157
|
-
return '`length` is not a positive integer in file';
|
|
158
|
-
}
|
|
159
|
-
if ((err = checkmd5sum(file))) return err;
|
|
160
|
-
if (!file.path) return '`path` field not found in file';
|
|
161
|
-
if (!Array.isArray(file.path)) return '`path` is not a list in file';
|
|
162
|
-
if (file.path.length === 0) return '`path` is empty in file';
|
|
163
|
-
|
|
164
|
-
if (!file.path.every(Buffer.isBuffer)) {
|
|
165
|
-
return '`path` is not a list of buffers in file';
|
|
166
|
-
}
|
|
167
|
-
if (!file.path.every(isValidPathSegment)) {
|
|
168
|
-
return '`path` contains invalid path segments in file';
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
if (torrent.info.length != null) {
|
|
172
|
-
return 'Cannot have `info.length` in multi file mode';
|
|
173
|
-
}
|
|
174
|
-
if (torrent.info.md5sum != null) {
|
|
175
|
-
return 'Cannot have `info.md5sum` in multi file mode';
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Single file mode.
|
|
179
|
-
} else {
|
|
180
|
-
if (torrent.info.length == null) {
|
|
181
|
-
return '`info.length` not found in single file mode';
|
|
182
|
-
}
|
|
183
|
-
if (!util.isInteger(torrent.info.length) || torrent.info.length < 0) {
|
|
184
|
-
return '`info.length` is not a positive integer in file';
|
|
185
|
-
}
|
|
186
|
-
if ((err = checkmd5sum(torrent.info))) return err;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (torrent.info.name == null) return '`info.name` not found';
|
|
190
|
-
if ((err = checkBuffer(torrent.info, 'name'))) return err;
|
|
191
|
-
if (!isValidPathSegment(torrent.info.name)) {
|
|
192
|
-
return '`name` contains invalid characters';
|
|
193
|
-
}
|
|
194
|
-
if (torrent.info['piece length'] == null) {
|
|
195
|
-
return '`info.piece length` not found';
|
|
196
|
-
}
|
|
197
|
-
const pieceLength = torrent.info['piece length'];
|
|
198
|
-
if (!util.isInteger(pieceLength) || pieceLength <= 0) {
|
|
199
|
-
return '`info.piece length` is not a positive integer';
|
|
200
|
-
}
|
|
201
|
-
if (pieceLength < 16384 || (pieceLength & (pieceLength - 1)) !== 0) {
|
|
202
|
-
return '`info.piece length` must be a power of 2 and at least 16384';
|
|
203
|
-
}
|
|
204
|
-
if (torrent.info.pieces == null) return '`info.pieces` not found';
|
|
205
|
-
if (!Buffer.isBuffer(torrent.info.pieces)) {
|
|
206
|
-
return '`info.pieces` is not a buffer';
|
|
207
|
-
}
|
|
208
|
-
if (torrent.info.pieces.length % 20 !== 0) {
|
|
209
|
-
return '`info.pieces` length is not divisible by 20';
|
|
210
|
-
}
|
|
211
|
-
if (torrent.info.private != null &&
|
|
212
|
-
torrent.info.private !== 0 && torrent.info.private !== 1) {
|
|
213
|
-
return '`info.private` can only be 0 or 1';
|
|
214
|
-
}
|
|
215
|
-
if ((err = checkBuffer(torrent.info, 'source'))) return err;
|
|
216
|
-
return null;
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Checks a torrent object, makes sure its metadata matches the schema.
|
|
222
|
-
*
|
|
223
|
-
* @param {Object} torrent
|
|
224
|
-
* @param {Function(!Error, Torrent)} callback
|
|
225
|
-
*/
|
|
226
|
-
exports.validate = (torrent, callback) => {
|
|
227
|
-
let errmsg = checkTorrent(torrent);
|
|
228
|
-
if (errmsg !== null) {
|
|
229
|
-
let err = Error(errmsg);
|
|
230
|
-
err.name = 'SchemaError';
|
|
231
|
-
return callback(err);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Convert all buffers into strings that are not hash pieces.
|
|
235
|
-
buf2str(torrent, '');
|
|
236
|
-
|
|
237
|
-
// Put metadata into torrent object and call callback with the result.
|
|
238
|
-
callback(null, new Torrent(torrent));
|
|
239
|
-
};
|
package/lib/torrent.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
const crypto = require('crypto');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const b = require('bncode');
|
|
4
|
-
const MemoryStream = require('memorystream');
|
|
5
|
-
|
|
6
|
-
const Hasher = require('./hasher');
|
|
7
|
-
const buffersMatch = require('./util').buffersMatch;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
module.exports = class Torrent {
|
|
11
|
-
/**
|
|
12
|
-
* @constructor
|
|
13
|
-
* @param {Object} metadata
|
|
14
|
-
*/
|
|
15
|
-
constructor(metadata) {
|
|
16
|
-
this.metadata = metadata;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @return {string} Returns info hash of torrent
|
|
22
|
-
*/
|
|
23
|
-
infoHash() {
|
|
24
|
-
return crypto
|
|
25
|
-
.createHash('sha1')
|
|
26
|
-
.update(b.encode(this.metadata.info))
|
|
27
|
-
.digest('hex');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Creates a readable stream that emits raw torrent data.
|
|
33
|
-
*
|
|
34
|
-
* @return {ReadableStream}
|
|
35
|
-
*/
|
|
36
|
-
createReadStream() {
|
|
37
|
-
const memStream = new MemoryStream();
|
|
38
|
-
memStream.readable = true;
|
|
39
|
-
|
|
40
|
-
// Bencode data and emit it from readstream.
|
|
41
|
-
const data = b.encode(this.metadata);
|
|
42
|
-
|
|
43
|
-
process.nextTick(() => {
|
|
44
|
-
memStream.write(data, () => {
|
|
45
|
-
memStream.emit('end');
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
return memStream;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Shortcut to pipe the readable stream created by Torrent#createReadStream
|
|
55
|
-
* to a writable stream of a file, then return it.
|
|
56
|
-
*
|
|
57
|
-
* @param {string} path
|
|
58
|
-
* @param {Object} options
|
|
59
|
-
* @return {WritableStream}
|
|
60
|
-
*/
|
|
61
|
-
createWriteStream(path, options) {
|
|
62
|
-
const rs = this.createReadStream();
|
|
63
|
-
const ws = fs.createWriteStream(path, options);
|
|
64
|
-
rs.pipe(ws);
|
|
65
|
-
return ws;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Hash checks torrent.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} dir Directory where files are.
|
|
73
|
-
* @param {Object} options
|
|
74
|
-
* @return {Hasher}
|
|
75
|
-
*/
|
|
76
|
-
hashCheck(dir, options) {
|
|
77
|
-
options = options || {};
|
|
78
|
-
|
|
79
|
-
const info = this.metadata.info;
|
|
80
|
-
|
|
81
|
-
// Check if this is a single or multi file mode torrent.
|
|
82
|
-
let files = info.files || [{ path: [info.name], length: info.length }];
|
|
83
|
-
|
|
84
|
-
// Call the hasher.
|
|
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
|
-
|
|
91
|
-
hasher.on('hash', (index, hash, file, position, length) => {
|
|
92
|
-
// Check that hash matches.
|
|
93
|
-
if (buffersMatch(info.pieces, hash, index * 20)) {
|
|
94
|
-
percentMatched = Math.round(++piecesMatched / pieces * 10000) / 100;
|
|
95
|
-
hasher.emit('match', index, hash, percentMatched, file, position, length);
|
|
96
|
-
} else {
|
|
97
|
-
hasher.emit('matcherror', index, file, position, length);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return hasher;
|
|
102
|
-
}
|
|
103
|
-
};
|