@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
38 lines (37 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readableFrom = exports.readableCreate = void 0;
const stream_1 = require("stream");
/**
* Convenience function to create a Readable that can be pushed into (similar to RxJS Subject).
* Push `null` to it to complete (similar to RxJS `.complete()`).
*
* Difference from Readable.from() is that this readable is not "finished" yet and allows pushing more to it.
*
* Caution!
* The implementation of this Readable is not fully compliant,
* e.g the read() method doesn't return anything, so, it will hand the Node process (or cause it to process.exit(0))
* if read() will be called AFTER everything was pushed and Readable is closed (by pushing `null`).
* Beware of it when e.g doing unit testing! Jest prefers to hang (not exit-0).
*
* @deprecated because of the caution above
*/
function readableCreate(items = [], opt) {
const readable = new stream_1.Readable({
objectMode: true,
...opt,
read() { }, // Caution, if this is called and Readable has not finished yet (null wasn't pushed) - it'll hang the process!
});
for (const item of items) {
readable.push(item);
}
return readable;
}
exports.readableCreate = readableCreate;
/**
* Convenience type-safe wrapper around Readable.from() that infers the Type of input.
*/
function readableFrom(items, opt) {
return stream_1.Readable.from(items, opt);
}
exports.readableFrom = readableFrom;