1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.transformTap = void 0;
|
4 | const stream_1 = require("stream");
|
5 | /**
|
6 | * Similar to RxJS `tap` - allows to run a function for each stream item, without affecting the result.
|
7 | * Item is passed through to the output.
|
8 | *
|
9 | * Can also act as a counter, since `index` is passed to `fn`
|
10 | */
|
11 | function transformTap(fn, opt = {}) {
|
12 | let index = 0;
|
13 | return new stream_1.Transform({
|
14 | objectMode: true,
|
15 | ...opt,
|
16 | async transform(chunk, _encoding, cb) {
|
17 | // console.log('tap', chunk)
|
18 | try {
|
19 | await fn(chunk, index++);
|
20 | }
|
21 | catch (err) {
|
22 | console.error(err);
|
23 | // suppressed error
|
24 | }
|
25 | cb(null, chunk); // pass through the item
|
26 | },
|
27 | });
|
28 | }
|
29 | exports.transformTap = transformTap;
|