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.
@@ -0,0 +1,81 @@
1
+ import fs from 'node:fs';
2
+ import { Stream } from 'node:stream';
3
+ import StreamSpeed from 'streamspeed';
4
+ import { type Queue } from './queue';
5
+ export interface FileEntry {
6
+ length: number;
7
+ path: string[];
8
+ md5sum?: string;
9
+ }
10
+ export interface FileTask extends FileEntry {
11
+ piece: number;
12
+ offset: number;
13
+ }
14
+ export interface PieceBuffer {
15
+ buffer: Buffer | null;
16
+ length: number;
17
+ bytesWritten: number;
18
+ }
19
+ export interface HasherOptions {
20
+ maxFiles?: number;
21
+ stopOnErr?: boolean;
22
+ }
23
+ export default class Hasher extends Stream {
24
+ pieceLength: number;
25
+ maxFiles: number;
26
+ stopOnErr: boolean;
27
+ dir: string;
28
+ files: FileEntry[];
29
+ pieces: number;
30
+ ready: boolean;
31
+ hashing: boolean;
32
+ paused: boolean;
33
+ stopped: boolean;
34
+ readable: boolean;
35
+ err?: Error;
36
+ filetasks: FileTask[];
37
+ totalSize: number;
38
+ lastPieceLength: number;
39
+ streamGroup: StreamSpeed;
40
+ /**
41
+ * Calculates piece hashes of file list.
42
+ */
43
+ constructor(dir: string, list: string[] | FileEntry[], pieceLength?: number | null, options?: HasherOptions);
44
+ /**
45
+ * Create filetasks array
46
+ */
47
+ _CreateFileTasks(): void;
48
+ /**
49
+ * Starts hashing.
50
+ */
51
+ _start(): void;
52
+ _writePiece(pieces: Array<PieceBuffer | null>, index: number, offset: number, data: Buffer, start: number, end: number, file?: string, readSoFar?: number): number;
53
+ /**
54
+ * Recursively traverses files to call fs.stat on them.
55
+ * Will add all files in folders if any are encountered.
56
+ */
57
+ _getFilesStats(statQueue: Queue<string, fs.Stats>, list: string[], callback: (err?: Error | null) => void): void;
58
+ /**
59
+ * Stat is needed to know file size.
60
+ */
61
+ _getFileStat(statQueue: Queue<string, fs.Stats>, file: string, callback: (err?: Error | null) => void): void;
62
+ /**
63
+ * Allows pausing of the hashing process by storing functions from the
64
+ * file and read queues that will not get called back until resume()
65
+ * is called.
66
+ */
67
+ pause(): boolean;
68
+ /**
69
+ * Resumes hashing.
70
+ */
71
+ resume(): boolean;
72
+ /**
73
+ * Continues hashing if paused or pauses if not.
74
+ */
75
+ toggle(): boolean;
76
+ /**
77
+ * Stops hashing completely.
78
+ * Closes file descriptors and does not emit any more events.
79
+ */
80
+ destroy(): boolean;
81
+ }
package/dist/hasher.js ADDED
@@ -0,0 +1,398 @@
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
+ const node_crypto_1 = __importDefault(require("node:crypto"));
40
+ const node_fs_1 = __importDefault(require("node:fs"));
41
+ const node_path_1 = __importDefault(require("node:path"));
42
+ const node_stream_1 = require("node:stream");
43
+ const async_1 = require("async");
44
+ const streamspeed_1 = __importDefault(require("streamspeed"));
45
+ const queue_1 = __importDefault(require("./queue"));
46
+ const util = __importStar(require("./util"));
47
+ /**
48
+ * defaults
49
+ */
50
+ const PIECE_LENGTH = 262144; // 256kb
51
+ const MAX_FILES_OPENED = 250;
52
+ /**
53
+ * Hashes a buffer.
54
+ */
55
+ const sha1 = (buffer) => {
56
+ return node_crypto_1.default.createHash('sha1').update(buffer).digest();
57
+ };
58
+ /**
59
+ * Rounds a number to given decimal place.
60
+ */
61
+ const round = (num, dec) => {
62
+ const pow = 10 ** dec;
63
+ return Math.round(num * pow) / pow;
64
+ };
65
+ /**
66
+ * Allows arrays of objects with a `path` property to be
67
+ * sorted alphabetically
68
+ */
69
+ const sortByPath = (a, b) => {
70
+ const len = Math.min(a.path.length, b.path.length);
71
+ for (let i = 0; i < len; i++) {
72
+ if (a.path[i] < b.path[i])
73
+ return -1;
74
+ if (a.path[i] > b.path[i])
75
+ return 1;
76
+ }
77
+ return a.path.length - b.path.length;
78
+ };
79
+ class Hasher extends node_stream_1.Stream {
80
+ pieceLength;
81
+ maxFiles;
82
+ stopOnErr;
83
+ dir;
84
+ files;
85
+ pieces;
86
+ ready;
87
+ hashing;
88
+ paused;
89
+ stopped;
90
+ readable;
91
+ err;
92
+ filetasks;
93
+ totalSize;
94
+ lastPieceLength;
95
+ streamGroup;
96
+ /**
97
+ * Calculates piece hashes of file list.
98
+ */
99
+ constructor(dir, list, pieceLength, options) {
100
+ super();
101
+ options = options || {};
102
+ // Set defaults.
103
+ this.pieceLength = pieceLength || PIECE_LENGTH;
104
+ this.maxFiles = options.maxFiles || MAX_FILES_OPENED;
105
+ this.stopOnErr = options.stopOnErr || false;
106
+ // Init arrays that will be used every time during hashing.
107
+ this.dir = dir;
108
+ this.files = [];
109
+ this.pieces = 0;
110
+ this.ready = false;
111
+ this.hashing = false;
112
+ this.paused = false;
113
+ this.stopped = false;
114
+ this.readable = false;
115
+ this.filetasks = [];
116
+ this.totalSize = 0;
117
+ this.lastPieceLength = 0;
118
+ this.streamGroup = new streamspeed_1.default();
119
+ if (this.stopOnErr) {
120
+ this.once('error', (err) => {
121
+ this.err = err;
122
+ });
123
+ }
124
+ node_fs_1.default.access(dir, (err) => {
125
+ if (err) {
126
+ return this.emit('error', err);
127
+ }
128
+ const fromDisk = typeof list[0] === 'string';
129
+ const readTasks = (readErr) => {
130
+ // Create shallow copy of the files array.
131
+ this.files = Array.from(this.files);
132
+ // When creating a torrent from files on disk, sort them alphabetically.
133
+ // File lists from an existing torrent must keep their original order,
134
+ // since the piece hashes depend on it, so they are not sorted.
135
+ if (fromDisk) {
136
+ this.files.sort(sortByPath);
137
+ }
138
+ if (readErr)
139
+ return this.emit('error', readErr);
140
+ // If files tree traversed with no errors,
141
+ // add read tasks to filetasks.
142
+ this._CreateFileTasks();
143
+ // Ready to start hashing.
144
+ this.ready = true;
145
+ this.emit('ready');
146
+ this.ready = true;
147
+ this._start();
148
+ };
149
+ // Check if list contains objects or strings.
150
+ if (fromDisk) {
151
+ // Create queue to handle having many files opened.
152
+ const statqueue = (0, queue_1.default)((filepath, cb) => {
153
+ node_fs_1.default.stat(filepath, cb);
154
+ }, this.maxFiles);
155
+ this._getFilesStats(statqueue, list, readTasks);
156
+ // In the case of objects, a file list from a torrent file was given.
157
+ }
158
+ else {
159
+ this.files = list;
160
+ process.nextTick(readTasks);
161
+ }
162
+ });
163
+ }
164
+ /**
165
+ * Create filetasks array
166
+ */
167
+ _CreateFileTasks() {
168
+ // Variables to help calculate piece position.
169
+ let totalSize = 0;
170
+ let piece = 0;
171
+ let offset = 0;
172
+ // Create tasks to figure out what pieces belong to exactly
173
+ // what files and what part of the files.
174
+ this.filetasks = [];
175
+ this.files.forEach((task) => {
176
+ this.filetasks.push({
177
+ length: task.length,
178
+ path: task.path,
179
+ // Piece this task will read from or write to.
180
+ piece,
181
+ // Piece buffer offset to start reading or writing at.
182
+ offset,
183
+ });
184
+ // Add file length to total size.
185
+ totalSize += task.length;
186
+ // Get piece next task will write to and its offset.
187
+ piece = Math.floor(totalSize / this.pieceLength);
188
+ offset = totalSize - this.pieceLength * piece;
189
+ });
190
+ this.totalSize = totalSize;
191
+ this.pieces = piece + 1;
192
+ // Take note of the length of the last piece.
193
+ // The rest of the pieces will be of `this.pieceLength` length.
194
+ this.lastPieceLength = totalSize - this.pieceLength * piece;
195
+ }
196
+ /**
197
+ * Starts hashing.
198
+ */
199
+ _start() {
200
+ if (!this.ready) {
201
+ this.emit('error', new Error('Cannot start before being ready'));
202
+ return;
203
+ }
204
+ this.hashing = true;
205
+ this.stopped = false;
206
+ // Create piece objects.
207
+ const pieces = [];
208
+ for (let i = 0; i < this.pieces; i++) {
209
+ pieces[i] = {
210
+ buffer: null,
211
+ length: this.pieceLength,
212
+ bytesWritten: 0,
213
+ };
214
+ }
215
+ const lastPiece = pieces[pieces.length - 1];
216
+ if (lastPiece) {
217
+ lastPiece.length = this.lastPieceLength;
218
+ }
219
+ // Keep track of progress.
220
+ let totalRead = 0;
221
+ // Keep track of reading speed.
222
+ this.streamGroup = new streamspeed_1.default();
223
+ let lastPercent = 0;
224
+ let speed = 0;
225
+ let avg = 0;
226
+ this.streamGroup.on('speed', (s, a) => {
227
+ speed = s;
228
+ avg = a;
229
+ });
230
+ const filesqueue = (0, queue_1.default)((task, callback) => {
231
+ if (this.err || this.stopped)
232
+ return callback();
233
+ // Keep track how much has been read.
234
+ let readSoFar = 0;
235
+ let pieceIndex = task.piece;
236
+ // Open file for reading.
237
+ const baseDir = node_path_1.default.resolve(this.dir);
238
+ const file = node_path_1.default.resolve(baseDir, ...task.path);
239
+ const relative = node_path_1.default.relative(baseDir, file);
240
+ if (relative.startsWith('..') || node_path_1.default.isAbsolute(relative)) {
241
+ this.emit('error', new Error(`Path traversal detected: ${file}`));
242
+ return callback();
243
+ }
244
+ const rs = node_fs_1.default.createReadStream(file);
245
+ this.streamGroup.add(rs);
246
+ rs.on('open', () => {
247
+ this.emit('open', file);
248
+ });
249
+ rs.on('data', (chunk) => {
250
+ const data = chunk;
251
+ // Recalculate the index of current piece based on `readSoFar`.
252
+ pieceIndex =
253
+ task.piece + Math.floor((task.offset + readSoFar) / this.pieceLength);
254
+ // Write data to piece buffer.
255
+ const targetStart = (task.offset + readSoFar) % this.pieceLength;
256
+ let sourceWritten = this._writePiece(pieces, pieceIndex, targetStart, data, 0, data.length, file, readSoFar);
257
+ while (sourceWritten < data.length) {
258
+ pieceIndex += 1;
259
+ sourceWritten += this._writePiece(pieces, pieceIndex, 0, data, sourceWritten, data.length, file, readSoFar);
260
+ }
261
+ // Update amount of bytes written to buffer.
262
+ readSoFar += data.length;
263
+ totalRead += data.length;
264
+ // Emit hash event with piece info and progress.
265
+ const percent = round((totalRead / this.totalSize) * 100, 2);
266
+ if (lastPercent !== percent) {
267
+ this.emit('progress', percent, speed, avg);
268
+ lastPercent = percent;
269
+ }
270
+ });
271
+ // Close this file when there is an error or finished reading.
272
+ rs.on('error', (err) => {
273
+ this.emit('error', err);
274
+ callback();
275
+ });
276
+ rs.on('end', () => {
277
+ callback();
278
+ });
279
+ }, this.maxFiles);
280
+ // When all file queues are finished, join all the pieces.
281
+ filesqueue.drain = () => {
282
+ this.hashing = false;
283
+ this.emit('end');
284
+ };
285
+ // Start queueing jobs.
286
+ this.filetasks.forEach((filetask) => {
287
+ filesqueue.push(filetask);
288
+ });
289
+ }
290
+ _writePiece(pieces, index, offset, data, start, end, file, readSoFar) {
291
+ const piece = pieces[index];
292
+ if (!piece)
293
+ return 0;
294
+ if (piece.buffer === null) {
295
+ piece.buffer = Buffer.alloc(piece.length);
296
+ }
297
+ // Write data to piece buffer and update bytes written.
298
+ const size = Math.min(end - start, piece.length - offset);
299
+ piece.bytesWritten += size;
300
+ data.copy(piece.buffer, offset, start, start + size);
301
+ // Check if piece buffer is full.
302
+ if (piece.bytesWritten === piece.length) {
303
+ pieces[index] = null;
304
+ const buf = Buffer.from(sha1(piece.buffer));
305
+ this.emit('hash', index, buf, file, readSoFar, size);
306
+ }
307
+ return size;
308
+ }
309
+ /**
310
+ * Recursively traverses files to call fs.stat on them.
311
+ * Will add all files in folders if any are encountered.
312
+ */
313
+ _getFilesStats(statQueue, list, callback) {
314
+ // Read all file stats in parallel.
315
+ (0, async_1.parallel)(list.map((file) => {
316
+ return this._getFileStat.bind(this, statQueue, file);
317
+ }), callback);
318
+ }
319
+ /**
320
+ * Stat is needed to know file size.
321
+ */
322
+ _getFileStat(statQueue, file, callback) {
323
+ const filepath = node_path_1.default.join(this.dir, file);
324
+ statQueue.push(filepath, (err, stat) => {
325
+ if (err || !stat)
326
+ return callback(err);
327
+ // If this is a directory, add all files in it to torrent files.
328
+ if (stat.isDirectory()) {
329
+ node_fs_1.default.readdir(filepath, (readdirErr, dirfiles) => {
330
+ if (readdirErr)
331
+ return callback(readdirErr);
332
+ const subfiles = dirfiles.map((dirfile) => node_path_1.default.join(file, dirfile));
333
+ // Call _getFilesStats again.
334
+ this._getFilesStats(statQueue, subfiles, callback);
335
+ });
336
+ }
337
+ else if (stat.isFile()) {
338
+ this.files.push({
339
+ length: stat.size,
340
+ path: util.splitPath(file),
341
+ });
342
+ callback(null);
343
+ }
344
+ else {
345
+ callback(null);
346
+ }
347
+ });
348
+ }
349
+ /**
350
+ * Allows pausing of the hashing process by storing functions from the
351
+ * file and read queues that will not get called back until resume()
352
+ * is called.
353
+ */
354
+ pause() {
355
+ if (this.paused || !this.hashing)
356
+ return false;
357
+ this.paused = true;
358
+ this.streamGroup.getStreams().forEach((rs) => {
359
+ rs.pause();
360
+ });
361
+ return true;
362
+ }
363
+ /**
364
+ * Resumes hashing.
365
+ */
366
+ resume() {
367
+ if (!this.paused)
368
+ return false;
369
+ this.paused = false;
370
+ this.streamGroup.getStreams().forEach((rs) => {
371
+ rs.resume();
372
+ });
373
+ return true;
374
+ }
375
+ /**
376
+ * Continues hashing if paused or pauses if not.
377
+ */
378
+ toggle() {
379
+ return this.paused ? this.resume() : this.pause();
380
+ }
381
+ /**
382
+ * Stops hashing completely.
383
+ * Closes file descriptors and does not emit any more events.
384
+ */
385
+ destroy() {
386
+ if (!this.hashing)
387
+ return false;
388
+ this.hashing = false;
389
+ this.stopped = true;
390
+ this.paused = false;
391
+ this.streamGroup.getStreams().forEach((rs) => {
392
+ rs.destroy();
393
+ });
394
+ return true;
395
+ }
396
+ }
397
+ exports.default = Hasher;
398
+ //# sourceMappingURL=hasher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hasher.js","sourceRoot":"","sources":["../src/hasher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8DAAiC;AACjC,sDAAyB;AACzB,0DAA6B;AAC7B,6CAAqC;AACrC,iCAAiC;AACjC,8DAAsC;AAEtC,oDAA4C;AAC5C,6CAA+B;AAE/B;;GAEG;AACH,MAAM,YAAY,GAAG,MAAM,CAAC,CAAC,QAAQ;AACrC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B;;GAEG;AACH,MAAM,IAAI,GAAG,CAAC,MAAc,EAAU,EAAE;IACtC,OAAO,qBAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC;AAC3D,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,KAAK,GAAG,CAAC,GAAW,EAAE,GAAW,EAAU,EAAE;IACjD,MAAM,GAAG,GAAG,EAAE,IAAI,GAAG,CAAC;IACtB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;AACrC,CAAC,CAAC;AAwBF;;;GAGG;AACH,MAAM,UAAU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,GAAG,IAAI,CAAC;QAEnB,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;YAC3C,EAAE,CAAC,KAAK,EAAE,CAAC;QACb,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO,KAAK,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QAEpB,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;YAC3C,EAAE,CAAC,MAAM,EAAE,CAAC;QACd,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACpD,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO,KAAK,CAAC;QAChC,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QAEpB,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;YAC3C,EAAE,CAAC,OAAO,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAtZD,yBAsZC"}
@@ -0,0 +1,5 @@
1
+ import Hasher, { type FileEntry, type FileTask, type HasherOptions, type PieceBuffer } from './hasher';
2
+ import { type MakeCallback, type MakeOptions, make, makeWrite } from './make';
3
+ import read, { type ReadCallback } from './read';
4
+ import Torrent, { type TorrentInfo, type TorrentMetadata } from './torrent';
5
+ export { type FileEntry, type FileTask, Hasher, type HasherOptions, type MakeCallback, type MakeOptions, make, makeWrite, type PieceBuffer, type ReadCallback, read, Torrent, type TorrentInfo, type TorrentMetadata, };
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Torrent = exports.read = exports.makeWrite = exports.make = exports.Hasher = void 0;
7
+ const hasher_1 = __importDefault(require("./hasher"));
8
+ exports.Hasher = hasher_1.default;
9
+ const make_1 = require("./make");
10
+ Object.defineProperty(exports, "make", { enumerable: true, get: function () { return make_1.make; } });
11
+ Object.defineProperty(exports, "makeWrite", { enumerable: true, get: function () { return make_1.makeWrite; } });
12
+ const read_1 = __importDefault(require("./read"));
13
+ exports.read = read_1.default;
14
+ const torrent_1 = __importDefault(require("./torrent"));
15
+ exports.Torrent = torrent_1.default;
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAKkB;AAQhB,iBAbK,gBAAM,CAaL;AAPR,iCAA8E;AAW5E,qFAX4C,WAAI,OAW5C;AACJ,0FAZkD,gBAAS,OAYlD;AAXX,kDAAiD;AAc/C,eAdK,cAAI,CAcL;AAbN,wDAA4E;AAc1E,kBAdK,iBAAO,CAcL"}
package/dist/make.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import Hasher from './hasher';
2
+ import Torrent from './torrent';
3
+ export interface MakeOptions {
4
+ announceList?: Array<Array<string | Buffer>>;
5
+ comment?: string;
6
+ name?: string;
7
+ pieceLength?: number;
8
+ private?: boolean;
9
+ moreInfo?: Record<string, unknown>;
10
+ maxFiles?: number;
11
+ maxMemory?: number | string;
12
+ multimode?: boolean;
13
+ }
14
+ export type MakeCallback = (err: Error | null, torrent?: Torrent) => void;
15
+ /**
16
+ * Makes torrent file object
17
+ */
18
+ export declare const make: (announce: string, dir: string, files?: string | string[] | MakeOptions | MakeCallback, options?: MakeOptions | MakeCallback, callback?: MakeCallback) => Hasher;
19
+ /**
20
+ * Calls make and creates a write stream with the returned read stream.
21
+ */
22
+ export declare const makeWrite: (output: string, announce: string, dir: string, files?: string | string[] | MakeOptions | MakeCallback, options?: MakeOptions | MakeCallback, callback?: MakeCallback) => Hasher;