Skip to main content
Module

x/codepoint_iterator/iterable.ts>getIterableFromStream

Fast uint8array to utf-8 codepoint iterator for streams and array buffers by @okikio & @jonathantneal
Latest
function getIterableFromStream
import { getIterableFromStream } from "https://deno.land/x/codepoint_iterator@v1.1.1/iterable.ts";

Converts a ReadableStream into an async iterable. This allows for easier consumption of stream data using asynchronous iteration, providing a more modern approach to handling streamed data.

Ideally this would already be built into ReadableStream, but it's currently not so this should help tide over til js runtimes support async iterables for ReadableStreams.

Examples

Example 1

const responseStream = fetch('https://example.com/data').then(res => res.body);
for await (const chunk of getIterableStream(await responseStream)) {
  console.log(new TextDecoder().decode(chunk)); // Assuming the stream is text data
}

Converting a ReadableStream from a fetch request into an async iterable, and then asynchronously iterating over each chunk of data, decoding and logging the text content.

Type Parameters

optional
T = Uint8Array

The type of data chunks contained within the ReadableStream, defaulting to Uint8Array.

Parameters

stream: ReadableStream<T>

The ReadableStream to be converted into an async iterable. This stream can contain any type of data, typically Uint8Array for binary data.

Returns

AsyncIterable<T>

An AsyncIterable that yields data chunks from the ReadableStream as they are read.