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/dist/hasher.js ADDED
@@ -0,0 +1,827 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.isStreamEntry = exports.isReadableStream = void 0;
40
+ const node_crypto_1 = __importDefault(require("node:crypto"));
41
+ const node_fs_1 = __importDefault(require("node:fs"));
42
+ const node_http_1 = require("node:http");
43
+ const node_path_1 = __importDefault(require("node:path"));
44
+ const node_stream_1 = require("node:stream");
45
+ const async_1 = require("async");
46
+ const streamspeed_1 = __importDefault(require("streamspeed"));
47
+ const queue_1 = __importDefault(require("./queue"));
48
+ const schema_1 = require("./schema");
49
+ const util = __importStar(require("./util"));
50
+ /**
51
+ * defaults
52
+ */
53
+ const PIECE_LENGTH = 262144; // 256kb
54
+ const MAX_FILES_OPENED = 250;
55
+ /**
56
+ * Hashes a buffer.
57
+ */
58
+ const sha1 = (buffer) => {
59
+ return node_crypto_1.default.createHash('sha1').update(buffer).digest();
60
+ };
61
+ /**
62
+ * Rounds a number to given decimal place.
63
+ */
64
+ const round = (num, dec) => {
65
+ const pow = 10 ** dec;
66
+ return Math.round(num * pow) / pow;
67
+ };
68
+ const isReadableStream = (val) => typeof val === 'object' &&
69
+ val !== null &&
70
+ typeof val.pipe === 'function';
71
+ exports.isReadableStream = isReadableStream;
72
+ const isStreamEntry = (val) => typeof val === 'object' && val !== null && 'stream' in val;
73
+ exports.isStreamEntry = isStreamEntry;
74
+ const isStreamInput = (item) => (0, exports.isReadableStream)(item) || (0, exports.isStreamEntry)(item);
75
+ /**
76
+ * Infers a path from a stream that knows where its data comes from,
77
+ * such as an `fs.ReadStream`.
78
+ */
79
+ const inferStreamPath = (stream) => {
80
+ const streamPath = stream.path;
81
+ if (typeof streamPath === 'string')
82
+ return [node_path_1.default.basename(streamPath)];
83
+ if (Buffer.isBuffer(streamPath)) {
84
+ return [node_path_1.default.basename(streamPath.toString())];
85
+ }
86
+ return null;
87
+ };
88
+ const getReadStreamLength = (stream) => {
89
+ // A partially read stream no longer represents its full file.
90
+ if (stream.bytesRead > 0 || !stream.path)
91
+ return Promise.resolve(undefined);
92
+ return new Promise((resolve) => {
93
+ node_fs_1.default.stat(stream.path, (err, stat) => {
94
+ if (err || !stat.isFile()) {
95
+ resolve(undefined);
96
+ return;
97
+ }
98
+ const range = stream;
99
+ const start = typeof range.start === 'number' ? range.start : 0;
100
+ const end = Number.isFinite(range.end)
101
+ ? range.end
102
+ : stat.size - 1;
103
+ const lastByte = Math.min(end, stat.size - 1);
104
+ resolve(Math.max(0, lastByte - start + 1));
105
+ });
106
+ });
107
+ };
108
+ /**
109
+ * Infers a stream's length when that information is cheap and reliable.
110
+ * `fs.ReadStream` lengths come from the file size, adjusted for any
111
+ * `start`/`end` range. HTTP responses can use `Content-Length`.
112
+ */
113
+ const inferStreamLength = (stream) => {
114
+ if (stream instanceof node_fs_1.default.ReadStream)
115
+ return getReadStreamLength(stream);
116
+ if (stream instanceof node_http_1.IncomingMessage) {
117
+ const header = stream.headers['content-length'];
118
+ if (typeof header === 'string') {
119
+ const length = Number.parseInt(header, 10);
120
+ if (Number.isInteger(length) && length >= 0) {
121
+ return Promise.resolve(length);
122
+ }
123
+ }
124
+ }
125
+ return Promise.resolve(undefined);
126
+ };
127
+ /**
128
+ * Allows arrays of objects with a `path` property to be
129
+ * sorted alphabetically
130
+ */
131
+ const sortByPath = (a, b) => {
132
+ const len = Math.min(a.path.length, b.path.length);
133
+ for (let i = 0; i < len; i++) {
134
+ if (a.path[i] < b.path[i])
135
+ return -1;
136
+ if (a.path[i] > b.path[i])
137
+ return 1;
138
+ }
139
+ return a.path.length - b.path.length;
140
+ };
141
+ /**
142
+ * Builds a torrent's file list and hashes it into pieces.
143
+ *
144
+ * Inputs are first resolved and validated, then piece offsets are planned. When
145
+ * every input length is known, files are read in parallel. If any stream length
146
+ * is unknown, hashing switches to a single sequential pass because each input's
147
+ * starting offset depends on everything before it.
148
+ */
149
+ class Hasher extends node_stream_1.Stream {
150
+ pieceLength;
151
+ maxFiles;
152
+ stopOnErr;
153
+ dir;
154
+ files;
155
+ pieces;
156
+ ready;
157
+ hashing;
158
+ paused;
159
+ stopped;
160
+ readable;
161
+ err;
162
+ filetasks;
163
+ totalSize;
164
+ lastPieceLength;
165
+ streamGroup;
166
+ unknownLengths;
167
+ /**
168
+ * Resolves the inputs and prepares the hasher.
169
+ */
170
+ constructor(dir, list, pieceLength, options) {
171
+ super();
172
+ options = options || {};
173
+ // Set defaults.
174
+ this.pieceLength = pieceLength || PIECE_LENGTH;
175
+ this.maxFiles = options.maxFiles || MAX_FILES_OPENED;
176
+ this.stopOnErr = options.stopOnErr || false;
177
+ // Init arrays that will be used every time during hashing.
178
+ this.dir = dir;
179
+ this.files = [];
180
+ this.pieces = 0;
181
+ this.ready = false;
182
+ this.hashing = false;
183
+ this.paused = false;
184
+ this.stopped = false;
185
+ this.unknownLengths = false;
186
+ this.readable = false;
187
+ this.filetasks = [];
188
+ this.totalSize = 0;
189
+ this.lastPieceLength = 0;
190
+ this.streamGroup = new streamspeed_1.default();
191
+ if (this.stopOnErr) {
192
+ this.once('error', (err) => {
193
+ this.err = err;
194
+ });
195
+ }
196
+ if (list.some(isStreamInput)) {
197
+ this._resolveInputs(list).then((items) => this._beginHashing(items), (err) => this._emitError(err));
198
+ return;
199
+ }
200
+ this._beginPathInputs(list);
201
+ }
202
+ // ---------------------------------------------------------------------------
203
+ // Input resolution
204
+ // ---------------------------------------------------------------------------
205
+ /**
206
+ * Reports an error on the next tick so callers can attach listeners.
207
+ */
208
+ _emitError(err) {
209
+ process.nextTick(() => this.emit('error', err));
210
+ }
211
+ /**
212
+ * Resolves streams and paths into a single ordered list of torrent entries.
213
+ *
214
+ * Unlike pure path lists (which are sorted globally), the order of the input
215
+ * list is authoritative here. Directories are expanded in place and their
216
+ * entries are sorted within that directory.
217
+ */
218
+ async _resolveInputs(list) {
219
+ const baseName = node_path_1.default.basename(this.dir) || 'stream';
220
+ const items = [];
221
+ for (const input of list) {
222
+ if ((0, exports.isReadableStream)(input)) {
223
+ items.push(this._streamItem(input, baseName));
224
+ }
225
+ else if ((0, exports.isStreamEntry)(input)) {
226
+ items.push(this._streamEntryItem(input, baseName));
227
+ }
228
+ else if (typeof input === 'string') {
229
+ await this._collectPath(input, items);
230
+ }
231
+ else {
232
+ throw new Error('Mixed lists can only contain file paths and streams');
233
+ }
234
+ }
235
+ await Promise.all(items.map(async (item) => {
236
+ if ('stream' in item && item.length === undefined) {
237
+ item.length = await inferStreamLength(item.stream);
238
+ }
239
+ }));
240
+ this._validateItems(items);
241
+ this._assignNames(items);
242
+ return items;
243
+ }
244
+ /**
245
+ * Builds a torrent entry for a bare readable stream.
246
+ */
247
+ _streamItem(stream, baseName) {
248
+ return {
249
+ stream,
250
+ path: inferStreamPath(stream) ?? [baseName],
251
+ autoNamed: true,
252
+ };
253
+ }
254
+ /**
255
+ * Builds a torrent entry for a `{ stream, path, length }` input.
256
+ */
257
+ _streamEntryItem(entry, baseName) {
258
+ const rawPath = entry.path;
259
+ let itemPath;
260
+ if (rawPath == null) {
261
+ itemPath = inferStreamPath(entry.stream) ?? [baseName];
262
+ }
263
+ else if (Array.isArray(rawPath)) {
264
+ itemPath = rawPath;
265
+ }
266
+ else {
267
+ itemPath = util.splitPath(rawPath);
268
+ }
269
+ return {
270
+ stream: entry.stream,
271
+ path: itemPath,
272
+ length: entry.length,
273
+ autoNamed: rawPath == null,
274
+ };
275
+ }
276
+ /**
277
+ * Validates an input path, then expands it into files in list order.
278
+ */
279
+ async _collectPath(input, items) {
280
+ const relPath = node_path_1.default.normalize(input);
281
+ if (node_path_1.default.isAbsolute(relPath) || relPath.split(node_path_1.default.sep).includes('..')) {
282
+ throw new Error(`Path traversal detected: ${relPath}`);
283
+ }
284
+ await this._walkPath(relPath, items);
285
+ }
286
+ /**
287
+ * Recursively walks a path. Directories are expanded in sorted order.
288
+ */
289
+ async _walkPath(relPath, items) {
290
+ const fullPath = node_path_1.default.join(this.dir, relPath);
291
+ const stat = await node_fs_1.default.promises.stat(fullPath);
292
+ if (stat.isFile()) {
293
+ items.push({
294
+ path: util.splitPath(relPath),
295
+ length: stat.size,
296
+ filepath: fullPath,
297
+ });
298
+ return;
299
+ }
300
+ if (stat.isDirectory()) {
301
+ const names = (await node_fs_1.default.promises.readdir(fullPath)).sort();
302
+ for (const name of names) {
303
+ await this._walkPath(node_path_1.default.join(relPath, name), items);
304
+ }
305
+ }
306
+ }
307
+ /**
308
+ * Rejects invalid paths and stream lengths.
309
+ */
310
+ _validateItems(items) {
311
+ for (const item of items) {
312
+ const label = item.path.join('/');
313
+ if (item.path.length === 0 ||
314
+ !item.path.every((seg) => typeof seg === 'string' && (0, schema_1.isValidPathSegment)(Buffer.from(seg)))) {
315
+ throw new Error(`Path traversal detected: ${label}`);
316
+ }
317
+ if ('stream' in item &&
318
+ item.length !== undefined &&
319
+ (!Number.isInteger(item.length) || item.length < 0)) {
320
+ throw new Error(`Invalid stream length for "${label}": must be a non-negative integer`);
321
+ }
322
+ }
323
+ }
324
+ /**
325
+ * Reserves explicit paths and gives unnamed streams unique suffixes.
326
+ */
327
+ _assignNames(items) {
328
+ const usedPaths = new Set();
329
+ // Explicit paths, including files found on disk, must be unique.
330
+ for (const item of items) {
331
+ if (item.autoNamed)
332
+ continue;
333
+ const key = item.path.join('/');
334
+ if (usedPaths.has(key)) {
335
+ throw new Error(`Duplicate path: ${key}`);
336
+ }
337
+ usedPaths.add(key);
338
+ }
339
+ // Unnamed streams avoid both disk paths and other stream paths.
340
+ for (const item of items) {
341
+ if (!item.autoNamed)
342
+ continue;
343
+ const basePath = item.path;
344
+ let candidate = basePath;
345
+ let suffix = 0;
346
+ while (usedPaths.has(candidate.join('/'))) {
347
+ suffix++;
348
+ candidate = [
349
+ ...basePath.slice(0, -1),
350
+ `${basePath[basePath.length - 1]}-${suffix}`,
351
+ ];
352
+ }
353
+ item.path = candidate;
354
+ usedPaths.add(candidate.join('/'));
355
+ }
356
+ // A file path cannot also be a directory prefix of another path.
357
+ for (const item of items) {
358
+ for (let i = 1; i < item.path.length; i++) {
359
+ const prefix = item.path.slice(0, i).join('/');
360
+ if (usedPaths.has(prefix)) {
361
+ throw new Error(`Path conflict: ${prefix}`);
362
+ }
363
+ }
364
+ }
365
+ }
366
+ /**
367
+ * Starts reading pure path inputs. Paths are sorted so the resulting torrent
368
+ * does not depend on filesystem iteration order.
369
+ */
370
+ _beginPathInputs(list) {
371
+ node_fs_1.default.access(this.dir, (err) => {
372
+ if (err) {
373
+ this.emit('error', err);
374
+ return;
375
+ }
376
+ if (typeof list[0] === 'string') {
377
+ this._loadPaths(list);
378
+ }
379
+ else {
380
+ this._useFileEntries(list);
381
+ }
382
+ });
383
+ }
384
+ /**
385
+ * Stats and expands paths, then plans the piece layout.
386
+ */
387
+ _loadPaths(paths) {
388
+ // Limit how many files are opened while walking the tree.
389
+ const statqueue = (0, queue_1.default)((filepath, cb) => {
390
+ node_fs_1.default.stat(filepath, cb);
391
+ }, this.maxFiles);
392
+ this._getFilesStats(statqueue, paths, (err) => {
393
+ // Sort even on failure so partially collected files are deterministic.
394
+ this.files = Array.from(this.files).sort(sortByPath);
395
+ if (err) {
396
+ this.emit('error', err);
397
+ return;
398
+ }
399
+ this._CreateFileTasks();
400
+ this._readyAndStart(() => this._start());
401
+ });
402
+ }
403
+ /**
404
+ * Uses an already-known file list, such as one from an existing torrent.
405
+ * Order is preserved because it determines the piece hashes.
406
+ */
407
+ _useFileEntries(entries) {
408
+ this.files = Array.from(entries);
409
+ process.nextTick(() => {
410
+ this._CreateFileTasks();
411
+ this._readyAndStart(() => this._start());
412
+ });
413
+ }
414
+ /**
415
+ * Starts hashing a resolved stream/path list.
416
+ *
417
+ * When every length is known, piece offsets can be planned up front and files
418
+ * are hashed in parallel. If any stream length is unknown, hashing falls back
419
+ * to a single sequential pass because later offsets cannot be known yet.
420
+ */
421
+ _beginHashing(items) {
422
+ if (items.some((item) => item.length === undefined)) {
423
+ this.unknownLengths = true;
424
+ this.files = [];
425
+ this._readyAndStart(() => this._startSequential(items));
426
+ return;
427
+ }
428
+ this.files = items.map((item) => ({
429
+ length: item.length,
430
+ path: item.path,
431
+ }));
432
+ this._CreateFileTasks();
433
+ // Attach stream-backed tasks; path-backed tasks are opened when hashed.
434
+ items.forEach((item, i) => {
435
+ if ('stream' in item)
436
+ this.filetasks[i].stream = item.stream;
437
+ });
438
+ this._readyAndStart(() => this._start());
439
+ }
440
+ /**
441
+ * Marks the hasher ready, then starts hashing on the next tick.
442
+ */
443
+ _readyAndStart(start) {
444
+ process.nextTick(() => {
445
+ this.ready = true;
446
+ this.emit('ready');
447
+ start();
448
+ });
449
+ }
450
+ // ---------------------------------------------------------------------------
451
+ // Piece planning
452
+ // ---------------------------------------------------------------------------
453
+ /**
454
+ * Plans which pieces each file covers.
455
+ *
456
+ * Files are laid out consecutively, so a file may start partway into a piece
457
+ * and may span several pieces. Each task records its first piece and the
458
+ * offset within that piece.
459
+ */
460
+ _CreateFileTasks() {
461
+ // Variables to help calculate piece position.
462
+ let totalSize = 0;
463
+ let piece = 0;
464
+ let offset = 0;
465
+ // Create tasks to figure out what pieces belong to exactly
466
+ // what files and what part of the files.
467
+ this.filetasks = [];
468
+ this.files.forEach((task) => {
469
+ this.filetasks.push({
470
+ length: task.length,
471
+ path: task.path,
472
+ // Piece this task will read from or write to.
473
+ piece,
474
+ // Piece buffer offset to start reading or writing at.
475
+ offset,
476
+ });
477
+ // Add file length to total size.
478
+ totalSize += task.length;
479
+ // Get piece next task will write to and its offset.
480
+ piece = Math.floor(totalSize / this.pieceLength);
481
+ offset = totalSize - this.pieceLength * piece;
482
+ });
483
+ this.totalSize = totalSize;
484
+ this.pieces = Math.ceil(totalSize / this.pieceLength);
485
+ // The last piece holds the leftover bytes, or a full piece when the total
486
+ // size divides evenly. A zero-byte torrent has no pieces.
487
+ this.lastPieceLength =
488
+ this.pieces === 0 ? 0 : totalSize - this.pieceLength * (this.pieces - 1);
489
+ }
490
+ // ---------------------------------------------------------------------------
491
+ // Hashing
492
+ // ---------------------------------------------------------------------------
493
+ /**
494
+ * Hashes a fully planned file list in parallel.
495
+ *
496
+ * Only used when every input length is known, since piece offsets must be
497
+ * calculated before reading starts.
498
+ */
499
+ _start() {
500
+ if (!this.ready) {
501
+ this.emit('error', new Error('Cannot start before being ready'));
502
+ return;
503
+ }
504
+ // A ready listener may have reported an error, such as no files found.
505
+ if (this.err)
506
+ return;
507
+ this.hashing = true;
508
+ this.stopped = false;
509
+ // Create piece objects.
510
+ const pieces = [];
511
+ for (let i = 0; i < this.pieces; i++) {
512
+ pieces[i] = {
513
+ buffer: null,
514
+ length: this.pieceLength,
515
+ bytesWritten: 0,
516
+ };
517
+ }
518
+ const lastPiece = pieces[pieces.length - 1];
519
+ if (lastPiece) {
520
+ lastPiece.length = this.lastPieceLength;
521
+ }
522
+ // Keep track of progress.
523
+ let totalRead = 0;
524
+ // Keep track of reading speed.
525
+ this.streamGroup = new streamspeed_1.default();
526
+ let lastPercent = 0;
527
+ let speed = 0;
528
+ let avg = 0;
529
+ this.streamGroup.on('speed', (s, a) => {
530
+ speed = s;
531
+ avg = a;
532
+ });
533
+ const filesqueue = (0, queue_1.default)((task, callback) => {
534
+ if (this.err || this.stopped)
535
+ return callback();
536
+ // Keep track how much has been read.
537
+ let readSoFar = 0;
538
+ let pieceIndex = task.piece;
539
+ let finished = false;
540
+ const finish = (err) => {
541
+ if (finished)
542
+ return;
543
+ finished = true;
544
+ if (err && !this.err)
545
+ this.emit('error', err);
546
+ callback();
547
+ };
548
+ // Open file for reading.
549
+ const baseDir = node_path_1.default.resolve(this.dir);
550
+ const file = node_path_1.default.resolve(baseDir, ...task.path);
551
+ const relative = node_path_1.default.relative(baseDir, file);
552
+ if (relative.startsWith('..') ||
553
+ node_path_1.default.isAbsolute(relative) ||
554
+ !task.path.every((seg) => (0, schema_1.isValidPathSegment)(Buffer.from(seg)))) {
555
+ finish(new Error(`Path traversal detected: ${file}`));
556
+ return;
557
+ }
558
+ const rs = task.stream || node_fs_1.default.createReadStream(file);
559
+ this.streamGroup.add(rs);
560
+ rs.on('open', () => {
561
+ this.emit('open', file);
562
+ });
563
+ rs.on('data', (chunk) => {
564
+ if (finished)
565
+ return;
566
+ if (this.err) {
567
+ rs.destroy();
568
+ finish();
569
+ return;
570
+ }
571
+ const data = chunk;
572
+ if (readSoFar + data.length > task.length) {
573
+ rs.destroy();
574
+ finish(new Error(`Stream length mismatch for "${file}": expected ${task.length} bytes but received more`));
575
+ return;
576
+ }
577
+ // A chunk can begin partway through a piece and may span several.
578
+ // Recalculate the current piece from the task's starting offset.
579
+ pieceIndex =
580
+ task.piece + Math.floor((task.offset + readSoFar) / this.pieceLength);
581
+ // Write the chunk into its piece buffer(s).
582
+ const targetStart = (task.offset + readSoFar) % this.pieceLength;
583
+ let sourceWritten = this._writePiece(pieces, pieceIndex, targetStart, data, 0, data.length, file, readSoFar);
584
+ while (sourceWritten < data.length) {
585
+ pieceIndex += 1;
586
+ sourceWritten += this._writePiece(pieces, pieceIndex, 0, data, sourceWritten, data.length, file, readSoFar);
587
+ }
588
+ // Update amount of bytes written to buffer.
589
+ readSoFar += data.length;
590
+ totalRead += data.length;
591
+ // Emit hash event with piece info and progress.
592
+ const percent = round((totalRead / this.totalSize) * 100, 2);
593
+ if (lastPercent !== percent) {
594
+ this.emit('progress', percent, speed, avg);
595
+ lastPercent = percent;
596
+ }
597
+ });
598
+ // Close this file when there is an error or finished reading.
599
+ rs.on('error', (err) => {
600
+ finish(err);
601
+ });
602
+ rs.on('end', () => {
603
+ if (finished)
604
+ return;
605
+ if (readSoFar !== task.length) {
606
+ finish(new Error(`Stream length mismatch for "${file}": expected ${task.length} bytes but received ${readSoFar}`));
607
+ return;
608
+ }
609
+ finish();
610
+ });
611
+ }, this.maxFiles);
612
+ // When all file queues are finished, join all the pieces.
613
+ filesqueue.drain = () => {
614
+ this.hashing = false;
615
+ if (this.err)
616
+ return;
617
+ this.emit('end');
618
+ };
619
+ // Start queueing jobs.
620
+ this.filetasks.forEach((filetask) => {
621
+ filesqueue.push(filetask);
622
+ });
623
+ }
624
+ /**
625
+ * Hashes inputs one at a time in list order.
626
+ *
627
+ * Used whenever any stream length is unknown: later piece offsets cannot be
628
+ * planned until earlier inputs have been read, so parallelism is not possible.
629
+ * File entries are recorded as each input finishes, which lets `make` build
630
+ * metadata once the whole list has been consumed.
631
+ */
632
+ _startSequential(items) {
633
+ this.hashing = true;
634
+ this.stopped = false;
635
+ this.streamGroup = new streamspeed_1.default();
636
+ this.totalSize = 0;
637
+ const pieceBuffer = Buffer.alloc(this.pieceLength);
638
+ let bytesWritten = 0;
639
+ let pieceIndex = 0;
640
+ const filesqueue = (0, queue_1.default)((item, callback) => {
641
+ if (this.err || this.stopped)
642
+ return callback();
643
+ let fileLength = 0;
644
+ let finished = false;
645
+ const file = node_path_1.default.join(this.dir, ...item.path);
646
+ const rs = 'stream' in item ? item.stream : node_fs_1.default.createReadStream(item.filepath);
647
+ this.streamGroup.add(rs);
648
+ const finish = (err) => {
649
+ if (finished)
650
+ return;
651
+ finished = true;
652
+ if (err && !this.err)
653
+ this.emit('error', err);
654
+ callback();
655
+ };
656
+ rs.on('data', (chunk) => {
657
+ if (finished)
658
+ return;
659
+ const data = chunk;
660
+ fileLength += data.length;
661
+ this.totalSize += data.length;
662
+ // Fill the current piece buffer. Whenever it fills, hash it and
663
+ // continue with the rest of the chunk in the next piece.
664
+ let offset = 0;
665
+ while (offset < data.length) {
666
+ const size = Math.min(data.length - offset, this.pieceLength - bytesWritten);
667
+ data.copy(pieceBuffer, bytesWritten, offset, offset + size);
668
+ bytesWritten += size;
669
+ offset += size;
670
+ if (bytesWritten === this.pieceLength) {
671
+ const buf = Buffer.from(sha1(pieceBuffer));
672
+ this.emit('hash', pieceIndex, buf, file, fileLength - data.length + offset - size, size);
673
+ pieceIndex++;
674
+ bytesWritten = 0;
675
+ }
676
+ }
677
+ });
678
+ rs.on('error', (err) => {
679
+ finish(err);
680
+ });
681
+ rs.on('end', () => {
682
+ if (finished)
683
+ return;
684
+ if (item.length !== undefined && fileLength !== item.length) {
685
+ finish(new Error(`Stream length mismatch for "${file}": expected ${item.length} bytes but received ${fileLength}`));
686
+ return;
687
+ }
688
+ this.files.push({
689
+ length: fileLength,
690
+ path: item.path,
691
+ });
692
+ finish();
693
+ });
694
+ }, 1);
695
+ filesqueue.drain = () => {
696
+ this.hashing = false;
697
+ if (this.err)
698
+ return;
699
+ // Hash the final partial piece, if there is one. When the total size
700
+ // divides evenly, the last full piece was already emitted above.
701
+ if (bytesWritten > 0) {
702
+ const lastSlice = pieceBuffer.subarray(0, bytesWritten);
703
+ const buf = Buffer.from(sha1(lastSlice));
704
+ const lastFile = node_path_1.default.join(this.dir, ...this.files[this.files.length - 1].path);
705
+ this.emit('hash', pieceIndex, buf, lastFile, 0, bytesWritten);
706
+ this.lastPieceLength = bytesWritten;
707
+ pieceIndex++;
708
+ }
709
+ else {
710
+ this.lastPieceLength = this.totalSize === 0 ? 0 : this.pieceLength;
711
+ }
712
+ this.pieces = pieceIndex;
713
+ this.emit('end');
714
+ };
715
+ items.forEach((item) => {
716
+ filesqueue.push(item);
717
+ });
718
+ }
719
+ _writePiece(pieces, index, offset, data, start, end, file, readSoFar) {
720
+ const piece = pieces[index];
721
+ if (!piece)
722
+ return 0;
723
+ if (piece.buffer === null) {
724
+ piece.buffer = Buffer.alloc(piece.length);
725
+ }
726
+ // Write data to piece buffer and update bytes written.
727
+ const size = Math.min(end - start, piece.length - offset);
728
+ piece.bytesWritten += size;
729
+ data.copy(piece.buffer, offset, start, start + size);
730
+ // Check if piece buffer is full.
731
+ if (piece.bytesWritten === piece.length) {
732
+ pieces[index] = null;
733
+ const buf = Buffer.from(sha1(piece.buffer));
734
+ this.emit('hash', index, buf, file, readSoFar, size);
735
+ }
736
+ return size;
737
+ }
738
+ /**
739
+ * Recursively traverses files to call fs.stat on them.
740
+ * Will add all files in folders if any are encountered.
741
+ */
742
+ _getFilesStats(statQueue, list, callback) {
743
+ // Read all file stats in parallel.
744
+ (0, async_1.parallel)(list.map((file) => {
745
+ return this._getFileStat.bind(this, statQueue, file);
746
+ }), callback);
747
+ }
748
+ /**
749
+ * Stat is needed to know file size.
750
+ */
751
+ _getFileStat(statQueue, file, callback) {
752
+ const filepath = node_path_1.default.join(this.dir, file);
753
+ statQueue.push(filepath, (err, stat) => {
754
+ if (err || !stat)
755
+ return callback(err);
756
+ // If this is a directory, add all files in it to torrent files.
757
+ if (stat.isDirectory()) {
758
+ node_fs_1.default.readdir(filepath, (readdirErr, dirfiles) => {
759
+ if (readdirErr)
760
+ return callback(readdirErr);
761
+ const subfiles = dirfiles.map((dirfile) => node_path_1.default.join(file, dirfile));
762
+ // Call _getFilesStats again.
763
+ this._getFilesStats(statQueue, subfiles, callback);
764
+ });
765
+ }
766
+ else if (stat.isFile()) {
767
+ this.files.push({
768
+ length: stat.size,
769
+ path: util.splitPath(file),
770
+ });
771
+ callback(null);
772
+ }
773
+ else {
774
+ callback(null);
775
+ }
776
+ });
777
+ }
778
+ /**
779
+ * Allows pausing of the hashing process by storing functions from the
780
+ * file and read queues that will not get called back until resume()
781
+ * is called.
782
+ */
783
+ pause() {
784
+ if (this.paused || !this.hashing)
785
+ return false;
786
+ this.paused = true;
787
+ this.streamGroup.getStreams().forEach((rs) => {
788
+ rs.pause();
789
+ });
790
+ return true;
791
+ }
792
+ /**
793
+ * Resumes hashing.
794
+ */
795
+ resume() {
796
+ if (!this.paused)
797
+ return false;
798
+ this.paused = false;
799
+ this.streamGroup.getStreams().forEach((rs) => {
800
+ rs.resume();
801
+ });
802
+ return true;
803
+ }
804
+ /**
805
+ * Continues hashing if paused or pauses if not.
806
+ */
807
+ toggle() {
808
+ return this.paused ? this.resume() : this.pause();
809
+ }
810
+ /**
811
+ * Stops hashing completely.
812
+ * Closes file descriptors and does not emit any more events.
813
+ */
814
+ destroy() {
815
+ if (!this.hashing)
816
+ return false;
817
+ this.hashing = false;
818
+ this.stopped = true;
819
+ this.paused = false;
820
+ this.streamGroup.getStreams().forEach((rs) => {
821
+ rs.destroy();
822
+ });
823
+ return true;
824
+ }
825
+ }
826
+ exports.default = Hasher;
827
+ //# sourceMappingURL=hasher.js.map