packfs-core
Version:
Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.
89 lines • 3.32 kB
JavaScript
;
/**
* In-memory storage backend for testing and caching
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryBackend = void 0;
const logger_js_1 = require("../core/logger.js");
class MemoryBackend {
constructor() {
this.files = new Map();
this.logger = logger_js_1.Logger.getInstance().createChildLogger('MemoryBackend');
}
async initialize() {
this.logger.info('Initializing memory backend');
// No initialization needed for memory backend
}
async read(path) {
this.logger.debug(`Reading file: ${path}`);
const file = this.files.get(path);
if (!file) {
this.logger.error(`File not found: ${path}`);
throw new Error(`File not found: ${path}`);
}
this.logger.info(`Successfully read file: ${path}`, { size: file.data.length });
return file.data;
}
async write(path, data) {
this.logger.debug(`Writing file: ${path}`, { size: data.length });
const metadata = {
path,
size: data.length,
mtime: new Date(),
isDirectory: false,
permissions: 0o644
};
this.files.set(path, { data, metadata });
this.logger.info(`Successfully wrote file: ${path}`, { size: data.length });
}
async exists(path) {
this.logger.debug(`Checking existence: ${path}`);
const exists = this.files.has(path);
this.logger.debug(`File ${exists ? 'exists' : 'does not exist'}: ${path}`);
return exists;
}
async stat(path) {
this.logger.debug(`Getting file stats: ${path}`);
const file = this.files.get(path);
if (!file) {
this.logger.error(`File not found: ${path}`);
throw new Error(`File not found: ${path}`);
}
this.logger.debug(`Got file stats: ${path}`, file.metadata);
return file.metadata;
}
async list(path) {
this.logger.debug(`Listing directory: ${path}`);
// Simple implementation - return all files that start with the path
const entries = [];
for (const filePath of this.files.keys()) {
if (filePath.startsWith(path) && filePath !== path) {
const relativePath = filePath.substring(path.length + 1);
const parts = relativePath.split('/');
if (parts.length === 1) {
entries.push(parts[0]);
}
}
}
const uniqueEntries = [...new Set(entries)];
this.logger.info(`Listed directory: ${path}`, { count: uniqueEntries.length });
return uniqueEntries;
}
async delete(path) {
this.logger.debug(`Deleting: ${path}`);
if (!this.files.has(path)) {
this.logger.error(`File not found: ${path}`);
throw new Error(`File not found: ${path}`);
}
this.files.delete(path);
this.logger.info(`Deleted file: ${path}`);
}
async cleanup() {
this.logger.info('Cleaning up memory backend');
const fileCount = this.files.size;
this.files.clear();
this.logger.debug(`Cleared ${fileCount} files from memory`);
}
}
exports.MemoryBackend = MemoryBackend;
//# sourceMappingURL=memory.js.map