UNPKG

@phroun/paged-buffer

Version:

High-performance buffer system for editing massive files with intelligent memory management and undo/redo capabilities

446 lines 15.3 kB
/** * Enhanced PagedBuffer with page coordinate-based marks */ export class PagedBuffer { constructor(pageSize?: number, storage?: null, maxMemoryPages?: number); pageSize: number; storage: MemoryPageStorage; maxMemoryPages: number; filename: any; fileSize: number; fileMtime: Date | null; fileChecksum: string | null; virtualPageManager: VirtualPageManager; lineAndMarksManager: LineAndMarksManager; totalSize: number; state: string; hasUnsavedChanges: boolean; missingDataRanges: any[]; detachmentReason: string | null; notifications: any[]; notificationCallbacks: any[]; changeStrategy: { noEdits: string; withEdits: string; sizeChanged: string; }; lastFileCheck: number | null; fileCheckInterval: number; undoSystem: BufferUndoSystem | null; /** * Mark buffer as detached due to data loss * @param {string} reason - Reason for detachment * @param {MissingDataRange[]} missingRanges - Missing data ranges */ _markAsDetached(reason: string, missingRanges?: MissingDataRange[]): void; /** * Mark buffer as having unsaved changes * @private */ private _markAsModified; /** * Mark buffer as saved (no unsaved changes) * @private */ private _markAsSaved; /** * Merge overlapping missing data ranges * @private */ private _mergeMissingRanges; /** * Add notification callback * @param {Function} callback - Callback function for notifications */ onNotification(callback: Function): void; /** * Emit a notification * @param {string} type - Notification type * @param {string} severity - Severity level * @param {string} message - Human-readable message * @param {Object} metadata - Additional data */ _notify(type: string, severity: string, message: string, metadata?: Object): void; /** * Load a file into the buffer * @param {string} filename - Path to the file */ loadFile(filename: string): Promise<void>; /** * Enhanced loadContent with proper initial state */ loadContent(content: any): void; /** * Enhanced loadBinaryContent with proper initial state */ loadBinaryContent(content: any): void; /** * Calculate file checksum for change detection * @param {string} filename - File to checksum * @returns {Promise<string>} - File checksum */ _calculateFileChecksum(filename: string): Promise<string>; /** * Check for file changes * @returns {Promise<Object>} - Change information */ checkFileChanges(): Promise<Object>; /** * Get bytes from absolute position with optional marks extraction * @param {number} start - Start byte position * @param {number} end - End byte position * @param {boolean} includeMarks - Whether to include marks in result * @returns {Promise<Buffer|ExtractedContent>} - Data or data with marks */ getBytes(start: number, end: number, includeMarks?: boolean): Promise<Buffer | ExtractedContent>; /** * Insert bytes at absolute position with optional marks * @param {number} position - Insertion position * @param {Buffer} data - Data to insert * @param {Array<{name: string, relativeOffset: number}>} marks - Marks to insert */ insertBytes(position: number, data: Buffer, marks?: Array<{ name: string; relativeOffset: number; }>): Promise<void>; /** * Enhanced deleteBytes with marks reporting * @param {number} start - Start position * @param {number} end - End position * @param {boolean} reportMarks - Whether to report marks that were in deleted content * @returns {Promise<Buffer|ExtractedContent>} - Deleted data with optional marks report */ deleteBytes(start: number, end: number, reportMarks?: boolean): Promise<Buffer | ExtractedContent>; /** * Enhanced overwriteBytes with marks support * @param {number} position - Overwrite position * @param {Buffer} data - New data * @param {Array<{name: string, relativeOffset: number}>} marks - Marks to insert * @returns {Promise<Buffer|ExtractedContent>} - Overwritten data with optional marks */ overwriteBytes(position: number, data: Buffer, marks?: Array<{ name: string; relativeOffset: number; }>): Promise<Buffer | ExtractedContent>; /** * Set a named mark at a byte address * @param {string} markName - Name of the mark * @param {number} byteAddress - Byte address in buffer */ setMark(markName: string, byteAddress: number): void; /** * Get the byte address of a named mark * @param {string} markName - Name of the mark * @returns {number|null} - Byte address or null if not found */ getMark(markName: string): number | null; /** * Remove a named mark * @param {string} markName - Name of the mark * @returns {boolean} - True if mark was found and removed */ removeMark(markName: string): boolean; /** * Get all marks between two byte addresses * @param {number} startAddress - Start address (inclusive) * @param {number} endAddress - End address (inclusive) * @returns {Array<{name: string, address: number}>} - Marks in range */ getMarksInRange(startAddress: number, endAddress: number): Array<{ name: string; address: number; }>; /** * Get all marks in the buffer * @returns {Array<{name: string, address: number}>} - All marks */ getAllMarks(): Array<{ name: string; address: number; }>; /** * Set marks from a key-value object (for persistence) * @param {Object} marksObject - Object mapping mark names to virtual addresses */ setMarks(marksObject: Object): void; /** * Clear all marks */ clearAllMarks(): void; /** * Enable undo/redo functionality * @param {Object} config - Undo system configuration */ enableUndo(config?: Object): void; /** * Disable undo/redo functionality */ disableUndo(): void; /** * Begin a named undo transaction * @param {string} name - Name/description of the transaction * @param {Object} options - Transaction options */ beginUndoTransaction(name: string, options?: Object): void; /** * Commit the current undo transaction * @param {string} finalName - Optional final name * @returns {boolean} - True if transaction was committed */ commitUndoTransaction(finalName?: string): boolean; /** * Rollback the current undo transaction * @returns {Promise<boolean>} - True if transaction was rolled back */ rollbackUndoTransaction(): Promise<boolean>; /** * Check if currently in an undo transaction * @returns {boolean} - True if in transaction */ inUndoTransaction(): boolean; /** * Get current undo transaction info * @returns {Object|null} - Transaction info or null */ getCurrentUndoTransaction(): Object | null; /** * Undo the last operation * @returns {Promise<boolean>} - True if successful */ undo(): Promise<boolean>; /** * Redo the last undone operation * @returns {Promise<boolean>} - True if successful */ redo(): Promise<boolean>; /** * Check if undo is available * @returns {boolean} - True if undo is available */ canUndo(): boolean; /** * Check if redo is available * @returns {boolean} - True if redo is available */ canRedo(): boolean; /** * Get total size of buffer * @returns {number} - Total size */ getTotalSize(): number; /** * Get buffer state (data integrity) * @returns {string} - Buffer state */ getState(): string; /** * Check if buffer has unsaved changes * @returns {boolean} - True if there are unsaved changes */ hasChanges(): boolean; /** * Check if buffer can be saved to its original location * @returns {boolean} - True if safe to save to original location */ canSaveToOriginal(): boolean; /** * Get comprehensive buffer status * @returns {Object} - Complete status information */ getStatus(): Object; /** * Get enhanced memory usage stats with line and marks information * @returns {Object} - Memory statistics */ getMemoryStats(): Object; /** * Get detachment information * @returns {Object} Detachment details */ getDetachmentInfo(): Object; /** * Get all notifications * @returns {Array} - Array of notifications */ getNotifications(): any[]; /** * Clear notifications * @param {string} [type] - Optional type filter */ clearNotifications(type?: string): void; /** * Set file change handling strategy * @param {Object} strategies - Strategy configuration */ setChangeStrategy(strategies: Object): void; /** * Generate missing data summary for save operations * @private */ private _generateMissingDataSummary; /** * Create marker for missing data at a specific position * @private */ private _createMissingDataMarker; /** * Create marker for missing data at end of file * @private */ private _createEndOfFileMissingMarker; /** * Create emergency marker for data that became unavailable during save * @private */ private _createEmergencyMissingMarker; /** * Write data with markers indicating where missing data belongs - FIXED for large files * @private */ private _writeDataWithMissingMarkers; /** * Write a segment of data in manageable chunks * @param {fs.FileHandle} fd - File handle to write to * @param {number} startPos - Start position in virtual buffer * @param {number} endPos - End position in virtual buffer * @param {number} maxChunkSize - Maximum size per chunk * @private */ private _writeSegmentInChunks; /** * Enhanced save method with smart behavior and atomic operations */ saveFile(filename?: any, options?: {}): Promise<void>; /** * Enhanced saveAs that handles detached buffers gracefully */ saveAs(filename: any, forcePartialOrOptions?: {}, options?: {}): Promise<void>; /** * Enhanced save method with positional missing data markers * @private */ private _performSave; /** * Atomic save that uses temporary copy to prevent corruption */ _performAtomicSave(filename: any, options?: {}): Promise<void>; /** * Create a temporary copy of the original file */ _createTempCopy(originalPath: any): Promise<string>; /** * Update VPM to use a different source file path */ _updateVPMSourceFile(newPath: any): void; /** * Cleanup temporary copy */ _cleanupTempCopy(tempPath: any): Promise<void>; /** * Update metadata after successful save */ _updateMetadataAfterSave(filename: any): Promise<void>; /** * Check if file exists */ _fileExists(filePath: any): Promise<boolean>; /** * Method to manually mark buffer as clean (for testing/special cases) */ _markAsClean(): void; /** * Method to check if buffer has been modified * @deprecated Use hasChanges() instead */ isModified(): boolean; /** * Method to check if buffer is detached */ isDetached(): boolean; /** * Method to check if buffer is clean */ isClean(): boolean; /** * Get total number of lines in the buffer (SYNCHRONOUS) * @returns {number} - Total line count */ getLineCount(): number; /** * Get information about a specific line (SYNCHRONOUS) * @param {number} lineNumber - Line number (1-based) * @returns {LineOperationResult|null} - Line info or null if not found */ getLineInfo(lineNumber: number): LineOperationResult | null; /** * Get information about multiple lines at once (SYNCHRONOUS) * @param {number} startLine - Start line number (1-based, inclusive) * @param {number} endLine - End line number (1-based, inclusive) * @returns {LineOperationResult[]} - Array of line info */ getMultipleLines(startLine: number, endLine: number): LineOperationResult[]; /** * Convert byte address to line number (SYNCHRONOUS) * @param {number} byteAddress - Byte address in buffer * @returns {number} - Line number (1-based) or 0 if invalid */ getLineNumberFromAddress(byteAddress: number): number; /** * Convert line/character position to absolute byte position (SYNCHRONOUS) * @param {Object} pos - {line, character} (both 1-based, character = byte offset in line) * @returns {number} - Absolute byte position */ lineCharToBytePosition(pos: Object): number; /** * Convert absolute byte position to line/character position (SYNCHRONOUS) * @param {number} bytePos - Absolute byte position * @returns {Object} - {line, character} (both 1-based, character = byte offset in line + 1) */ byteToLineCharPosition(bytePos: number): Object; /** * Ensure page containing address is loaded (ASYNC) * @param {number} address - Byte address to load * @returns {Promise<boolean>} - True if page was loaded successfully */ seekAddress(address: number): Promise<boolean>; /** * Insert content with line/character position (convenience method) * @param {Object} pos - {line, character} (both 1-based, character = byte offset) * @param {string} text - Text to insert * @returns {Promise<{newPosition: Object}>} */ insertTextAtPosition(pos: Object, text: string): Promise<{ newPosition: Object; }>; /** * Delete content between line/character positions (convenience method) * @param {Object} startPos - {line, character} (both 1-based, character = byte offset) * @param {Object} endPos - {line, character} (both 1-based, character = byte offset) * @returns {Promise<{deletedText: string}>} */ deleteTextBetweenPositions(startPos: Object, endPos: Object): Promise<{ deletedText: string; }>; } /** * Tracks missing data ranges in detached buffers */ export class MissingDataRange { constructor(virtualStart: any, virtualEnd: any, originalFileStart?: null, originalFileEnd?: null, reason?: string); virtualStart: any; virtualEnd: any; originalFileStart: any; originalFileEnd: any; reason: string; size: number; /** * Generate human-readable description of missing data */ toDescription(): string; } import { MemoryPageStorage } from "./storage/memory-page-storage"; import { VirtualPageManager } from "./virtual-page-manager"; import { LineAndMarksManager } from "./utils/line-marks-manager"; import { BufferUndoSystem } from "./undo-system"; import { ExtractedContent } from "./utils/line-marks-manager"; //# sourceMappingURL=paged-buffer.d.ts.map