libmodpm
Version:
Modrinth package manager library
160 lines (159 loc) • 4.53 kB
TypeScript
import { TypedEventTarget } from "./events/TypedEventTarget.js";
import { HTTPClient } from "./HTTPClient.js";
/**
* Represents a file to be downloaded.
*
* @final
*/
declare abstract class Entry {
/**
* Direct download URL of the file.
*/
readonly url: URL;
/**
* SHA-512 hash of the file, encoded as a hex string.
*/
readonly hash: string;
/**
* File name of the file.
*/
readonly name: string;
/**
* Size of the file, in bytes.
*/
readonly size?: number;
/**
* Creates a new file entry.
*
* @param url Direct download URL of the file.
* @param hash SHA-512 hash of the file, encoded as a hex string.
* @param name File name of the file.
* @param [size] Size of the file, in bytes.
*/
protected constructor(url: URL, hash: string, name: string, size?: number);
/**
* Writes a buffer to the file in the specified directory.
*
* @param directory Directory to write the file to.
* @param contents Contents of the file.
*/
abstract write(directory: string, contents: BufferSource): Promise<void>;
/**
* Returns a writable stream to the file in the specified directory.
*
* @param directory Directory to write the file to.
*/
abstract getStream(directory: string): Promise<WritableStream<BufferSource>>;
/**
* Checks if the file exists in the directory and has the correct hash.
*
* @param directory Directory to check.
*/
abstract exists(directory: string): Promise<boolean>;
}
/**
* Represents a download error.
*
* @final
*/
declare class DownloadError extends Error {
/**
* Creates a new download error.
*
* @param message Error message.
*/
constructor(message: string);
}
/**
* Downloads files from a list of URLs into a specified directory.
*
* @template T Error type.
* @final
*/
export declare class Downloader extends HTTPClient<DownloadError> {
static readonly Entry: typeof Entry;
static readonly DownloadError: typeof DownloadError;
/**
* Downloader events.
*/
readonly events: TypedEventTarget<{
start: CustomEvent<Entry>;
progress: CustomEvent<{
entry: Entry;
progress: number;
}>;
end: CustomEvent<Entry>;
downloadError: CustomEvent<{
entry: Entry;
error: DownloadError;
}>;
hashError: CustomEvent<{
entry: Entry;
}>;
}>;
/**
* Internal events.
*/
private readonly internalEvents;
/**
* Controller for aborting downloader workers.
*/
private readonly abortController;
/**
* Files to download.
*/
private readonly entries;
/**
* Number of files to download in parallel.
*/
private readonly concurrency;
/**
* Directory to download files into.
*/
private readonly directory;
/**
* Download progress.
*/
private readonly progress;
/**
* Creates a new downloader.
*
* @param userAgent User agent string used when making requests.
* @param entries Files to download.
* @param concurrency Number of files to download in parallel.
* @param directory Directory to download files into.
*/
constructor(userAgent: string, entries: Entry[], concurrency: number, directory: string);
/**
* Aborts downloading.
*/
abort(): void;
/**
* Begins downloading the files.
*
* @returns `false` if downloading was interrupted and did not complete, `true` otherwise.
*/
download(): Promise<boolean>;
/**
* Retrieves file size and name by sending an HTTP HEAD request to the specified URL.
*
* @param url URL of the file.
* @returns File size from the `Content-Length` header (or `null` if not present) and file name from the
* `Content-Disposition` header or the last URI path component.
* @throws {@link DownloadError} If the request fails.
* @throws {@link !TypeError} If fetching fails.
*/
getSizeAndName(url: URL): Promise<{
size: number | null;
name: string;
}>;
protected createError(res: Response): Promise<DownloadError>;
/**
* Creates a new download worker.
*
* @param signal Abort signal for the worker.
* @returns `false` if the worker terminated early due to error or abort, `true` otherwise.
*/
private worker;
}
export {};