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/dist/make.js ADDED
@@ -0,0 +1,219 @@
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.makeWrite = exports.make = void 0;
40
+ const node_fs_1 = __importDefault(require("node:fs"));
41
+ const node_path_1 = __importDefault(require("node:path"));
42
+ const b = __importStar(require("bncode"));
43
+ const buffers_1 = __importDefault(require("buffers"));
44
+ const ordered_emitter_1 = __importDefault(require("ordered-emitter"));
45
+ const hasher_1 = __importDefault(require("./hasher"));
46
+ const schema = __importStar(require("./schema"));
47
+ const torrent_1 = __importDefault(require("./torrent"));
48
+ const util = __importStar(require("./util"));
49
+ // Used to display program info when creating torrents
50
+ // and with the cli.
51
+ const pkg = JSON.parse(node_fs_1.default.readFileSync(`${__dirname}/../package.json`, 'utf8'));
52
+ const APP_NAME = pkg.name;
53
+ const APP_VERSION = pkg.version;
54
+ /**
55
+ * Makes torrent file object
56
+ */
57
+ const make = (announce, dir, files, options, callback) => {
58
+ // Handle all of the optional arguments.
59
+ const typeofFiles = typeof files;
60
+ const typeofOptions = typeof options;
61
+ let fileList;
62
+ let opts;
63
+ let cb;
64
+ if (typeofFiles === 'function') {
65
+ cb = files;
66
+ opts = {};
67
+ fileList = ['.'];
68
+ }
69
+ else if (typeofFiles === 'object' && !Array.isArray(files)) {
70
+ cb = typeofOptions === 'function' ? options : () => { };
71
+ opts = files;
72
+ fileList = ['.'];
73
+ }
74
+ else {
75
+ if (typeofOptions === 'function') {
76
+ cb = options;
77
+ opts = {};
78
+ }
79
+ else {
80
+ cb = callback || (() => { });
81
+ opts = options || {};
82
+ }
83
+ if (typeofFiles === 'string')
84
+ fileList = [files];
85
+ else if (typeofFiles === 'undefined')
86
+ fileList = ['.'];
87
+ else
88
+ fileList = files;
89
+ }
90
+ const hashOptions = {
91
+ maxFiles: opts.maxFiles,
92
+ stopOnErr: true,
93
+ };
94
+ // Default piece length is 256kb.
95
+ const pieceLength = opts.pieceLength ? 1 << opts.pieceLength : 262144;
96
+ // Start hashing files.
97
+ const hasher = new hasher_1.default(dir, fileList, pieceLength, hashOptions);
98
+ const order = new ordered_emitter_1.default();
99
+ const buf = (0, buffers_1.default)();
100
+ const info = {};
101
+ let data;
102
+ let piecesLength;
103
+ let piecesPosition;
104
+ let err;
105
+ hasher.readable = true;
106
+ // Check announce URL.
107
+ if (!util.isURL(announce)) {
108
+ err = Error(`Not a URL: ${announce}`);
109
+ process.nextTick(() => {
110
+ hasher.destroy();
111
+ hasher.emit('error', err);
112
+ cb(err);
113
+ });
114
+ return hasher;
115
+ }
116
+ // Check list of files is not empty.
117
+ if (fileList.length === 0) {
118
+ err = Error('no files given');
119
+ process.nextTick(() => {
120
+ hasher.destroy();
121
+ hasher.emit('error', err);
122
+ cb(err);
123
+ });
124
+ return hasher;
125
+ }
126
+ // Make metadata object.
127
+ const metadata = {};
128
+ metadata.announce = announce;
129
+ // Check and validate announce list.
130
+ if (opts.announceList != null) {
131
+ const msg = schema.announceList(opts.announceList);
132
+ if (msg !== null) {
133
+ err = Error(msg);
134
+ process.nextTick(() => {
135
+ hasher.destroy();
136
+ hasher.emit('error', err);
137
+ cb(err);
138
+ });
139
+ return hasher;
140
+ }
141
+ metadata['announce-list'] = opts.announceList;
142
+ }
143
+ // Check comment options.
144
+ if (opts.comment != null)
145
+ metadata.comment = opts.comment;
146
+ metadata['created by'] = `${APP_NAME} ${APP_VERSION}`;
147
+ metadata['creation date'] = Math.round(Date.now() / 1000);
148
+ metadata.info = info;
149
+ // Wait until hasher finishes examining file lengths.
150
+ hasher.on('ready', () => {
151
+ // Multi file mode.
152
+ if (opts.multimode || hasher.files.length > 1) {
153
+ info.files = hasher.files;
154
+ info.name = opts.name || node_path_1.default.basename(dir);
155
+ // Single file mode.
156
+ }
157
+ else {
158
+ info.length = hasher.files[0].length;
159
+ info.name = node_path_1.default.join(...hasher.files[0].path);
160
+ }
161
+ info['piece length'] = pieceLength;
162
+ info.pieces = null;
163
+ if (opts.private)
164
+ info.private = 1;
165
+ // Generate fake pieces to encode them.
166
+ piecesLength = 20 * hasher.pieces;
167
+ info.pieces = Buffer.alloc(piecesLength);
168
+ // Add additional moreInfo to info.
169
+ for (const moreKey in opts.moreInfo) {
170
+ // Only add moreInfo if it doesn't overwrite info.
171
+ if (!Object.hasOwn(info, moreKey)) {
172
+ info[moreKey] = opts.moreInfo[moreKey];
173
+ }
174
+ }
175
+ // Bencode data.
176
+ data = Buffer.from(b.encode(metadata), 'binary');
177
+ const strData = data.toString();
178
+ const dict = `6:pieces${piecesLength}:`;
179
+ piecesPosition = strData.indexOf(dict) + dict.length;
180
+ piecesPosition = Buffer.byteLength(strData.slice(0, piecesPosition));
181
+ // Write first part of torrent to stream.
182
+ process.nextTick(() => {
183
+ hasher.emit('data', data.slice(0, piecesPosition));
184
+ });
185
+ });
186
+ // Write last part when hashing is finished.
187
+ hasher.on('end', () => {
188
+ info.pieces = buf.toBuffer();
189
+ // Note that listeners to this `data` event will be called before
190
+ // listeners to the `end` event since its emitted inside this listener.
191
+ hasher.emit('data', data.slice(piecesPosition + piecesLength));
192
+ cb(null, new torrent_1.default(metadata));
193
+ });
194
+ // listen for hash events
195
+ hasher.on('hash', (index, hash) => {
196
+ order.emit('hash', { order: index, hash });
197
+ });
198
+ // Emit hash events to memStream in correct order.
199
+ order.on('hash', (obj) => {
200
+ buf.push(obj.hash);
201
+ hasher.emit('data', obj.hash);
202
+ });
203
+ // Handle stream errors.
204
+ hasher.on('error', (streamErr) => {
205
+ cb(streamErr);
206
+ });
207
+ return hasher;
208
+ };
209
+ exports.make = make;
210
+ /**
211
+ * Calls make and creates a write stream with the returned read stream.
212
+ */
213
+ const makeWrite = (output, announce, dir, files, options, callback) => {
214
+ const rs = (0, exports.make)(announce, dir, files, options, callback);
215
+ rs.pipe(node_fs_1.default.createWriteStream(output));
216
+ return rs;
217
+ };
218
+ exports.makeWrite = makeWrite;
219
+ //# sourceMappingURL=make.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"make.js","sourceRoot":"","sources":["../src/make.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAAyB;AACzB,0DAA6B;AAC7B,0CAA4B;AAC5B,sDAA8B;AAC9B,sEAA6C;AAE7C,sDAAsD;AACtD,iDAAmC;AACnC,wDAA4E;AAC5E,6CAA+B;AAE/B,sDAAsD;AACtD,o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AAC,EAAE,CAAC,MAAM,EAAE,CAAC,GAAoC,EAAE,EAAE;QACxD,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACnB,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,wBAAwB;IACxB,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,SAAgB,EAAE,EAAE;QACtC,EAAE,CAAC,SAAS,CAAC,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AA9KW,QAAA,IAAI,QA8Kf;AAEF;;GAEG;AACI,MAAM,SAAS,GAAG,CACvB,MAAc,EACd,QAAgB,EAChB,GAAW,EACX,KAAsD,EACtD,OAAoC,EACpC,QAAuB,EACf,EAAE;IACV,MAAM,EAAE,GAAG,IAAA,YAAI,EAAC,QAAQ,EAAE,GAAG,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IACzD,EAAE,CAAC,IAAI,CAAC,iBAAE,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;IACtC,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AAXW,QAAA,SAAS,aAWpB"}
@@ -0,0 +1,18 @@
1
+ export type ConcurrencyFn<T> = (workers: number, task?: T | null, length?: number) => boolean;
2
+ export type WorkerCallback<R = unknown> = (err?: Error | null, res?: R) => void;
3
+ export type WorkerFn<T, R = unknown> = (data: T, callback: WorkerCallback<R>) => void;
4
+ export interface Queue<T, R = unknown> {
5
+ saturated: (() => void) | null;
6
+ empty: (() => void) | null;
7
+ drain: (() => void) | null;
8
+ push(data: T, callback?: WorkerCallback<R>): void;
9
+ process(): void;
10
+ length(): number;
11
+ running(): number;
12
+ concurrency: ConcurrencyFn<T>;
13
+ }
14
+ /**
15
+ * Creates a queue
16
+ */
17
+ declare const createQueue: <T, R = unknown>(worker: WorkerFn<T, R>, initialConcurrency: number | ConcurrencyFn<T>) => Queue<T, R>;
18
+ export default createQueue;
package/dist/queue.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Creates a queue
5
+ */
6
+ const createQueue = (worker, initialConcurrency) => {
7
+ let workers = 0;
8
+ let concurrency;
9
+ const tasks = [];
10
+ const q = {
11
+ saturated: null,
12
+ empty: null,
13
+ drain: null,
14
+ push(data, callback) {
15
+ const length = tasks.push({ data, callback });
16
+ if (q.saturated && concurrency(workers, null, length)) {
17
+ q.saturated();
18
+ }
19
+ process.nextTick(q.process);
20
+ },
21
+ process() {
22
+ const task = tasks.shift();
23
+ if (!task)
24
+ return;
25
+ if (q.concurrency(workers, task.data)) {
26
+ if (q.empty && tasks.length === 0)
27
+ q.empty();
28
+ workers++;
29
+ worker(task.data, (err, res) => {
30
+ workers--;
31
+ if (task.callback)
32
+ task.callback(err, res);
33
+ if (q.drain && tasks.length + workers === 0) {
34
+ q.drain();
35
+ }
36
+ else {
37
+ q.process();
38
+ }
39
+ });
40
+ }
41
+ else {
42
+ tasks.unshift(task);
43
+ }
44
+ },
45
+ length: () => tasks.length,
46
+ running: () => workers,
47
+ get concurrency() {
48
+ return concurrency;
49
+ },
50
+ set concurrency(c) {
51
+ if (typeof c !== 'function') {
52
+ concurrency = (w, _task, length) => {
53
+ return length ? length === c : w < c;
54
+ };
55
+ }
56
+ else {
57
+ concurrency = c;
58
+ }
59
+ },
60
+ };
61
+ q.concurrency = initialConcurrency;
62
+ return q;
63
+ };
64
+ exports.default = createQueue;
65
+ //# sourceMappingURL=queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sourceRoot":"","sources":["../src/queue.ts"],"names":[],"mappings":";;AAuBA;;GAEG;AACH,MAAM,WAAW,GAAG,CAClB,MAAsB,EACtB,kBAA6C,EAChC,EAAE;IACf,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,IA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AAC,CAAC,CAAC;YACL,CAAC;iBAAM,CAAC;gBACN,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;QACD,MAAM,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM;QAC1B,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO;QACtB,IAAI,WAAW;YACb,OAAO,WAAW,CAAC;QACrB,CAAC;QACD,IAAI,WAAW,CAAC,CAA4B;YAC1C,IAAI,OAAO,CAAC,KAAK,UAAU,EAAE,CAAC;gBAC5B,WAAW,GAAG,CAAC,CAAS,EAAE,KAAgB,EAAE,MAAe,EAAE,EAAE;oBAC7D,OAAO,MAAM,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACvC,CAAC,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,WAAW,GAAG,CAAC,CAAC;YAClB,CAAC;QACH,CAAC;KACF,CAAC;IAEF,CAAC,CAAC,WAAW,GAAG,kBAAkB,CAAC;IACnC,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AAEF,kBAAe,WAAW,CAAC"}
package/dist/read.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { Readable, Stream } from 'node:stream';
2
+ import Torrent from './torrent';
3
+ export type ReadCallback = (err: Error | null, torrent?: Torrent) => void;
4
+ /**
5
+ * Read torrent data.
6
+ */
7
+ declare const read: (file: string | Readable | Stream, validate?: boolean | ReadCallback, callback?: ReadCallback) => Readable | Stream;
8
+ export default read;
package/dist/read.js ADDED
@@ -0,0 +1,73 @@
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_fs_1 = __importDefault(require("node:fs"));
40
+ const bncode_1 = require("bncode");
41
+ const schema = __importStar(require("./schema"));
42
+ const torrent_1 = __importDefault(require("./torrent"));
43
+ /**
44
+ * Read torrent data.
45
+ */
46
+ const read = (file, validate = true, callback) => {
47
+ if (typeof validate === 'function') {
48
+ callback = validate;
49
+ validate = true;
50
+ }
51
+ if (!callback) {
52
+ callback = () => { };
53
+ }
54
+ const rs = typeof file === 'string' ? node_fs_1.default.createReadStream(file) : file;
55
+ const bstream = new bncode_1.Stream();
56
+ rs.pipe(bstream);
57
+ bstream.on('error', (err) => {
58
+ if ('fd' in rs && typeof rs.fd === 'number') {
59
+ node_fs_1.default.close(rs.fd, () => { });
60
+ }
61
+ callback(err);
62
+ });
63
+ bstream.on('data', (result) => {
64
+ if (validate) {
65
+ schema.validate(result, callback);
66
+ return;
67
+ }
68
+ callback(null, new torrent_1.default(result));
69
+ });
70
+ return rs;
71
+ };
72
+ exports.default = read;
73
+ //# sourceMappingURL=read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","sourceRoot":"","sources":["../src/read.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAAyB;AAEzB,mCAA2C;AAE3C,iDAAmC;AACnC,wDAA0D;AAI1D;;GAEG;AACH,MAAM,IAAI,GAAG,CACX,IAAgC,EAChC,WAAmC,IAAI,EACvC,QAAuB,EACJ,EAAE;IACrB,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QACnC,QAAQ,GAAG,QAAQ,CAAC;QACpB,QAAQ,GAAG,IAAI,CAAC;IAClB,CAAC;IACD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,QAAQ,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;IACtB,CAAC;IAED,MAAM,EAAE,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,iBAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvE,MAAM,OAAO,GAAG,IAAI,eAAO,EAAE,CAAC;IAC9B,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAEjB,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAU,EAAE,EAAE;QACjC,IAAI,IAAI,IAAI,EAAE,IAAI,OAAO,EAAE,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;YAC5C,iBAAE,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC5B,CAAC;QACD,QAAQ,CAAC,GAAG,CAAC,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,MAAe,EAAE,EAAE;QACrC,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YAClC,OAAO;QACT,CAAC;QACD,QAAQ,CAAC,IAAI,EAAE,IAAI,iBAAO,CAAC,MAAyB,CAAC,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AAEF,kBAAe,IAAI,CAAC"}
@@ -0,0 +1,18 @@
1
+ import Torrent from './torrent';
2
+ export type RawDict = Record<string, unknown>;
3
+ /**
4
+ * Checks that a path segment is safe and does not traverse directories.
5
+ */
6
+ export declare const isValidPathSegment: (buf: unknown) => boolean;
7
+ /**
8
+ * Checks announce list field.
9
+ */
10
+ export declare const announceList: (list: unknown) => string | null;
11
+ /**
12
+ * Checks torrent metadata is correct.
13
+ */
14
+ export declare const checkTorrent: (torrentInput: unknown) => string | null;
15
+ /**
16
+ * Checks a torrent object, makes sure its metadata matches the schema.
17
+ */
18
+ export declare const validate: (torrent: unknown, callback: (err: Error | null, torrent?: Torrent) => void) => void;