UNPKG

@synet/fs

Version:

Robust, battle-tested filesystem abstraction for Node.js

91 lines (90 loc) 2.76 kB
import { type Event, EventEmitter } from "@synet/unit"; import type { IAsyncFileSystem } from "./async-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 IAsyncFileSystem implementation to provide analytics capabilities */ export declare class AnalyticsFileSystem implements IAsyncFileSystem { private baseFileSystem; private options; private eventEmitter; private stats; private operationCount; private readonly emitThreshold; constructor(baseFileSystem: IAsyncFileSystem, 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; exists(path: string): Promise<boolean>; readFile(path: string): Promise<string>; writeFile(path: string, data: string): Promise<void>; deleteFile(path: string): Promise<void>; deleteDir(path: string): Promise<void>; readDir(dirPath: string): Promise<string[]>; ensureDir(path: string): Promise<void>; chmod(path: string, mode: number): Promise<void>; clear?(dirPath: string): Promise<void>; } /** * Factory function to create async 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: IAsyncFileSystem, options?: AnalyticsFileSystemOptions): { instance: AnalyticsFileSystem; eventEmitter: EventEmitter<AnalyticsStatsEvent>; };