(controller)
| 55 | |
| 56 | const stream = new ReadableStream<Uint8Array>({ |
| 57 | async start(controller) { |
| 58 | const encoder = new TextEncoder() |
| 59 | try { |
| 60 | if (format === 'csv') { |
| 61 | controller.enqueue( |
| 62 | encoder.encode(`${toCsvRow(columns.map((c) => neutralizeCsvFormula(c.name)))}\n`) |
| 63 | ) |
| 64 | } else { |
| 65 | controller.enqueue(encoder.encode('[')) |
| 66 | } |
| 67 | |
| 68 | let offset = 0 |
| 69 | let firstJsonRow = true |
| 70 | while (true) { |
| 71 | const result = await queryRows( |
| 72 | table, |
| 73 | { limit: EXPORT_BATCH_SIZE, offset, includeTotal: false }, |
| 74 | requestId |
| 75 | ) |
| 76 | |
| 77 | for (const row of result.rows) { |
| 78 | if (format === 'csv') { |
| 79 | const values = columns.map((c) => formatCsvValue(row.data[getColumnId(c)])) |
| 80 | controller.enqueue(encoder.encode(`${toCsvRow(values)}\n`)) |
| 81 | } else { |
| 82 | const prefix = firstJsonRow ? '' : ',' |
| 83 | firstJsonRow = false |
| 84 | controller.enqueue( |
| 85 | encoder.encode(prefix + JSON.stringify(rowDataIdToName(row.data, nameById))) |
| 86 | ) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (result.rows.length < EXPORT_BATCH_SIZE) break |
| 91 | offset += result.rows.length |
| 92 | } |
| 93 | |
| 94 | if (format === 'json') controller.enqueue(encoder.encode(']')) |
| 95 | controller.close() |
| 96 | |
| 97 | logger.info(`[${requestId}] Exported table ${tableId}`, { |
| 98 | format, |
| 99 | rowCount: table.rowCount, |
| 100 | }) |
| 101 | } catch (err) { |
| 102 | logger.error(`[${requestId}] Export failed for table ${tableId}`, err) |
| 103 | controller.error(err) |
| 104 | } |
| 105 | }, |
| 106 | }) |
| 107 | |
| 108 | return new NextResponse(stream, { |
nothing calls this directly
no test coverage detected