@memory-bank/mcp
Version:
Memory-enabled Co-Pilot (MCP) server for managing project documentation and context
187 lines • 6.39 kB
JavaScript
import path from 'node:path';
import { DomainError } from '../../../shared/errors/DomainError.js';
import { InfrastructureError, InfrastructureErrorCodes } from '../../../shared/errors/InfrastructureError.js';
import { logger } from '../../../shared/utils/logger.js';
/**
* Base class: Provides common functionality for file system based memory bank repositories
*/
export class FileSystemMemoryBankRepositoryBase {
fileSystemService;
configProvider;
/**
* Constructor
* @param fileSystemService File system service
* @param configProvider Configuration provider
*/
constructor(fileSystemService, configProvider) {
this.fileSystemService = fileSystemService;
this.configProvider = configProvider;
}
/**
* Check if directory exists
* @param dirPath Directory path
* @returns true if directory exists
*/
async directoryExists(dirPath) {
try {
return await this.fileSystemService.directoryExists(dirPath);
}
catch (error) {
throw new InfrastructureError(InfrastructureErrorCodes.FILE_SYSTEM_ERROR, `Failed to check if directory exists: ${error.message}`, { originalError: error });
}
}
/**
* Check if file exists
* @param filePath File path
* @returns true if file exists
*/
async fileExists(filePath) {
try {
return await this.fileSystemService.fileExists(filePath);
}
catch (error) {
throw new InfrastructureError(InfrastructureErrorCodes.FILE_SYSTEM_ERROR, `Failed to check if file exists: ${error.message}`, { originalError: error });
}
}
/**
* Create directory
* @param dirPath Directory path
*/
async createDirectory(dirPath) {
try {
await this.fileSystemService.createDirectory(dirPath);
}
catch (error) {
throw new InfrastructureError(InfrastructureErrorCodes.FILE_SYSTEM_ERROR, `Failed to create directory: ${error.message}`, { originalError: error });
}
}
/**
* Write file
* @param filePath File path
* @param content File content
*/
async writeFile(filePath, content) {
try {
// Ensure directory exists
const dirPath = path.dirname(filePath);
await this.createDirectory(dirPath);
// Write file
await this.fileSystemService.writeFile(filePath, content);
}
catch (error) {
if (error instanceof DomainError || error instanceof InfrastructureError) {
throw error;
}
throw new InfrastructureError(InfrastructureErrorCodes.FILE_WRITE_ERROR, `Failed to write file: ${error.message}`, { originalError: error });
}
}
/**
* Read file
* @param filePath File path
* @returns File content
*/
async readFile(filePath) {
try {
return await this.fileSystemService.readFile(filePath);
}
catch (error) {
if (error instanceof DomainError || error instanceof InfrastructureError) {
throw error;
}
throw new InfrastructureError(InfrastructureErrorCodes.FILE_READ_ERROR, `Failed to read file: ${error.message}`, { originalError: error });
}
}
/**
* Delete file
* @param filePath File path
* @returns true if deletion was successful
*/
async deleteFile(filePath) {
try {
return await this.fileSystemService.deleteFile(filePath);
}
catch (error) {
if (error instanceof DomainError || error instanceof InfrastructureError) {
throw error;
}
throw new InfrastructureError(InfrastructureErrorCodes.FILE_SYSTEM_ERROR, `Failed to delete file: ${error.message}`, { originalError: error });
}
}
/**
* List files
* @param dirPath Directory path
* @returns Array of file paths
*/
async listFiles(dirPath) {
try {
return await this.fileSystemService.listFiles(dirPath);
}
catch (error) {
if (error instanceof DomainError || error instanceof InfrastructureError) {
throw error;
}
throw new InfrastructureError(InfrastructureErrorCodes.FILE_SYSTEM_ERROR, `Failed to list files: ${error.message}`, { originalError: error });
}
}
/**
* Get file stats
* @param filePath File path
* @returns File stats information
*/
async getFileStats(filePath) {
try {
return await this.fileSystemService.getFileStats(filePath);
}
catch (error) {
if (error instanceof DomainError || error instanceof InfrastructureError) {
throw error;
}
throw new InfrastructureError(InfrastructureErrorCodes.FILE_SYSTEM_ERROR, `Failed to get file stats: ${error.message}`, { originalError: error });
}
}
/**
* Generate UUID
* @returns UUID string
*/
generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
/**
* Check if path is valid (path traversal protection)
* @param requestedPath Path
* @param basePath Base path
* @returns true if path is valid
*/
isValidPath(requestedPath, basePath) {
const normalizedPath = path.normalize(requestedPath);
const resolvedPath = path.resolve(basePath, normalizedPath);
// Check if path is outside the base directory
return resolvedPath.startsWith(path.resolve(basePath));
}
/**
* Log an error
* @param message Error message
* @param error Error object
*/
logError(message, error) {
logger.error(message, error);
}
/**
* Log debug information
* @param message Debug message
* @param context Optional context
*/
logDebug(message, context) {
if (context) {
logger.debug(message, context);
}
else {
logger.debug(message);
}
}
}
//# sourceMappingURL=FileSystemMemoryBankRepositoryBase.js.map