bun-torrent 0.0.2-beta → 0.0.3-beta
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/client.d.ts +50 -0
- package/dist/client.error.d.ts +8 -0
- package/dist/client.error.js +12 -0
- package/dist/client.error.js.map +1 -0
- package/dist/client.js +112 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/peer/availability.d.ts +50 -0
- package/{src/peer/availability.ts → dist/peer/availability.js} +14 -31
- package/dist/peer/availability.js.map +1 -0
- package/dist/peer/consts.d.ts +8 -0
- package/{src/peer/consts.ts → dist/peer/consts.js} +1 -3
- package/dist/peer/consts.js.map +1 -0
- package/dist/peer/handshake/handshake.error.d.ts +11 -0
- package/dist/peer/handshake/handshake.error.js +15 -0
- package/dist/peer/handshake/handshake.error.js.map +1 -0
- package/dist/peer/handshake/index.d.ts +7 -0
- package/{src/peer/handshake/index.ts → dist/peer/handshake/index.js} +8 -42
- package/dist/peer/handshake/index.js.map +1 -0
- package/{src/peer/index.ts → dist/peer/index.d.ts} +2 -21
- package/dist/peer/index.js +8 -0
- package/dist/peer/index.js.map +1 -0
- package/dist/peer/messages/error.d.ts +12 -0
- package/dist/peer/messages/error.js +16 -0
- package/dist/peer/messages/error.js.map +1 -0
- package/dist/peer/messages/helpers.d.ts +5 -0
- package/{src/peer/messages/helpers.ts → dist/peer/messages/helpers.js} +7 -18
- package/dist/peer/messages/helpers.js.map +1 -0
- package/dist/peer/messages/index.d.ts +6 -0
- package/{src/peer/messages/index.ts → dist/peer/messages/index.js} +30 -80
- package/dist/peer/messages/index.js.map +1 -0
- package/{src/peer/messages/types.ts → dist/peer/messages/types.d.ts} +3 -25
- package/dist/peer/messages/types.js +14 -0
- package/dist/peer/messages/types.js.map +1 -0
- package/dist/peer/peer-id.d.ts +1 -0
- package/{src/peer/peer-id.ts → dist/peer/peer-id.js} +2 -3
- package/dist/peer/peer-id.js.map +1 -0
- package/dist/peer/pool/index.d.ts +65 -0
- package/dist/peer/pool/index.js +207 -0
- package/dist/peer/pool/index.js.map +1 -0
- package/dist/peer/pool/pool.error.d.ts +10 -0
- package/dist/peer/pool/pool.error.js +15 -0
- package/dist/peer/pool/pool.error.js.map +1 -0
- package/dist/peer/session/index.d.ts +35 -0
- package/dist/peer/session/index.js +195 -0
- package/dist/peer/session/index.js.map +1 -0
- package/dist/peer/session/session.error.d.ts +12 -0
- package/dist/peer/session/session.error.js +17 -0
- package/dist/peer/session/session.error.js.map +1 -0
- package/dist/peer/types.js +2 -0
- package/dist/peer/types.js.map +1 -0
- package/dist/torrent/bencode/decoder.d.ts +2 -0
- package/dist/torrent/bencode/decoder.error.d.ts +24 -0
- package/dist/torrent/bencode/decoder.error.js +34 -0
- package/dist/torrent/bencode/decoder.error.js.map +1 -0
- package/{src/torrent/bencode/decoder.ts → dist/torrent/bencode/decoder.js} +44 -92
- package/dist/torrent/bencode/decoder.js.map +1 -0
- package/dist/torrent/bencode/encoder.d.ts +2 -0
- package/dist/torrent/bencode/encoder.error.d.ts +10 -0
- package/dist/torrent/bencode/encoder.error.js +14 -0
- package/dist/torrent/bencode/encoder.error.js.map +1 -0
- package/{src/torrent/bencode/encoder.ts → dist/torrent/bencode/encoder.js} +24 -38
- package/dist/torrent/bencode/encoder.js.map +1 -0
- package/{src/torrent/bencode/index.ts → dist/torrent/bencode/index.d.ts} +0 -2
- package/dist/torrent/bencode/index.js +6 -0
- package/dist/torrent/bencode/index.js.map +1 -0
- package/{src/torrent/bencode/types.ts → dist/torrent/bencode/types.d.ts} +7 -9
- package/dist/torrent/bencode/types.js +10 -0
- package/dist/torrent/bencode/types.js.map +1 -0
- package/dist/torrent/bencode/utils.d.ts +5 -0
- package/dist/torrent/bencode/utils.js +12 -0
- package/dist/torrent/bencode/utils.js.map +1 -0
- package/dist/torrent/download/index.d.ts +2 -0
- package/dist/torrent/download/index.js +2 -0
- package/dist/torrent/download/index.js.map +1 -0
- package/dist/torrent/download/manager.d.ts +79 -0
- package/dist/torrent/download/manager.js +270 -0
- package/dist/torrent/download/manager.js.map +1 -0
- package/dist/torrent/file-selection.d.ts +6 -0
- package/dist/torrent/file-selection.js +32 -0
- package/dist/torrent/file-selection.js.map +1 -0
- package/dist/torrent/index.d.ts +17 -0
- package/dist/torrent/index.js +11 -0
- package/dist/torrent/index.js.map +1 -0
- package/dist/torrent/parser/helpers.d.ts +8 -0
- package/{src/torrent/parser/helpers.ts → dist/torrent/parser/helpers.js} +13 -24
- package/dist/torrent/parser/helpers.js.map +1 -0
- package/dist/torrent/parser/index.d.ts +2 -0
- package/{src/torrent/parser/index.ts → dist/torrent/parser/index.js} +25 -103
- package/dist/torrent/parser/index.js.map +1 -0
- package/dist/torrent/parser/info-hash.d.ts +2 -0
- package/dist/torrent/parser/info-hash.js +6 -0
- package/dist/torrent/parser/info-hash.js.map +1 -0
- package/dist/torrent/parser/parser.error.d.ts +14 -0
- package/dist/torrent/parser/parser.error.js +19 -0
- package/dist/torrent/parser/parser.error.js.map +1 -0
- package/dist/torrent/pieces/DefaultPlanner.d.ts +98 -0
- package/{src/torrent/pieces/DefaultPlanner.ts → dist/torrent/pieces/DefaultPlanner.js} +49 -93
- package/dist/torrent/pieces/DefaultPlanner.js.map +1 -0
- package/dist/torrent/pieces/index.d.ts +5 -0
- package/dist/torrent/pieces/index.js +5 -0
- package/dist/torrent/pieces/index.js.map +1 -0
- package/{src/torrent/pieces/planner.ts → dist/torrent/pieces/planner.d.ts} +2 -7
- package/dist/torrent/pieces/planner.error.d.ts +11 -0
- package/dist/torrent/pieces/planner.error.js +16 -0
- package/dist/torrent/pieces/planner.error.js.map +1 -0
- package/dist/torrent/pieces/planner.js +11 -0
- package/dist/torrent/pieces/planner.js.map +1 -0
- package/{src/torrent/pieces/types.ts → dist/torrent/pieces/types.d.ts} +1 -13
- package/dist/torrent/pieces/types.js +2 -0
- package/dist/torrent/pieces/types.js.map +1 -0
- package/dist/torrent/pieces/utils.d.ts +34 -0
- package/{src/torrent/pieces/utils.ts → dist/torrent/pieces/utils.js} +8 -37
- package/dist/torrent/pieces/utils.js.map +1 -0
- package/dist/torrent/pieces/validation.d.ts +14 -0
- package/{src/torrent/pieces/validation.ts → dist/torrent/pieces/validation.js} +4 -13
- package/dist/torrent/pieces/validation.js.map +1 -0
- package/dist/torrent/session/index.d.ts +74 -0
- package/dist/torrent/session/index.js +114 -0
- package/dist/torrent/session/index.js.map +1 -0
- package/dist/torrent/storage/index.d.ts +35 -0
- package/{src/torrent/storage/index.ts → dist/torrent/storage/index.js} +33 -94
- package/dist/torrent/storage/index.js.map +1 -0
- package/dist/torrent/storage/storage.error.d.ts +11 -0
- package/dist/torrent/storage/storage.error.js +16 -0
- package/dist/torrent/storage/storage.error.js.map +1 -0
- package/{src/torrent/storage/types.ts → dist/torrent/storage/types.d.ts} +0 -2
- package/dist/torrent/storage/types.js +2 -0
- package/dist/torrent/storage/types.js.map +1 -0
- package/{src/torrent/types.ts → dist/torrent/types.d.ts} +0 -1
- package/dist/torrent/types.js +2 -0
- package/dist/torrent/types.js.map +1 -0
- package/dist/tracker/http.d.ts +4 -0
- package/{src/tracker/http.ts → dist/tracker/http.js} +24 -74
- package/dist/tracker/http.js.map +1 -0
- package/dist/tracker/index.d.ts +12 -0
- package/dist/tracker/index.js +49 -0
- package/dist/tracker/index.js.map +1 -0
- package/dist/tracker/tracker.error.d.ts +19 -0
- package/dist/tracker/tracker.error.js +24 -0
- package/dist/tracker/tracker.error.js.map +1 -0
- package/dist/tracker/types.d.ts +11 -0
- package/dist/tracker/types.js +2 -0
- package/dist/tracker/types.js.map +1 -0
- package/dist/tracker/udp.d.ts +3 -0
- package/{src/tracker/udp.ts → dist/tracker/udp.js} +27 -96
- package/dist/tracker/udp.js.map +1 -0
- package/dist/utils/buffers.d.ts +5 -0
- package/{src/utils/buffers.ts → dist/utils/buffers.js} +7 -14
- package/dist/utils/buffers.js.map +1 -0
- package/dist/utils/errors.d.ts +4 -0
- package/{src/utils/errors.ts → dist/utils/errors.js} +4 -4
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/formats.d.ts +1 -0
- package/{src/utils/formats.ts → dist/utils/formats.js} +4 -3
- package/dist/utils/formats.js.map +1 -0
- package/dist/utils/sha1.d.ts +1 -0
- package/dist/utils/sha1.js +3 -0
- package/dist/utils/sha1.js.map +1 -0
- package/package.json +10 -6
- package/src/app.ts +0 -65
- package/src/client.error.ts +0 -12
- package/src/client.test.ts +0 -121
- package/src/client.ts +0 -185
- package/src/index.test.ts +0 -7
- package/src/index.ts +0 -76
- package/src/peer/__tests__/peer-id.test.ts +0 -24
- package/src/peer/availability.test.ts +0 -50
- package/src/peer/handshake/handshake.error.ts +0 -15
- package/src/peer/handshake/handshake.test.ts +0 -125
- package/src/peer/messages/error.ts +0 -16
- package/src/peer/messages/messages.test.ts +0 -231
- package/src/peer/pool/index.ts +0 -301
- package/src/peer/pool/pool.error.ts +0 -17
- package/src/peer/pool/pool.test.ts +0 -305
- package/src/peer/session/index.ts +0 -276
- package/src/peer/session/session.error.ts +0 -19
- package/src/peer/session/session.test.ts +0 -110
- package/src/torrent/bencode/__tests__/decoder.test.ts +0 -212
- package/src/torrent/bencode/__tests__/encoder.test.ts +0 -138
- package/src/torrent/bencode/__tests__/encoder.unit.test.ts +0 -24
- package/src/torrent/bencode/__tests__/integration.test.ts +0 -64
- package/src/torrent/bencode/decoder.error.ts +0 -36
- package/src/torrent/bencode/encoder.error.ts +0 -14
- package/src/torrent/bencode/utils.ts +0 -17
- package/src/torrent/download/index.ts +0 -9
- package/src/torrent/download/manager.test.ts +0 -393
- package/src/torrent/download/manager.ts +0 -376
- package/src/torrent/file-selection.ts +0 -51
- package/src/torrent/index.ts +0 -61
- package/src/torrent/parser/info-hash.test.ts +0 -39
- package/src/torrent/parser/info-hash.ts +0 -7
- package/src/torrent/parser/parser.error.ts +0 -21
- package/src/torrent/parser/parser.test.ts +0 -286
- package/src/torrent/pieces/index.ts +0 -20
- package/src/torrent/pieces/planner.error.ts +0 -18
- package/src/torrent/pieces/planner.test.ts +0 -303
- package/src/torrent/pieces/validation.test.ts +0 -32
- package/src/torrent/session/index.ts +0 -195
- package/src/torrent/session/session.test.ts +0 -279
- package/src/torrent/storage/storage.error.ts +0 -18
- package/src/torrent/storage/storage.test.ts +0 -326
- package/src/tracker/http.test.ts +0 -66
- package/src/tracker/index.ts +0 -93
- package/src/tracker/tracker.error.ts +0 -26
- package/src/tracker/types.ts +0 -17
- package/src/tracker/udp.test.ts +0 -155
- package/src/utils/__tests__/sha1.test.ts +0 -16
- package/src/utils/sha1.ts +0 -4
- /package/{src/peer/types.ts → dist/peer/types.d.ts} +0 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type DownloadProgressEventMode } from './torrent/download';
|
|
2
|
+
import { Torrent } from './torrent/session/index';
|
|
3
|
+
import type { TorrentMetadata } from './torrent/types';
|
|
4
|
+
import type { TorrentFileSelection } from './torrent/file-selection';
|
|
5
|
+
export declare enum DownloadState {
|
|
6
|
+
PARSING = "parsing",
|
|
7
|
+
TRACKING = "tracking",
|
|
8
|
+
CONNECTING = "connecting",
|
|
9
|
+
DOWNLOADING = "downloading"
|
|
10
|
+
}
|
|
11
|
+
export type ClientConfig = {
|
|
12
|
+
files?: TorrentFileSelection;
|
|
13
|
+
maxInFlightRequestsPerPeer?: number;
|
|
14
|
+
outputDirectory?: string;
|
|
15
|
+
progressEvents?: DownloadProgressEventMode;
|
|
16
|
+
requestTimeoutMs?: number;
|
|
17
|
+
speedSampleIntervalMs?: number;
|
|
18
|
+
};
|
|
19
|
+
export declare const DEFAULT_CLIENT_CONFIG: {
|
|
20
|
+
readonly maxInFlightRequestsPerPeer: 20;
|
|
21
|
+
readonly progressEvents: "piece";
|
|
22
|
+
readonly requestTimeoutMs: 15000;
|
|
23
|
+
readonly speedSampleIntervalMs: 500;
|
|
24
|
+
};
|
|
25
|
+
export declare class Client {
|
|
26
|
+
private readonly config;
|
|
27
|
+
private readonly peerId;
|
|
28
|
+
constructor(config?: ClientConfig);
|
|
29
|
+
download(input: {
|
|
30
|
+
torrentFile: string | Uint8Array | ArrayBuffer;
|
|
31
|
+
}, options?: DownloadOptions): Promise<Torrent>;
|
|
32
|
+
inspect(input: {
|
|
33
|
+
torrentFile: string | Uint8Array | ArrayBuffer;
|
|
34
|
+
}): Promise<TorrentMetadata>;
|
|
35
|
+
private trackPeers;
|
|
36
|
+
}
|
|
37
|
+
export type DownloadOptions = {
|
|
38
|
+
announcePort?: number;
|
|
39
|
+
files?: TorrentFileSelection;
|
|
40
|
+
maxInFlightRequestsPerPeer?: number;
|
|
41
|
+
minConnections?: number;
|
|
42
|
+
onChangeState?: (state: DownloadState) => unknown;
|
|
43
|
+
outputDirectory?: string;
|
|
44
|
+
progressEvents?: DownloadProgressEventMode;
|
|
45
|
+
requestTimeoutMs?: number;
|
|
46
|
+
speedSampleIntervalMs?: number;
|
|
47
|
+
};
|
|
48
|
+
export type TorrentFileInput = string | Uint8Array | ArrayBuffer;
|
|
49
|
+
export declare const readTorrentFile: (input: TorrentFileInput) => Promise<Uint8Array>;
|
|
50
|
+
export { Client as TorrentClient };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BunTorrentError } from './utils/errors';
|
|
2
|
+
export declare enum ClientErrorCode {
|
|
3
|
+
INVALID_FILE_SELECTION = "CLIENT_INVALID_FILE_SELECTION",
|
|
4
|
+
UNSUPPORTED_TORRENT_FILE_INPUT = "CLIENT_UNSUPPORTED_TORRENT_FILE_INPUT"
|
|
5
|
+
}
|
|
6
|
+
export declare class ClientError extends BunTorrentError {
|
|
7
|
+
constructor(code: ClientErrorCode, message: string);
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BunTorrentError } from './utils/errors';
|
|
2
|
+
export var ClientErrorCode;
|
|
3
|
+
(function (ClientErrorCode) {
|
|
4
|
+
ClientErrorCode["INVALID_FILE_SELECTION"] = "CLIENT_INVALID_FILE_SELECTION";
|
|
5
|
+
ClientErrorCode["UNSUPPORTED_TORRENT_FILE_INPUT"] = "CLIENT_UNSUPPORTED_TORRENT_FILE_INPUT";
|
|
6
|
+
})(ClientErrorCode || (ClientErrorCode = {}));
|
|
7
|
+
export class ClientError extends BunTorrentError {
|
|
8
|
+
constructor(code, message) {
|
|
9
|
+
super(message, code);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=client.error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.error.js","sourceRoot":"","sources":["../src/client.error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD,MAAM,CAAN,IAAY,eAGX;AAHD,WAAY,eAAe;IACvB,2EAAwD,CAAA;IACxD,2FAAwE,CAAA;AAC5E,CAAC,EAHW,eAAe,KAAf,eAAe,QAG1B;AAED,MAAM,OAAO,WAAY,SAAQ,eAAe;IAC5C,YAAY,IAAqB,EAAE,OAAe;QAC9C,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC;CACJ"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createPeerId } from './peer/peer-id';
|
|
2
|
+
import { openPeerPool } from './peer/pool';
|
|
3
|
+
import { DownloadManager } from './torrent/download';
|
|
4
|
+
import { parseTorrent } from './torrent/parser';
|
|
5
|
+
import { Torrent } from './torrent/session/index';
|
|
6
|
+
import { ClientError, ClientErrorCode } from './client.error';
|
|
7
|
+
import { trackPeers, TrackerError, TrackerErrorCode } from './tracker';
|
|
8
|
+
import { getUnknownSelectedFiles, normalizeTorrentFileSelection } from './torrent/file-selection';
|
|
9
|
+
export var DownloadState;
|
|
10
|
+
(function (DownloadState) {
|
|
11
|
+
DownloadState["PARSING"] = "parsing";
|
|
12
|
+
DownloadState["TRACKING"] = "tracking";
|
|
13
|
+
DownloadState["CONNECTING"] = "connecting";
|
|
14
|
+
DownloadState["DOWNLOADING"] = "downloading";
|
|
15
|
+
})(DownloadState || (DownloadState = {}));
|
|
16
|
+
export const DEFAULT_CLIENT_CONFIG = {
|
|
17
|
+
maxInFlightRequestsPerPeer: 20,
|
|
18
|
+
progressEvents: 'piece',
|
|
19
|
+
requestTimeoutMs: 15_000,
|
|
20
|
+
speedSampleIntervalMs: 500,
|
|
21
|
+
};
|
|
22
|
+
export class Client {
|
|
23
|
+
config;
|
|
24
|
+
peerId;
|
|
25
|
+
constructor(config = {}) {
|
|
26
|
+
this.config = config;
|
|
27
|
+
this.peerId = createPeerId();
|
|
28
|
+
}
|
|
29
|
+
async download(input, options = {}) {
|
|
30
|
+
options.onChangeState?.(DownloadState.PARSING);
|
|
31
|
+
const meta = await this.inspect(input);
|
|
32
|
+
const downloadConfig = resolveDownloadConfig(this.config, options);
|
|
33
|
+
assertValidFileSelection(meta, downloadConfig.files);
|
|
34
|
+
options.onChangeState?.(DownloadState.TRACKING);
|
|
35
|
+
const peers = await this.trackPeers(meta, options);
|
|
36
|
+
options.onChangeState?.(DownloadState.CONNECTING);
|
|
37
|
+
const pool = await openPeerPool(peers, {
|
|
38
|
+
infoHash: meta.infoHash,
|
|
39
|
+
peerId: this.peerId,
|
|
40
|
+
targetConnections: 20,
|
|
41
|
+
totalPieces: meta.pieces.length,
|
|
42
|
+
minConnections: options.minConnections ?? 0,
|
|
43
|
+
maxConnecting: 30,
|
|
44
|
+
timeoutMs: 5_000,
|
|
45
|
+
});
|
|
46
|
+
options.onChangeState?.(DownloadState.DOWNLOADING);
|
|
47
|
+
return new Torrent(meta, pool, new DownloadManager({
|
|
48
|
+
metadata: meta,
|
|
49
|
+
files: downloadConfig.files,
|
|
50
|
+
outputDirectory: downloadConfig.outputDirectory,
|
|
51
|
+
peerPool: pool,
|
|
52
|
+
maxInFlightRequestsPerPeer: downloadConfig.maxInFlightRequestsPerPeer,
|
|
53
|
+
progressEvents: downloadConfig.progressEvents,
|
|
54
|
+
requestTimeoutMs: downloadConfig.requestTimeoutMs,
|
|
55
|
+
speedSampleIntervalMs: downloadConfig.speedSampleIntervalMs,
|
|
56
|
+
}), normalizeTorrentFileSelection(downloadConfig.files));
|
|
57
|
+
}
|
|
58
|
+
async inspect(input) {
|
|
59
|
+
const bytes = await readTorrentFile(input.torrentFile);
|
|
60
|
+
return parseTorrent(bytes);
|
|
61
|
+
}
|
|
62
|
+
async trackPeers(meta, options) {
|
|
63
|
+
try {
|
|
64
|
+
return await trackPeers({
|
|
65
|
+
meta,
|
|
66
|
+
peerId: this.peerId,
|
|
67
|
+
announcePort: options.announcePort,
|
|
68
|
+
timeoutMs: 5_000,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (isNonFatalTrackerError(error))
|
|
73
|
+
return [];
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const isNonFatalTrackerError = (error) => error instanceof TrackerError &&
|
|
79
|
+
(error.code === TrackerErrorCode.ANNOUNCE_FAILED ||
|
|
80
|
+
error.code === TrackerErrorCode.NO_PEERS ||
|
|
81
|
+
error.code === TrackerErrorCode.NO_SUPPORTED_TRACKERS);
|
|
82
|
+
const resolveDownloadConfig = (config, options) => ({
|
|
83
|
+
files: options.files ?? config.files ?? null,
|
|
84
|
+
maxInFlightRequestsPerPeer: options.maxInFlightRequestsPerPeer ??
|
|
85
|
+
config.maxInFlightRequestsPerPeer ??
|
|
86
|
+
DEFAULT_CLIENT_CONFIG.maxInFlightRequestsPerPeer,
|
|
87
|
+
outputDirectory: options.outputDirectory ?? config.outputDirectory ?? process.cwd(),
|
|
88
|
+
progressEvents: options.progressEvents ?? config.progressEvents ?? DEFAULT_CLIENT_CONFIG.progressEvents,
|
|
89
|
+
requestTimeoutMs: options.requestTimeoutMs ??
|
|
90
|
+
config.requestTimeoutMs ??
|
|
91
|
+
DEFAULT_CLIENT_CONFIG.requestTimeoutMs,
|
|
92
|
+
speedSampleIntervalMs: options.speedSampleIntervalMs ??
|
|
93
|
+
config.speedSampleIntervalMs ??
|
|
94
|
+
DEFAULT_CLIENT_CONFIG.speedSampleIntervalMs,
|
|
95
|
+
});
|
|
96
|
+
const assertValidFileSelection = (metadata, files) => {
|
|
97
|
+
const unknownFiles = getUnknownSelectedFiles(metadata, files);
|
|
98
|
+
if (unknownFiles.length === 0)
|
|
99
|
+
return;
|
|
100
|
+
throw new ClientError(ClientErrorCode.INVALID_FILE_SELECTION, `Unknown torrent file selection: ${unknownFiles.join(', ')}`);
|
|
101
|
+
};
|
|
102
|
+
export const readTorrentFile = async (input) => {
|
|
103
|
+
if (typeof input === 'string')
|
|
104
|
+
return await Bun.file(input).bytes();
|
|
105
|
+
if (input instanceof Uint8Array)
|
|
106
|
+
return input;
|
|
107
|
+
if (input instanceof ArrayBuffer)
|
|
108
|
+
return new Uint8Array(input);
|
|
109
|
+
throw new ClientError(ClientErrorCode.UNSUPPORTED_TORRENT_FILE_INPUT, 'Unsupported torrent file input');
|
|
110
|
+
};
|
|
111
|
+
export { Client as TorrentClient };
|
|
112
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAkC,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;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C;AACN,CAAC,CAAC;AAIF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,KAAuB,EAAuB,EAAE;IAClF,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,MAAM,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,CAAC;IACpE,IAAI,KAAK,YAAY,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9C,IAAI,KAAK,YAAY,WAAW;QAAE,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAE/D,MAAM,IAAI,WAAW,CACjB,eAAe,CAAC,8BAA8B,EAC9C,gCAAgC,CACnC,CAAC;AACN,CAAC,CAAC;AAEF,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Nikola Nedeljkovic
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
export * from './client';
|
|
6
|
+
export { ClientError, ClientErrorCode } from './client.error';
|
|
7
|
+
export { BencodeDecodeError, BencodeDecodeErrorCode, BencodeEncodeError, BencodeEncodeErrorCode, computeInfoHash, decodeBencode, encodeBencode, parseTorrent, Torrent, TorrentState, toBValue, TorrentParseError, TorrentParseErrorCode, type TorrentFileSelection, type TorrentMetadata, } from './torrent/index';
|
|
8
|
+
export { connectToPeers, createPeerId, decodeHandshake, decodePeerMessage, encodePeerMessage, encodeHandshake, HandshakeErrorCode, PeerMessageError, PeerMessageErrorCode, PeerMessageId, PeerHandshakeError, PeerPoolError, PeerPoolErrorCode, PeerPool, PeerSession, PeerSessionError, PeerSessionErrorCode, openPeerPool, type PeerHandshake, } from './peer/index';
|
|
9
|
+
export { TrackerError, TrackerErrorCode } from './tracker';
|
|
10
|
+
export type { BitfieldMessage, CancelMessage, ChokeMessage, HaveMessage, InterestedMessage, KeepAliveMessage, NotInterestedMessage, PeerMessage, PieceMessage, RequestMessage, UnchokeMessage, PeerConnectionSession, PeerPoolOptions, PeerSessionConnectOptions, } from './peer/index';
|
|
11
|
+
export { BunTorrentError } from './utils/errors';
|
|
12
|
+
export type { BBytes, BDict, BInteger, BList, BValue, BencodeInput, TorrentFiles, TorrentStats, TorrentStateChange, } from './torrent/index';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Nikola Nedeljkovic
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
export * from './client';
|
|
6
|
+
export { ClientError, ClientErrorCode } from './client.error';
|
|
7
|
+
export { BencodeDecodeError, BencodeDecodeErrorCode, BencodeEncodeError, BencodeEncodeErrorCode, computeInfoHash, decodeBencode, encodeBencode, parseTorrent, Torrent, TorrentState, toBValue, TorrentParseError, TorrentParseErrorCode, } from './torrent/index';
|
|
8
|
+
export { connectToPeers, createPeerId, decodeHandshake, decodePeerMessage, encodePeerMessage, encodeHandshake, HandshakeErrorCode, PeerMessageError, PeerMessageErrorCode, PeerMessageId, PeerHandshakeError, PeerPoolError, PeerPoolErrorCode, PeerPool, PeerSession, PeerSessionError, PeerSessionErrorCode, openPeerPool, } from './peer/index';
|
|
9
|
+
export { TrackerError, TrackerErrorCode } from './tracker';
|
|
10
|
+
export { BunTorrentError } from './utils/errors';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE9D,OAAO,EACH,kBAAkB,EAClB,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,eAAe,EACf,aAAa,EACb,aAAa,EACb,YAAY,EACZ,OAAO,EACP,YAAY,EACZ,QAAQ,EACR,iBAAiB,EACjB,qBAAqB,GAGxB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACH,cAAc,EACd,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,aAAa,EACb,kBAAkB,EAClB,aAAa,EACb,iBAAiB,EACjB,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,oBAAoB,EACpB,YAAY,GAEf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAkB3D,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type PeerPieceAvailabilityOptions = {
|
|
2
|
+
bitfield?: Uint8Array;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* Mutable view of which torrent pieces a peer claims to have.
|
|
6
|
+
*/
|
|
7
|
+
export declare class PeerPieceAvailability {
|
|
8
|
+
readonly totalPieces: number;
|
|
9
|
+
private readonly pieces;
|
|
10
|
+
/**
|
|
11
|
+
* Create availability state from an optional peer bitfield.
|
|
12
|
+
*
|
|
13
|
+
* @param totalPieces - Number of pieces in the torrent.
|
|
14
|
+
* @param options - Optional initial bitfield from the peer.
|
|
15
|
+
*/
|
|
16
|
+
constructor(totalPieces: number, options?: PeerPieceAvailabilityOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Return true when the peer claims to have a piece.
|
|
19
|
+
*
|
|
20
|
+
* @param pieceIndex - Zero-based piece index.
|
|
21
|
+
* @returns Whether the peer has the piece. Invalid indexes return false.
|
|
22
|
+
*/
|
|
23
|
+
hasPiece(pieceIndex: number): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Mark one piece as available after receiving a peer `have` message.
|
|
26
|
+
*
|
|
27
|
+
* @param pieceIndex - Zero-based piece index.
|
|
28
|
+
*/
|
|
29
|
+
markHave(pieceIndex: number): void;
|
|
30
|
+
/**
|
|
31
|
+
* Replace availability from a peer `bitfield` message.
|
|
32
|
+
*
|
|
33
|
+
* @param bitfield - Raw bitfield payload from the peer.
|
|
34
|
+
*/
|
|
35
|
+
setBitfield(bitfield: Uint8Array): void;
|
|
36
|
+
/**
|
|
37
|
+
* Return available piece indexes in ascending order.
|
|
38
|
+
*
|
|
39
|
+
* @returns Piece indexes currently marked as available.
|
|
40
|
+
*/
|
|
41
|
+
toPieceIndexes(): number[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create mutable availability state from an optional peer bitfield.
|
|
45
|
+
*
|
|
46
|
+
* @param totalPieces - Number of pieces in the torrent.
|
|
47
|
+
* @param bitfield - Optional raw bitfield payload from the peer.
|
|
48
|
+
* @returns Peer piece availability state.
|
|
49
|
+
*/
|
|
50
|
+
export declare const createPeerPieceAvailability: (totalPieces: number, bitfield?: Uint8Array) => PeerPieceAvailability;
|
|
@@ -1,72 +1,58 @@
|
|
|
1
|
-
export type PeerPieceAvailabilityOptions = {
|
|
2
|
-
bitfield?: Uint8Array;
|
|
3
|
-
};
|
|
4
|
-
|
|
5
1
|
/**
|
|
6
2
|
* Mutable view of which torrent pieces a peer claims to have.
|
|
7
3
|
*/
|
|
8
4
|
export class PeerPieceAvailability {
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
totalPieces;
|
|
6
|
+
pieces;
|
|
11
7
|
/**
|
|
12
8
|
* Create availability state from an optional peer bitfield.
|
|
13
9
|
*
|
|
14
10
|
* @param totalPieces - Number of pieces in the torrent.
|
|
15
11
|
* @param options - Optional initial bitfield from the peer.
|
|
16
12
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
) {
|
|
21
|
-
this.pieces = Array.from({ length: totalPieces }, (_, pieceIndex) =>
|
|
22
|
-
options.bitfield ? hasBitfieldPiece(options.bitfield, pieceIndex) : false,
|
|
23
|
-
);
|
|
13
|
+
constructor(totalPieces, options = {}) {
|
|
14
|
+
this.totalPieces = totalPieces;
|
|
15
|
+
this.pieces = Array.from({ length: totalPieces }, (_, pieceIndex) => options.bitfield ? hasBitfieldPiece(options.bitfield, pieceIndex) : false);
|
|
24
16
|
}
|
|
25
|
-
|
|
26
17
|
/**
|
|
27
18
|
* Return true when the peer claims to have a piece.
|
|
28
19
|
*
|
|
29
20
|
* @param pieceIndex - Zero-based piece index.
|
|
30
21
|
* @returns Whether the peer has the piece. Invalid indexes return false.
|
|
31
22
|
*/
|
|
32
|
-
|
|
23
|
+
hasPiece(pieceIndex) {
|
|
33
24
|
return this.pieces[pieceIndex] ?? false;
|
|
34
25
|
}
|
|
35
|
-
|
|
36
26
|
/**
|
|
37
27
|
* Mark one piece as available after receiving a peer `have` message.
|
|
38
28
|
*
|
|
39
29
|
* @param pieceIndex - Zero-based piece index.
|
|
40
30
|
*/
|
|
41
|
-
|
|
31
|
+
markHave(pieceIndex) {
|
|
42
32
|
if (!Number.isInteger(pieceIndex) || pieceIndex < 0 || pieceIndex >= this.totalPieces) {
|
|
43
33
|
return;
|
|
44
34
|
}
|
|
45
|
-
|
|
46
35
|
this.pieces[pieceIndex] = true;
|
|
47
36
|
}
|
|
48
|
-
|
|
49
37
|
/**
|
|
50
38
|
* Replace availability from a peer `bitfield` message.
|
|
51
39
|
*
|
|
52
40
|
* @param bitfield - Raw bitfield payload from the peer.
|
|
53
41
|
*/
|
|
54
|
-
|
|
42
|
+
setBitfield(bitfield) {
|
|
55
43
|
for (let pieceIndex = 0; pieceIndex < this.totalPieces; pieceIndex += 1) {
|
|
56
44
|
this.pieces[pieceIndex] = hasBitfieldPiece(bitfield, pieceIndex);
|
|
57
45
|
}
|
|
58
46
|
}
|
|
59
|
-
|
|
60
47
|
/**
|
|
61
48
|
* Return available piece indexes in ascending order.
|
|
62
49
|
*
|
|
63
50
|
* @returns Piece indexes currently marked as available.
|
|
64
51
|
*/
|
|
65
|
-
|
|
52
|
+
toPieceIndexes() {
|
|
66
53
|
return this.pieces.flatMap((available, pieceIndex) => (available ? [pieceIndex] : []));
|
|
67
54
|
}
|
|
68
55
|
}
|
|
69
|
-
|
|
70
56
|
/**
|
|
71
57
|
* Create mutable availability state from an optional peer bitfield.
|
|
72
58
|
*
|
|
@@ -74,15 +60,12 @@ export class PeerPieceAvailability {
|
|
|
74
60
|
* @param bitfield - Optional raw bitfield payload from the peer.
|
|
75
61
|
* @returns Peer piece availability state.
|
|
76
62
|
*/
|
|
77
|
-
export const createPeerPieceAvailability = (
|
|
78
|
-
|
|
79
|
-
bitfield?: Uint8Array,
|
|
80
|
-
): PeerPieceAvailability => new PeerPieceAvailability(totalPieces, { bitfield });
|
|
81
|
-
|
|
82
|
-
const hasBitfieldPiece = (bitfield: Uint8Array, pieceIndex: number): boolean => {
|
|
63
|
+
export const createPeerPieceAvailability = (totalPieces, bitfield) => new PeerPieceAvailability(totalPieces, { bitfield });
|
|
64
|
+
const hasBitfieldPiece = (bitfield, pieceIndex) => {
|
|
83
65
|
const byte = bitfield[Math.floor(pieceIndex / 8)];
|
|
84
|
-
if (byte === undefined)
|
|
85
|
-
|
|
66
|
+
if (byte === undefined)
|
|
67
|
+
return false;
|
|
86
68
|
const mask = 0x80 >> (pieceIndex % 8);
|
|
87
69
|
return (byte & mask) !== 0;
|
|
88
70
|
};
|
|
71
|
+
//# sourceMappingURL=availability.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"availability.js","sourceRoot":"","sources":["../../src/peer/availability.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAUV;IATH,MAAM,CAAY;IAEnC;;;;;OAKG;IACH,YACoB,WAAmB,EACnC,UAAwC,EAAE;QAD1B,gBAAW,GAAX,WAAW,CAAQ;QAGnC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,EA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,EAAW,EAAE;IAC3E,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC,CAAC;IAClD,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IAErC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;IACtC,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const PROTOCOL = "BitTorrent protocol";
|
|
2
|
+
export declare const PROTOCOL_BYTES: NodeJS.NonSharedUint8Array;
|
|
3
|
+
export declare const PEER_ID_PREFIX = "-BT0001-";
|
|
4
|
+
export declare const HANDSHAKE_LENGTH = 68;
|
|
5
|
+
export declare const RESERVED_LENGTH = 8;
|
|
6
|
+
export declare const INFO_HASH_LENGTH = 20;
|
|
7
|
+
export declare const PEER_ID_LENGTH = 20;
|
|
8
|
+
export declare const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
export const PROTOCOL = 'BitTorrent protocol';
|
|
2
2
|
export const PROTOCOL_BYTES = new TextEncoder().encode(PROTOCOL);
|
|
3
|
-
|
|
4
3
|
export const PEER_ID_PREFIX = '-BT0001-';
|
|
5
|
-
|
|
6
4
|
export const HANDSHAKE_LENGTH = 68;
|
|
7
5
|
export const RESERVED_LENGTH = 8;
|
|
8
6
|
export const INFO_HASH_LENGTH = 20;
|
|
9
7
|
export const PEER_ID_LENGTH = 20;
|
|
10
|
-
|
|
11
8
|
export const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
9
|
+
//# sourceMappingURL=consts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../../src/peer/consts.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAG,qBAAqB,CAAC;AAC9C,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAEjE,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC;AAEzC,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AACnC,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AACjC,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AACnC,MAAM,CAAC,MAAM,cAAc,GAAG,EAAE,CAAC;AAEjC,MAAM,CAAC,MAAM,QAAQ,GAAG,gEAAgE,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BunTorrentError } from '../../utils/errors';
|
|
2
|
+
export declare enum HandshakeErrorCode {
|
|
3
|
+
INFOHASH_INVALID_LENGTH = "HANDSHK_INFOHASH_INVALID_LENGTH",
|
|
4
|
+
INVALID_LENGTH = "HANDSHK_INVALID_LENGTH",
|
|
5
|
+
INVALID_PROTOCOL = "HANDSHK_INVALID_PROTOCOL",
|
|
6
|
+
PEERID_INVALID_LENGTH = "HANDSHK_PEERID_INVALID_LENGTH",
|
|
7
|
+
RESERVED_INVALID_LENGTH = "HANDSHK_RESERVED_INVALID_LENGTH"
|
|
8
|
+
}
|
|
9
|
+
export declare class PeerHandshakeError extends BunTorrentError {
|
|
10
|
+
constructor(code: HandshakeErrorCode, message: string);
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BunTorrentError } from '../../utils/errors';
|
|
2
|
+
export var HandshakeErrorCode;
|
|
3
|
+
(function (HandshakeErrorCode) {
|
|
4
|
+
HandshakeErrorCode["INFOHASH_INVALID_LENGTH"] = "HANDSHK_INFOHASH_INVALID_LENGTH";
|
|
5
|
+
HandshakeErrorCode["INVALID_LENGTH"] = "HANDSHK_INVALID_LENGTH";
|
|
6
|
+
HandshakeErrorCode["INVALID_PROTOCOL"] = "HANDSHK_INVALID_PROTOCOL";
|
|
7
|
+
HandshakeErrorCode["PEERID_INVALID_LENGTH"] = "HANDSHK_PEERID_INVALID_LENGTH";
|
|
8
|
+
HandshakeErrorCode["RESERVED_INVALID_LENGTH"] = "HANDSHK_RESERVED_INVALID_LENGTH";
|
|
9
|
+
})(HandshakeErrorCode || (HandshakeErrorCode = {}));
|
|
10
|
+
export class PeerHandshakeError extends BunTorrentError {
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(message, code);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=handshake.error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.error.js","sourceRoot":"","sources":["../../../src/peer/handshake/handshake.error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,MAAM,CAAN,IAAY,kBAMX;AAND,WAAY,kBAAkB;IAC1B,iFAA2D,CAAA;IAC3D,+DAAyC,CAAA;IACzC,mEAA6C,CAAA;IAC7C,6EAAuD,CAAA;IACvD,iFAA2D,CAAA;AAC/D,CAAC,EANW,kBAAkB,KAAlB,kBAAkB,QAM7B;AAED,MAAM,OAAO,kBAAmB,SAAQ,eAAe;IACnD,YAAY,IAAwB,EAAE,OAAe;QACjD,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC;CACJ"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PeerHandshake } from '../types';
|
|
2
|
+
export declare const encodeHandshake: (input: {
|
|
3
|
+
infoHash: Uint8Array;
|
|
4
|
+
peerId: Uint8Array;
|
|
5
|
+
reserved?: Uint8Array;
|
|
6
|
+
}) => Uint8Array;
|
|
7
|
+
export declare const decodeHandshake: (handshake: Uint8Array) => PeerHandshake;
|
|
@@ -1,75 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
HANDSHAKE_LENGTH,
|
|
3
|
-
INFO_HASH_LENGTH,
|
|
4
|
-
PEER_ID_LENGTH,
|
|
5
|
-
PROTOCOL,
|
|
6
|
-
PROTOCOL_BYTES,
|
|
7
|
-
RESERVED_LENGTH,
|
|
8
|
-
} from '../consts';
|
|
1
|
+
import { HANDSHAKE_LENGTH, INFO_HASH_LENGTH, PEER_ID_LENGTH, PROTOCOL, PROTOCOL_BYTES, RESERVED_LENGTH, } from '../consts';
|
|
9
2
|
import { HandshakeErrorCode, PeerHandshakeError } from './handshake.error';
|
|
10
|
-
import type { PeerHandshake } from '../types';
|
|
11
|
-
|
|
12
3
|
const textDecoder = new TextDecoder('utf-8', { fatal: true });
|
|
13
|
-
|
|
14
|
-
export const encodeHandshake = (input: {
|
|
15
|
-
infoHash: Uint8Array;
|
|
16
|
-
peerId: Uint8Array;
|
|
17
|
-
reserved?: Uint8Array;
|
|
18
|
-
}): Uint8Array => {
|
|
4
|
+
export const encodeHandshake = (input) => {
|
|
19
5
|
if (input.infoHash.byteLength !== INFO_HASH_LENGTH)
|
|
20
|
-
return fail(
|
|
21
|
-
HandshakeErrorCode.INFOHASH_INVALID_LENGTH,
|
|
22
|
-
'Torrent infohash is invalid length',
|
|
23
|
-
);
|
|
6
|
+
return fail(HandshakeErrorCode.INFOHASH_INVALID_LENGTH, 'Torrent infohash is invalid length');
|
|
24
7
|
if (input.peerId.byteLength !== PEER_ID_LENGTH)
|
|
25
8
|
return fail(HandshakeErrorCode.PEERID_INVALID_LENGTH, 'Torrent peerId is invalid length');
|
|
26
|
-
|
|
27
9
|
let offset = 0;
|
|
28
10
|
const handshake = new Uint8Array(HANDSHAKE_LENGTH);
|
|
29
|
-
|
|
30
11
|
handshake[offset++] = PROTOCOL_BYTES.byteLength;
|
|
31
12
|
handshake.set(PROTOCOL_BYTES, offset);
|
|
32
13
|
offset += PROTOCOL_BYTES.byteLength;
|
|
33
|
-
|
|
34
14
|
const reserved = input.reserved ?? new Uint8Array(RESERVED_LENGTH);
|
|
35
15
|
if (reserved.byteLength !== RESERVED_LENGTH)
|
|
36
|
-
return fail(
|
|
37
|
-
HandshakeErrorCode.RESERVED_INVALID_LENGTH,
|
|
38
|
-
'Handshake reserved is invalid length',
|
|
39
|
-
);
|
|
40
|
-
|
|
16
|
+
return fail(HandshakeErrorCode.RESERVED_INVALID_LENGTH, 'Handshake reserved is invalid length');
|
|
41
17
|
handshake.set(reserved, offset);
|
|
42
18
|
offset += RESERVED_LENGTH;
|
|
43
|
-
|
|
44
19
|
handshake.set(input.infoHash, offset);
|
|
45
20
|
offset += INFO_HASH_LENGTH;
|
|
46
|
-
|
|
47
21
|
handshake.set(input.peerId, offset);
|
|
48
|
-
|
|
49
22
|
return handshake;
|
|
50
23
|
};
|
|
51
|
-
|
|
52
|
-
export const decodeHandshake = (handshake: Uint8Array): PeerHandshake => {
|
|
24
|
+
export const decodeHandshake = (handshake) => {
|
|
53
25
|
if (handshake.byteLength !== HANDSHAKE_LENGTH)
|
|
54
26
|
return fail(HandshakeErrorCode.INVALID_LENGTH, 'Handshake is invalid length');
|
|
55
|
-
|
|
56
27
|
let offset = 0;
|
|
57
|
-
const protocolLength = handshake[offset++]
|
|
28
|
+
const protocolLength = handshake[offset++];
|
|
58
29
|
const protocolEnd = offset + protocolLength;
|
|
59
30
|
const protocol = textDecoder.decode(handshake.subarray(offset, protocolEnd));
|
|
60
31
|
offset = protocolEnd;
|
|
61
|
-
|
|
62
32
|
if (protocol !== PROTOCOL)
|
|
63
33
|
return fail(HandshakeErrorCode.INVALID_PROTOCOL, 'Handshake protocol is invalid');
|
|
64
|
-
|
|
65
34
|
const reserved = handshake.subarray(offset, offset + RESERVED_LENGTH);
|
|
66
35
|
offset += RESERVED_LENGTH;
|
|
67
|
-
|
|
68
36
|
const infoHash = handshake.subarray(offset, offset + INFO_HASH_LENGTH);
|
|
69
37
|
offset += INFO_HASH_LENGTH;
|
|
70
|
-
|
|
71
38
|
const peerId = handshake.subarray(offset, offset + PEER_ID_LENGTH);
|
|
72
|
-
|
|
73
39
|
return {
|
|
74
40
|
protocol,
|
|
75
41
|
reserved,
|
|
@@ -77,7 +43,7 @@ export const decodeHandshake = (handshake: Uint8Array): PeerHandshake => {
|
|
|
77
43
|
peerId,
|
|
78
44
|
};
|
|
79
45
|
};
|
|
80
|
-
|
|
81
|
-
const fail = (code: HandshakeErrorCode, message: string): never => {
|
|
46
|
+
const fail = (code, message) => {
|
|
82
47
|
throw new PeerHandshakeError(code, message);
|
|
83
48
|
};
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/peer/handshake/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,QAAQ,EACR,cAAc,EACd,eAAe,GAClB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAG3E,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAE9D,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,KAI/B,EAAc,EAAE;IACb,IAAI,KAAK,CAAC,QAAQ,CAAC,UAAU,KAAK,gBAAgB;QAC9C,OAAO,IAAI,CACP,kBAAkB,CAAC,uBAAuB,EAC1C,oCAAoC,CACvC,CAAC;IACN,IAAI,KAAK,CAAC,MAAM,CAAC,UAAU,KAAK,cAAc;QAC1C,OAAO,IAAI,CAAC,kBAAkB,CAAC,qBAAqB,EAAE,kCAAkC,CAAC,CAAC;IAE9F,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAEnD,SAAS,CAAC,MAAM,EAAE,CAAC,GAAG,cA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Q,GAAG,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,CAAC,CAAC;IACtE,MAAM,IAAI,eAAe,CAAC;IAE1B,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB,CAAC,CAAC;IACvE,MAAM,IAAI,gBAAgB,CAAC;IAE3B,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,CAAC,CAAC;IAEnE,OAAO;QACH,QAAQ;QACR,QAAQ;QACR,QAAQ;QACR,MAAM;KACT,CAAC;AACN,CAAC,CAAC;AAEF,MAAM,IAAI,GAAG,CAAC,IAAwB,EAAE,OAAe,EAAS,EAAE;IAC9D,MAAM,IAAI,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAChD,CAAC,CAAC"}
|
|
@@ -3,29 +3,10 @@ export { createPeerPieceAvailability, PeerPieceAvailability } from './availabili
|
|
|
3
3
|
export { PeerSession, PeerSessionError, PeerSessionErrorCode } from './session';
|
|
4
4
|
export { encodeHandshake, decodeHandshake } from './handshake';
|
|
5
5
|
export { HandshakeErrorCode, PeerHandshakeError } from './handshake/handshake.error';
|
|
6
|
-
export {
|
|
7
|
-
decodePeerMessage,
|
|
8
|
-
encodePeerMessage,
|
|
9
|
-
PeerMessageError,
|
|
10
|
-
PeerMessageErrorCode,
|
|
11
|
-
PeerMessageId,
|
|
12
|
-
} from './messages';
|
|
6
|
+
export { decodePeerMessage, encodePeerMessage, PeerMessageError, PeerMessageErrorCode, PeerMessageId, } from './messages';
|
|
13
7
|
export { connectToPeers, openPeerPool, PeerPool, PeerPoolError, PeerPoolErrorCode } from './pool';
|
|
14
|
-
|
|
15
8
|
export type { PeerHandshake } from './types';
|
|
16
9
|
export type { PeerPieceAvailabilityOptions } from './availability';
|
|
17
10
|
export type { PeerSessionConnectOptions } from './session';
|
|
18
11
|
export type { PeerConnectionSession, PeerPoolOptions } from './pool';
|
|
19
|
-
export type {
|
|
20
|
-
BitfieldMessage,
|
|
21
|
-
CancelMessage,
|
|
22
|
-
ChokeMessage,
|
|
23
|
-
HaveMessage,
|
|
24
|
-
InterestedMessage,
|
|
25
|
-
KeepAliveMessage,
|
|
26
|
-
NotInterestedMessage,
|
|
27
|
-
PeerMessage,
|
|
28
|
-
PieceMessage,
|
|
29
|
-
RequestMessage,
|
|
30
|
-
UnchokeMessage,
|
|
31
|
-
} from './messages/types';
|
|
12
|
+
export type { BitfieldMessage, CancelMessage, ChokeMessage, HaveMessage, InterestedMessage, KeepAliveMessage, NotInterestedMessage, PeerMessage, PieceMessage, RequestMessage, UnchokeMessage, } from './messages/types';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createPeerId } from './peer-id';
|
|
2
|
+
export { createPeerPieceAvailability, PeerPieceAvailability } from './availability';
|
|
3
|
+
export { PeerSession, PeerSessionError, PeerSessionErrorCode } from './session';
|
|
4
|
+
export { encodeHandshake, decodeHandshake } from './handshake';
|
|
5
|
+
export { HandshakeErrorCode, PeerHandshakeError } from './handshake/handshake.error';
|
|
6
|
+
export { decodePeerMessage, encodePeerMessage, PeerMessageError, PeerMessageErrorCode, PeerMessageId, } from './messages';
|
|
7
|
+
export { connectToPeers, openPeerPool, PeerPool, PeerPoolError, PeerPoolErrorCode } from './pool';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/peer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,2BAA2B,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAChF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AACrF,OAAO,EACH,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,aAAa,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BunTorrentError } from '../../utils/errors';
|
|
2
|
+
export declare enum PeerMessageErrorCode {
|
|
3
|
+
INVALID_LENGTH_PREFIX = "PEER_MSG_INVALID_LENGTH_PREFIX",
|
|
4
|
+
INVALID_MESSAGE_LENGTH = "PEER_MSG_INVALID_MESSAGE_LENGTH",
|
|
5
|
+
INVALID_PAYLOAD_LENGTH = "PEER_MSG_INVALID_PAYLOAD_LENGTH",
|
|
6
|
+
INVALID_UINT32 = "PEER_MSG_INVALID_UINT32",
|
|
7
|
+
UNKNOWN_MESSAGE_ID = "PEER_MSG_UNKNOWN_MESSAGE_ID",
|
|
8
|
+
UNSUPPORTED_MESSAGE = "PEER_MSG_UNSUPPORTED_MESSAGE"
|
|
9
|
+
}
|
|
10
|
+
export declare class PeerMessageError extends BunTorrentError {
|
|
11
|
+
constructor(code: PeerMessageErrorCode, message: string);
|
|
12
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BunTorrentError } from '../../utils/errors';
|
|
2
|
+
export var PeerMessageErrorCode;
|
|
3
|
+
(function (PeerMessageErrorCode) {
|
|
4
|
+
PeerMessageErrorCode["INVALID_LENGTH_PREFIX"] = "PEER_MSG_INVALID_LENGTH_PREFIX";
|
|
5
|
+
PeerMessageErrorCode["INVALID_MESSAGE_LENGTH"] = "PEER_MSG_INVALID_MESSAGE_LENGTH";
|
|
6
|
+
PeerMessageErrorCode["INVALID_PAYLOAD_LENGTH"] = "PEER_MSG_INVALID_PAYLOAD_LENGTH";
|
|
7
|
+
PeerMessageErrorCode["INVALID_UINT32"] = "PEER_MSG_INVALID_UINT32";
|
|
8
|
+
PeerMessageErrorCode["UNKNOWN_MESSAGE_ID"] = "PEER_MSG_UNKNOWN_MESSAGE_ID";
|
|
9
|
+
PeerMessageErrorCode["UNSUPPORTED_MESSAGE"] = "PEER_MSG_UNSUPPORTED_MESSAGE";
|
|
10
|
+
})(PeerMessageErrorCode || (PeerMessageErrorCode = {}));
|
|
11
|
+
export class PeerMessageError extends BunTorrentError {
|
|
12
|
+
constructor(code, message) {
|
|
13
|
+
super(message, code);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/peer/messages/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,MAAM,CAAN,IAAY,oBAOX;AAPD,WAAY,oBAAoB;IAC5B,gFAAwD,CAAA;IACxD,kFAA0D,CAAA;IAC1D,kFAA0D,CAAA;IAC1D,kEAA0C,CAAA;IAC1C,0EAAkD,CAAA;IAClD,4EAAoD,CAAA;AACxD,CAAC,EAPW,oBAAoB,KAApB,oBAAoB,QAO/B;AAED,MAAM,OAAO,gBAAiB,SAAQ,eAAe;IACjD,YAAY,IAA0B,EAAE,OAAe;QACnD,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC;CACJ"}
|