import { TextEncoder } from "node:util"; /** * Convert an async iterable of strings into a ReadableStream. * @param iterable - Async iterable of strings. * @returns ReadableStream of Uint8Array chunks. */ export const streamAsyncStrings = (iterable: AsyncIterable): ReadableStream => { const encoder = new TextEncoder(); const iterator = iterable[Symbol.asyncIterator](); return new ReadableStream({ async pull(controller): Promise { const { value, done } = await iterator.next(); if (done) { controller.close(); return; } controller.enqueue(encoder.encode(value)); }, async cancel(): Promise { if (iterator.return) { await iterator.return(); } }, }); }; /** * Build SSE headers for streaming responses. * @param extra - Additional headers. * @returns Headers object. */ export const buildSseHeaders = (extra: Record = {}): Record => ({ "Content-Type": "text/event-stream", "Cache-Control": "no-cache, no-transform", Connection: "keep-alive", "X-Accel-Buffering": "no", ...extra, });