UNPKG

998 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.writableLimit = void 0;
4const stream_1 = require("stream");
5/**
6 * Allows to stop the Readable stream after the pipeline has processed X number of rows.
7 * It counts OUTPUT rows (not input), because this Writable is always at the end of the Pipeline.
8 * It ensures that everything has been processed before issuing a STOP on the readable.
9 */
10function writableLimit(readable, limit) {
11 let i = 0;
12 return new stream_1.Writable({
13 objectMode: true,
14 write(chunk, _, cb) {
15 if (limit === 0)
16 return cb(); // no limit, just passthrough
17 i++;
18 if (i === limit) {
19 console.log(`writableLimit of ${limit} reached`);
20 readable.destroy();
21 cb(); // do we need it?
22 }
23 else {
24 cb(); // passthrough
25 }
26 },
27 });
28}
29exports.writableLimit = writableLimit;