UNPKG

@shockpkg/core

Version:
894 lines (893 loc) 25.5 kB
/// <reference types="node" /> import { Readable } from 'stream'; import { Dispatcher } from './dispatcher'; import { Lock } from './lock'; import { Package } from './package'; import { Packages } from './packages'; import { Request } from './request'; import { IPackageCleanupAfter, IPackageCleanupBefore, IPackageDownloadAfter, IPackageDownloadBefore, IPackageDownloadProgress, IPackageExtractAfter, IPackageExtractBefore, IPackageExtractProgress, IPackageInstallAfter, IPackageInstallBefore, IPackageInstallCurrent, IPackageInstalled, IPackageReceipt, IPackageRemovedObsolete, IPackageStreamAfter, IPackageStreamBefore, IPackageStreamProgress, PackageLike } from './types'; import { Zip } from './zip'; /** * Manager constructor. * * @param path The path, defaults to environment variable or relative. */ export declare class Manager extends Object { /** * Package install before events. */ readonly eventPackageInstallBefore: Dispatcher<IPackageInstallBefore>; /** * Package install after events. */ readonly eventPackageInstallAfter: Dispatcher<IPackageInstallAfter>; /** * Package install current events. */ readonly eventPackageInstallCurrent: Dispatcher<IPackageInstallCurrent>; /** * Package download before events. */ readonly eventPackageDownloadBefore: Dispatcher<IPackageDownloadBefore>; /** * Package download after events. */ readonly eventPackageDownloadAfter: Dispatcher<IPackageDownloadAfter>; /** * Package download progress events. */ readonly eventPackageDownloadProgress: Dispatcher<IPackageDownloadProgress>; /** * Package stream before events. */ readonly eventPackageStreamBefore: Dispatcher<IPackageStreamBefore>; /** * Package stream after events. */ readonly eventPackageStreamAfter: Dispatcher<IPackageStreamAfter>; /** * Package stream progress events. */ readonly eventPackageStreamProgress: Dispatcher<IPackageStreamProgress>; /** * Package extract before events. */ readonly eventPackageExtractBefore: Dispatcher<IPackageExtractBefore>; /** * Package extract after events. */ readonly eventPackageExtractAfter: Dispatcher<IPackageExtractAfter>; /** * Package extract progress events. */ readonly eventPackageExtractProgress: Dispatcher<IPackageExtractProgress>; /** * Package cleanup before events. */ readonly eventPackageCleanupBefore: Dispatcher<IPackageCleanupBefore>; /** * Package cleanup after events. */ readonly eventPackageCleanupAfter: Dispatcher<IPackageCleanupAfter>; /** * Packages URL. */ protected readonly _packagesUrl: string; /** * Packages file. */ protected readonly _packagesFile: string; /** * Package file. */ protected readonly _packageFile: string; /** * Main directory. */ protected readonly _mainDir: string; /** * Meta directory. */ protected readonly _metaDir: string; /** * Temp directory. */ protected readonly _tempDir: string; /** * Path environment variable name. */ protected readonly _pathEnv: string; /** * Packages URL environment variable name. */ protected readonly _packagesUrlEnv: string; /** * Inited flag. */ protected _inited: boolean; /** * Destroyed flag. */ protected _destroyed: boolean; /** * Exclusive access flag. */ protected _exclusive: boolean; /** * Root path. */ protected readonly _path: string; /** * Lock file instance. */ protected readonly _lock: Lock; /** * Packages instance. */ protected readonly _packages: Packages; /** * Request instance. */ protected readonly _request: Request; constructor(path?: string | null); /** * Root path. * * @returns The path. */ readonly path: string; /** * Packages URL. * * @returns The URL. */ readonly packagesUrl: string; /** * Packages file. * * @returns The file. */ readonly packagesFile: string; /** * Package file. * * @returns The path. */ readonly packageFile: string; /** * Packages file path. * * @returns The path. */ readonly pathMetaPackages: string; /** * Meta directory. * * @returns The directory. */ readonly metaDir: string; /** * Temp directory. * * @returns The directory. */ readonly tempDir: string; /** * Meta directory path for root path. * * @returns The path. */ readonly pathMeta: string; /** * Instance inited and not yet destroyed. * * @returns Is active. */ readonly active: boolean; /** * Packages loaded. * * @returns Is loaded. */ readonly loaded: boolean; /** * The lock file compromised. * * @returns Is compromised. */ readonly lockCompromised: boolean; /** * Assert instance not inited. */ assertNotInited(): void; /** * Assert instance is active. * Implies inited, not-destroyed, and lock-not-compromised assertions. */ assertActive(): void; /** * Assert instance all loaded, including the packages list. * Implies all active assertions. */ assertLoaded(): void; /** * Initialize instance. */ init(): Promise<void>; /** * Destroy instance. */ destroy(): Promise<void>; /** * Run asyncronous function with automatic init and destroy. * * @param func Async function. * @returns Return value of the async function. */ with<T>(func: (self: this) => Promise<T>): Promise<T>; /** * Itterate over the packages. * * @returns Package itterator. */ packageItter(): IterableIterator<Package>; /** * Get package by the unique name. * * @param name Package name. * @returns The package or null. */ packageByName(name: string): Package | null; /** * Get package by the sha256 hash. * * @param sha256 Package sha256. * @returns The package or null. */ packageBySha256(sha256: string): Package | null; /** * Get package by the unique value. * * @param unique Package unique. * @returns The package or null. */ packageByUnique(unique: string): Package | null; /** * Check if package is in packages collection. * * @param pkg Package instance. * @returns If the package instance is present. */ packageIsMember(pkg: Package): boolean; /** * Read package install receipt. * * @param pkg The package. * @returns Install receipt. */ packageInstallReceipt(pkg: PackageLike): Promise<IPackageReceipt>; /** * Get package install file. * * @param pkg The package. * @returns Path to install file. */ packageInstallFile(pkg: PackageLike): Promise<string>; /** * Verify package install file, using size and hash. * * @param pkg The package. */ packageInstallVerify(pkg: PackageLike): Promise<void>; /** * Packages ordered by dependencies. * * @param pkgs Packages list. * @returns Packages list, sorted order. */ packagesDependOrdered(pkgs: PackageLike[]): Package[]; /** * Update the package manager installed data. * Updates the packages list. * * @returns Update report. */ update(): Promise<{ updated: import("./types").IPackageUpdated[]; added: import("./types").IPackageUpdated[]; removed: import("./types").IPackageUpdated[]; }>; /** * Check if a package is installed. * * @param pkg The package. * @returns True if already installed, else false. */ isInstalled(pkg: PackageLike): Promise<boolean>; /** * Check if a package is installed and up-to-date. * * @param pkg The package. * @returns True if already up-to-date, else false. */ isCurrent(pkg: PackageLike): Promise<boolean>; /** * List all installed packages. * * @returns A list of installed package objects. */ installed(): Promise<Package[]>; /** * List all outdated packages. * * @returns The list of outdated package objects. */ outdated(): Promise<Package[]>; /** * An alias for upgradeSlim. * * @returns List of packages upgraded. */ upgrade(): Promise<IPackageInstalled[]>; /** * Upgrade any outdated packages. * * @returns List of packages upgraded. */ upgradeFull(): Promise<IPackageInstalled[]>; /** * Upgrade any outdated packages, using slim install method. * * @returns List of packages upgraded. */ upgradeSlim(): Promise<IPackageInstalled[]>; /** * An alias for installSlim. * * @param pkg The package. * @returns True if was installed, false if already installed. */ install(pkg: PackageLike): Promise<Package[]>; /** * Install package, with parents. * Returns the list of packages installed to install. * Returns empty array if current version is already installed. * * @param pkg The package. * @returns List of packages processed. */ installFull(pkg: PackageLike): Promise<Package[]>; /** * Install multiple package with parents, higher dependencies first. * * @param pkgs Packages list. * @returns Installed list. */ installFullMulti(pkgs: PackageLike[]): Promise<IPackageInstalled[]>; /** * Install package, without parents. * Returns the list of packages downloaded or extracted to install. * Returns empty array if current version is already installed. * * @param pkg The package. * @returns List of packages processed. */ installSlim(pkg: PackageLike): Promise<Package[]>; /** * Install multiple package without parents, higher dependencies first. * * @param pkgs Packages list. * @returns Installed list. */ installSlimMulti(pkgs: PackageLike[]): Promise<IPackageInstalled[]>; /** * Remove package. * * @param pkg The package. * @returns True if removed, false if nothing to remove. */ remove(pkg: PackageLike): Promise<boolean>; /** * Check if package name is obsolete. * * @param pkg The package. * @returns True if package obslete, else false. */ isObsolete(pkg: string): Promise<boolean>; /** * List obsolete package names. * * @returns A list of obsolete package names. */ obsolete(): Promise<string[]>; /** * Cleanup all obsolete and outdated packages. * * @returns Lists of removed packages. */ cleanup(): Promise<IPackageRemovedObsolete[]>; /** * Join path on the base path. * * @param parts Path parts. * @returns Joined path. */ pathTo(...parts: string[]): string; /** * Join path on the meta path. * * @param parts Path parts. * @returns Joined path. */ pathToMeta(...parts: string[]): string; /** * Join path on the temp folder path. * * @param parts Path parts. * @returns Joined path. */ pathToTemp(...parts: string[]): string; /** * Join path on package base path. * * @param pkg The package. * @param parts Path parts. * @returns Joined path. */ pathToPackage(pkg: PackageLike, ...parts: string[]): string; /** * Join path on package meta path. * * @param pkg The package. * @param parts Path parts. * @returns Joined path. */ pathToPackageMeta(pkg: PackageLike, ...parts: string[]): string; /** * Join path on package base path. * * @param pkg The package. * @param parts Path parts. * @returns Joined path. */ protected _pathToPackage(pkg: PackageLike, ...parts: string[]): string; /** * Join path on package meta path. * * @param pkg The package. * @param parts Path parts. * @returns Joined path. */ protected _pathToPackageMeta(pkg: PackageLike, ...parts: string[]): string; /** * Obtain exclusive access for the duration of a syncronous callback. * * @param func Syncronous function. * @returns Return value of the syncronous callback. */ protected _exclusiveSync<T>(func: (self: this) => T): T; /** * Obtain exclusive access for the duration of a asyncronous callback. * * @param func Asyncronous function. * @returns Return value of the asyncronous callback. */ protected _exclusiveAsync<T>(func: (self: this) => Promise<T>): Promise<T>; /** * Itterate over the packages. */ protected _packageItter(): IterableIterator<Package>; /** * Get package by the unique name. * * @param name Package name. * @returns The package or null. */ protected _packageByName(name: string): Package | null; /** * Get package by the sha256 hash. * * @param sha256 Package sha256. * @returns The package or null. */ protected _packageBySha256(sha256: string): Package | null; /** * Get package by the unique value. * * @param unique Package unique. * @returns The package or null. */ protected _packageByUnique(unique: string): Package | null; /** * Check if package is in packages collection. * * @param pkg Package instance. * @returns If the package instance is present. */ protected _packageIsMember(pkg: Package): boolean; /** * Get package install file. * * @param pkg The package. * @returns Path to install file. */ protected _packageInstallFile(pkg: PackageLike): Promise<string>; /** * Verify package install file, using size and hash. * * @param pkg The package. */ protected _packageInstallVerify(pkg: PackageLike): Promise<void>; /** * Get package object by object, name, or hash. * If package object is passed, check that object is known. * Throw error if package is unknown. * * @param pkg The package. * @returns Package object. */ protected _packageToPackage(pkg: PackageLike): Package; /** * Get package name by object, name, or hash. * If package object is passed, check that object is known. * If string is passed and unknown, returns string. * * @param pkg The package. * @param mustExist Must exist. * @returns Package object. */ protected _packageToName(pkg: PackageLike, mustExist?: boolean): string; /** * List package parent packages. * * @param pkg The package. * @returns Packages list. */ protected _packageParents(pkg: PackageLike): Package[]; /** * List package parent packages not updated. * * @param pkg The package. * @returns Packages list. */ protected _packageParentsNotUpdated(pkg: PackageLike): Promise<Package[]>; /** * List the packages that need to be installed. * * @param pkg The package. * @returns Package root or null and the children list. */ protected _packageInstallList(pkg: PackageLike): Promise<Package[]>; /** * Packages ordered by dependencies. * * @param pkgs Packages list. * @returns Packages list, sorted order. */ protected _packagesDependOrdered(pkgs: PackageLike[]): Package[]; /** * Read package installed receipt. * * @param pkg The package. * @returns Package object. */ protected _packageMetaReceiptRead(pkg: PackageLike): Promise<IPackageReceipt>; /** * Write package installed receipt. * * @param pkg The package. */ protected _packageMetaReceiptWrite(pkg: PackageLike): Promise<void>; /** * CReate package installed receipt object from a package. * * @param pkg The package. * @returns Receipt object. */ protected _packageMetaReceiptFromPackage(pkg: PackageLike): Promise<IPackageReceipt>; /** * Check if package meta directory exists. * * @param pkg The package. * @returns True if the meta directory path, else false. */ protected _packageMetaDirExists(pkg: PackageLike): Promise<boolean>; /** * Ensure package directory exists. * * @param pkg The package. */ protected _packageDirsEnsure(pkg: PackageLike): Promise<void>; /** * Assert instance not inited. */ protected _assertNotInited(): void; /** * Assert instance is inited. */ protected _assertInited(): void; /** * Assert instance not destroyed. */ protected _assertNotDestroyed(): void; /** * Assert instance lock was not compromised. */ protected _assertLockNotCompromised(): void; /** * Assert instance is active. * Implies inited, not-destroyed, and lock-not-compromised assertions. */ protected _assertActive(): void; /** * Assert instance all loaded, including the packages list. * Implies all active assertions. */ protected _assertLoaded(): void; /** * Assert not current running exclusive method. */ protected _assertNotExclusive(): void; /** * Assert the package is in packages collection. * * @param pkg Package instance. */ protected _assertpackageIsMember(pkg: Package): void; /** * Assert correct status code. * * @param expected Expected status code. * @param statusCode The actual status code. */ protected _assertStatusCode(expected: number, statusCode: number): void; /** * Assert correct content length. * * @param expected Expected content length, as a number. * @param contentLength The actual content length as string. */ protected _assertContentLength(expected: number, contentLength: string): void; /** * Initialize instance. */ protected _init(): Promise<void>; /** * Destroy instance. */ protected _destroy(): Promise<void>; /** * Update the package manager. * Updates the packages list. * * @returns Update report. */ protected _update(): Promise<{ updated: import("./types").IPackageUpdated[]; added: import("./types").IPackageUpdated[]; removed: import("./types").IPackageUpdated[]; }>; /** * Check if a package is installed. * * @param pkg The package. * @returns True if already installed, else false. */ protected _isInstalled(pkg: PackageLike): Promise<boolean>; /** * Check if a package is installed and up-to-date. * * @param pkg The package. * @returns True if already up-to-date, else false. */ protected _isCurrent(pkg: PackageLike): Promise<boolean>; /** * List all installed packages. * * @returns A list of installed package objects. */ protected _installed(): Promise<Package[]>; /** * List all outdated packages. * * @returns The list of outdated package objects. */ protected _outdated(): Promise<Package[]>; /** * An alias for upgradeSlim. * * @returns List of packages upgraded. */ protected _upgrade(): Promise<IPackageInstalled[]>; /** * Upgrade any outdated packages. * * @returns List of packages upgraded. */ protected _upgradeFull(): Promise<IPackageInstalled[]>; /** * Upgrade any outdated packages, using slim install method. * * @returns List of packages upgraded. */ protected _upgradeSlim(): Promise<IPackageInstalled[]>; /** * An alias for installSlim. * * @param pkg The package. * @returns True if was installed, false if already installed. */ protected _install(pkg: PackageLike): Promise<Package[]>; /** * Install package, with parents. * Returns the list of packages installed to install. * Returns empty array if current version is already installed. * * @param pkg The package. * @returns List of packages processed. */ protected _installFull(pkg: PackageLike): Promise<Package[]>; /** * Install multiple package with parents, higher dependencies first. * * @param pkgs Packages list. * @returns Installed list. */ protected _installFullMulti(pkgs: PackageLike[]): Promise<IPackageInstalled[]>; /** * Install package, without parents. * Returns the list of packages downloaded or extracted to install. * Returns empty array if current version is already installed. * * @param pkg The package. * @returns List of packages processed. */ protected _installSlim(pkg: PackageLike): Promise<Package[]>; /** * Install multiple package without parents, higher dependencies first. * * @param pkgs Packages list. * @returns Installed list. */ protected _installSlimMulti(pkgs: PackageLike[]): Promise<IPackageInstalled[]>; /** * Remove package. * * @param pkg The package. * @returns True if removed, false if nothing to remove. */ protected _remove(pkg: PackageLike): Promise<boolean>; /** * Check if package name is obsolete. * * @param pkg The package. * @returns True if package obslete, else false. */ protected _isObsolete(pkg: string): Promise<boolean>; /** * List obsolete package names. * * @returns A list of obsolete package names. */ protected _obsolete(): Promise<string[]>; /** * Cleanup all obsolete and outdated packages. * * @returns Lists of removed packages. */ protected _cleanup(): Promise<IPackageRemovedObsolete[]>; /** * List all packages in the directory. * Only those directories with the meta directory are returned. * Dot directories are also always skipped. * * @returns List of all recognized package directories. */ protected _packagesDirList(): Promise<string[]>; /** * Extract package from archive path. * * @param pkg The package. * @param file Out file. * @param archive Archive file. */ protected _packageExtract(pkg: PackageLike, file: string, archive: string): Promise<void>; /** * Extract package from zip instance. * * @param pkg The package. * @param file Out file. * @param zip Archive file. */ protected _packageExtractZip(pkg: PackageLike, file: string, zip: Zip): Promise<void>; /** * Download package. * * @param pkg The package. * @param file Out file. */ protected _packageDownload(pkg: PackageLike, file: string): Promise<void>; /** * Stream package out of root package. * * @param pkg The package. * @param file Out file. */ protected _packageStream(pkg: PackageLike, file: string): Promise<void>; /** * Create streamer function for a package. * Only works for a root package. * * @param pkg The package. * @returns Streamer function. */ protected _packageStreamStreamer(pkg: PackageLike): (start: number, end: number) => Readable; /** * Request the packages file. * * @returns File contents as string. */ protected _requestPackages(): Promise<string>; /** * Update the packages list. * * @returns Update report. */ protected _updatePackages(): Promise<{ updated: import("./types").IPackageUpdated[]; added: import("./types").IPackageUpdated[]; removed: import("./types").IPackageUpdated[]; }>; /** * Ensure base directories exists. */ protected _ensureDirs(): Promise<void>; /** * Ensure temp directory exists. * * @param clean Clean existing. */ protected _tempDirEnsure(clean?: boolean): Promise<void>; /** * Ensure temp directory removed. */ protected _tempDirRemove(): Promise<void>; /** * Create the main path. * * @param path The path, defaults to environment variable or relative. * @returns Main path. */ protected _createPath(path: string | null): string; /** * Create the packages URL. * * @param defaultUrl The default URL if the environment variable not set. * @returns Packages URL. */ protected _createPackagesUrl(defaultUrl: string): string; /** * Create the Lock instance. * * @returns Lock instance. */ protected _createLock(): Lock; /** * Create the Packages instance. * * @returns Packages instance. */ protected _createPackages(): Packages; /** * Create the Request instance. * * @returns Request instance. */ protected _createRequest(): Request; /** * Create a Zip instance. * * @returns Zip instance. */ protected _createZip(): Zip; }