import { Aria2DownloadBitTorrentMode, Conn, Socket, Aria2ServerVersion, Aria2ServerGlobalStat, PreconfiguredSocket } from 'maria2'; import * as maria2_index from 'maria2/index'; import { PartialDeep } from 'type-fest'; import { Aria2InputOptions } from '@naria2/options'; export { Aria2BasicGlobalOptions, Aria2BasicInputOptions, Aria2BtGlobalOptions, Aria2BtGlobalOptionsKey, Aria2BtInputOptions, Aria2BtMetalinkOptions, Aria2DhtGlobalOptions, Aria2DhtGlobalOptionsKey, Aria2DhtInputOptions, Aria2FTPOptions, Aria2GlobalOptions, Aria2HTTPFTPOptions, Aria2HTTPOptions, Aria2InputOptions, Aria2ProxyOptions, Aria2RPCOptions, Aria2RPCOptionsKey, ListenPorts, StrSize } from '@naria2/options'; type MaybePromise = T | Promise; type Aria2EventKeyPrefix = `start` | `pause` | `stop` | `complete` | `bt-complete` | `error`; interface ClientOptions { secret?: string; /** * Timeout for each request (ms). * @default 5000 * @public */ timeout?: number; /** * Timeout for waiting socket (ms). * @default 5000 * @public */ openTimeout?: number; /** * Time interval for polling download status (ms). * * @default 1000 */ progressInterval?: number; } interface DownloadOptions { /** * uris is an array of URIs (string). uris is used for Web-seeding. For single file torrents, the URI can be a complete URI pointing to the resource; if URI ends with /, name in torrent file is added. For multi-file torrents, name and path in torrent are added to form a URI for each file. options is a struct and its members are pairs of option name and value. */ uris?: string[]; /** * If position is given, it must be an integer starting from 0. The new download will be inserted at position in the waiting queue. If position is omitted or position is larger than the current size of the queue, the new download is appended to the end of the queue. */ position?: number; } interface TorrentPiece { readonly length: number; readonly missing: number; } interface DownloadBitTorrentStatus { announceList: string[]; comment: string | undefined; creationDate: Date; mode: Aria2DownloadBitTorrentMode; info: { name: string | undefined; }; } type Aria2TorrentEvents = Record; declare class Task { protected readonly client: Aria2Client; readonly gid: string; readonly following: Task | undefined; readonly followedBy: Task[]; private _bittorrent; private _timestamp; private _status; constructor(client: Aria2Client, gid: string, following?: Task); private get conn(); get status(): Pick; updateStatus(): Promise>; get state(): maria2_index.Aria2DownloadState; get files(): maria2_index.Aria2FileStatus[]; /** * Progress percent, max: 100 */ get progress(): number; /** * Download speed measured in bytes/sec */ get downloadSpeed(): string; /** * Check this task is metadata */ get isMetadata(): boolean; /** * Get bittorrent information */ get bittorrent(): DownloadBitTorrentStatus | undefined; pause(force?: boolean): Promise; unpause(): Promise; remove(force?: boolean): Promise; on(key: Key, handler: (param: Aria2TorrentEvents[Key]) => void | Promise): void; off(key: Key, handler?: (param: Aria2TorrentEvents[Key]) => void | Promise): void; /** * Watch task download state until complete or bt-complete * * @param handler Download file progress callback * @param target Watch state until target event is fired * @returns Watch state until target event is fired */ watch(handler: (task: Task) => void | Promise, target?: `complete` | `bt-complete`): Promise; /** * Watch torrent download state until complete or bt-complete * * @param onMetadata Download [METADATA] progress callback * @param onFollowedBy Download torrent file itself progress callback * @param target Watch state until target event is fired * @returns */ watchTorrent(onMetadata: ((task: Task) => void | Promise) | null | undefined, onFollowedBy: (task: Task) => void | Promise, target?: `complete` | `bt-complete`): Promise; complete(): Promise; btComplete(): Promise; /** * Wait for the full torrent download completion * * If this is a metadata torrent (used for downloading metadata), it has two steps: * * 1. Downloading the metadata * 2. Downloading the actual data * * If this is a actual data torrent, it will just wait for the download compeltion * * @param target * * @returns */ waitForCompletion(target?: `complete` | `bt-complete`): Promise; } type Aria2MonitorEvents = Record<`${Aria2EventKeyPrefix}:${string}`, Task>; declare class Aria2Monitor { private readonly client; private readonly disposables; private readonly emitter; private readonly tasks; private readonly watchingIds; constructor(client: Aria2Client); private get conn(); start(): Promise; close(): void; private updateStatus; listActive(): Promise; listWaiting(offset?: number, number?: number): Promise; listPaused(offset?: number, number?: number): Promise; listStopped(offset?: number, number?: number): Promise; getTask(gid: string): Promise; watchStatus(gid: string, fn?: ((task: Task) => void | Promise) | null | undefined, target?: `complete` | `bt-complete`): Promise; on(key: Key, handler: (param: Aria2MonitorEvents[Key]) => void | Promise): void; off(key: Key, handler?: (param: Aria2MonitorEvents[Key]) => void | Promise): void; private onDownloadProgress; private onDownloadStart; private onDownloadPause; private onDownloadStop; private onDownloadComplete; private onBtDownloadComplete; private onDownloadError; } declare class Aria2Client { private _conn; private _monitor; private _options; constructor(conn: Conn, options?: ClientOptions); get conn(): Conn; get options(): Required>; get socket(): Socket; get monitor(): Aria2Monitor; close(): void; /** * This method shuts down aria2. * * @param force Call forceShutdown or shutdown */ shutdown(force?: boolean): Promise; downloadTorrent(torrent: MaybePromise, options?: PartialDeep & DownloadOptions): Promise; downloadUri(uri: MaybePromise, options?: PartialDeep & DownloadOptions): Promise; /** * This method returns the version of aria2 and the list of enabled features. * The response is a struct and contains following keys. */ getVersion(): Promise; /** * This method returns global statistics such as the overall download and upload speeds. * The response is a struct and contains the following keys. Values are strings. */ getGlobalStat(): Promise; listActive(): Promise; listWaiting(): Promise; listPaused(): Promise; listStopped(): Promise; } declare function createClient(_socket: MaybePromise, options?: ClientOptions): Promise; export { Aria2Client, type Aria2EventKeyPrefix, type ClientOptions, type DownloadBitTorrentStatus, type DownloadOptions, Task, type TorrentPiece, createClient };