UNPKG

@loom-io/core

Version:

A file system wrapper for Node.js and Bun

117 lines (116 loc) 5.08 kB
import { LoomFile } from "./file.js"; import { LineResult, SearchResult } from "../helper/result.js"; import { TextItemList } from "../helper/textItemList.js"; import { FileHandler, SourceAdapter } from "../definitions.js"; interface ReadWrite { getSizeInBytes(): Promise<number>; } export interface Reader extends ReadWrite { search(value: string | Buffer): Promise<SearchResult | undefined>; searchFirst(value: string | Buffer): Promise<SearchResult | undefined>; searchFirst(value: string | Buffer, start: number): Promise<SearchResult | undefined>; searchLast(value: string | Buffer): Promise<SearchResult | undefined>; searchLast(value: string | Buffer, start: number): Promise<SearchResult | undefined>; read(start: number, length: number): Promise<Buffer>; close(): Promise<void>; } export interface ReaderInternal extends Reader { loopForward(value: Buffer, first: number, last: number): Promise<TextItemList | undefined>; loopReverse(value: Buffer, first: number, last: number): Promise<TextItemList | undefined>; } export interface Writer { close(): Promise<void>; } export declare class Editor implements Reader, Writer, ReaderInternal, Disposable { protected ref: LoomFile; protected file: FileHandler; protected chunkSize: number; protected lineInfo: TextItemList | undefined; protected newLineCharacter: Buffer; protected currentLine: number; protected EOF: boolean; static from(adapter: SourceAdapter, file: LoomFile): Promise<Editor>; constructor(ref: LoomFile, file: FileHandler); /** * Get the raw file handle */ get raw(): FileHandler; getSizeInBytes(): Promise<number>; /** * Close the file */ close(): Promise<void>; /** * Symbol to close automatically with using */ [Symbol.asyncDispose](): Promise<void>; [Symbol.dispose](): void; /** * Alias for searchFirst * * @param value - value to search * @returns */ search(value: string | Buffer): Promise<SearchResult | undefined>; /** * Search for a string in the file by chunking the file into pieces to avoid memory overflow. * set start to 'EOF' to search from the end of the file. * * @param value - value to search * @param start - start position in the file */ searchFirst(value: string | Buffer, start?: number): Promise<SearchResult | undefined>; /** * Search for a string in the file by chunking the file into pieces to avoid memory overflow. * set start to 'EOF' to search from the end of the file. * * @param value - value to search * @param start - last value included in the search */ searchLast(value: string | Buffer, start?: number): Promise<SearchResult | undefined>; protected calcChunkSize(valueLength: number): number; loopForward(value: Buffer, first: number, last: number): Promise<TextItemList | undefined>; loopReverse(value: Buffer, first: number | undefined, last: number): Promise<TextItemList | undefined>; protected searchInChunk(value: Buffer, chunk: Buffer): number[]; /** * Generate the next chunk position and length for fs.read function * * @param current - Start position of the last chunk * @param chunkSize - chunk size of the last chunk * @param valueLength - length of the value to search * @param min - minimum positive position in file * @returns */ protected loopReverseCalcNextChunk(current: number, chunkSize: number, valueLength: number, min: number): { position: number; length: number; }; protected convertChunkMatchesToItems(matches: number[], valueLength: number, chunkPosition: number, isReverseRead?: boolean): TextItemList | undefined; /** * Read a chunk of the file * * @param start - start position in the file * @param length - length of the data to read * @returns - return a buffer with the data read from the file */ read(start: number, length: number): Promise<Buffer>; protected handleFileWithOnlyOneLine(separator: Buffer): Promise<LineResult>; /** * Analyze the file junkvisely till the first line is found * and return a LineResult object to read the line or step to the next one. * * @param separator - line separator to use, default is new line character LF * @returns - return a LineResult object witch allow you to read line by line forward (next) and backward (prev) */ firstLine(separator?: Buffer | string): Promise<LineResult>; /** * Analyze the file junkvisely from the end till the last line is found * and return a LineResult object to read the line or step to the previous one. * * @param separator - line separator to use, default is new line character LF * @returns - return a LineResult object witch allow you to read line by line forward (next) and backward (prev) */ lastLine(separator?: Buffer | string): Promise<LineResult>; get [Symbol.toStringTag](): string; } export {};