nt 0.7.2 → 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/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,212 +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 announce fields are correct.
20
- *
21
- * @param {Object} torrent
22
- * @return {!string} Possible error
23
- */
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';
29
- }
30
- return null;
31
- };
32
-
33
-
34
- /**
35
- * Checks announce list field.
36
- *
37
- * @param {Object} torrent
38
- * @return {!string} Possible error
39
- */
40
- const checkAnnounceList = exports.announceList = (list) => {
41
- if (list) {
42
- if (!Array.isArray(list)) return '`announce-list` is not a list';
43
-
44
- for (let item of list) {
45
- if (!Array.isArray(item)) {
46
- return '`announce-list` is not a list of lists';
47
- }
48
- for (let ann of item) {
49
- if (!Buffer.isBuffer(ann)) {
50
- return 'Item in `announce-list` is not a buffer';
51
- }
52
- if (!util.isURL(ann)) {
53
- return 'Item in `announce-list` list is not a URL';
54
- }
55
- }
56
- }
57
- }
58
- return null;
59
- };
60
-
61
-
62
- /**
63
- * Check if the creation date exists and is an integer.
64
- *
65
- * @param {number} date
66
- * @return {!string} Possible error
67
- */
68
- const checkCreationDate = (date) => {
69
- return date != null && !util.isInteger(date) ?
70
- '`creation date` is not an integer' : null;
71
- };
72
-
73
-
74
- /**
75
- * Check md5 sum of torrent is correct.
76
- *
77
- * @param {Object} parent Info metadata of torrent or file object if
78
- * in multi file mode.
79
- * @return {!string} Possible error
80
- */
81
- const md5sumRegexp = /[a-f0-9]{32}/i;
82
- const checkmd5sum = (parent) => {
83
- if (parent.md5sum != null) {
84
- if (!Buffer.isBuffer(parent.md5sum)) return '`md5sum` is not a buffer';
85
- if (!md5sumRegexp.test(parent.md5sum)) {
86
- return '`md5sum` is not a 32 length hex in file';
87
- }
88
- }
89
- return null;
90
- };
91
-
92
-
93
- /**
94
- * Converts all buffers in the torrent object to utf8 strings.
95
- * Except for the key `info.pieces` which will remain a buffer.
96
- *
97
- * @param {Object} torrent
98
- */
99
- const buf2str = (torrent, path) => {
100
- Object.keys(torrent).forEach((key) => {
101
- const val = torrent[key];
102
-
103
- if (Buffer.isBuffer(val)) {
104
- if (path !== '.info' || key !== 'pieces') {
105
- torrent[key] = val.toString('utf8');
106
- }
107
- } else if (typeof val === 'object') {
108
- buf2str(val, path + '.' + key);
109
- }
110
- });
111
- };
112
-
113
-
114
- /**
115
- * Checks torrent metadata is correct.
116
- *
117
- * @param {Object} torrent
118
- * @return {!string} Possible error with the torrent object
119
- */
120
- const checkTorrent = exports.checkTorrent = (torrent) => {
121
- let err;
122
-
123
- if (typeof torrent !== 'object') return 'Torrent is not hash';
124
- if ((err = checkAnnounce(torrent, 'announce'))) return err;
125
- if ((err = checkAnnounceList(torrent['announce-list']))) return err;
126
- if ((err = checkCreationDate(torrent['creation date']))) return err;
127
- if ((err = checkBuffer(torrent, 'comment'))) return err;
128
- if ((err = checkBuffer(torrent, 'created by'))) return err;
129
- if ((err = checkBuffer(torrent, 'encoding'))) return err;
130
-
131
- if (torrent.info == null) return '`info` field not found';
132
-
133
- // Multi file mode.
134
- if (torrent.info.files != null) {
135
- if (!Array.isArray(torrent.info.files)) return '`info.files` is not a list';
136
- for (let file of torrent.info.files) {
137
- if (file.length == null) {
138
- return '`length` field not found in file';
139
- }
140
- if (!util.isInteger(file.length) || file.length < 0) {
141
- return '`length` is not a positive integer in file';
142
- }
143
- if ((err = checkmd5sum(file))) return err;
144
- if (!file.path) return '`path` field not found in file';
145
- if (!Array.isArray(file.path)) return '`path` is not a list in file';
146
-
147
- if (!file.path.some(Buffer.isBuffer)) {
148
- return '`path` is not a list of buffers in file';
149
- }
150
- }
151
- if (torrent.info.length != null) {
152
- return 'Cannot have `info.length` in multi file mode';
153
- }
154
- if (torrent.info.md5sum != null) {
155
- return 'Cannot have `info.md5sum` in multi file mode';
156
- }
157
-
158
- // Single file mode.
159
- } else {
160
- if (torrent.info.length == null) {
161
- return '`info.length` not found in single file mode';
162
- }
163
- if (!util.isInteger(torrent.info.length || torrent.info.length < 0)) {
164
- return '`info.length` is not a positive integer in file';
165
- }
166
- if ((err = checkmd5sum(torrent.info))) return err;
167
- }
168
-
169
- if ((err = checkBuffer(torrent.info, 'name'))) return err;
170
- if (torrent.info['piece length'] == null) {
171
- return '`info.piece length` not found';
172
- }
173
- if (!util.isInteger(torrent.info['piece length']) ||
174
- torrent.info['piece length'] < 0) {
175
- return '`info.piece length` is not a positive integer';
176
- }
177
- if (torrent.info.pieces == null) return '`info.pieces` not found';
178
- if (!Buffer.isBuffer(torrent.info.pieces)) {
179
- return '`info.pieces` is not a buffer';
180
- }
181
- if (torrent.info.pieces.length % 20 !== 0) {
182
- return '`info.pieces` length is not divisible by 20';
183
- }
184
- if (torrent.info.private != null &&
185
- torrent.info.private !== 0 && torrent.info.private !== 1) {
186
- return '`info.private` can only be 0 or 1';
187
- }
188
- if ((err = checkBuffer(torrent.info, 'source'))) return err;
189
- return null;
190
- };
191
-
192
-
193
- /**
194
- * Checks a torrent object, makes sure its metadata matches the schema.
195
- *
196
- * @param {Object} torrent
197
- * @param {Function(!Error, Torrent)} callback
198
- */
199
- exports.validate = (torrent, callback) => {
200
- let errmsg = checkTorrent(torrent);
201
- if (errmsg !== null) {
202
- let err = Error(errmsg);
203
- err.name = 'SchemaError';
204
- return callback(err);
205
- }
206
-
207
- // Convert all buffers into strings that are not hash pieces.
208
- buf2str(torrent, '');
209
-
210
- // Put metadata into torrent object and call callback with the result.
211
- callback(null, new Torrent(torrent));
212
- };
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
- };
package/lib/util.js DELETED
@@ -1,45 +0,0 @@
1
- const urlRegexp = /^(http|udp|ftp|dht)s?:\/\//;
2
-
3
- /**
4
- * Returns true if str is a URL.
5
- *
6
- * @param {string} str
7
- * @return {boolean}
8
- */
9
- exports.isURL = str => urlRegexp.test(str);
10
-
11
-
12
- /**
13
- * Returns true if n is an integer.
14
- *
15
- * @param {number} n
16
- * @return {boolean}
17
- */
18
- exports.isInteger = n => !isNaN(parseInt(n, 10));
19
-
20
-
21
- /**
22
- * Splits a path into an array, platform safe.
23
- *
24
- * @param {string} path
25
- * @return {Array.<string>}
26
- */
27
- exports.splitPath = path => path.split(path.sep);
28
-
29
-
30
- /**
31
- * Returns true if part of buffer `b` beginning at `start` matches buffer `a`.
32
- *
33
- * @param {Buffer} a
34
- * @param {Buffer} b
35
- * @param {number} start
36
- */
37
- exports.buffersMatch = (a, b, start) => {
38
- for (let i = 0, l = b.length; i < l; i++) {
39
- if (a[start + i] !== b[i]) {
40
- return false;
41
- }
42
- }
43
-
44
- return true;
45
- };