import type { ServerResponse } from 'node:http'; import { prepareHeaders } from '../util/prepare-headers'; import { writeToServerResponse } from '../util/write-to-server-response'; /** * Writes a text stream to a Node.js ServerResponse object. * Each text chunk is encoded as UTF-8 and written as a separate chunk. * Sets a `Content-Type` header to `text/plain; charset=utf-8`. * * @param options - The options for piping the stream. * @param options.response - The Node.js ServerResponse to write to. * @param options.status - Optional HTTP status code. * @param options.statusText - Optional HTTP status text. * @param options.headers - Optional response headers. * @param options.stream - The text stream to pipe. * @returns A promise that resolves when the stream has been written. */ export function pipeTextStreamToResponse({ response, status, statusText, headers, stream, }: { response: ServerResponse; stream: ReadableStream; } & ResponseInit): Promise { return writeToServerResponse({ response, status, statusText, headers: prepareHeaders(headers, { 'content-type': 'text/plain; charset=utf-8', }), stream: stream.pipeThrough(new TextEncoderStream()), }); }