@push.rocks/smartfile
Version:
High-level file representation classes (SmartFile, StreamFile, VirtualDirectory) for efficient in-memory file management in Node.js using TypeScript. Works seamlessly with @push.rocks/smartfs for filesystem operations.
50 lines (49 loc) • 2.04 kB
TypeScript
import { Readable } from 'stream';
/**
* The StreamFile class represents a file as a stream.
* It allows creating streams from a file path, a URL, or a buffer.
* Use SmartFileFactory to create instances of this class.
*/
export declare class StreamFile {
static fromPath(filePath: string, smartFs?: any): Promise<StreamFile>;
static fromUrl(url: string, smartFs?: any): Promise<StreamFile>;
static fromBuffer(buffer: Buffer, relativeFilePath?: string, smartFs?: any): StreamFile;
/**
* Creates a StreamFile from an existing Readable stream with an option for multiple uses.
* @param stream A Node.js Readable stream.
* @param relativeFilePath Optional file path for the stream.
* @param multiUse If true, the stream can be read multiple times, caching its content.
* @param smartFs Optional SmartFs instance for filesystem operations
* @returns A StreamFile instance.
*/
static fromStream(stream: Readable, relativeFilePath?: string, multiUse?: boolean, smartFs?: any): StreamFile;
relativeFilePath?: string;
private streamSource;
private smartFs?;
private cachedStreamBuffer?;
multiUse: boolean;
used: boolean;
byteLengthComputeFunction?: () => Promise<number>;
private constructor();
private checkMultiUse;
/**
* Creates a new readable stream from the source.
*/
createReadStream(): Promise<Readable>;
/**
* Writes the stream to the disk at the specified path.
* @param filePathArg The file path where the stream should be written.
*/
writeToDisk(filePathArg: string): Promise<void>;
writeToDir(dirPathArg: string): Promise<void>;
getContentAsBuffer(): Promise<Buffer<ArrayBufferLike>>;
getContentAsString(formatArg?: 'utf8' | 'binary'): Promise<string>;
/**
* Returns the size of the file content in bytes.
*/
getSize(): Promise<number | null>;
/**
* Converts the StreamFile to a SmartFile by loading content into memory
*/
toSmartFile(): Promise<any>;
}