Wrapper on Node.js stream.Readable()
.
E.g. you can use:
const rStream = require('safe-readable-stream');
const outStream = rStream.createSafeReadableStream({
logger,
objectMode: false,
});
// Good, await will wait until stream will be ready to read next chunks.
await outStream.pushArray(['A', 'B', 'C']);
// Pushing next chunk.
// But without wait. Not good.
// Next push can return rejected promise
// if underlying push will require next event loop tick.
outStream.push('B');
// Promise can be rejected.
// Because it is not safe to push data outside _read call.
await outStream.finish();
It is useful when you are reading some data from some source by chunks and at some soment you got error from the source. So you should pass the error to the receiving part.
So for receiving part you can use such code:
const rStream = require('safe-readable-stream');
response.on('data', (data) => {
const errStr = rStream.checkErrorString(data);
if (errStr) {
// Error was sent.
l.println(`Checked error string: ${errStr}`);
}
});