nt 0.8.0 → 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/README.md +17 -4
- package/dist/hasher.d.ts +117 -5
- package/dist/hasher.js +478 -49
- package/dist/hasher.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js.map +1 -1
- package/dist/make.d.ts +10 -3
- package/dist/make.js +109 -31
- package/dist/make.js.map +1 -1
- package/dist/schema.js +7 -0
- package/dist/schema.js.map +1 -1
- package/dist/util.d.ts +1 -1
- package/dist/util.js +10 -2
- package/dist/util.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -22,9 +22,16 @@ nt.read('path/to/file.torrent', (err, torrent) => {
|
|
|
22
22
|
## Make a torrent
|
|
23
23
|
|
|
24
24
|
```javascript
|
|
25
|
+
const fs = require('node:fs');
|
|
26
|
+
const nt = require('nt');
|
|
27
|
+
|
|
25
28
|
let rs = nt.make('http://myannounce.net/url', __dirname + '/files');
|
|
26
29
|
rs.pipe(fs.createWriteStream('mytorrent.torrent'));
|
|
27
30
|
|
|
31
|
+
// from a stream, without a directory
|
|
32
|
+
nt.make('http://myannounce.net/url', fs.createReadStream('video.mp4'))
|
|
33
|
+
.pipe(fs.createWriteStream('video.torrent'));
|
|
34
|
+
|
|
28
35
|
// callback style
|
|
29
36
|
nt.makeWrite('outputfile', 'http://announce.me', __dirname + '/files',
|
|
30
37
|
['somefile.ext', 'another.one', 'inside/afolder.mkv', 'afolder'],
|
|
@@ -58,13 +65,19 @@ Reads a local file, or a readable stream. Returns readable stream.
|
|
|
58
65
|
|
|
59
66
|
An error can be returned if the torrent is formatted incorrectly. Does not check if the dictonaries are listed alphabetically. Refer to the [BitTorrent Specification](http://wiki.theory.org/BitTorrentSpecification) for more info on torrent metainfo.
|
|
60
67
|
|
|
61
|
-
### make(announceURL, dir, [
|
|
68
|
+
### make(announceURL, dir, [streamOrEntries], [options], [callback(Error, Torrent)])
|
|
69
|
+
|
|
70
|
+
### make(announceURL, streamOrEntries, [options], [callback(Error, Torrent)])
|
|
71
|
+
|
|
72
|
+
Makes a new torrent. `dir` is the root directory of the torrent, and can be omitted when the input consists only of streams. The `streamOrEntries` argument can be a file path, a directory, a `Readable` stream, a `{ stream, path?, length? }` object, or an array mixing any of them. If `streamOrEntries` is omitted, all files in `dir`, including those in subdirectories, are added.
|
|
73
|
+
|
|
74
|
+
`fs.ReadStream` inputs supply their path and length automatically, including ranged streams; HTTP responses with a `Content-Length` header supply their length automatically. Provide `path` and `length` for other streams when known. Stream order is preserved and directories are expanded in place.
|
|
62
75
|
|
|
63
|
-
|
|
76
|
+
`options` can have the following:
|
|
64
77
|
|
|
65
78
|
* `announceList` - An array of arrays of additional announce URLs.
|
|
66
79
|
* `comment`
|
|
67
|
-
* `name` -
|
|
80
|
+
* `name` - Used in multi file mode, and to name unnamed streams. Defaults to the directory name, or `stream`, with unique suffixes when there are several unnamed streams.
|
|
68
81
|
* `pieceLength` - How to break up the pieces. Must be an integer `n` that says piece length will be `2^n`. Default is 256KB, or 2^18.
|
|
69
82
|
* `private` - Set true if this is a private torrent.
|
|
70
83
|
* `moreInfo` - These go into the `info` dictionary of the torrent. Useful if you want to make a torrent have a unique info hash from a certain tracker.
|
|
@@ -74,7 +87,7 @@ Makes a new torrent. `dir` is root directory of the torrent. The `files` array w
|
|
|
74
87
|
|
|
75
88
|
`make` returns a Hasher object that emits raw bencoded `data` events.
|
|
76
89
|
|
|
77
|
-
### makeWrite(output,
|
|
90
|
+
### makeWrite(output, announce, dir, [streamOrEntries], [options], [callback(Error, Torrent)])
|
|
78
91
|
|
|
79
92
|
A shortcut that pumps the returned readable stream from `make` into a writable stream that points to the file `output`. Returns a Hasher object.
|
|
80
93
|
|
package/dist/hasher.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
-
import { Stream } from 'node:stream';
|
|
2
|
+
import { type Readable, Stream } from 'node:stream';
|
|
3
3
|
import StreamSpeed from 'streamspeed';
|
|
4
4
|
import { type Queue } from './queue';
|
|
5
5
|
export interface FileEntry {
|
|
@@ -7,10 +7,32 @@ export interface FileEntry {
|
|
|
7
7
|
path: string[];
|
|
8
8
|
md5sum?: string;
|
|
9
9
|
}
|
|
10
|
+
export interface StreamEntry {
|
|
11
|
+
stream: Readable;
|
|
12
|
+
/** Defaults to the stream's inferred path, or the torrent name. */
|
|
13
|
+
path?: string | string[];
|
|
14
|
+
length?: number;
|
|
15
|
+
}
|
|
16
|
+
export type HasherInput = string | FileEntry | Readable | StreamEntry;
|
|
10
17
|
export interface FileTask extends FileEntry {
|
|
11
18
|
piece: number;
|
|
12
19
|
offset: number;
|
|
20
|
+
stream?: Readable;
|
|
13
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* An input resolved to a torrent path. Exactly one source field is set:
|
|
24
|
+
* `stream` for readable inputs or `filepath` for files on disk. `length` stays
|
|
25
|
+
* undefined for streams whose size cannot be inferred.
|
|
26
|
+
*/
|
|
27
|
+
type ResolvedItem = {
|
|
28
|
+
path: string[];
|
|
29
|
+
length?: number;
|
|
30
|
+
autoNamed?: boolean;
|
|
31
|
+
} & ({
|
|
32
|
+
stream: Readable;
|
|
33
|
+
} | {
|
|
34
|
+
filepath: string;
|
|
35
|
+
});
|
|
14
36
|
export interface PieceBuffer {
|
|
15
37
|
buffer: Buffer | null;
|
|
16
38
|
length: number;
|
|
@@ -20,6 +42,16 @@ export interface HasherOptions {
|
|
|
20
42
|
maxFiles?: number;
|
|
21
43
|
stopOnErr?: boolean;
|
|
22
44
|
}
|
|
45
|
+
export declare const isReadableStream: (val: unknown) => val is Readable;
|
|
46
|
+
export declare const isStreamEntry: (val: unknown) => val is StreamEntry;
|
|
47
|
+
/**
|
|
48
|
+
* Builds a torrent's file list and hashes it into pieces.
|
|
49
|
+
*
|
|
50
|
+
* Inputs are first resolved and validated, then piece offsets are planned. When
|
|
51
|
+
* every input length is known, files are read in parallel. If any stream length
|
|
52
|
+
* is unknown, hashing switches to a single sequential pass because each input's
|
|
53
|
+
* starting offset depends on everything before it.
|
|
54
|
+
*/
|
|
23
55
|
export default class Hasher extends Stream {
|
|
24
56
|
pieceLength: number;
|
|
25
57
|
maxFiles: number;
|
|
@@ -37,18 +69,97 @@ export default class Hasher extends Stream {
|
|
|
37
69
|
totalSize: number;
|
|
38
70
|
lastPieceLength: number;
|
|
39
71
|
streamGroup: StreamSpeed;
|
|
72
|
+
unknownLengths: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Resolves the inputs and prepares the hasher.
|
|
75
|
+
*/
|
|
76
|
+
constructor(dir: string, list: HasherInput[], pieceLength?: number | null, options?: HasherOptions);
|
|
77
|
+
/**
|
|
78
|
+
* Reports an error on the next tick so callers can attach listeners.
|
|
79
|
+
*/
|
|
80
|
+
private _emitError;
|
|
81
|
+
/**
|
|
82
|
+
* Resolves streams and paths into a single ordered list of torrent entries.
|
|
83
|
+
*
|
|
84
|
+
* Unlike pure path lists (which are sorted globally), the order of the input
|
|
85
|
+
* list is authoritative here. Directories are expanded in place and their
|
|
86
|
+
* entries are sorted within that directory.
|
|
87
|
+
*/
|
|
88
|
+
private _resolveInputs;
|
|
89
|
+
/**
|
|
90
|
+
* Builds a torrent entry for a bare readable stream.
|
|
91
|
+
*/
|
|
92
|
+
private _streamItem;
|
|
93
|
+
/**
|
|
94
|
+
* Builds a torrent entry for a `{ stream, path, length }` input.
|
|
95
|
+
*/
|
|
96
|
+
private _streamEntryItem;
|
|
97
|
+
/**
|
|
98
|
+
* Validates an input path, then expands it into files in list order.
|
|
99
|
+
*/
|
|
100
|
+
private _collectPath;
|
|
101
|
+
/**
|
|
102
|
+
* Recursively walks a path. Directories are expanded in sorted order.
|
|
103
|
+
*/
|
|
104
|
+
private _walkPath;
|
|
105
|
+
/**
|
|
106
|
+
* Rejects invalid paths and stream lengths.
|
|
107
|
+
*/
|
|
108
|
+
private _validateItems;
|
|
109
|
+
/**
|
|
110
|
+
* Reserves explicit paths and gives unnamed streams unique suffixes.
|
|
111
|
+
*/
|
|
112
|
+
private _assignNames;
|
|
113
|
+
/**
|
|
114
|
+
* Starts reading pure path inputs. Paths are sorted so the resulting torrent
|
|
115
|
+
* does not depend on filesystem iteration order.
|
|
116
|
+
*/
|
|
117
|
+
private _beginPathInputs;
|
|
40
118
|
/**
|
|
41
|
-
*
|
|
119
|
+
* Stats and expands paths, then plans the piece layout.
|
|
42
120
|
*/
|
|
43
|
-
|
|
121
|
+
private _loadPaths;
|
|
44
122
|
/**
|
|
45
|
-
*
|
|
123
|
+
* Uses an already-known file list, such as one from an existing torrent.
|
|
124
|
+
* Order is preserved because it determines the piece hashes.
|
|
125
|
+
*/
|
|
126
|
+
private _useFileEntries;
|
|
127
|
+
/**
|
|
128
|
+
* Starts hashing a resolved stream/path list.
|
|
129
|
+
*
|
|
130
|
+
* When every length is known, piece offsets can be planned up front and files
|
|
131
|
+
* are hashed in parallel. If any stream length is unknown, hashing falls back
|
|
132
|
+
* to a single sequential pass because later offsets cannot be known yet.
|
|
133
|
+
*/
|
|
134
|
+
private _beginHashing;
|
|
135
|
+
/**
|
|
136
|
+
* Marks the hasher ready, then starts hashing on the next tick.
|
|
137
|
+
*/
|
|
138
|
+
private _readyAndStart;
|
|
139
|
+
/**
|
|
140
|
+
* Plans which pieces each file covers.
|
|
141
|
+
*
|
|
142
|
+
* Files are laid out consecutively, so a file may start partway into a piece
|
|
143
|
+
* and may span several pieces. Each task records its first piece and the
|
|
144
|
+
* offset within that piece.
|
|
46
145
|
*/
|
|
47
146
|
_CreateFileTasks(): void;
|
|
48
147
|
/**
|
|
49
|
-
*
|
|
148
|
+
* Hashes a fully planned file list in parallel.
|
|
149
|
+
*
|
|
150
|
+
* Only used when every input length is known, since piece offsets must be
|
|
151
|
+
* calculated before reading starts.
|
|
50
152
|
*/
|
|
51
153
|
_start(): void;
|
|
154
|
+
/**
|
|
155
|
+
* Hashes inputs one at a time in list order.
|
|
156
|
+
*
|
|
157
|
+
* Used whenever any stream length is unknown: later piece offsets cannot be
|
|
158
|
+
* planned until earlier inputs have been read, so parallelism is not possible.
|
|
159
|
+
* File entries are recorded as each input finishes, which lets `make` build
|
|
160
|
+
* metadata once the whole list has been consumed.
|
|
161
|
+
*/
|
|
162
|
+
_startSequential(items: ResolvedItem[]): void;
|
|
52
163
|
_writePiece(pieces: Array<PieceBuffer | null>, index: number, offset: number, data: Buffer, start: number, end: number, file?: string, readSoFar?: number): number;
|
|
53
164
|
/**
|
|
54
165
|
* Recursively traverses files to call fs.stat on them.
|
|
@@ -79,3 +190,4 @@ export default class Hasher extends Stream {
|
|
|
79
190
|
*/
|
|
80
191
|
destroy(): boolean;
|
|
81
192
|
}
|
|
193
|
+
export {};
|