@arkts/sdk-downloader
Version:
Download and extract the ArkTS SDK.
182 lines (181 loc) • 4.81 kB
text/typescript
import * as node_https0 from "node:https";
import * as unzipper1 from "unzipper";
import { Emitter } from "mitt";
import progress from "progress-stream";
import * as tar from "tar";
//#region src/enums/sdk.d.ts
declare enum SdkVersion {
API10 = "4.0.0",
API11 = "4.1.0",
API12 = "5.0.0",
API13 = "5.0.1",
API14 = "5.0.2",
API15 = "5.0.3",
API18 = "5.1.0",
API20 = "6.0.0-Beta1",
}
declare enum SdkArch {
X86 = 0,
ARM = 1,
}
declare enum SdkOS {
MacOS = 0,
Windows = 1,
Linux = 2,
}
type SdkUrlStorage = Record<SdkVersion, Record<SdkArch, Record<SdkOS, string | null>>>;
/**
* Get the SDK URL for a given version, architecture, and operating system.
* @param version - The version of the SDK.
* @param arch - The architecture of the SDK.
* @param os - The operating system of the SDK.
* @returns The URL of the SDK.
*/
declare function getSdkUrl(version: SdkVersion, arch: SdkArch, os: SdkOS): string | null;
/**
* Get all the SDK URLs.
* @returns The SDK URLs.
*/
declare function getSdkUrls(): SdkUrlStorage;
//#endregion
//#region src/options.d.ts
interface UrlOptions {
/**
* The version of the SDK.
*/
version: SdkVersion;
/**
* The architecture of the SDK.
*/
arch: SdkArch;
/**
* The operating system of the SDK.
*/
os: SdkOS;
}
interface DownloadProgressEvent extends progress.Progress {
/**
* The network speed of the download.
*/
network: number;
/**
* The unit of the network speed.
*/
unit: "KB" | "MB";
/**
* The increment of the {@linkcode DownloadProgressEvent.percentage}.
*/
increment: number;
}
interface DownloadOptions {
/**
* The URL of the SDK.
*/
url: string | UrlOptions;
/**
* The cache directory of the tar file.
*/
cacheDir: string;
/**
* The target directory of the SDK.
*/
targetDir: string;
/**
* Whether to clean the cache directory after the download is complete.
*
* @default true
*/
clean?: boolean;
/**
* The type of request to use.
*/
requestType?: "http" | "https";
/**
* The signal to abort the download.
*/
signal?: AbortSignal;
/**
* The extra options to pass to the request.
*/
requestOptions?: node_https0.RequestOptions;
/**
* Whether to enable resume download. If enabled, the download will automatically resume from where it left off.
*
* @default true
*/
resumeDownload?: boolean;
/**
* The temporary file path for storing partial downloads. If not specified, will use tarCacheDir + 'download.tmp.tar.gz'.
*/
tempFilePath?: string;
/**
* The callback function to be called when the download progress changes.
*
* @param e - The progress event.
*/
onDownloadProgress?(e: DownloadProgressEvent): void | Promise<void>;
/**
* The callback function to be called when the tar file is extracted.
*
* @param entry - The entry of the tar file.
*/
onTarExtracted?(entry: tar.ReadEntry): void | Promise<void>;
/**
* The callback function to be called when the zip file is extracted.
*
* @param entry - The entry of the zip file.
*/
onZipExtracted?(entry: unzipper1.Entry): void | Promise<void>;
}
interface DownloadExecutor extends Emitter<DownloadEventMap> {
startDownload(requestOptions?: node_https0.RequestOptions): Promise<void>;
checkSha256(): Promise<void>;
extractTar(): Promise<void>;
extractZip(): Promise<void>;
clean(): Promise<void>;
}
// eslint-disable-next-line ts/consistent-type-definitions
type DownloadEventMap = {
"download-progress": DownloadProgressEvent;
"tar-extracted": tar.ReadEntry;
"zip-extracted": unzipper1.Entry;
};
//#endregion
//#region src/download.d.ts
/**
* Download the ArkTS SDK.
*
* @param options - The options for the download.
*/
declare function createDownloader(options: DownloadOptions): Promise<DownloadExecutor>;
/**
* Download the ArkTS SDK.
*
* @deprecated Deprecate this entry in newer version.
* @param options - The options for the download.
*/
declare function download(options: DownloadOptions): Promise<DownloadExecutor>;
//#endregion
//#region src/errors/download.d.ts
interface DownloadErrorOptions {
message?: string;
cause?: unknown;
}
declare class DownloadError extends Error {
code: DownloadError.Code;
constructor(code: DownloadError.Code, options?: DownloadErrorOptions);
}
declare namespace DownloadError {
enum Code {
DownloadFailed = "DOWNLOAD_FAILED",
ZipExtractionFailed = "ZIP_EXTRACTION_FAILED",
InvalidUrl = "INVALID_URL",
Sha256Mismatch = "SHA256_MISMATCH",
FileNotFound = "FILE_NOT_FOUND",
InvalidSha256 = "INVALID_SHA256",
FileReadError = "FILE_READ_ERROR",
}
}
//#endregion
export { DownloadError, DownloadOptions, DownloadProgressEvent, SdkArch, SdkOS, SdkVersion, createDownloader, download, getSdkUrl, getSdkUrls };
//# sourceMappingURL=index.d.cts.map