| 6 | const logger = createLogger('TxtParser') |
| 7 | |
| 8 | export class TxtParser implements FileParser { |
| 9 | async parseFile(filePath: string): Promise<FileParseResult> { |
| 10 | try { |
| 11 | if (!filePath) { |
| 12 | throw new Error('No file path provided') |
| 13 | } |
| 14 | |
| 15 | const buffer = await readFile(filePath) |
| 16 | |
| 17 | return this.parseBuffer(buffer) |
| 18 | } catch (error) { |
| 19 | logger.error('TXT file error:', error) |
| 20 | throw new Error(`Failed to parse TXT file: ${(error as Error).message}`) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | async parseBuffer(buffer: Buffer): Promise<FileParseResult> { |
| 25 | try { |
| 26 | logger.info('Parsing buffer, size:', buffer.length) |
| 27 | |
| 28 | const rawContent = buffer.toString('utf-8') |
| 29 | const result = sanitizeTextForUTF8(rawContent) |
| 30 | |
| 31 | return { |
| 32 | content: result, |
| 33 | metadata: { |
| 34 | characterCount: result.length, |
| 35 | tokenCount: result.length / 4, |
| 36 | }, |
| 37 | } |
| 38 | } catch (error) { |
| 39 | logger.error('TXT buffer parsing error:', error) |
| 40 | throw new Error(`Failed to parse TXT buffer: ${(error as Error).message}`) |
| 41 | } |
| 42 | } |
| 43 | } |
nothing calls this directly
no outgoing calls
no test coverage detected