@synet/fs
Version:
Robust, battle-tested filesystem abstraction for Node.js
91 lines (90 loc) • 2.68 kB
TypeScript
import { type Event, EventEmitter } from "@synet/unit";
import type { IFileSystem } from "./filesystem.interface";
/**
* File operation types for analytics tracking
*/
export type FileAction = "read" | "write" | "delete";
/**
* Individual file access record
*/
export interface FileAccess {
file: string;
timestamp: string;
access: FileAction;
}
/**
* Analytics statistics summary
*/
export interface Stats {
stats: {
read: number;
write: number;
delete: number;
};
fileReads: FileAccess[];
}
/**
* Configuration options for AnalyticsFileSystem
*/
export interface AnalyticsFileSystemOptions {
/**
* Number of operations before emitting stats event
* @default 100
*/
emitOn?: number;
}
/**
* Event emitted when analytics threshold is reached
*/
export interface AnalyticsStatsEvent extends Event {
type: "analytics.stats";
data: Stats;
}
/**
* Analytics filesystem that tracks filesystem operations and provides usage statistics
* Wraps any IFileSystem implementation to provide analytics capabilities
*/
export declare class AnalyticsFileSystem implements IFileSystem {
private baseFileSystem;
private options;
private eventEmitter;
private stats;
private operationCount;
private readonly emitThreshold;
constructor(baseFileSystem: IFileSystem, options?: AnalyticsFileSystemOptions);
/**
* Get current analytics statistics
*/
getStats(): Stats;
/**
* Get the event emitter for analytics events
*/
getEventEmitter(): EventEmitter<AnalyticsStatsEvent>;
/**
* Record a file operation and check if stats should be emitted
*/
private recordOperation;
/**
* Emit current stats and reset counters
*/
private emitStats;
existsSync(path: string): boolean;
readFileSync(path: string): string;
writeFileSync(path: string, data: string): void;
deleteFileSync(path: string): void;
deleteDirSync(path: string): void;
readDirSync(dirPath: string): string[];
ensureDirSync(path: string): void;
chmodSync(path: string, mode: number): void;
clear?(dirPath: string): void;
}
/**
* Factory function to create AnalyticsFileSystem with easy access to event emitter
* @param baseFileSystem The underlying filesystem implementation
* @param options Configuration options
* @returns Object containing the analytics filesystem instance and event emitter
*/
export declare function createAnalyticsFileSystem(baseFileSystem: IFileSystem, options?: AnalyticsFileSystemOptions): {
instance: AnalyticsFileSystem;
eventEmitter: EventEmitter<AnalyticsStatsEvent>;
};