@shockpkg/core
Version:
shockpkg core
140 lines (139 loc) • 4.92 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
import { Readable } from 'stream';
import fse from 'fs-extra';
import { HashEncoding, IHash, IRequestStream, IZipItterEntry, OnData, OnResponse } from './types';
/**
* Like array filter method, but with asyncronous callback.
*
* @param list The array to filter.
* @param filter Filter function.
* @returns Filtered array.
*/
export declare function arrayFilterAsync<T>(list: T[], filter: (entry: T) => Promise<any>): Promise<T[]>;
/**
* Like array map method, but with asyncronous callback.
*
* @param list The array to map.
* @param map Map function.
* @returns Mapped array.
*/
export declare function arrayMapAsync<T, U>(list: T[], map: (entry: T) => Promise<U>): Promise<U[]>;
/**
* Promise for event emitter object to end successfully or in an error.
*
* @param obj Event emitter.
* @param end The end event name.
*/
export declare function streamEndError(obj: EventEmitter, end: string): Promise<void>;
/**
* Catch promise on error and return default value.
*
* @param p Promise object.
* @param d Default value.
* @returns Resulting value.
*/
export declare function promiseCatch<T, U>(p: Promise<T>, d: U): Promise<T | U>;
/**
* Promise for lstating a path, null on error.
*
* @param path File path.
* @returns Stat object or null.
*/
export declare function lstatExists(path: string): Promise<fse.Stats | null>;
/**
* Read directory, optional skip dot files, sorted order.
*
* @param path Path to the directory to list.
* @param dotfile Include dot files in the list or not.
* @returns Directory list, sorted order.
*/
export declare function readDir(path: string, dotfile?: boolean): Promise<string[]>;
/**
* Hash file using the specified algoithm.
*
* @param path File path.
* @param algorithm Hash algorithm.
* @param encoding Digest encoding.
* @returns Hash digest.
*/
export declare function hashFile(path: string, algorithm: string, encoding: HashEncoding): Promise<string>;
/**
* Normalize a hash string based on the encoding.
*
* @param hash Hash value.
* @param encoding Hash encoding.
* @returns Normalized hash.
*/
export declare function hashNormalize(hash: string, encoding: HashEncoding): string;
/**
* Hash file using the specified algoithms.
*
* @param path File path.
* @param hashes Hash list.
*/
export declare function fileHash(path: string, hashes: IHash[]): Promise<void>;
/**
* Verify file hash list using the specified algoithms.
*
* @param path File path.
* @param hashes Hash list.
*/
export declare function fileHashVerify(path: string, hashes: IHash[]): Promise<void>;
/**
* Verify file size or throw error.
*
* @param path File path.
* @param size Expected size.
*/
export declare function fileSizeVerify(path: string, size: number): Promise<void>;
/**
* Sort entries on dependencies listed for each entry.
* Sorts the array in-place.
*
* @param list The array to sort.
* @param deps Get the list of dependencies for each entry.
* @returns Sorted array.
*/
export declare function dependSort<T>(list: T[], deps: (entry: T) => T[]): T[];
/**
* Stream verifier.
*
* @param source Request stream.
* @param endEvent The end event name.
* @param size Expected size.
* @param hashes Expected hashes.
* @param onData Data event handler, can throw to cancel download.
*/
export declare function streamVerify(source: Readable, endEvent: string, size?: number | null, hashes?: IHash[] | null, onData?: OnData | null): Promise<void>;
/**
* Stream a request stream to a specified directory.
*
* @param source Request stream.
* @param size Expected size.
* @param hashes Expected hashes.
* @param onResponse Response event handler, can throw to cancel download.
* @param onData Data event handler, can throw to cancel download.
*/
export declare function streamRequest(source: IRequestStream, size?: number | null, hashes?: IHash[] | null, onResponse?: OnResponse | null, onData?: OnData | null): Promise<void>;
/**
* Write a request stream to a specified file.
*
* @param source Request stream.
* @param path File path.
* @param size Expected size.
* @param hashes Expected hashes.
* @param onResponse Response event handler, can throw to cancel download.
* @param onData Data event handler, can throw to cancel download.
*/
export declare function streamRequestDownload(source: IRequestStream, path: string, size?: number | null, hashes?: IHash[] | null, onResponse?: OnResponse | null, onData?: OnData | null): Promise<void>;
/**
* Write a ZIP entry to a specified file.
*
* @param entry ZIP entry.
* @param path File path.
* @param size Expected size.
* @param hashes Expected hashes.
* @param onData Data event handler, can throw to cancel download.
*/
export declare function zipEntryExtract(entry: IZipItterEntry, path: string, size?: number | null, hashes?: IHash[] | null, onData?: OnData | null): Promise<void>;