ofReadableStream / ofWritableStream

import { ofReadableStream } from '@hullo/node'

These functions create wrappers for Readable/Writable NodeJS Streams.

ofReadableStream and ofWritableStream respect back-pressure mechanism of NodeJS Streams.

This example will allow reading 1 byte (highWaterMark: 1) of the script at one time, then it is piped through asynchronous transformation with mapAsync that will not really transform the data, but rather defer passing it along by 32 ms if a character is not a space. Lastly it gets to stdout as it subscribes whole pipeline. This gives an effect of letters appear in terminal one by one.

import { createReadStream } from "fs";
import { mapAsync, timeout } from "@hullo/core";
import { ofReadableStream, ofWritableStream } from "@hullo/node";
const fileContents = ofReadableStream(
createReadStream(__filename, {
highWaterMark: 1
})
);
const stdout = ofWritableStream(process.stdout);
fileContents
.pipe(
mapAsync(async data => {
await timeout(data === " " ? 0 : 32);
return data;
})
)
.subscribe(stdout);