rawi
Version:
Rawi (راوي) is the developer-friendly AI CLI that brings the power of 11 major AI providers directly to your terminal. With seamless shell integration, persistent conversations, and 200+ specialized prompt templates, Rawi transforms your command line into
1 lines • 6.82 kB
Source Map (JSON)
{"version":3,"sources":["/home/mkabumattar/work/withrawi/rawi/dist/chunk-KKTV3DG4.cjs","../src/core/file-readers/validators/file-reader.errors.ts"],"names":["FileReaderError","message","code","FileNotFoundError","filePath"],"mappings":"AAAA;ACAO,IAAMA,CAAAA,CAAN,MAAA,QAA8B,KAAM,CACzC,WAAA,CACEC,CAAAA,CACgBC,CAAAA,CAChB,CACA,KAAA,CAAMD,CAAO,CAAA,CAFG,IAAA,CAAA,IAAA,CAAAC,CAAAA,CAGhB,IAAA,CAAK,IAAA,CAAO,iBACd,CACF,CAAA,CAEaC,CAAAA,aAAN,MAAA,QAAgCH,CAAgB,CACrD,WAAA,CAAYI,CAAAA,CAAkB,CAC5B,KAAA,CAAM,CAAA,gBAAA,EAAmBA,CAAQ,CAAA,CAAA;ADVqH","file":"/home/mkabumattar/work/withrawi/rawi/dist/chunk-KKTV3DG4.cjs","sourcesContent":[null,"export class FileReaderError extends Error {\n constructor(\n message: string,\n public readonly code?: string,\n ) {\n super(message);\n this.name = 'FileReaderError';\n }\n}\n\nexport class FileNotFoundError extends FileReaderError {\n constructor(filePath: string) {\n super(`File not found: ${filePath}`, 'FILE_NOT_FOUND');\n this.name = 'FileNotFoundError';\n }\n}\n\nexport class FileSizeError extends FileReaderError {\n constructor(filePath: string, actualSize: number, maxSize: number) {\n const actualMB = Math.round(actualSize / 1024 / 1024);\n const maxMB = Math.round(maxSize / 1024 / 1024);\n super(\n `File size (${actualMB}MB) exceeds maximum allowed size (${maxMB}MB): ${filePath}`,\n 'FILE_SIZE_EXCEEDED',\n );\n this.name = 'FileSizeError';\n }\n}\n\nexport class UnsupportedFileTypeError extends FileReaderError {\n constructor(fileType: string, filePath: string) {\n super(\n `Unsupported file type \"${fileType}\": ${filePath}`,\n 'UNSUPPORTED_FILE_TYPE',\n );\n this.name = 'UnsupportedFileTypeError';\n }\n}\n\nexport class FileParsingError extends FileReaderError {\n constructor(filePath: string, reason: string) {\n super(\n `Failed to parse file \"${filePath}\": ${reason}`,\n 'FILE_PARSING_ERROR',\n );\n this.name = 'FileParsingError';\n }\n}\n\nexport class CorruptedFileError extends FileReaderError {\n constructor(filePath: string, details?: string) {\n const message = details\n ? `Corrupted or invalid file \"${filePath}\": ${details}`\n : `Corrupted or invalid file: ${filePath}`;\n super(message, 'CORRUPTED_FILE');\n this.name = 'CorruptedFileError';\n }\n}\n\nexport class EncryptedFileError extends FileReaderError {\n constructor(filePath: string) {\n super(\n `Encrypted or password-protected file not supported: ${filePath}`,\n 'ENCRYPTED_FILE',\n );\n this.name = 'EncryptedFileError';\n }\n}\n\nexport class FilePermissionError extends FileReaderError {\n constructor(filePath: string) {\n super(\n `Insufficient permissions to read file: ${filePath}`,\n 'PERMISSION_DENIED',\n );\n this.name = 'FilePermissionError';\n }\n}\n\nexport class NoReaderAvailableError extends FileReaderError {\n constructor(fileType: string) {\n super(\n `No reader available for file type: ${fileType}`,\n 'NO_READER_AVAILABLE',\n );\n this.name = 'NoReaderAvailableError';\n }\n}\n\nconst MAX_PATH_LENGTH = 260;\nconst SUSPICIOUS_PATTERNS = [/\\.\\./, /^[/\\\\]/, /[<>:\"|?*]/];\n\nexport function validateFilePath(filePath: string): void {\n if (!filePath || typeof filePath !== 'string') {\n throw new FileReaderError(\n 'Invalid file path: must be a non-empty string',\n 'INVALID_PATH',\n );\n }\n\n if (filePath.length > MAX_PATH_LENGTH) {\n throw new FileReaderError(\n `File path too long (${filePath.length} characters, max ${MAX_PATH_LENGTH})`,\n 'PATH_TOO_LONG',\n );\n }\n\n for (const pattern of SUSPICIOUS_PATTERNS) {\n if (pattern.test(filePath)) {\n throw new FileReaderError(\n `Potentially unsafe file path detected: ${filePath}`,\n 'UNSAFE_PATH',\n );\n }\n }\n\n if (filePath.includes('\\0')) {\n throw new FileReaderError('File path contains null bytes', 'INVALID_PATH');\n }\n}\n\nexport function validateFileSize(\n size: number,\n maxSize: number,\n filePath: string,\n): void {\n if (size < 0) {\n throw new FileReaderError(`Invalid file size: ${size}`, 'INVALID_SIZE');\n }\n\n if (size === 0) {\n throw new FileReaderError(`File is empty: ${filePath}`, 'EMPTY_FILE');\n }\n\n if (size > maxSize) {\n throw new FileSizeError(filePath, size, maxSize);\n }\n}\n\nexport function validateFileExtension(\n filePath: string,\n expectedExtensions: string[],\n): boolean {\n const actualExtension = filePath.toLowerCase().split('.').pop() || '';\n return expectedExtensions.some(\n (ext) => ext.toLowerCase() === actualExtension,\n );\n}\n\nexport function sanitizeFileName(fileName: string): string {\n return fileName\n .replace(/[<>:\"|?*]/g, '_')\n .replace(/\\.\\./g, '_')\n .replace(/^\\./, '_')\n .substring(0, 255);\n}\n\nexport function createUserFriendlyError(\n error: Error,\n filePath: string,\n): string {\n if (error instanceof FileNotFoundError) {\n return `The file \"${filePath}\" could not be found. Please check the file path and try again.`;\n }\n\n if (error instanceof FileSizeError) {\n return `The file \"${filePath}\" is too large. Please use a smaller file or increase the size limit.`;\n }\n\n if (error instanceof UnsupportedFileTypeError) {\n return 'The file type is not supported. Supported formats: PDF, DOCX, XLSX, and text-based files.';\n }\n\n if (error instanceof FileParsingError) {\n return `The file \"${filePath}\" could not be parsed. It may be corrupted or in an unsupported format.`;\n }\n\n if (error instanceof CorruptedFileError) {\n return `The file \"${filePath}\" appears to be corrupted or damaged. Please try with a different file.`;\n }\n\n if (error instanceof EncryptedFileError) {\n return `The file \"${filePath}\" is password-protected or encrypted. Please use an unprotected version.`;\n }\n\n if (error instanceof FilePermissionError) {\n return `Permission denied when trying to read \"${filePath}\". Please check file permissions.`;\n }\n\n if (error instanceof NoReaderAvailableError) {\n return 'No reader is available for this file type. Supported formats: PDF, DOCX, XLSX, and text-based files.';\n }\n\n return `An error occurred while reading \"${filePath}\": ${error.message}`;\n}\n\nexport function logFileReaderError(\n error: Error,\n filePath: string,\n verbose = false,\n): void {\n if (verbose) {\n console.error(`[FileReader Error] ${error.name}: ${error.message}`);\n console.error(`[FileReader Error] File: ${filePath}`);\n if (error.stack) {\n console.error(`[FileReader Error] Stack: ${error.stack}`);\n }\n } else {\n console.error(`Failed to read file \"${filePath}\": ${error.message}`);\n }\n}\n"]}