import { BObject } from "./utils/codec.js"; import { FileListV1, FileTreeDirNode } from "./utils/fileTree.js"; import { FileDirLikes } from "./utils/fileDirLike.js"; /** * support padding attribute on file */ declare global { interface File { readonly padding?: boolean; } } /** * meta version can only be 2 at the time being */ export type MetaVersion = 2; /** * torrent type: v1, v2, hybrid */ export declare enum TorrentType { /** * v1 torrent * * [BEP 3](https://www.bittorrent.org/beps/bep_0003.html) */ V1 = "v1", /** * v2 torrent * * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html) */ V2 = "v2", /** * v1 + v2 hybrid torrent * * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html#upgrade-path) */ HYBRID = "hybrid" } /** * base torrent options */ export interface TorrentOptionsBase { /** * add created by whom */ addCreatedBy?: boolean; /** * add creation date */ addCreationDate?: boolean; /** * announce url */ announce?: string; /** * announce url list */ announceList?: string[][]; /** * block length: 16384 (16 KiB) by default * (do not alter this value) */ blockLength?: number; /** * comment */ comment?: string; /** * is private torrent */ isPrivate?: boolean; /** * torrent name */ name?: string; /** * piece length: a power of 2 number, * will automatically calculate when this value is missing */ pieceLength?: number; /** * source */ source?: string; /** * url list * * [BEP 9](http://www.bittorrent.org/beps/bep_0019.html#metadata-extension) */ urlList?: string[]; } /** * v1 torrent options */ export interface TorrentOptionsV1 extends TorrentOptionsBase { /** * add padding files * this option is only available in V1 type torrents * because files are forcibly padded in HYBRID type * and don't need padding in V2 type */ addPaddingFiles?: boolean; /** * sort file: only available in V1 type * files are forcibly sorted in V2 and HYBRID type */ sortFiles?: boolean; /** * torrent type: V1 */ type: TorrentType.V1; } /** * v2 torrent options */ export interface TorrentOptionsV2 extends TorrentOptionsBase { /** * meta version */ metaVersion?: MetaVersion; /** * torrent type: V2 */ type: TorrentType.V2; } /** * v1 + v2 hybrid torrent options */ export interface TorrentOptionsHybrid extends TorrentOptionsBase { /** * meta version */ metaVersion?: MetaVersion; /** * torrent type: HYBRID */ type: TorrentType.HYBRID; } /** * torrent options */ export type TorrentOptions = T extends TorrentType.V1 ? TorrentOptionsV1 : T extends TorrentType.V2 ? TorrentOptionsV2 : T extends TorrentType.HYBRID ? TorrentOptionsHybrid : never; /** * info base */ export interface InfoBase extends BObject { /** * The suggested name to save the file (or directory) as. * It is purely advisory * * [BEP 3](https://www.bittorrent.org/beps/bep_0003.html#:~:text=The-,name,-key%20maps%20to) * | * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html#:~:text=info%20dictionary-,name,-A%20display%20name) */ name: string; /** * The number of bytes that each logical piece * in the peer protocol refers to * * [BEP 3](https://www.bittorrent.org/beps/bep_0003.html#:~:text=is%20purely%20advisory.-,piece%20length,-maps%20to%20the) * | * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html#upgrade-path:~:text=is%20purely%20advisory.-,piece%20length,-The%20number%20of) */ ["piece length"]: number; /** * is private torrent */ private?: boolean; /** * source */ source?: string; } /** * v1 info base */ export interface InfoBaseV1 extends InfoBase { /** * Pieces maps to a string whose length is a multiple of 20 * * [BEP 3](https://www.bittorrent.org/beps/bep_0003.html#:~:text=M%20as%20default%29.-,pieces,-maps%20to%20a) */ pieces: ArrayBuffer | string; } /** * v1 single file info */ export interface InfoSingleFileV1 extends InfoBaseV1 { length: number; } /** * v1 multi file info */ export interface InfoMultiFileV1 extends InfoBaseV1 { files: FileListV1; } /** * v1 info */ export type InfoV1 = InfoSingleFileV1 | InfoMultiFileV1; /** * v2 info */ export interface InfoV2 extends InfoBase { /** * A tree of dictionaries where dictionary keys * represent UTF-8 encoded path elements * * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html#upgrade-path:~:text=about%20invalid%20files.-,file%20tree,-A%20tree%20of) */ ["file tree"]: FileTreeDirNode; /** * An integer value, set to 2 to indicate compatibility * with the current revision of this specification * * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html#upgrade-path:~:text=an%20alignment%20gap.-,meta%20version,-An%20integer%20value) */ ["meta version"]: number; } /** * hybrid single file info */ export interface InfoSingleFileHybrid extends InfoSingleFileV1, InfoV2 { } /** * hybrid multi file info */ export interface InfoMultiFileHybrid extends InfoMultiFileV1, InfoV2 { } /** * hybrid info */ export type InfoHybrid = InfoSingleFileHybrid | InfoMultiFileHybrid; /** * info */ export type Info = T extends TorrentType.V1 ? InfoV1 : T extends TorrentType.V2 ? InfoV2 : T extends TorrentType.HYBRID ? InfoHybrid : never; /** * v2 piece layers */ export type PieceLayers = Map; /** * base meta info */ export interface MetaInfoBase extends BObject { /** * The URL of the tracker * * [BEP 3](https://www.bittorrent.org/beps/bep_0003.html#:~:text=the%20following%20keys%3A-,announce,-The%20URL%20of) * | * [BEP 52](https://www.bittorrent.org/beps/bep_0052.html#:~:text=the%20following%20keys%3A-,announce,-The%20URL%20of) */ announce?: string; /** * This key will refer to a list of lists of URLs, * and will contain a list of tiers of announces * * [BEP 12](http://bittorrent.org/beps/bep_0012.html#:~:text=This%20key%20will%20refer%20to%20a%20list%20of%20lists%20of%20URLs%2C%20and%20will%20contain%20a%20list%20of%20tiers%20of%20announces) */ ["announce-list"]?: string[][]; /** * Free-form textual comments of the author * * [BitTorrent Specification](https://courses.edsa-project.eu/pluginfile.php/1514/mod_resource/content/0/bitTorrent_part2.htm#:~:text=00%3A00%20UTC%29-,comment,-%3A%20%28optional%29%20free%2Dform) */ comment?: string; /** * Name and version of the program used to create the .torrent * * [BitTorrent Specification](https://courses.edsa-project.eu/pluginfile.php/1514/mod_resource/content/0/bitTorrent_part2.htm#:~:text=the%20author%20%28string%29-,created%20by,-%3A%20%28optional%29%20name%20and) */ ["created by"]?: string; /** * The creation time of the torrent, * in standard UNIX epoch format * * [BitTorrent Specification](https://courses.edsa-project.eu/pluginfile.php/1514/mod_resource/content/0/bitTorrent_part2.htm#:~:text=is%20here.-,creation%20date,-%3A%20%28optional%29%20the%20creation) */ ["creation date"]?: number; } /** * v1 meta info */ export interface MetaInfoV1 extends MetaInfoBase { info: Info; } /** * v2 meta info */ export interface MetaInfoV2 extends MetaInfoBase { info: Info; ["piece layers"]?: PieceLayers; } /** * hybrid meta info */ export interface MetaInfoHybrid extends MetaInfoBase { info: Info; ["piece layers"]?: PieceLayers; } /** * meta info */ export type MetaInfo = T extends TorrentType.V1 ? MetaInfoV1 : T extends TorrentType.V2 ? MetaInfoV2 : T extends TorrentType.HYBRID ? MetaInfoHybrid : never; /** * common piece lengths */ export declare enum CommonPieceLength { "16KB" = 16384, "32KB" = 32768, "64KB" = 65536, "128KB" = 131072, "256KB" = 262144, "512KB" = 524288, "1MB" = 1048576, "2MB" = 2097152, "4MB" = 4194304, "8MB" = 8388608, "16MB" = 16777216, "32MB" = 33554432 } /** * default block length 1 << 14 = 16384 */ export declare const BLOCK_LENGTH: number; /** * default meta version = 2 */ export declare const META_VERSION: MetaVersion; export type OnProgress = (current: number, total: number) => void | Promise; export declare function create(fileDirLikes: FileDirLikes, opts?: TorrentOptions, onProgress?: OnProgress): Promise; export type SetProgressTotal = (totalNumberOrFunction: number | ((total: number, current?: number) => number | Promise)) => Promise; export type UpdateProgress = () => Promise;