( filePath: string, lineSchema: z.ZodType<T>, )
| 67 | } |
| 68 | |
| 69 | export async function readJsonlFile<T>( |
| 70 | filePath: string, |
| 71 | lineSchema: z.ZodType<T>, |
| 72 | ): Promise<T[]> { |
| 73 | let raw: string; |
| 74 | try { |
| 75 | raw = await readFile(filePath, 'utf-8'); |
| 76 | } catch (error) { |
| 77 | if (isNotFound(error)) return []; |
| 78 | throw error; |
| 79 | } |
| 80 | |
| 81 | const entries: T[] = []; |
| 82 | for (const line of raw.split('\n')) { |
| 83 | const trimmed = line.trim(); |
| 84 | if (trimmed.length === 0) continue; |
| 85 | try { |
| 86 | const parsed = JSON.parse(trimmed) as unknown; |
| 87 | const result = lineSchema.safeParse(parsed); |
| 88 | if (result.success) entries.push(result.data); |
| 89 | } catch { |
| 90 | // JSONL is append-only user data; tolerate bad rows and keep the rest. |
| 91 | } |
| 92 | } |
| 93 | return entries; |
| 94 | } |
| 95 | |
| 96 | export async function appendJsonlLine<T>( |
| 97 | filePath: string, |
no test coverage detected