Skip to content

Function: convertArrayLikeToStream() ​

Call Signature ​

convertArrayLikeToStream(inp, options?): Readable

Create a readable stream of the bytes of a buffer, one byte per chunk as a number (see convertArrayLikeToIterable). The stream is in object mode by default: a byte stream cannot be piped into a byte sink (a file, a socket), and objectMode: false makes it fail with an ERR_INVALID_ARG_TYPE error, emitted by the stream, as its chunks are numbers. To stream the buffer as bytes, use Readable.from([buffer]) instead.

Parameters ​

inp ​

Buffer

The buffer to stream; an empty buffer gives a stream ending at once.

options? ​

ReadableOptions<Readable>

The options of the stream (Readable.from): objectMode defaults to true, highWaterMark counts chunks (16 by default).

Returns ​

Readable

A readable stream of the bytes, as numbers.

Example ​

typescript
import {convertArrayLikeToStream} from 'lakutata/utils'

const bytes: number[] = []
for await (const byte of convertArrayLikeToStream(Buffer.from('hi'))) bytes.push(byte)
console.log(bytes)
//[104, 105]

Call Signature ​

convertArrayLikeToStream(inp, options?): Readable

Create a readable stream of the elements of a typed array, one element per chunk as a number (a bigint for the BigInt64Array), in object mode by default (see convertArrayLikeToIterable): objectMode: false makes the stream fail with an ERR_INVALID_ARG_TYPE error, emitted by the stream, as its chunks are not bytes.

Parameters ​

inp ​

TypedArray

The typed array to stream; an empty one gives a stream ending at once.

options? ​

ReadableOptions<Readable>

The options of the stream (Readable.from): objectMode defaults to true, highWaterMark counts chunks (16 by default).

Returns ​

Readable

A readable stream of the elements.

Example ​

typescript
import {convertArrayLikeToStream} from 'lakutata/utils'

const values: number[] = []
for await (const value of convertArrayLikeToStream(new Uint16Array([1, 500]))) values.push(value)
console.log(values)
//[1, 500]

Call Signature ​

convertArrayLikeToStream(inp, options?): Readable

Create a readable stream of the characters of a string, one UTF-16 code unit per chunk (an emoji gives its two surrogate halves in two chunks, see convertArrayLikeToIterable), in object mode by default. With objectMode: false each chunk is converted into its UTF-8 bytes, and a lone surrogate half into the replacement character U+FFFD.

Parameters ​

inp ​

string

The string to stream; an empty string gives a stream ending at once.

options? ​

ReadableOptions<Readable>

The options of the stream (Readable.from): objectMode defaults to true, highWaterMark counts chunks (16 by default) in object mode.

Returns ​

Readable

A readable stream of the characters.

Example ​

typescript
import {convertArrayLikeToStream} from 'lakutata/utils'

const chunks: string[] = []
for await (const chunk of convertArrayLikeToStream('abc')) chunks.push(chunk)
console.log(chunks)
//['a', 'b', 'c']

Released under the MIT License.