toText

import { toText } from "jsr:@ende/streams";

function

async function toText(stream: ReadableStream<string> | ReadableStream<Uint8Array>): Promise<string>

Converts a ReadableSteam of strings or Uint8Arrays to a single string. Works the same as Response.text and Request.text, but also extends to support streams of strings.

stream: ReadableStream<string> | ReadableStream<Uint8Array>

A ReadableStream to convert into a string.

Returns

A Promise that resolves to the string.

Example: Basic usage with a stream of strings

import { toText } from "@ende/streams/to-text";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from(["Hello, ", "world!"]);
assertEquals(await toText(stream), "Hello, world!");

Example: Basic usage with a stream of Uint8Arrays

import { toText } from "@ende/streams/to-text";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from(["Hello, ", "world!"])
  .pipeThrough(new TextEncoderStream());
assertEquals(await toText(stream), "Hello, world!");

Source