@toast-studios/asset-manager
Version:
A React Native asset management library with intelligent caching and loading strategies
190 lines (189 loc) • 5.75 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
import { StorageInfo, ValidationResult } from '../types/index';
/**
* Storage management utility for asset files with hash-based deduplication
*/
export declare class StorageManager {
private basePath;
private enableLogging;
private assetRegistry;
private hashPathMapping;
private assetRegistryPath;
private hashPathMappingPath;
private assetRegistryLoaded;
private hashPathMappingLoaded;
private pendingAssetUpdates;
private pendingHashUpdates;
private saveDebounceTimer?;
private readonly saveDebounceMs;
private isDestroyed;
constructor(basePath: string, enableLogging?: boolean);
/**
* Schedule a batched save operation with debouncing
*/
private scheduleSave;
/**
* Immediately flush all pending saves to disk
*/
private flushPendingSaves;
/**
* Initialize storage manager
*/
initialize(): Promise<void>;
/**
* Load asset registry from disk
*/
private loadAssetRegistry;
/**
* Save asset registry to disk immediately (used by batching system)
*/
private saveAssetRegistryToDisk;
/**
* Save asset registry with batching optimization
*/
private saveAssetRegistry;
/**
* Load hash path mapping from disk
*/
private loadHashPathMapping;
/**
* Save hash path mapping to disk immediately (used by batching system)
*/
private saveHashPathMappingToDisk;
/**
* Save hash path mapping with batching optimization
*/
private saveHashPathMapping;
/**
* Get comprehensive storage information
*/
getStorageInfo(): Promise<StorageInfo>;
/**
* Check if asset exists locally with optional ETag validation
*/
assetExists(assetPath: string, etag?: string): Promise<boolean>;
/**
* Check if asset exists locally with optional ETag validation using asset ID
*/
assetExistsById(assetId: string, assetPath: string, etag?: string): Promise<boolean>;
/**
* Check if an asset with the given hash already exists
*/
private hasAssetWithHash;
/**
* Save asset data to storage with hash-based deduplication
*/
saveAsset(data: Buffer, path: string, etag?: string, assetId?: string, expectedHash?: string): Promise<void>;
/**
* Create a reference to an existing asset instead of duplicating it
*/
private createAssetReference;
/**
* Register a new asset hash in the hash path mapping (batched)
*/
private registerAssetHash;
/**
* Get the actual file path for an asset, resolving hash-based references
*/
getAssetPath(assetId: string): Promise<string | null>;
/**
* Validate asset integrity using hash comparison with caching
*/
validateAsset(path: string, expectedHash?: string): Promise<ValidationResult>;
/**
* Validate asset integrity by asset ID using cached hash registry
*/
validateAssetById(assetId: string, path: string, expectedHash?: string): Promise<ValidationResult>;
/**
* Save ETag for an asset in the unified registry
*/
saveETag(assetId: string, filePath: string, etag: string): Promise<void>;
/**
* Get path for temporary downloads
*/
getTempPath(filename: string): string;
/**
* Get path for extracted assets
*/
getExtractedPath(subPath: string): string;
/**
* Get path for bundle cache
*/
getBundlePath(bundleId: string): string;
/**
* Move file from one location to another
*/
moveFile(fromPath: string, toPath: string): Promise<void>;
/**
* Get full path from relative path
*/
private getFullPath;
/**
* Calculate total size of a directory recursively
*/
private calculateDirectorySize;
/**
* Get all files in a directory recursively
*/
private getAllFiles;
/**
* Get cached hash or calculate new one if file has changed (using centralized registry)
*/
private getCachedOrCalculateHashById;
/**
* Helper method to count how many assets reference a specific hash
*/
private countHashReferences;
/**
* Delete asset by ID
*/
deleteAssetById(assetId: string, _path: string): Promise<void>;
/**
* Cleanup old/unused assets based on age and size constraints
*/
cleanup(options?: {
maxAge?: number;
maxSize?: number;
}): Promise<number>;
/**
* Clean up orphaned asset registry entries (entries for assets that no longer exist)
* Note: This is a simplified cleanup that only removes backward-compatibility path-based entries.
* In practice, asset IDs in the registry should be managed by the asset manifest system.
*/
private cleanupOrphanedRegistryEntries;
/**
* Clean up legacy .hash, .hash.meta, and .etag files from the old system
*/
private cleanupLegacyFiles;
/**
* Get deduplication statistics
*/
getDeduplicationStats(): Promise<{
totalAssets: number;
uniqueFiles: number;
savedSpace: number;
dedupRatio: number;
}>;
/**
* Log message if logging is enabled
*/
private log;
/**
* Cleanup resources and flush pending saves
*/
destroy(): Promise<void>;
/**
* Manually flush all pending saves to disk immediately
* Useful for critical operations or before app shutdown
*/
flush(): Promise<void>;
/**
* Get statistics about batching performance
*/
getBatchingStats(): {
pendingAssetUpdates: number;
pendingHashUpdates: number;
hasPendingTimer: boolean;
};
}