UNPKG

ai

Version:

AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript

30 lines (29 loc) 788 B
/** * Consumes a ReadableStream until it's fully read. * * This function reads the stream chunk by chunk until the stream is exhausted. * It doesn't process or return the data from the stream; it simply ensures * that the entire stream is read. * * @param {ReadableStream} stream - The ReadableStream to be consumed. * @returns {Promise<void>} A promise that resolves when the stream is fully consumed. */ export async function consumeStream({ stream, onError, }: { stream: ReadableStream; onError?: (error: unknown) => void; }): Promise<void> { const reader = stream.getReader(); try { while (true) { const { done } = await reader.read(); if (done) break; } } catch (error) { onError?.(error); } finally { reader.releaseLock(); } }