🌊 Reads a stream fully into a string.

Info

Byte chunks are decoded as UTF-8 across chunk boundaries, so a multi-byte character split between two chunks still decodes correctly. String chunks are concatenated as they are.

Syntax

TypeScript
import { streamToText } from '@opentf/std';

streamToText(stream: ReadableStream<Uint8Array | string>): Promise<string>;

Parameters

NameTypeDescription
streamReadableStream<Uint8Array | string>The stream to drain.

Returns

A promise for the decoded text.

Examples

TypeScript
await streamToText(response.body) //=> 'Hello World'

Chunk boundaries do not corrupt characters:

TypeScript
const bytes = stringToBytes('héllo 🌍');
// Split mid-character — still decodes correctly.
await streamToText(streamOf([bytes.slice(0, 3), bytes.slice(3)])) //=> 'héllo 🌍'
Last updated on
Edit this page