export function createBatchTransform( batchSize: number, processBatch: (items: T[]) => Promise, onBatchComplete?: (processedCount: number) => void, ): TransformStream { let batch: T[] = []; let totalProcessed = 0; return new TransformStream({ transform(item, controller) { batch.push(item); if (batch.length >= batchSize) { return processBatch(batch).then((result) => { controller.enqueue(result); totalProcessed += batch.length; onBatchComplete?.(totalProcessed); batch = []; }); } return; }, flush(controller) { if (batch.length > 0) { return processBatch(batch).then((result) => { controller.enqueue(result); totalProcessed += batch.length; onBatchComplete?.(totalProcessed); batch = []; }); } return; }, }); }