| 15 | } |
| 16 | |
| 17 | export class CsvParser implements FileParser { |
| 18 | async parseFile(filePath: string): Promise<FileParseResult> { |
| 19 | if (!filePath) { |
| 20 | throw new Error('No file path provided') |
| 21 | } |
| 22 | |
| 23 | if (!existsSync(filePath)) { |
| 24 | throw new Error(`File not found: ${filePath}`) |
| 25 | } |
| 26 | |
| 27 | const stream = createReadStream(filePath, { |
| 28 | highWaterMark: CONFIG.STREAM_CHUNK_SIZE, |
| 29 | }) |
| 30 | |
| 31 | return this.parseStream(stream) |
| 32 | } |
| 33 | |
| 34 | async parseBuffer(buffer: Buffer): Promise<FileParseResult> { |
| 35 | const bufferSize = buffer.length |
| 36 | logger.info( |
| 37 | `Parsing CSV buffer, size: ${bufferSize} bytes (${(bufferSize / 1024 / 1024).toFixed(2)} MB)` |
| 38 | ) |
| 39 | |
| 40 | const stream = new Readable({ read() {} }) |
| 41 | stream.push(buffer) |
| 42 | stream.push(null) |
| 43 | |
| 44 | return this.parseStream(stream) |
| 45 | } |
| 46 | |
| 47 | private parseStream(inputStream: NodeJS.ReadableStream): Promise<FileParseResult> { |
| 48 | return new Promise((resolve, reject) => { |
| 49 | let rowCount = 0 |
| 50 | let errorCount = 0 |
| 51 | let headers: string[] = [] |
| 52 | let processedContent = '' |
| 53 | const sampledRows: any[] = [] |
| 54 | const errors: string[] = [] |
| 55 | let firstRowProcessed = false |
| 56 | let aborted = false |
| 57 | |
| 58 | const parserOptions: Options = { |
| 59 | columns: true, // Use first row as headers |
| 60 | skip_empty_lines: true, // Skip empty lines |
| 61 | trim: true, // Trim whitespace |
| 62 | relax_column_count: true, // Allow variable column counts |
| 63 | relax_quotes: true, // Be lenient with quotes |
| 64 | skip_records_with_error: true, // Skip bad records |
| 65 | raw: false, |
| 66 | cast: false, |
| 67 | } |
| 68 | const parser = parse(parserOptions) |
| 69 | |
| 70 | parser.on('readable', () => { |
| 71 | let record |
| 72 | while ((record = parser.read()) !== null && !aborted) { |
| 73 | rowCount++ |
| 74 |
nothing calls this directly
no outgoing calls
no test coverage detected