@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
30 lines (29 loc) • 998 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writableLimit = void 0;
const stream_1 = require("stream");
/**
* Allows to stop the Readable stream after the pipeline has processed X number of rows.
* It counts OUTPUT rows (not input), because this Writable is always at the end of the Pipeline.
* It ensures that everything has been processed before issuing a STOP on the readable.
*/
function writableLimit(readable, limit) {
let i = 0;
return new stream_1.Writable({
objectMode: true,
write(chunk, _, cb) {
if (limit === 0)
return cb(); // no limit, just passthrough
i++;
if (i === limit) {
console.log(`writableLimit of ${limit} reached`);
readable.destroy();
cb(); // do we need it?
}
else {
cb(); // passthrough
}
},
});
}
exports.writableLimit = writableLimit;