Home

Web Stream Tools

This library contains both basic convenience functions such as readToEnd, concat, slice, clone, webToNode and nodeToWeb, and more complex functions for transforming and parsing streams. Examples of the latter can be found below.

Unlike transform, transformPair will always return a stream, even if input is not.

Parsing data on a stream which is expected to be in a specific format

There are also helper functions for reading a specific number of bytes, or a single line, etc:

stream.parse(input, reader => {
const byte = await reader.readByte(); // Single byte or undefined
const bytes = await reader.readBytes(n); // Uint8Array of up to n bytes, or undefined
const line = await reader.readLine(); // Returns String up to and including the first \n, or undefined. This function is specifically for a stream of Strings.
// There's also peekBytes() and unshift(), which you can use to look ahead in the stream.
const stream = reader.remainder(); // New stream containing the remainder of the original stream. Only available when using a Reader from stream.parse()
});

Most of the functions above are also available when getting a reader using stream.getReader() instead of stream.parse().

All of the functions above also work when reading a stream containing Strings instead of a Uint8Arrays, and will return Strings in that case.

Cloning and slicing streams

There are also a few functions not for reading the stream, but for manipulating the stream for another function to read:

stream.slice(input, begin, end); // Returns a stream pointing to part of the original stream, or a Uint8Array
stream.clone(input); // Returns a copy of the stream so that two functions can read it. Note: this does *not* clone a Uint8Array, since this function is only meant for reading the same data twice.
stream.passiveClone(input); // Also returns a copy of the stream, but doesn't return data immediately when you read from it, only returns data when you read from the original stream. This is meant for respecting backpressure.

Note: these three functions do not work well with Node streams. Please convert Node streams to Web streams with stream.nodeToWeb first before using them.